1 | # PyKota |
---|
2 | # |
---|
3 | # PyKota : Print Quotas for CUPS and LPRng |
---|
4 | # |
---|
5 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
6 | # This program is free software; you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation; either version 2 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # This program is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with this program; if not, write to the Free Software |
---|
18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | # |
---|
20 | # $Id$ |
---|
21 | # |
---|
22 | # $Log$ |
---|
23 | # Revision 1.5 2003/05/03 07:27:14 jalet |
---|
24 | # Should be able to list a group's members |
---|
25 | # |
---|
26 | # Revision 1.4 2003/05/01 21:08:44 jalet |
---|
27 | # More work on LDAP |
---|
28 | # |
---|
29 | # Revision 1.3 2003/04/30 17:40:02 jalet |
---|
30 | # I've got my assigned number for LDAP by the IANA. |
---|
31 | # |
---|
32 | # Revision 1.2 2003/04/27 08:27:34 jalet |
---|
33 | # connection to LDAP backend |
---|
34 | # |
---|
35 | # Revision 1.1 2003/04/27 08:04:15 jalet |
---|
36 | # LDAP storage backend's skeleton added. DOESN'T WORK. |
---|
37 | # |
---|
38 | # |
---|
39 | # |
---|
40 | |
---|
41 | # |
---|
42 | # My IANA assigned number, for |
---|
43 | # "Conseil Internet & Logiciels Libres, J�me Alet" |
---|
44 | # is 16868. Use this as a base to create the LDAP schema. |
---|
45 | # |
---|
46 | |
---|
47 | import fnmatch |
---|
48 | |
---|
49 | from pykota.storage import PyKotaStorageError |
---|
50 | |
---|
51 | try : |
---|
52 | import ldap |
---|
53 | except ImportError : |
---|
54 | import sys |
---|
55 | # TODO : to translate or not to translate ? |
---|
56 | raise PyKotaStorageError, "This python version (%s) doesn't seem to have the python-ldap module installed correctly." % sys.version.split()[0] |
---|
57 | |
---|
58 | class Storage : |
---|
59 | def __init__(self, host, dbname, user, passwd) : |
---|
60 | """Opens the LDAP connection.""" |
---|
61 | raise PyKotaStorageError, "Sorry, the LDAP backend for PyKota is not yet implemented !" |
---|
62 | self.closed = 1 |
---|
63 | try : |
---|
64 | self.database = ldap.initialize(host) |
---|
65 | self.database.simple_bind_s(user, passwd) |
---|
66 | self.basedn = dbname |
---|
67 | except ldap.SERVER_DOWN : |
---|
68 | raise PyKotaStorageError, "LDAP backend for PyKota seems to be down !" # TODO : translate |
---|
69 | else : |
---|
70 | self.closed = 0 |
---|
71 | |
---|
72 | def __del__(self) : |
---|
73 | """Closes the database connection.""" |
---|
74 | if not self.closed : |
---|
75 | del self.database |
---|
76 | self.closed = 1 |
---|
77 | |
---|
78 | def doSearch(self, key, fields, base="") : |
---|
79 | """Does an LDAP search query.""" |
---|
80 | try : |
---|
81 | # prepends something more restrictive at the beginning of the base dn |
---|
82 | newbase = ",".join([base, self.basedn]) |
---|
83 | if newbase.startswith(",") : |
---|
84 | newbase = newbase[1:] |
---|
85 | result = self.database.search_s(newbase, ldap.SCOPE_SUBTREE, key, fields) |
---|
86 | except ldap.NO_SUCH_OBJECT : |
---|
87 | return |
---|
88 | else : |
---|
89 | return result |
---|
90 | |
---|
91 | def getMatchingPrinters(self, printerpattern) : |
---|
92 | """Returns the list of all printers as tuples (id, name) for printer names which match a certain pattern.""" |
---|
93 | pass |
---|
94 | |
---|
95 | def getPrinterId(self, printername) : |
---|
96 | """Returns a printerid given a printername.""" |
---|
97 | pass |
---|
98 | |
---|
99 | def getPrinterPrices(self, printerid) : |
---|
100 | """Returns a printer prices per page and per job given a printerid.""" |
---|
101 | pass |
---|
102 | |
---|
103 | def setPrinterPrices(self, printerid, perpage, perjob) : |
---|
104 | """Sets prices per job and per page for a given printer.""" |
---|
105 | pass |
---|
106 | |
---|
107 | def getUserId(self, username) : |
---|
108 | """Returns a userid given a username.""" |
---|
109 | result = self.doSearch("uid=%s" % username, ["uid"], base="ou=People") |
---|
110 | if result is not None : |
---|
111 | (userid, dummy) = result[0] |
---|
112 | return userid |
---|
113 | |
---|
114 | def getGroupId(self, groupname) : |
---|
115 | """Returns a groupid given a grupname.""" |
---|
116 | result = self.doSearch("cn=%s" % groupname, ["cn"], base="ou=Group") |
---|
117 | if result is not None : |
---|
118 | (groupid, dummy) = result[0] |
---|
119 | return groupid |
---|
120 | |
---|
121 | def getJobHistoryId(self, jobid, userid, printerid) : |
---|
122 | """Returns the history line's id given a (jobid, userid, printerid).""" |
---|
123 | pass |
---|
124 | |
---|
125 | def getPrinterUsers(self, printerid) : |
---|
126 | """Returns the list of usernames which uses a given printer.""" |
---|
127 | pass |
---|
128 | |
---|
129 | def getPrinterGroups(self, printerid) : |
---|
130 | """Returns the list of groups which uses a given printer.""" |
---|
131 | pass |
---|
132 | |
---|
133 | def getGroupMembersNames(self, groupname) : |
---|
134 | """Returns the list of user's names which are member of this group.""" |
---|
135 | result = self.doSearch("cn=%s" % groupname, ["memberUid"], base="cn=%s,ou=Group" % groupname) |
---|
136 | if result is not None : |
---|
137 | (groupid, fields) = result[0] |
---|
138 | return fields["memberUid"] |
---|
139 | |
---|
140 | def getUserGroupsNames(self, userid) : |
---|
141 | """Returns the list of groups' names the user is a member of.""" |
---|
142 | pass |
---|
143 | |
---|
144 | def addPrinter(self, printername) : |
---|
145 | """Adds a printer to the quota storage, returns its id.""" |
---|
146 | pass |
---|
147 | |
---|
148 | def addUser(self, username) : |
---|
149 | """Adds a user to the quota storage, returns its id.""" |
---|
150 | pass |
---|
151 | |
---|
152 | def addGroup(self, groupname) : |
---|
153 | """Adds a group to the quota storage, returns its id.""" |
---|
154 | pass |
---|
155 | |
---|
156 | def addUserPQuota(self, username, printerid) : |
---|
157 | """Initializes a user print quota on a printer, adds the user to the quota storage if needed.""" |
---|
158 | pass |
---|
159 | |
---|
160 | def addGroupPQuota(self, groupname, printerid) : |
---|
161 | """Initializes a group print quota on a printer, adds the group to the quota storage if needed.""" |
---|
162 | pass |
---|
163 | |
---|
164 | def increaseUserBalance(self, userid, amount) : |
---|
165 | """Increases (or decreases) an user's account balance by a given amount.""" |
---|
166 | pass |
---|
167 | |
---|
168 | def getUserBalance(self, userid) : |
---|
169 | """Returns the current account balance for a given user.""" |
---|
170 | pass |
---|
171 | |
---|
172 | def getGroupBalance(self, groupid) : |
---|
173 | """Returns the current account balance for a given group, as the sum of each of its users' account balance.""" |
---|
174 | pass |
---|
175 | |
---|
176 | def getUserLimitBy(self, userid) : |
---|
177 | """Returns the way in which user printing is limited.""" |
---|
178 | pass |
---|
179 | |
---|
180 | def getGroupLimitBy(self, groupid) : |
---|
181 | """Returns the way in which group printing is limited.""" |
---|
182 | pass |
---|
183 | |
---|
184 | def setUserBalance(self, userid, balance) : |
---|
185 | """Sets the account balance for a given user to a fixed value.""" |
---|
186 | pass |
---|
187 | |
---|
188 | def limitUserBy(self, userid, limitby) : |
---|
189 | """Limits a given user based either on print quota or on account balance.""" |
---|
190 | pass |
---|
191 | |
---|
192 | def limitGroupBy(self, groupid, limitby) : |
---|
193 | """Limits a given group based either on print quota or on sum of its users' account balances.""" |
---|
194 | pass |
---|
195 | |
---|
196 | def setUserPQuota(self, userid, printerid, softlimit, hardlimit) : |
---|
197 | """Sets soft and hard limits for a user quota on a specific printer given (userid, printerid).""" |
---|
198 | pass |
---|
199 | |
---|
200 | def setGroupPQuota(self, groupid, printerid, softlimit, hardlimit) : |
---|
201 | """Sets soft and hard limits for a group quota on a specific printer given (groupid, printerid).""" |
---|
202 | pass |
---|
203 | |
---|
204 | def resetUserPQuota(self, userid, printerid) : |
---|
205 | """Resets the page counter to zero for a user on a printer. Life time page counter is kept unchanged.""" |
---|
206 | pass |
---|
207 | |
---|
208 | def resetGroupPQuota(self, groupid, printerid) : |
---|
209 | """Resets the page counter to zero for a group on a printer. Life time page counter is kept unchanged.""" |
---|
210 | pass |
---|
211 | |
---|
212 | def updateUserPQuota(self, userid, printerid, pagecount) : |
---|
213 | """Updates the used user Quota information given (userid, printerid) and a job size in pages.""" |
---|
214 | pass |
---|
215 | |
---|
216 | def getUserPQuota(self, userid, printerid) : |
---|
217 | """Returns the Print Quota information for a given (userid, printerid).""" |
---|
218 | pass |
---|
219 | |
---|
220 | def getGroupPQuota(self, groupid, printerid) : |
---|
221 | """Returns the Print Quota information for a given (groupid, printerid).""" |
---|
222 | pass |
---|
223 | |
---|
224 | def setUserDateLimit(self, userid, printerid, datelimit) : |
---|
225 | """Sets the limit date for a soft limit to become an hard one given (userid, printerid).""" |
---|
226 | pass |
---|
227 | |
---|
228 | def setGroupDateLimit(self, groupid, printerid, datelimit) : |
---|
229 | """Sets the limit date for a soft limit to become an hard one given (groupid, printerid).""" |
---|
230 | pass |
---|
231 | |
---|
232 | def addJobToHistory(self, jobid, userid, printerid, pagecounter, action) : |
---|
233 | """Adds a job to the history: (jobid, userid, printerid, last page counter taken from requester).""" |
---|
234 | pass |
---|
235 | |
---|
236 | def updateJobSizeInHistory(self, historyid, jobsize) : |
---|
237 | """Updates a job size in the history given the history line's id.""" |
---|
238 | pass |
---|
239 | |
---|
240 | def getPrinterPageCounter(self, printerid) : |
---|
241 | """Returns the last page counter value for a printer given its id, also returns last username, last jobid and history line id.""" |
---|
242 | pass |
---|
243 | |
---|
244 | def addUserToGroup(self, userid, groupid) : |
---|
245 | """Adds an user to a group.""" |
---|
246 | pass |
---|
247 | |
---|
248 | def deleteUser(self, userid) : |
---|
249 | """Completely deletes an user from the Quota Storage.""" |
---|
250 | pass |
---|
251 | |
---|
252 | def deleteGroup(self, groupid) : |
---|
253 | """Completely deletes an user from the Quota Storage.""" |
---|
254 | pass |
---|
255 | |
---|
256 | def computePrinterJobPrice(self, printerid, jobsize) : |
---|
257 | """Returns the price for a job on a given printer.""" |
---|
258 | # TODO : create a base class with things like this |
---|
259 | prices = self.getPrinterPrices(printerid) |
---|
260 | if prices is None : |
---|
261 | perpage = perjob = 0.0 |
---|
262 | else : |
---|
263 | (perpage, perjob) = prices |
---|
264 | return perjob + (perpage * jobsize) |
---|