[1016] | 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$ |
---|
[1075] | 23 | # Revision 1.17 2003/07/07 12:51:07 jalet |
---|
| 24 | # Small fix |
---|
| 25 | # |
---|
[1070] | 26 | # Revision 1.16 2003/07/07 12:11:13 jalet |
---|
| 27 | # Small fix |
---|
| 28 | # |
---|
[1068] | 29 | # Revision 1.15 2003/07/07 11:49:24 jalet |
---|
| 30 | # Lots of small fixes with the help of PyChecker |
---|
| 31 | # |
---|
[1067] | 32 | # Revision 1.14 2003/07/07 08:33:18 jalet |
---|
| 33 | # Bug fix due to a typo in LDAP code |
---|
| 34 | # |
---|
[1062] | 35 | # Revision 1.13 2003/07/05 07:46:50 jalet |
---|
| 36 | # The previous bug fix was incomplete. |
---|
| 37 | # |
---|
[1051] | 38 | # Revision 1.12 2003/06/30 13:54:21 jalet |
---|
| 39 | # Sorts by user / group name |
---|
| 40 | # |
---|
[1041] | 41 | # Revision 1.11 2003/06/25 14:10:01 jalet |
---|
| 42 | # Hey, it may work (edpykota --reset excepted) ! |
---|
| 43 | # |
---|
[1032] | 44 | # Revision 1.10 2003/06/16 21:55:15 jalet |
---|
| 45 | # More work on LDAP, again. Problem detected. |
---|
| 46 | # |
---|
[1031] | 47 | # Revision 1.9 2003/06/16 11:59:09 jalet |
---|
| 48 | # More work on LDAP |
---|
| 49 | # |
---|
[1030] | 50 | # Revision 1.8 2003/06/15 22:26:52 jalet |
---|
| 51 | # More work on LDAP |
---|
| 52 | # |
---|
[1029] | 53 | # Revision 1.7 2003/06/14 22:44:21 jalet |
---|
| 54 | # More work on LDAP storage backend. |
---|
| 55 | # |
---|
[1027] | 56 | # Revision 1.6 2003/06/13 19:07:57 jalet |
---|
| 57 | # Two big bugs fixed, time to release something ;-) |
---|
| 58 | # |
---|
[1021] | 59 | # Revision 1.5 2003/06/10 16:37:54 jalet |
---|
| 60 | # Deletion of the second user which is not needed anymore. |
---|
| 61 | # Added a debug configuration field in /etc/pykota.conf |
---|
| 62 | # All queries can now be sent to the logger in debug mode, this will |
---|
| 63 | # greatly help improve performance when time for this will come. |
---|
| 64 | # |
---|
[1020] | 65 | # Revision 1.4 2003/06/10 10:45:32 jalet |
---|
| 66 | # Not implemented methods now raise an exception when called. |
---|
| 67 | # |
---|
[1018] | 68 | # Revision 1.3 2003/06/06 20:49:15 jalet |
---|
| 69 | # Very latest schema. UNTESTED. |
---|
| 70 | # |
---|
[1017] | 71 | # Revision 1.2 2003/06/06 14:21:08 jalet |
---|
| 72 | # New LDAP schema. |
---|
| 73 | # Small bug fixes. |
---|
| 74 | # |
---|
[1016] | 75 | # Revision 1.1 2003/06/05 11:19:13 jalet |
---|
| 76 | # More good work on LDAP storage. |
---|
| 77 | # |
---|
| 78 | # |
---|
| 79 | # |
---|
| 80 | |
---|
| 81 | # |
---|
| 82 | # My IANA assigned number, for |
---|
| 83 | # "Conseil Internet & Logiciels Libres, J�me Alet" |
---|
| 84 | # is 16868. Use this as a base to create the LDAP schema. |
---|
| 85 | # |
---|
| 86 | |
---|
[1030] | 87 | import time |
---|
| 88 | import md5 |
---|
[1016] | 89 | |
---|
| 90 | from pykota.storage import PyKotaStorageError |
---|
[1041] | 91 | from pykota.storage import StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageLastJob,StorageUserPQuota,StorageGroupPQuota |
---|
[1016] | 92 | |
---|
| 93 | try : |
---|
| 94 | import ldap |
---|
[1030] | 95 | from ldap import modlist |
---|
[1016] | 96 | except ImportError : |
---|
| 97 | import sys |
---|
| 98 | # TODO : to translate or not to translate ? |
---|
| 99 | raise PyKotaStorageError, "This python version (%s) doesn't seem to have the python-ldap module installed correctly." % sys.version.split()[0] |
---|
| 100 | |
---|
| 101 | class Storage : |
---|
[1021] | 102 | def __init__(self, pykotatool, host, dbname, user, passwd) : |
---|
[1016] | 103 | """Opens the LDAP connection.""" |
---|
| 104 | # raise PyKotaStorageError, "Sorry, the LDAP backend for PyKota is not yet implemented !" |
---|
[1029] | 105 | self.closed = 1 |
---|
[1021] | 106 | self.tool = pykotatool |
---|
| 107 | self.debug = pykotatool.config.getDebug() |
---|
[1029] | 108 | self.info = pykotatool.config.getLDAPInfo() |
---|
[1016] | 109 | try : |
---|
| 110 | self.database = ldap.initialize(host) |
---|
| 111 | self.database.simple_bind_s(user, passwd) |
---|
| 112 | self.basedn = dbname |
---|
| 113 | except ldap.SERVER_DOWN : |
---|
| 114 | raise PyKotaStorageError, "LDAP backend for PyKota seems to be down !" # TODO : translate |
---|
[1031] | 115 | except ldap.LDAPError : |
---|
| 116 | raise PyKotaStorageError, "Unable to connect to LDAP server %s as %s." % (host, user) # TODO : translate |
---|
[1016] | 117 | else : |
---|
| 118 | self.closed = 0 |
---|
[1021] | 119 | if self.debug : |
---|
| 120 | self.tool.logger.log_message("Database opened (host=%s, dbname=%s, user=%s)" % (host, dbname, user), "debug") |
---|
[1016] | 121 | |
---|
| 122 | def __del__(self) : |
---|
| 123 | """Closes the database connection.""" |
---|
| 124 | if not self.closed : |
---|
| 125 | del self.database |
---|
| 126 | self.closed = 1 |
---|
[1021] | 127 | if self.debug : |
---|
| 128 | self.tool.logger.log_message("Database closed.", "debug") |
---|
[1016] | 129 | |
---|
[1030] | 130 | def genUUID(self) : |
---|
| 131 | """Generates an unique identifier. |
---|
| 132 | |
---|
| 133 | TODO : this one is not unique accross several print servers, but should be sufficient for testing. |
---|
| 134 | """ |
---|
| 135 | return md5.md5("%s" % time.time()).hexdigest() |
---|
| 136 | |
---|
[1041] | 137 | def beginTransaction(self) : |
---|
| 138 | """Starts a transaction.""" |
---|
| 139 | if self.debug : |
---|
| 140 | self.tool.logger.log_message("Transaction begins... WARNING : No transactions in LDAP !", "debug") |
---|
| 141 | |
---|
| 142 | def commitTransaction(self) : |
---|
| 143 | """Commits a transaction.""" |
---|
| 144 | if self.debug : |
---|
| 145 | self.tool.logger.log_message("Transaction committed. WARNING : No transactions in LDAP !", "debug") |
---|
| 146 | |
---|
| 147 | def rollbackTransaction(self) : |
---|
| 148 | """Rollbacks a transaction.""" |
---|
| 149 | if self.debug : |
---|
| 150 | self.tool.logger.log_message("Transaction aborted. WARNING : No transaction in LDAP !", "debug") |
---|
| 151 | |
---|
[1030] | 152 | def doSearch(self, key, fields=None, base="", scope=ldap.SCOPE_SUBTREE) : |
---|
[1016] | 153 | """Does an LDAP search query.""" |
---|
| 154 | try : |
---|
[1030] | 155 | base = base or self.basedn |
---|
[1021] | 156 | if self.debug : |
---|
[1041] | 157 | self.tool.logger.log_message("QUERY : Filter : %s, BaseDN : %s, Scope : %s, Attributes : %s" % (key, base, scope, fields), "debug") |
---|
[1017] | 158 | result = self.database.search_s(base or self.basedn, scope, key, fields) |
---|
[1030] | 159 | except ldap.LDAPError : |
---|
| 160 | raise PyKotaStorageError, _("Search for %s(%s) from %s(scope=%s) returned no answer.") % (key, fields, base, scope) |
---|
[1016] | 161 | else : |
---|
[1031] | 162 | if self.debug : |
---|
| 163 | self.tool.logger.log_message("QUERY : Result : %s" % result, "debug") |
---|
[1016] | 164 | return result |
---|
[1030] | 165 | |
---|
| 166 | def doAdd(self, dn, fields) : |
---|
| 167 | """Adds an entry in the LDAP directory.""" |
---|
| 168 | try : |
---|
| 169 | if self.debug : |
---|
| 170 | self.tool.logger.log_message("QUERY : ADD(%s, %s)" % (dn, str(fields)), "debug") |
---|
| 171 | self.database.add_s(dn, modlist.addModlist(fields)) |
---|
| 172 | except ldap.LDAPError : |
---|
| 173 | raise PyKotaStorageError, _("Problem adding LDAP entry (%s, %s)") % (dn, str(fields)) |
---|
| 174 | else : |
---|
| 175 | return dn |
---|
| 176 | |
---|
[1041] | 177 | def doDelete(self, dn) : |
---|
| 178 | """Deletes an entry from the LDAP directory.""" |
---|
| 179 | try : |
---|
| 180 | if self.debug : |
---|
| 181 | self.tool.logger.log_message("QUERY : Delete(%s)" % dn, "debug") |
---|
| 182 | self.database.delete_s(dn) |
---|
| 183 | except ldap.LDAPError : |
---|
| 184 | raise PyKotaStorageError, _("Problem deleting LDAP entry (%s)") % dn |
---|
| 185 | |
---|
| 186 | def doModify(self, dn, fields, ignoreold=1) : |
---|
[1030] | 187 | """Modifies an entry in the LDAP directory.""" |
---|
| 188 | try : |
---|
| 189 | oldentry = self.doSearch("objectClass=*", base=dn, scope=ldap.SCOPE_BASE) |
---|
| 190 | if self.debug : |
---|
| 191 | self.tool.logger.log_message("QUERY : Modify(%s, %s ==> %s)" % (dn, oldentry[0][1], fields), "debug") |
---|
[1041] | 192 | self.database.modify_s(dn, modlist.modifyModlist(oldentry[0][1], fields, ignore_oldexistent=ignoreold)) |
---|
[1030] | 193 | except ldap.LDAPError : |
---|
| 194 | raise PyKotaStorageError, _("Problem modifying LDAP entry (%s, %s)") % (dn, fields) |
---|
| 195 | else : |
---|
| 196 | return dn |
---|
[1016] | 197 | |
---|
[1041] | 198 | def getUser(self, username) : |
---|
| 199 | """Extracts user information given its name.""" |
---|
| 200 | user = StorageUser(self, username) |
---|
| 201 | result = self.doSearch("(&(objectClass=pykotaAccount)(|(pykotaUserName=%s)(%s=%s)))" % (username, self.info["userrdn"], username), ["pykotaLimitBy"], base=self.info["userbase"]) |
---|
[1016] | 202 | if result : |
---|
[1041] | 203 | fields = result[0][1] |
---|
| 204 | user.ident = result[0][0] |
---|
[1062] | 205 | user.LimitBy = fields.get("pykotaLimitBy") |
---|
| 206 | if user.LimitBy is not None : |
---|
| 207 | user.LimitBy = user.LimitBy[0] |
---|
[1041] | 208 | result = self.doSearch("(&(objectClass=pykotaAccountBalance)(|(pykotaUserName=%s)(%s=%s)))" % (username, self.info["balancerdn"], username), ["pykotaBalance", "pykotaLifeTimePaid"], base=self.info["balancebase"]) |
---|
| 209 | if result : |
---|
| 210 | fields = result[0][1] |
---|
| 211 | user.idbalance = result[0][0] |
---|
| 212 | user.AccountBalance = fields.get("pykotaBalance") |
---|
| 213 | if user.AccountBalance is not None : |
---|
| 214 | if user.AccountBalance[0].upper() == "NONE" : |
---|
| 215 | user.AccountBalance = None |
---|
| 216 | else : |
---|
| 217 | user.AccountBalance = float(user.AccountBalance[0]) |
---|
| 218 | user.AccountBalance = user.AccountBalance or 0.0 |
---|
| 219 | user.LifeTimePaid = fields.get("pykotaLifeTimePaid") |
---|
| 220 | if user.LifeTimePaid is not None : |
---|
| 221 | if user.LifeTimePaid[0].upper() == "NONE" : |
---|
| 222 | user.LifeTimePaid = None |
---|
| 223 | else : |
---|
| 224 | user.LifeTimePaid = float(user.LifeTimePaid[0]) |
---|
| 225 | user.LifeTimePaid = user.LifeTimePaid or 0.0 |
---|
| 226 | user.Exists = 1 |
---|
| 227 | return user |
---|
| 228 | |
---|
| 229 | def getGroup(self, groupname) : |
---|
| 230 | """Extracts group information given its name.""" |
---|
| 231 | group = StorageGroup(self, groupname) |
---|
| 232 | result = self.doSearch("(&(objectClass=pykotaGroup)(|(pykotaGroupName=%s)(%s=%s)))" % (groupname, self.info["grouprdn"], groupname), ["pykotaLimitBy"], base=self.info["groupbase"]) |
---|
[1030] | 233 | if result : |
---|
[1041] | 234 | fields = result[0][1] |
---|
| 235 | group.ident = result[0][0] |
---|
[1062] | 236 | group.LimitBy = fields.get("pykotaLimitBy") |
---|
| 237 | if group.LimitBy is not None : |
---|
| 238 | group.LimitBy = group.LimitBy[0] |
---|
[1041] | 239 | group.AccountBalance = 0.0 |
---|
| 240 | group.LifeTimePaid = 0.0 |
---|
| 241 | group.Members = self.getGroupMembers(group) |
---|
| 242 | for member in group.Members : |
---|
[1075] | 243 | if member.Exists : |
---|
| 244 | group.AccountBalance += member.AccountBalance |
---|
| 245 | group.LifeTimePaid += member.LifeTimePaid |
---|
[1041] | 246 | group.Exists = 1 |
---|
| 247 | return group |
---|
| 248 | |
---|
| 249 | def getPrinter(self, printername) : |
---|
| 250 | """Extracts printer information given its name.""" |
---|
| 251 | printer = StoragePrinter(self, printername) |
---|
| 252 | result = self.doSearch("(&(objectClass=pykotaPrinter)(|(pykotaPrinterName=%s)(%s=%s)))" % (printername, self.info["printerrdn"], printername), ["pykotaPricePerPage", "pykotaPricePerJob"], base=self.info["printerbase"]) |
---|
[1016] | 253 | if result : |
---|
[1041] | 254 | fields = result[0][1] |
---|
| 255 | printer.ident = result[0][0] |
---|
| 256 | printer.PricePerJob = float(fields.get("pykotaPricePerJob")[0] or 0.0) |
---|
| 257 | printer.PricePerPage = float(fields.get("pykotaPricePerPage")[0] or 0.0) |
---|
| 258 | printer.LastJob = self.getPrinterLastJob(printer) |
---|
| 259 | printer.Exists = 1 |
---|
| 260 | return printer |
---|
[1016] | 261 | |
---|
[1041] | 262 | def getUserGroups(self, user) : |
---|
| 263 | """Returns the user's groups list.""" |
---|
| 264 | groups = [] |
---|
| 265 | result = self.doSearch("(&(objectClass=pykotaGroup)(%s=%s))" % (self.info["groupmembers"], user.Name), [self.info["grouprdn"]], base=self.info["groupbase"]) |
---|
[1016] | 266 | if result : |
---|
[1041] | 267 | for (groupid, fields) in result : |
---|
| 268 | groups.append(self.getGroup(fields.get(self.info["grouprdn"])[0])) |
---|
| 269 | return groups |
---|
| 270 | |
---|
| 271 | def getGroupMembers(self, group) : |
---|
| 272 | """Returns the group's members list.""" |
---|
| 273 | groupmembers = [] |
---|
| 274 | result = self.doSearch("(&(objectClass=pykotaGroup)(|(pykotaGroupName=%s)(%s=%s)))" % (group.Name, self.info["grouprdn"], group.Name), [self.info["groupmembers"]], base=self.info["groupbase"]) |
---|
[1031] | 275 | if result : |
---|
[1041] | 276 | for username in result[0][1].get(self.info["groupmembers"], []) : |
---|
| 277 | groupmembers.append(self.getUser(username)) |
---|
| 278 | return groupmembers |
---|
[1027] | 279 | |
---|
[1041] | 280 | def getUserPQuota(self, user, printer) : |
---|
| 281 | """Extracts a user print quota.""" |
---|
| 282 | userpquota = StorageUserPQuota(self, user, printer) |
---|
| 283 | if user.Exists : |
---|
| 284 | result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaUserName=%s)(pykotaPrinterName=%s))" % (user.Name, printer.Name), ["pykotaPageCounter", "pykotaLifePageCounter", "pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit"], base=self.info["userquotabase"]) |
---|
[1017] | 285 | if result : |
---|
[1041] | 286 | fields = result[0][1] |
---|
| 287 | userpquota.ident = result[0][0] |
---|
| 288 | userpquota.PageCounter = int(fields.get("pykotaPageCounter")[0] or 0) |
---|
| 289 | userpquota.LifePageCounter = int(fields.get("pykotaLifePageCounter")[0] or 0) |
---|
| 290 | userpquota.SoftLimit = fields.get("pykotaSoftLimit") |
---|
| 291 | if userpquota.SoftLimit is not None : |
---|
| 292 | if userpquota.SoftLimit[0].upper() == "NONE" : |
---|
| 293 | userpquota.SoftLimit = None |
---|
| 294 | else : |
---|
| 295 | userpquota.SoftLimit = int(userpquota.SoftLimit[0]) |
---|
| 296 | userpquota.HardLimit = fields.get("pykotaHardLimit") |
---|
| 297 | if userpquota.HardLimit is not None : |
---|
| 298 | if userpquota.HardLimit[0].upper() == "NONE" : |
---|
| 299 | userpquota.HardLimit = None |
---|
| 300 | elif userpquota.HardLimit is not None : |
---|
| 301 | userpquota.HardLimit = int(userpquota.HardLimit[0]) |
---|
| 302 | userpquota.DateLimit = fields.get("pykotaDateLimit") |
---|
| 303 | if userpquota.DateLimit is not None : |
---|
| 304 | if userpquota.DateLimit[0].upper() == "NONE" : |
---|
| 305 | userpquota.DateLimit = None |
---|
| 306 | else : |
---|
| 307 | userpquota.DateLimit = userpquota.DateLimit[0] |
---|
| 308 | userpquota.Exists = 1 |
---|
| 309 | return userpquota |
---|
[1016] | 310 | |
---|
[1041] | 311 | def getGroupPQuota(self, group, printer) : |
---|
| 312 | """Extracts a group print quota.""" |
---|
| 313 | grouppquota = StorageGroupPQuota(self, group, printer) |
---|
| 314 | if group.Exists : |
---|
| 315 | result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaGroupName=%s)(pykotaPrinterName=%s))" % (group.Name, printer.Name), ["pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit"], base=self.info["groupquotabase"]) |
---|
| 316 | if result : |
---|
| 317 | fields = result[0][1] |
---|
| 318 | grouppquota.ident = result[0][0] |
---|
| 319 | grouppquota.SoftLimit = fields.get("pykotaSoftLimit") |
---|
| 320 | if grouppquota.SoftLimit is not None : |
---|
| 321 | if grouppquota.SoftLimit[0].upper() == "NONE" : |
---|
| 322 | grouppquota.SoftLimit = None |
---|
| 323 | else : |
---|
| 324 | grouppquota.SoftLimit = int(grouppquota.SoftLimit[0]) |
---|
| 325 | grouppquota.HardLimit = fields.get("pykotaHardLimit") |
---|
| 326 | if grouppquota.HardLimit is not None : |
---|
| 327 | if grouppquota.HardLimit[0].upper() == "NONE" : |
---|
| 328 | grouppquota.HardLimit = None |
---|
| 329 | else : |
---|
| 330 | grouppquota.HardLimit = int(grouppquota.HardLimit[0]) |
---|
| 331 | grouppquota.DateLimit = fields.get("pykotaDateLimit") |
---|
| 332 | if grouppquota.DateLimit is not None : |
---|
| 333 | if grouppquota.DateLimit[0].upper() == "NONE" : |
---|
| 334 | grouppquota.DateLimit = None |
---|
| 335 | else : |
---|
| 336 | grouppquota.DateLimit = grouppquota.DateLimit[0] |
---|
| 337 | grouppquota.PageCounter = 0 |
---|
| 338 | grouppquota.LifePageCounter = 0 |
---|
| 339 | if (not hasattr(group, "Members")) or (group.Members is None) : |
---|
| 340 | group.Members = self.getGroupMembers(group) |
---|
| 341 | usernamesfilter = "".join(["(pykotaUserName=%s)" % member.Name for member in group.Members]) |
---|
| 342 | result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaPrinterName=%s)(|%s))" % (printer.Name, usernamesfilter), ["pykotaPageCounter", "pykotaLifePageCounter"], base=self.info["userquotabase"]) |
---|
| 343 | if result : |
---|
| 344 | for userpquota in result : |
---|
| 345 | grouppquota.PageCounter += int(userpquota[1].get("pykotaPageCounter")[0] or 0) |
---|
| 346 | grouppquota.LifePageCounter += int(userpquota[1].get("pykotaLifePageCounter")[0] or 0) |
---|
| 347 | grouppquota.Exists = 1 |
---|
| 348 | return grouppquota |
---|
| 349 | |
---|
| 350 | def getPrinterLastJob(self, printer) : |
---|
| 351 | """Extracts a printer's last job information.""" |
---|
| 352 | lastjob = StorageLastJob(self, printer) |
---|
| 353 | result = self.doSearch("(&(objectClass=pykotaLastjob)(|(pykotaPrinterName=%s)(%s=%s)))" % (printer.Name, self.info["printerrdn"], printer.Name), ["pykotaLastJobIdent"], base=self.info["lastjobbase"]) |
---|
[1016] | 354 | if result : |
---|
[1041] | 355 | lastjob.lastjobident = result[0][0] |
---|
| 356 | lastjobident = result[0][1]["pykotaLastJobIdent"][0] |
---|
| 357 | result = self.doSearch("objectClass=pykotaJob", ["pykotaUserName", "pykotaJobId", "pykotaPrinterPageCounter", "pykotaJobSize", "pykotaAction", "createTimestamp"], base="cn=%s,%s" % (lastjobident, self.info["jobbase"]), scope=ldap.SCOPE_BASE) |
---|
[1017] | 358 | if result : |
---|
[1041] | 359 | fields = result[0][1] |
---|
| 360 | lastjob.ident = result[0][0] |
---|
| 361 | lastjob.JobId = fields.get("pykotaJobId")[0] |
---|
| 362 | lastjob.User = self.getUser(fields.get("pykotaUserName")[0]) |
---|
| 363 | lastjob.PrinterPageCounter = int(fields.get("pykotaPrinterPageCounter")[0] or 0) |
---|
| 364 | lastjob.JobSize = int(fields.get("pykotaJobSize", [0])[0]) |
---|
| 365 | lastjob.JobAction = fields.get("pykotaAction")[0] |
---|
| 366 | date = fields.get("createTimestamp")[0] |
---|
| 367 | year = int(date[:4]) |
---|
| 368 | month = int(date[4:6]) |
---|
| 369 | day = int(date[6:8]) |
---|
| 370 | hour = int(date[8:10]) |
---|
| 371 | minute = int(date[10:12]) |
---|
| 372 | second = int(date[12:14]) |
---|
| 373 | lastjob.JobDate = "%04i-%02i-%02i %02i:%02i:%02i" % (year, month, day, hour, minute, second) |
---|
| 374 | lastjob.Exists = 1 |
---|
| 375 | return lastjob |
---|
[1016] | 376 | |
---|
[1041] | 377 | def getMatchingPrinters(self, printerpattern) : |
---|
| 378 | """Returns the list of all printers for which name matches a certain pattern.""" |
---|
| 379 | printers = [] |
---|
| 380 | # see comment at the same place in pgstorage.py |
---|
| 381 | result = self.doSearch("objectClass=pykotaPrinter", ["pykotaPrinterName", "pykotaPricePerPage", "pykotaPricePerJob"], base=self.info["printerbase"]) |
---|
[1016] | 382 | if result : |
---|
[1041] | 383 | for (printerid, fields) in result : |
---|
| 384 | printername = fields["pykotaPrinterName"][0] |
---|
| 385 | if self.tool.matchString(printername, [ printerpattern ]) : |
---|
| 386 | printer = StoragePrinter(self, printername) |
---|
| 387 | printer.ident = printerid |
---|
| 388 | printer.PricePerJob = float(fields.get("pykotaPricePerJob")[0] or 0.0) |
---|
| 389 | printer.PricePerPage = float(fields.get("pykotaPricePerPage")[0] or 0.0) |
---|
| 390 | printer.LastJob = self.getPrinterLastJob(printer) |
---|
| 391 | printer.Exists = 1 |
---|
| 392 | printers.append(printer) |
---|
| 393 | return printers |
---|
[1016] | 394 | |
---|
[1041] | 395 | def getPrinterUsersAndQuotas(self, printer, names=None) : |
---|
| 396 | """Returns the list of users who uses a given printer, along with their quotas.""" |
---|
| 397 | usersandquotas = [] |
---|
| 398 | result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaPrinterName=%s))" % printer.Name, ["pykotaUserName", "pykotaPageCounter", "pykotaLifePageCounter", "pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit"], base=self.info["userquotabase"]) |
---|
| 399 | if result : |
---|
| 400 | for (userquotaid, fields) in result : |
---|
| 401 | user = self.getUser(fields["pykotaUserName"][0]) |
---|
| 402 | if (names is None) or self.tool.matchString(user.Name, names) : |
---|
| 403 | userpquota = StorageUserPQuota(self, user, printer) |
---|
| 404 | userpquota.ident = userquotaid |
---|
| 405 | userpquota.PageCounter = int(fields.get("pykotaPageCounter")[0] or 0) |
---|
| 406 | userpquota.LifePageCounter = int(fields.get("pykotaLifePageCounter")[0] or 0) |
---|
| 407 | userpquota.SoftLimit = fields.get("pykotaSoftLimit") |
---|
| 408 | if userpquota.SoftLimit is not None : |
---|
| 409 | if userpquota.SoftLimit[0].upper() == "NONE" : |
---|
| 410 | userpquota.SoftLimit = None |
---|
| 411 | else : |
---|
| 412 | userpquota.SoftLimit = int(userpquota.SoftLimit[0]) |
---|
| 413 | userpquota.HardLimit = fields.get("pykotaHardLimit") |
---|
| 414 | if userpquota.HardLimit is not None : |
---|
| 415 | if userpquota.HardLimit[0].upper() == "NONE" : |
---|
| 416 | userpquota.HardLimit = None |
---|
| 417 | elif userpquota.HardLimit is not None : |
---|
| 418 | userpquota.HardLimit = int(userpquota.HardLimit[0]) |
---|
| 419 | userpquota.DateLimit = fields.get("pykotaDateLimit") |
---|
| 420 | if userpquota.DateLimit is not None : |
---|
| 421 | if userpquota.DateLimit[0].upper() == "NONE" : |
---|
| 422 | userpquota.DateLimit = None |
---|
| 423 | else : |
---|
| 424 | userpquota.DateLimit = userpquota.DateLimit[0] |
---|
| 425 | userpquota.Exists = 1 |
---|
| 426 | usersandquotas.append((user, userpquota)) |
---|
[1051] | 427 | usersandquotas.sort(lambda x, y : cmp(x[0].Name, y[0].Name)) |
---|
[1041] | 428 | return usersandquotas |
---|
| 429 | |
---|
| 430 | def getPrinterGroupsAndQuotas(self, printer, names=None) : |
---|
| 431 | """Returns the list of groups which uses a given printer, along with their quotas.""" |
---|
| 432 | groupsandquotas = [] |
---|
| 433 | result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaPrinterName=%s))" % printer.Name, ["pykotaGroupName"], base=self.info["groupquotabase"]) |
---|
| 434 | if result : |
---|
| 435 | for (groupquotaid, fields) in result : |
---|
| 436 | group = self.getGroup(fields.get("pykotaGroupName")[0]) |
---|
[1068] | 437 | if (names is None) or self.tool.matchString(group.Name, names) : |
---|
[1041] | 438 | grouppquota = self.getGroupPQuota(group, printer) |
---|
| 439 | groupsandquotas.append((group, grouppquota)) |
---|
[1051] | 440 | groupsandquotas.sort(lambda x, y : cmp(x[0].Name, y[0].Name)) |
---|
[1041] | 441 | return groupsandquotas |
---|
[1016] | 442 | |
---|
| 443 | def addPrinter(self, printername) : |
---|
[1041] | 444 | """Adds a printer to the quota storage, returns it.""" |
---|
[1030] | 445 | fields = { self.info["printerrdn"] : printername, |
---|
| 446 | "objectClass" : ["pykotaObject", "pykotaPrinter"], |
---|
[1041] | 447 | "cn" : printername, |
---|
[1030] | 448 | "pykotaPrinterName" : printername, |
---|
| 449 | "pykotaPricePerPage" : "0.0", |
---|
| 450 | "pykotaPricePerJob" : "0.0", |
---|
| 451 | } |
---|
| 452 | dn = "%s=%s,%s" % (self.info["printerrdn"], printername, self.info["printerbase"]) |
---|
[1041] | 453 | self.doAdd(dn, fields) |
---|
| 454 | return self.getPrinter(printername) |
---|
[1016] | 455 | |
---|
[1041] | 456 | def addUser(self, user) : |
---|
| 457 | """Adds a user to the quota storage, returns it.""" |
---|
| 458 | fields = { self.info["userrdn"] : user.Name, |
---|
| 459 | "objectClass" : ["pykotaObject", "pykotaAccount"], |
---|
| 460 | "cn" : user.Name, |
---|
| 461 | "pykotaUserName" : user.Name, |
---|
| 462 | "pykotaLimitBY" : (user.LimitBy or "quota"), |
---|
[1030] | 463 | } |
---|
[1041] | 464 | dn = "%s=%s,%s" % (self.info["userrdn"], user.Name, self.info["userbase"]) |
---|
| 465 | self.doAdd(dn, fields) |
---|
| 466 | fields = { |
---|
| 467 | "objectClass" : ["pykotaObject", "pykotaAccountBalance"], |
---|
| 468 | "cn" : user.Name, |
---|
| 469 | "pykotaUserName" : user.Name, |
---|
| 470 | "pykotaBalance" : str(user.AccountBalance or 0.0), |
---|
| 471 | "pykotaLifeTimePaid" : str(user.LifeTimePaid or 0.0), |
---|
| 472 | } |
---|
| 473 | dn = "cn=%s,%s" % (user.Name, self.info["balancebase"]) |
---|
| 474 | self.doAdd(dn, fields) |
---|
| 475 | return self.getUser(user.Name) |
---|
[1016] | 476 | |
---|
[1041] | 477 | def addGroup(self, group) : |
---|
| 478 | """Adds a group to the quota storage, returns it.""" |
---|
| 479 | fields = { self.info["grouprdn"] : group.Name, |
---|
[1030] | 480 | "objectClass" : ["pykotaObject", "pykotaGroup"], |
---|
[1041] | 481 | "cn" : group.Name, |
---|
| 482 | "pykotaGroupName" : group.Name, |
---|
| 483 | "pykotaLimitBY" : (group.LimitBy or "quota"), |
---|
[1030] | 484 | } |
---|
[1041] | 485 | dn = "%s=%s,%s" % (self.info["grouprdn"], group.Name, self.info["groupbase"]) |
---|
| 486 | self.doAdd(dn, fields) |
---|
| 487 | return self.getGroup(group.Name) |
---|
[1016] | 488 | |
---|
[1041] | 489 | def addUserToGroup(self, user, group) : |
---|
| 490 | """Adds an user to a group.""" |
---|
| 491 | if user.Name not in [u.Name for u in group.Members] : |
---|
| 492 | result = self.doSearch("objectClass=pykotaGroup", None, base=group.ident, scope=ldap.SCOPE_BASE) |
---|
| 493 | if result : |
---|
| 494 | fields = result[0][1] |
---|
[1070] | 495 | if not fields.has_key(self.info["groupmembers"]) : |
---|
| 496 | fields[self.info["groupmembers"]] = [] |
---|
[1041] | 497 | fields[self.info["groupmembers"]].append(user.Name) |
---|
| 498 | self.doModify(group.ident, fields) |
---|
| 499 | group.Members.append(user) |
---|
| 500 | |
---|
| 501 | def addUserPQuota(self, user, printer) : |
---|
| 502 | """Initializes a user print quota on a printer.""" |
---|
[1030] | 503 | uuid = self.genUUID() |
---|
[1041] | 504 | fields = { "cn" : uuid, |
---|
| 505 | "objectClass" : ["pykotaObject", "pykotaUserPQuota"], |
---|
| 506 | "pykotaUserName" : user.Name, |
---|
| 507 | "pykotaPrinterName" : printer.Name, |
---|
| 508 | "pykotaDateLimit" : "None", |
---|
[1030] | 509 | "pykotaPageCounter" : "0", |
---|
| 510 | "pykotaLifePageCounter" : "0", |
---|
| 511 | } |
---|
| 512 | dn = "cn=%s,%s" % (uuid, self.info["userquotabase"]) |
---|
[1031] | 513 | self.doAdd(dn, fields) |
---|
[1041] | 514 | return self.getUserPQuota(user, printer) |
---|
[1016] | 515 | |
---|
[1041] | 516 | def addGroupPQuota(self, group, printer) : |
---|
| 517 | """Initializes a group print quota on a printer.""" |
---|
[1030] | 518 | uuid = self.genUUID() |
---|
[1041] | 519 | fields = { "cn" : uuid, |
---|
| 520 | "objectClass" : ["pykotaObject", "pykotaGroupPQuota"], |
---|
| 521 | "pykotaGroupName" : group.Name, |
---|
| 522 | "pykotaPrinterName" : printer.Name, |
---|
[1030] | 523 | "pykotaDateLimit" : "None", |
---|
| 524 | } |
---|
| 525 | dn = "cn=%s,%s" % (uuid, self.info["groupquotabase"]) |
---|
[1031] | 526 | self.doAdd(dn, fields) |
---|
[1041] | 527 | return self.getGroupPQuota(group, printer) |
---|
[1016] | 528 | |
---|
[1041] | 529 | def writePrinterPrices(self, printer) : |
---|
| 530 | """Write the printer's prices back into the storage.""" |
---|
| 531 | fields = { |
---|
| 532 | "pykotaPricePerPage" : str(printer.PricePerPage), |
---|
| 533 | "pykotaPricePerJob" : str(printer.PricePerJob), |
---|
| 534 | } |
---|
| 535 | self.doModify(printer.ident, fields) |
---|
[1016] | 536 | |
---|
[1041] | 537 | def writeUserLimitBy(self, user, limitby) : |
---|
| 538 | """Sets the user's limiting factor.""" |
---|
[1031] | 539 | fields = { |
---|
[1041] | 540 | "pykotaLimitBy" : limitby, |
---|
[1031] | 541 | } |
---|
[1041] | 542 | self.doModify(user.ident, fields) |
---|
[1016] | 543 | |
---|
[1041] | 544 | def writeGroupLimitBy(self, group, limitby) : |
---|
| 545 | """Sets the group's limiting factor.""" |
---|
[1031] | 546 | fields = { |
---|
[1041] | 547 | "pykotaLimitBy" : limitby, |
---|
[1031] | 548 | } |
---|
[1041] | 549 | self.doModify(group.ident, fields) |
---|
[1016] | 550 | |
---|
[1041] | 551 | def writeUserPQuotaDateLimit(self, userpquota, datelimit) : |
---|
| 552 | """Sets the date limit permanently for a user print quota.""" |
---|
[1031] | 553 | fields = { |
---|
| 554 | "pykotaDateLimit" : "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second), |
---|
| 555 | } |
---|
[1041] | 556 | return self.doModify(userpquota.ident, fields) |
---|
| 557 | |
---|
| 558 | def writeGroupPQuotaDateLimit(self, grouppquota, datelimit) : |
---|
| 559 | """Sets the date limit permanently for a group print quota.""" |
---|
[1031] | 560 | fields = { |
---|
| 561 | "pykotaDateLimit" : "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second), |
---|
| 562 | } |
---|
[1041] | 563 | return self.doModify(grouppquota.ident, fields) |
---|
[1016] | 564 | |
---|
[1041] | 565 | def writeUserPQuotaPagesCounters(self, userpquota, newpagecounter, newlifepagecounter) : |
---|
| 566 | """Sets the new page counters permanently for a user print quota.""" |
---|
| 567 | fields = { |
---|
| 568 | "pykotaPageCounter" : str(newpagecounter), |
---|
| 569 | "pykotaLifePageCounter" : str(newlifepagecounter), |
---|
| 570 | } |
---|
| 571 | return self.doModify(userpquota.ident, fields) |
---|
| 572 | |
---|
| 573 | def writeUserAccountBalance(self, user, newbalance, newlifetimepaid=None) : |
---|
| 574 | """Sets the new account balance and eventually new lifetime paid.""" |
---|
| 575 | fields = { |
---|
| 576 | "pykotaBalance" : str(newbalance), |
---|
| 577 | } |
---|
| 578 | if newlifetimepaid is not None : |
---|
| 579 | fields.update({ "pykotaLifeTimePaid" : str(newlifetimepaid) }) |
---|
| 580 | return self.doModify(user.idbalance, fields) |
---|
| 581 | |
---|
| 582 | def writeLastJobSize(self, lastjob, jobsize) : |
---|
| 583 | """Sets the last job's size permanently.""" |
---|
| 584 | fields = { |
---|
| 585 | "pykotaJobSize" : str(jobsize), |
---|
| 586 | } |
---|
| 587 | self.doModify(lastjob.ident, fields) |
---|
| 588 | |
---|
| 589 | def writeJobNew(self, printer, user, jobid, pagecounter, action, jobsize=None) : |
---|
| 590 | """Adds a job in a printer's history.""" |
---|
[1032] | 591 | uuid = self.genUUID() |
---|
| 592 | fields = { |
---|
| 593 | "objectClass" : ["pykotaObject", "pykotaJob"], |
---|
| 594 | "cn" : uuid, |
---|
[1041] | 595 | "pykotaUserName" : user.Name, |
---|
| 596 | "pykotaPrinterName" : printer.Name, |
---|
[1032] | 597 | "pykotaJobId" : jobid, |
---|
| 598 | "pykotaPrinterPageCounter" : str(pagecounter), |
---|
| 599 | "pykotaAction" : action, |
---|
| 600 | } |
---|
| 601 | if jobsize is not None : |
---|
| 602 | fields.update({ "pykotaJobSize" : str(jobsize) }) |
---|
| 603 | dn = "cn=%s,%s" % (uuid, self.info["jobbase"]) |
---|
| 604 | self.doAdd(dn, fields) |
---|
[1041] | 605 | if printer.LastJob.Exists : |
---|
[1032] | 606 | fields = { |
---|
| 607 | "pykotaLastJobIdent" : uuid, |
---|
| 608 | } |
---|
[1041] | 609 | self.doModify(printer.LastJob.lastjobident, fields) |
---|
[1032] | 610 | else : |
---|
| 611 | lastjuuid = self.genUUID() |
---|
[1067] | 612 | lastjdn = "cn=%s,%s" % (lastjuuid, self.info["lastjobbase"]) |
---|
[1032] | 613 | fields = { |
---|
| 614 | "objectClass" : ["pykotaObject", "pykotaLastJob"], |
---|
| 615 | "cn" : lastjuuid, |
---|
[1041] | 616 | "pykotaPrinterName" : printer.Name, |
---|
[1032] | 617 | "pykotaLastJobIdent" : uuid, |
---|
| 618 | } |
---|
| 619 | self.doAdd(lastjdn, fields) |
---|
[1041] | 620 | |
---|
| 621 | def writeUserPQuotaLimits(self, userpquota, softlimit, hardlimit) : |
---|
| 622 | """Sets soft and hard limits for a user quota.""" |
---|
| 623 | fields = { |
---|
| 624 | "pykotaSoftLimit" : str(softlimit), |
---|
| 625 | "pykotaHardLimit" : str(hardlimit), |
---|
| 626 | } |
---|
| 627 | self.doModify(userpquota.ident, fields) |
---|
| 628 | |
---|
| 629 | def writeGroupPQuotaLimits(self, grouppquota, softlimit, hardlimit) : |
---|
| 630 | """Sets soft and hard limits for a group quota on a specific printer given (groupid, printerid).""" |
---|
| 631 | fields = { |
---|
| 632 | "pykotaSoftLimit" : str(softlimit), |
---|
| 633 | "pykotaHardLimit" : str(hardlimit), |
---|
| 634 | } |
---|
| 635 | self.doModify(grouppquota.ident, fields) |
---|
| 636 | |
---|
| 637 | def deleteUser(self, user) : |
---|
| 638 | """Completely deletes an user from the Quota Storage.""" |
---|
| 639 | # TODO : What should we do if we delete the last person who used a given printer ? |
---|
| 640 | # TODO : we can't reassign the last job to the previous one, because next user would be |
---|
| 641 | # TODO : incorrectly charged (overcharged). |
---|
| 642 | result = self.doSearch("(&(objectClass=pykotaLastJob)(pykotaUserName=%s))" % user.Name, base=self.info["lastjobbase"]) |
---|
| 643 | for (ident, fields) in result : |
---|
| 644 | self.doDelete(ident) |
---|
| 645 | result = self.doSearch("(&(objectClass=pykotaJob)(pykotaUserName=%s))" % user.Name, base=self.info["jobbase"]) |
---|
| 646 | for (ident, fields) in result : |
---|
| 647 | self.doDelete(ident) |
---|
| 648 | result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaUserName=%s))" % user.Name, ["pykotaUserName"], base=self.info["userquotabase"]) |
---|
| 649 | for (ident, fields) in result : |
---|
| 650 | self.doDelete(ident) |
---|
| 651 | result = self.doSearch("objectClass=pykotaAccount", None, base=user.ident, scope=ldap.SCOPE_BASE) |
---|
[1032] | 652 | if result : |
---|
[1041] | 653 | fields = result[0][1] |
---|
| 654 | for k in fields.keys() : |
---|
| 655 | if k.startswith("pykota") : |
---|
| 656 | del fields[k] |
---|
| 657 | elif k.lower() == "objectclass" : |
---|
| 658 | todelete = [] |
---|
| 659 | for i in range(len(fields[k])) : |
---|
| 660 | if fields[k][i].startswith("pykota") : |
---|
| 661 | todelete.append(i) |
---|
| 662 | todelete.sort() |
---|
| 663 | todelete.reverse() |
---|
| 664 | for i in todelete : |
---|
| 665 | del fields[k][i] |
---|
| 666 | if fields.get("objectclass") : |
---|
| 667 | self.doModify(user.ident, fields, ignoreold=0) |
---|
| 668 | else : |
---|
| 669 | self.doDelete(user.ident) |
---|
| 670 | result = self.doSearch("(&(objectClass=pykotaAccountBalance)(pykotaUserName=%s))" % user.Name, ["pykotaUserName"], base=self.info["balancebase"]) |
---|
| 671 | for (ident, fields) in result : |
---|
| 672 | self.doDelete(ident) |
---|
| 673 | |
---|
| 674 | def deleteGroup(self, group) : |
---|
| 675 | """Completely deletes a group from the Quota Storage.""" |
---|
| 676 | result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaGroupName=%s))" % group.Name, ["pykotaGroupName"], base=self.info["groupquotabase"]) |
---|
| 677 | for (ident, fields) in result : |
---|
| 678 | self.doDelete(ident) |
---|
| 679 | result = self.doSearch("objectClass=pykotaGroup", None, base=group.ident, scope=ldap.SCOPE_BASE) |
---|
[1016] | 680 | if result : |
---|
[1027] | 681 | fields = result[0][1] |
---|
[1041] | 682 | for k in fields.keys() : |
---|
| 683 | if k.startswith("pykota") : |
---|
| 684 | del fields[k] |
---|
| 685 | elif k.lower() == "objectclass" : |
---|
| 686 | todelete = [] |
---|
| 687 | for i in range(len(fields[k])) : |
---|
| 688 | if fields[k][i].startswith("pykota") : |
---|
| 689 | todelete.append(i) |
---|
| 690 | todelete.sort() |
---|
| 691 | todelete.reverse() |
---|
| 692 | for i in todelete : |
---|
| 693 | del fields[k][i] |
---|
| 694 | if fields.get("objectclass") : |
---|
| 695 | self.doModify(group.ident, fields, ignoreold=0) |
---|
| 696 | else : |
---|
| 697 | self.doDelete(group.ident) |
---|
| 698 | |
---|