[1016] | 1 | # PyKota |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[1016] | 3 | # |
---|
| 4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
| 6 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
| 7 | # This program is free software; you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
| 11 | # |
---|
| 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with this program; if not, write to the Free Software |
---|
| 19 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
| 23 | # $Log$ |
---|
[1200] | 24 | # Revision 1.36 2003/11/21 14:28:46 jalet |
---|
| 25 | # More complete job history. |
---|
| 26 | # |
---|
[1179] | 27 | # Revision 1.35 2003/11/12 13:06:37 jalet |
---|
| 28 | # Bug fix wrt no user/group name command line argument to edpykota |
---|
| 29 | # |
---|
[1170] | 30 | # Revision 1.34 2003/10/24 08:37:55 jalet |
---|
| 31 | # More complete messages in case of LDAP failure. |
---|
| 32 | # LDAP database connection is now unbound on exit too. |
---|
| 33 | # |
---|
[1149] | 34 | # Revision 1.33 2003/10/08 07:01:20 jalet |
---|
| 35 | # Job history can be disabled. |
---|
| 36 | # Some typos in README. |
---|
| 37 | # More messages in setup script. |
---|
| 38 | # |
---|
[1147] | 39 | # Revision 1.32 2003/10/07 14:23:25 jalet |
---|
| 40 | # More work on cache |
---|
| 41 | # |
---|
[1144] | 42 | # Revision 1.31 2003/10/07 09:07:30 jalet |
---|
| 43 | # Character encoding added to please latest version of Python |
---|
| 44 | # |
---|
[1141] | 45 | # Revision 1.30 2003/10/06 14:42:36 jalet |
---|
| 46 | # LDAP group access will be slower when cache is disabled, but at least code |
---|
| 47 | # is consistent with the rest of the caching mechanis, but at least code |
---|
| 48 | # is consistent with the rest of the caching mechanism |
---|
| 49 | # |
---|
[1137] | 50 | # Revision 1.29 2003/10/06 13:12:28 jalet |
---|
| 51 | # More work on caching |
---|
| 52 | # |
---|
[1133] | 53 | # Revision 1.28 2003/10/03 12:27:02 jalet |
---|
| 54 | # Several optimizations, especially with LDAP backend |
---|
| 55 | # |
---|
[1131] | 56 | # Revision 1.27 2003/10/03 08:57:55 jalet |
---|
| 57 | # Caching mechanism now caches all that's cacheable. |
---|
| 58 | # |
---|
[1130] | 59 | # Revision 1.26 2003/10/02 20:23:18 jalet |
---|
| 60 | # Storage caching mechanism added. |
---|
| 61 | # |
---|
[1119] | 62 | # Revision 1.25 2003/08/20 15:56:24 jalet |
---|
| 63 | # Better user and group deletion |
---|
| 64 | # |
---|
[1113] | 65 | # Revision 1.24 2003/07/29 20:55:17 jalet |
---|
| 66 | # 1.14 is out ! |
---|
| 67 | # |
---|
[1112] | 68 | # Revision 1.23 2003/07/29 19:52:32 jalet |
---|
| 69 | # Forgot to read the email field from LDAP |
---|
| 70 | # |
---|
[1111] | 71 | # Revision 1.22 2003/07/29 09:54:03 jalet |
---|
| 72 | # Added configurable LDAP mail attribute support |
---|
| 73 | # |
---|
[1105] | 74 | # Revision 1.21 2003/07/28 09:11:12 jalet |
---|
| 75 | # PyKota now tries to add its attributes intelligently in existing LDAP |
---|
| 76 | # directories. |
---|
| 77 | # |
---|
[1099] | 78 | # Revision 1.20 2003/07/25 10:41:30 jalet |
---|
| 79 | # Better documentation. |
---|
| 80 | # pykotme now displays the current user's account balance. |
---|
| 81 | # Some test changed in ldap module. |
---|
| 82 | # |
---|
[1084] | 83 | # Revision 1.19 2003/07/14 14:18:16 jalet |
---|
| 84 | # Wrong documentation strings |
---|
| 85 | # |
---|
[1081] | 86 | # Revision 1.18 2003/07/11 14:23:13 jalet |
---|
| 87 | # When adding an user only adds one object containing both the user and |
---|
| 88 | # its account balance instead of two objects. |
---|
| 89 | # |
---|
[1075] | 90 | # Revision 1.17 2003/07/07 12:51:07 jalet |
---|
| 91 | # Small fix |
---|
| 92 | # |
---|
[1070] | 93 | # Revision 1.16 2003/07/07 12:11:13 jalet |
---|
| 94 | # Small fix |
---|
| 95 | # |
---|
[1068] | 96 | # Revision 1.15 2003/07/07 11:49:24 jalet |
---|
| 97 | # Lots of small fixes with the help of PyChecker |
---|
| 98 | # |
---|
[1067] | 99 | # Revision 1.14 2003/07/07 08:33:18 jalet |
---|
| 100 | # Bug fix due to a typo in LDAP code |
---|
| 101 | # |
---|
[1062] | 102 | # Revision 1.13 2003/07/05 07:46:50 jalet |
---|
| 103 | # The previous bug fix was incomplete. |
---|
| 104 | # |
---|
[1051] | 105 | # Revision 1.12 2003/06/30 13:54:21 jalet |
---|
| 106 | # Sorts by user / group name |
---|
| 107 | # |
---|
[1041] | 108 | # Revision 1.11 2003/06/25 14:10:01 jalet |
---|
| 109 | # Hey, it may work (edpykota --reset excepted) ! |
---|
| 110 | # |
---|
[1032] | 111 | # Revision 1.10 2003/06/16 21:55:15 jalet |
---|
| 112 | # More work on LDAP, again. Problem detected. |
---|
| 113 | # |
---|
[1031] | 114 | # Revision 1.9 2003/06/16 11:59:09 jalet |
---|
| 115 | # More work on LDAP |
---|
| 116 | # |
---|
[1030] | 117 | # Revision 1.8 2003/06/15 22:26:52 jalet |
---|
| 118 | # More work on LDAP |
---|
| 119 | # |
---|
[1029] | 120 | # Revision 1.7 2003/06/14 22:44:21 jalet |
---|
| 121 | # More work on LDAP storage backend. |
---|
| 122 | # |
---|
[1027] | 123 | # Revision 1.6 2003/06/13 19:07:57 jalet |
---|
| 124 | # Two big bugs fixed, time to release something ;-) |
---|
| 125 | # |
---|
[1021] | 126 | # Revision 1.5 2003/06/10 16:37:54 jalet |
---|
| 127 | # Deletion of the second user which is not needed anymore. |
---|
| 128 | # Added a debug configuration field in /etc/pykota.conf |
---|
| 129 | # All queries can now be sent to the logger in debug mode, this will |
---|
| 130 | # greatly help improve performance when time for this will come. |
---|
| 131 | # |
---|
[1020] | 132 | # Revision 1.4 2003/06/10 10:45:32 jalet |
---|
| 133 | # Not implemented methods now raise an exception when called. |
---|
| 134 | # |
---|
[1018] | 135 | # Revision 1.3 2003/06/06 20:49:15 jalet |
---|
| 136 | # Very latest schema. UNTESTED. |
---|
| 137 | # |
---|
[1017] | 138 | # Revision 1.2 2003/06/06 14:21:08 jalet |
---|
| 139 | # New LDAP schema. |
---|
| 140 | # Small bug fixes. |
---|
| 141 | # |
---|
[1016] | 142 | # Revision 1.1 2003/06/05 11:19:13 jalet |
---|
| 143 | # More good work on LDAP storage. |
---|
| 144 | # |
---|
| 145 | # |
---|
| 146 | # |
---|
| 147 | |
---|
| 148 | # |
---|
| 149 | # My IANA assigned number, for |
---|
| 150 | # "Conseil Internet & Logiciels Libres, J�me Alet" |
---|
| 151 | # is 16868. Use this as a base to create the LDAP schema. |
---|
| 152 | # |
---|
| 153 | |
---|
[1030] | 154 | import time |
---|
| 155 | import md5 |
---|
[1016] | 156 | |
---|
[1130] | 157 | from pykota.storage import PyKotaStorageError,BaseStorage,StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageLastJob,StorageUserPQuota,StorageGroupPQuota |
---|
[1016] | 158 | |
---|
| 159 | try : |
---|
| 160 | import ldap |
---|
[1030] | 161 | from ldap import modlist |
---|
[1016] | 162 | except ImportError : |
---|
| 163 | import sys |
---|
| 164 | # TODO : to translate or not to translate ? |
---|
| 165 | raise PyKotaStorageError, "This python version (%s) doesn't seem to have the python-ldap module installed correctly." % sys.version.split()[0] |
---|
| 166 | |
---|
[1130] | 167 | class Storage(BaseStorage) : |
---|
[1021] | 168 | def __init__(self, pykotatool, host, dbname, user, passwd) : |
---|
[1016] | 169 | """Opens the LDAP connection.""" |
---|
| 170 | # raise PyKotaStorageError, "Sorry, the LDAP backend for PyKota is not yet implemented !" |
---|
[1130] | 171 | BaseStorage.__init__(self, pykotatool) |
---|
[1029] | 172 | self.info = pykotatool.config.getLDAPInfo() |
---|
[1016] | 173 | try : |
---|
| 174 | self.database = ldap.initialize(host) |
---|
| 175 | self.database.simple_bind_s(user, passwd) |
---|
| 176 | self.basedn = dbname |
---|
| 177 | except ldap.SERVER_DOWN : |
---|
| 178 | raise PyKotaStorageError, "LDAP backend for PyKota seems to be down !" # TODO : translate |
---|
[1031] | 179 | except ldap.LDAPError : |
---|
| 180 | raise PyKotaStorageError, "Unable to connect to LDAP server %s as %s." % (host, user) # TODO : translate |
---|
[1016] | 181 | else : |
---|
| 182 | self.closed = 0 |
---|
[1130] | 183 | self.tool.logdebug("Database opened (host=%s, dbname=%s, user=%s)" % (host, dbname, user)) |
---|
[1016] | 184 | |
---|
[1113] | 185 | def close(self) : |
---|
[1016] | 186 | """Closes the database connection.""" |
---|
| 187 | if not self.closed : |
---|
[1170] | 188 | self.database.unbind_s() |
---|
[1016] | 189 | self.closed = 1 |
---|
[1130] | 190 | self.tool.logdebug("Database closed.") |
---|
[1016] | 191 | |
---|
[1030] | 192 | def genUUID(self) : |
---|
| 193 | """Generates an unique identifier. |
---|
| 194 | |
---|
| 195 | TODO : this one is not unique accross several print servers, but should be sufficient for testing. |
---|
| 196 | """ |
---|
| 197 | return md5.md5("%s" % time.time()).hexdigest() |
---|
| 198 | |
---|
[1041] | 199 | def beginTransaction(self) : |
---|
| 200 | """Starts a transaction.""" |
---|
[1130] | 201 | self.tool.logdebug("Transaction begins... WARNING : No transactions in LDAP !") |
---|
[1041] | 202 | |
---|
| 203 | def commitTransaction(self) : |
---|
| 204 | """Commits a transaction.""" |
---|
[1130] | 205 | self.tool.logdebug("Transaction committed. WARNING : No transactions in LDAP !") |
---|
[1041] | 206 | |
---|
| 207 | def rollbackTransaction(self) : |
---|
| 208 | """Rollbacks a transaction.""" |
---|
[1130] | 209 | self.tool.logdebug("Transaction aborted. WARNING : No transaction in LDAP !") |
---|
[1041] | 210 | |
---|
[1030] | 211 | def doSearch(self, key, fields=None, base="", scope=ldap.SCOPE_SUBTREE) : |
---|
[1016] | 212 | """Does an LDAP search query.""" |
---|
| 213 | try : |
---|
[1030] | 214 | base = base or self.basedn |
---|
[1130] | 215 | self.tool.logdebug("QUERY : Filter : %s, BaseDN : %s, Scope : %s, Attributes : %s" % (key, base, scope, fields)) |
---|
[1017] | 216 | result = self.database.search_s(base or self.basedn, scope, key, fields) |
---|
[1170] | 217 | except ldap.LDAPError, msg : |
---|
| 218 | raise PyKotaStorageError, (_("Search for %s(%s) from %s(scope=%s) returned no answer.") % (key, fields, base, scope)) + " : %s" % str(msg) |
---|
[1016] | 219 | else : |
---|
[1130] | 220 | self.tool.logdebug("QUERY : Result : %s" % result) |
---|
[1016] | 221 | return result |
---|
[1030] | 222 | |
---|
| 223 | def doAdd(self, dn, fields) : |
---|
| 224 | """Adds an entry in the LDAP directory.""" |
---|
| 225 | try : |
---|
[1130] | 226 | self.tool.logdebug("QUERY : ADD(%s, %s)" % (dn, str(fields))) |
---|
[1030] | 227 | self.database.add_s(dn, modlist.addModlist(fields)) |
---|
[1170] | 228 | except ldap.LDAPError, msg : |
---|
| 229 | raise PyKotaStorageError, (_("Problem adding LDAP entry (%s, %s)") % (dn, str(fields))) + " : %s" % str(msg) |
---|
[1030] | 230 | else : |
---|
| 231 | return dn |
---|
| 232 | |
---|
[1041] | 233 | def doDelete(self, dn) : |
---|
| 234 | """Deletes an entry from the LDAP directory.""" |
---|
| 235 | try : |
---|
[1130] | 236 | self.tool.logdebug("QUERY : Delete(%s)" % dn) |
---|
[1041] | 237 | self.database.delete_s(dn) |
---|
[1170] | 238 | except ldap.LDAPError, msg : |
---|
| 239 | raise PyKotaStorageError, (_("Problem deleting LDAP entry (%s)") % dn) + " : %s" % str(msg) |
---|
[1041] | 240 | |
---|
| 241 | def doModify(self, dn, fields, ignoreold=1) : |
---|
[1030] | 242 | """Modifies an entry in the LDAP directory.""" |
---|
| 243 | try : |
---|
| 244 | oldentry = self.doSearch("objectClass=*", base=dn, scope=ldap.SCOPE_BASE) |
---|
[1130] | 245 | self.tool.logdebug("QUERY : Modify(%s, %s ==> %s)" % (dn, oldentry[0][1], fields)) |
---|
[1041] | 246 | self.database.modify_s(dn, modlist.modifyModlist(oldentry[0][1], fields, ignore_oldexistent=ignoreold)) |
---|
[1170] | 247 | except ldap.LDAPError, msg : |
---|
| 248 | raise PyKotaStorageError, (_("Problem modifying LDAP entry (%s, %s)") % (dn, fields)) + " : %s" % str(msg) |
---|
[1030] | 249 | else : |
---|
| 250 | return dn |
---|
[1016] | 251 | |
---|
[1179] | 252 | def getAllUsersNames(self) : |
---|
| 253 | """Extracts all user names.""" |
---|
| 254 | usernames = [] |
---|
| 255 | result = self.doSearch("objectClass=pykotaAccount", ["pykotaUserName"], base=self.info["userbase"]) |
---|
| 256 | if result : |
---|
| 257 | usernames = [record[1]["pykotaUserName"][0] for record in result] |
---|
| 258 | return usernames |
---|
| 259 | |
---|
| 260 | def getAllGroupsNames(self) : |
---|
| 261 | """Extracts all group names.""" |
---|
| 262 | groupnames = [] |
---|
| 263 | result = self.doSearch("objectClass=pykotaGroup", ["pykotaGroupName"], base=self.info["groupbase"]) |
---|
| 264 | if result : |
---|
| 265 | groupnames = [record[1]["pykotaGroupName"][0] for record in result] |
---|
| 266 | return groupnames |
---|
| 267 | |
---|
[1130] | 268 | def getUserFromBackend(self, username) : |
---|
[1041] | 269 | """Extracts user information given its name.""" |
---|
| 270 | user = StorageUser(self, username) |
---|
[1112] | 271 | result = self.doSearch("(&(objectClass=pykotaAccount)(|(pykotaUserName=%s)(%s=%s)))" % (username, self.info["userrdn"], username), ["pykotaLimitBy", self.info["usermail"]], base=self.info["userbase"]) |
---|
[1016] | 272 | if result : |
---|
[1041] | 273 | fields = result[0][1] |
---|
| 274 | user.ident = result[0][0] |
---|
[1111] | 275 | user.Email = fields.get(self.info["usermail"]) |
---|
| 276 | if user.Email is not None : |
---|
| 277 | user.Email = user.Email[0] |
---|
[1062] | 278 | user.LimitBy = fields.get("pykotaLimitBy") |
---|
| 279 | if user.LimitBy is not None : |
---|
| 280 | user.LimitBy = user.LimitBy[0] |
---|
[1041] | 281 | result = self.doSearch("(&(objectClass=pykotaAccountBalance)(|(pykotaUserName=%s)(%s=%s)))" % (username, self.info["balancerdn"], username), ["pykotaBalance", "pykotaLifeTimePaid"], base=self.info["balancebase"]) |
---|
| 282 | if result : |
---|
| 283 | fields = result[0][1] |
---|
| 284 | user.idbalance = result[0][0] |
---|
| 285 | user.AccountBalance = fields.get("pykotaBalance") |
---|
| 286 | if user.AccountBalance is not None : |
---|
| 287 | if user.AccountBalance[0].upper() == "NONE" : |
---|
| 288 | user.AccountBalance = None |
---|
| 289 | else : |
---|
| 290 | user.AccountBalance = float(user.AccountBalance[0]) |
---|
| 291 | user.AccountBalance = user.AccountBalance or 0.0 |
---|
| 292 | user.LifeTimePaid = fields.get("pykotaLifeTimePaid") |
---|
| 293 | if user.LifeTimePaid is not None : |
---|
| 294 | if user.LifeTimePaid[0].upper() == "NONE" : |
---|
| 295 | user.LifeTimePaid = None |
---|
| 296 | else : |
---|
| 297 | user.LifeTimePaid = float(user.LifeTimePaid[0]) |
---|
| 298 | user.LifeTimePaid = user.LifeTimePaid or 0.0 |
---|
| 299 | user.Exists = 1 |
---|
| 300 | return user |
---|
| 301 | |
---|
[1130] | 302 | def getGroupFromBackend(self, groupname) : |
---|
[1041] | 303 | """Extracts group information given its name.""" |
---|
| 304 | group = StorageGroup(self, groupname) |
---|
| 305 | result = self.doSearch("(&(objectClass=pykotaGroup)(|(pykotaGroupName=%s)(%s=%s)))" % (groupname, self.info["grouprdn"], groupname), ["pykotaLimitBy"], base=self.info["groupbase"]) |
---|
[1030] | 306 | if result : |
---|
[1041] | 307 | fields = result[0][1] |
---|
| 308 | group.ident = result[0][0] |
---|
[1062] | 309 | group.LimitBy = fields.get("pykotaLimitBy") |
---|
| 310 | if group.LimitBy is not None : |
---|
| 311 | group.LimitBy = group.LimitBy[0] |
---|
[1041] | 312 | group.AccountBalance = 0.0 |
---|
| 313 | group.LifeTimePaid = 0.0 |
---|
[1137] | 314 | for member in self.getGroupMembers(group) : |
---|
[1075] | 315 | if member.Exists : |
---|
| 316 | group.AccountBalance += member.AccountBalance |
---|
| 317 | group.LifeTimePaid += member.LifeTimePaid |
---|
[1041] | 318 | group.Exists = 1 |
---|
| 319 | return group |
---|
| 320 | |
---|
[1130] | 321 | def getPrinterFromBackend(self, printername) : |
---|
[1041] | 322 | """Extracts printer information given its name.""" |
---|
| 323 | printer = StoragePrinter(self, printername) |
---|
| 324 | result = self.doSearch("(&(objectClass=pykotaPrinter)(|(pykotaPrinterName=%s)(%s=%s)))" % (printername, self.info["printerrdn"], printername), ["pykotaPricePerPage", "pykotaPricePerJob"], base=self.info["printerbase"]) |
---|
[1016] | 325 | if result : |
---|
[1041] | 326 | fields = result[0][1] |
---|
| 327 | printer.ident = result[0][0] |
---|
| 328 | printer.PricePerJob = float(fields.get("pykotaPricePerJob")[0] or 0.0) |
---|
| 329 | printer.PricePerPage = float(fields.get("pykotaPricePerPage")[0] or 0.0) |
---|
| 330 | printer.LastJob = self.getPrinterLastJob(printer) |
---|
| 331 | printer.Exists = 1 |
---|
| 332 | return printer |
---|
| 333 | |
---|
[1130] | 334 | def getUserPQuotaFromBackend(self, user, printer) : |
---|
[1041] | 335 | """Extracts a user print quota.""" |
---|
| 336 | userpquota = StorageUserPQuota(self, user, printer) |
---|
| 337 | if user.Exists : |
---|
| 338 | result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaUserName=%s)(pykotaPrinterName=%s))" % (user.Name, printer.Name), ["pykotaPageCounter", "pykotaLifePageCounter", "pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit"], base=self.info["userquotabase"]) |
---|
[1017] | 339 | if result : |
---|
[1041] | 340 | fields = result[0][1] |
---|
| 341 | userpquota.ident = result[0][0] |
---|
| 342 | userpquota.PageCounter = int(fields.get("pykotaPageCounter")[0] or 0) |
---|
| 343 | userpquota.LifePageCounter = int(fields.get("pykotaLifePageCounter")[0] or 0) |
---|
| 344 | userpquota.SoftLimit = fields.get("pykotaSoftLimit") |
---|
| 345 | if userpquota.SoftLimit is not None : |
---|
| 346 | if userpquota.SoftLimit[0].upper() == "NONE" : |
---|
| 347 | userpquota.SoftLimit = None |
---|
| 348 | else : |
---|
| 349 | userpquota.SoftLimit = int(userpquota.SoftLimit[0]) |
---|
| 350 | userpquota.HardLimit = fields.get("pykotaHardLimit") |
---|
| 351 | if userpquota.HardLimit is not None : |
---|
| 352 | if userpquota.HardLimit[0].upper() == "NONE" : |
---|
| 353 | userpquota.HardLimit = None |
---|
| 354 | elif userpquota.HardLimit is not None : |
---|
| 355 | userpquota.HardLimit = int(userpquota.HardLimit[0]) |
---|
| 356 | userpquota.DateLimit = fields.get("pykotaDateLimit") |
---|
| 357 | if userpquota.DateLimit is not None : |
---|
| 358 | if userpquota.DateLimit[0].upper() == "NONE" : |
---|
| 359 | userpquota.DateLimit = None |
---|
| 360 | else : |
---|
| 361 | userpquota.DateLimit = userpquota.DateLimit[0] |
---|
| 362 | userpquota.Exists = 1 |
---|
| 363 | return userpquota |
---|
[1016] | 364 | |
---|
[1130] | 365 | def getGroupPQuotaFromBackend(self, group, printer) : |
---|
[1041] | 366 | """Extracts a group print quota.""" |
---|
| 367 | grouppquota = StorageGroupPQuota(self, group, printer) |
---|
| 368 | if group.Exists : |
---|
| 369 | result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaGroupName=%s)(pykotaPrinterName=%s))" % (group.Name, printer.Name), ["pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit"], base=self.info["groupquotabase"]) |
---|
| 370 | if result : |
---|
| 371 | fields = result[0][1] |
---|
| 372 | grouppquota.ident = result[0][0] |
---|
| 373 | grouppquota.SoftLimit = fields.get("pykotaSoftLimit") |
---|
| 374 | if grouppquota.SoftLimit is not None : |
---|
| 375 | if grouppquota.SoftLimit[0].upper() == "NONE" : |
---|
| 376 | grouppquota.SoftLimit = None |
---|
| 377 | else : |
---|
| 378 | grouppquota.SoftLimit = int(grouppquota.SoftLimit[0]) |
---|
| 379 | grouppquota.HardLimit = fields.get("pykotaHardLimit") |
---|
| 380 | if grouppquota.HardLimit is not None : |
---|
| 381 | if grouppquota.HardLimit[0].upper() == "NONE" : |
---|
| 382 | grouppquota.HardLimit = None |
---|
| 383 | else : |
---|
| 384 | grouppquota.HardLimit = int(grouppquota.HardLimit[0]) |
---|
| 385 | grouppquota.DateLimit = fields.get("pykotaDateLimit") |
---|
| 386 | if grouppquota.DateLimit is not None : |
---|
| 387 | if grouppquota.DateLimit[0].upper() == "NONE" : |
---|
| 388 | grouppquota.DateLimit = None |
---|
| 389 | else : |
---|
| 390 | grouppquota.DateLimit = grouppquota.DateLimit[0] |
---|
| 391 | grouppquota.PageCounter = 0 |
---|
| 392 | grouppquota.LifePageCounter = 0 |
---|
[1141] | 393 | usernamesfilter = "".join(["(pykotaUserName=%s)" % member.Name for member in self.getGroupMembers(group)]) |
---|
[1041] | 394 | result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaPrinterName=%s)(|%s))" % (printer.Name, usernamesfilter), ["pykotaPageCounter", "pykotaLifePageCounter"], base=self.info["userquotabase"]) |
---|
| 395 | if result : |
---|
| 396 | for userpquota in result : |
---|
| 397 | grouppquota.PageCounter += int(userpquota[1].get("pykotaPageCounter")[0] or 0) |
---|
| 398 | grouppquota.LifePageCounter += int(userpquota[1].get("pykotaLifePageCounter")[0] or 0) |
---|
| 399 | grouppquota.Exists = 1 |
---|
| 400 | return grouppquota |
---|
| 401 | |
---|
[1130] | 402 | def getPrinterLastJobFromBackend(self, printer) : |
---|
[1041] | 403 | """Extracts a printer's last job information.""" |
---|
| 404 | lastjob = StorageLastJob(self, printer) |
---|
| 405 | result = self.doSearch("(&(objectClass=pykotaLastjob)(|(pykotaPrinterName=%s)(%s=%s)))" % (printer.Name, self.info["printerrdn"], printer.Name), ["pykotaLastJobIdent"], base=self.info["lastjobbase"]) |
---|
[1016] | 406 | if result : |
---|
[1041] | 407 | lastjob.lastjobident = result[0][0] |
---|
| 408 | lastjobident = result[0][1]["pykotaLastJobIdent"][0] |
---|
| 409 | result = self.doSearch("objectClass=pykotaJob", ["pykotaUserName", "pykotaJobId", "pykotaPrinterPageCounter", "pykotaJobSize", "pykotaAction", "createTimestamp"], base="cn=%s,%s" % (lastjobident, self.info["jobbase"]), scope=ldap.SCOPE_BASE) |
---|
[1017] | 410 | if result : |
---|
[1041] | 411 | fields = result[0][1] |
---|
| 412 | lastjob.ident = result[0][0] |
---|
| 413 | lastjob.JobId = fields.get("pykotaJobId")[0] |
---|
| 414 | lastjob.User = self.getUser(fields.get("pykotaUserName")[0]) |
---|
| 415 | lastjob.PrinterPageCounter = int(fields.get("pykotaPrinterPageCounter")[0] or 0) |
---|
| 416 | lastjob.JobSize = int(fields.get("pykotaJobSize", [0])[0]) |
---|
| 417 | lastjob.JobAction = fields.get("pykotaAction")[0] |
---|
| 418 | date = fields.get("createTimestamp")[0] |
---|
| 419 | year = int(date[:4]) |
---|
| 420 | month = int(date[4:6]) |
---|
| 421 | day = int(date[6:8]) |
---|
| 422 | hour = int(date[8:10]) |
---|
| 423 | minute = int(date[10:12]) |
---|
| 424 | second = int(date[12:14]) |
---|
| 425 | lastjob.JobDate = "%04i-%02i-%02i %02i:%02i:%02i" % (year, month, day, hour, minute, second) |
---|
| 426 | lastjob.Exists = 1 |
---|
| 427 | return lastjob |
---|
[1016] | 428 | |
---|
[1137] | 429 | def getGroupMembersFromBackend(self, group) : |
---|
| 430 | """Returns the group's members list.""" |
---|
| 431 | groupmembers = [] |
---|
| 432 | result = self.doSearch("(&(objectClass=pykotaGroup)(|(pykotaGroupName=%s)(%s=%s)))" % (group.Name, self.info["grouprdn"], group.Name), [self.info["groupmembers"]], base=self.info["groupbase"]) |
---|
| 433 | if result : |
---|
| 434 | for username in result[0][1].get(self.info["groupmembers"], []) : |
---|
| 435 | groupmembers.append(self.getUser(username)) |
---|
| 436 | return groupmembers |
---|
| 437 | |
---|
| 438 | def getUserGroupsFromBackend(self, user) : |
---|
[1130] | 439 | """Returns the user's groups list.""" |
---|
| 440 | groups = [] |
---|
[1147] | 441 | result = self.doSearch("(&(objectClass=pykotaGroup)(%s=%s))" % (self.info["groupmembers"], user.Name), [self.info["grouprdn"], "pykotaGroupName", "pykotaLimitBy"], base=self.info["groupbase"]) |
---|
[1130] | 442 | if result : |
---|
| 443 | for (groupid, fields) in result : |
---|
[1147] | 444 | groupname = (fields.get("pykotaGroupName", [None]) or fields.get(self.info["grouprdn"], [None]))[0] |
---|
| 445 | group = self.getFromCache("GROUPS", groupname) |
---|
| 446 | if group is None : |
---|
| 447 | group = StorageGroup(self, groupname) |
---|
| 448 | group.ident = groupid |
---|
| 449 | group.LimitBy = fields.get("pykotaLimitBy") |
---|
| 450 | if group.LimitBy is not None : |
---|
| 451 | group.LimitBy = group.LimitBy[0] |
---|
| 452 | group.AccountBalance = 0.0 |
---|
| 453 | group.LifeTimePaid = 0.0 |
---|
| 454 | for member in self.getGroupMembers(group) : |
---|
| 455 | if member.Exists : |
---|
| 456 | group.AccountBalance += member.AccountBalance |
---|
| 457 | group.LifeTimePaid += member.LifeTimePaid |
---|
| 458 | group.Exists = 1 |
---|
| 459 | self.cacheEntry("GROUPS", group.Name, group) |
---|
| 460 | groups.append(group) |
---|
[1130] | 461 | return groups |
---|
| 462 | |
---|
[1041] | 463 | def getMatchingPrinters(self, printerpattern) : |
---|
| 464 | """Returns the list of all printers for which name matches a certain pattern.""" |
---|
| 465 | printers = [] |
---|
| 466 | # see comment at the same place in pgstorage.py |
---|
[1133] | 467 | result = self.doSearch("(&(objectClass=pykotaPrinter)(|%s))" % "".join(["(pykotaPrinterName=%s)" % pname for pname in printerpattern.split(",")]), ["pykotaPrinterName", "pykotaPricePerPage", "pykotaPricePerJob"], base=self.info["printerbase"]) |
---|
[1016] | 468 | if result : |
---|
[1041] | 469 | for (printerid, fields) in result : |
---|
| 470 | printername = fields["pykotaPrinterName"][0] |
---|
[1133] | 471 | printer = StoragePrinter(self, printername) |
---|
| 472 | printer.ident = printerid |
---|
| 473 | printer.PricePerJob = float(fields.get("pykotaPricePerJob")[0] or 0.0) |
---|
| 474 | printer.PricePerPage = float(fields.get("pykotaPricePerPage")[0] or 0.0) |
---|
| 475 | printer.LastJob = self.getPrinterLastJob(printer) |
---|
| 476 | printer.Exists = 1 |
---|
| 477 | printers.append(printer) |
---|
| 478 | self.cacheEntry("PRINTERS", printer.Name, printer) |
---|
[1041] | 479 | return printers |
---|
[1016] | 480 | |
---|
[1133] | 481 | def getPrinterUsersAndQuotas(self, printer, names=["*"]) : |
---|
[1041] | 482 | """Returns the list of users who uses a given printer, along with their quotas.""" |
---|
| 483 | usersandquotas = [] |
---|
[1133] | 484 | result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaPrinterName=%s)(|%s))" % (printer.Name, "".join(["(pykotaUserName=%s)" % uname for uname in names])), ["pykotaUserName", "pykotaPageCounter", "pykotaLifePageCounter", "pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit"], base=self.info["userquotabase"]) |
---|
[1041] | 485 | if result : |
---|
| 486 | for (userquotaid, fields) in result : |
---|
[1133] | 487 | user = self.getUser(fields.get("pykotaUserName")[0]) |
---|
| 488 | userpquota = StorageUserPQuota(self, user, printer) |
---|
| 489 | userpquota.ident = userquotaid |
---|
| 490 | userpquota.PageCounter = int(fields.get("pykotaPageCounter")[0] or 0) |
---|
| 491 | userpquota.LifePageCounter = int(fields.get("pykotaLifePageCounter")[0] or 0) |
---|
| 492 | userpquota.SoftLimit = fields.get("pykotaSoftLimit") |
---|
| 493 | if userpquota.SoftLimit is not None : |
---|
| 494 | if userpquota.SoftLimit[0].upper() == "NONE" : |
---|
| 495 | userpquota.SoftLimit = None |
---|
| 496 | else : |
---|
| 497 | userpquota.SoftLimit = int(userpquota.SoftLimit[0]) |
---|
| 498 | userpquota.HardLimit = fields.get("pykotaHardLimit") |
---|
| 499 | if userpquota.HardLimit is not None : |
---|
| 500 | if userpquota.HardLimit[0].upper() == "NONE" : |
---|
| 501 | userpquota.HardLimit = None |
---|
| 502 | elif userpquota.HardLimit is not None : |
---|
| 503 | userpquota.HardLimit = int(userpquota.HardLimit[0]) |
---|
| 504 | userpquota.DateLimit = fields.get("pykotaDateLimit") |
---|
| 505 | if userpquota.DateLimit is not None : |
---|
| 506 | if userpquota.DateLimit[0].upper() == "NONE" : |
---|
| 507 | userpquota.DateLimit = None |
---|
| 508 | else : |
---|
| 509 | userpquota.DateLimit = userpquota.DateLimit[0] |
---|
| 510 | userpquota.Exists = 1 |
---|
| 511 | usersandquotas.append((user, userpquota)) |
---|
| 512 | self.cacheEntry("USERPQUOTAS", "%s@%s" % (user.Name, printer.Name), userpquota) |
---|
[1051] | 513 | usersandquotas.sort(lambda x, y : cmp(x[0].Name, y[0].Name)) |
---|
[1041] | 514 | return usersandquotas |
---|
| 515 | |
---|
[1133] | 516 | def getPrinterGroupsAndQuotas(self, printer, names=["*"]) : |
---|
[1041] | 517 | """Returns the list of groups which uses a given printer, along with their quotas.""" |
---|
| 518 | groupsandquotas = [] |
---|
[1133] | 519 | result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaPrinterName=%s)(|%s))" % (printer.Name, "".join(["(pykotaGroupName=%s)" % gname for gname in names])), ["pykotaGroupName"], base=self.info["groupquotabase"]) |
---|
[1041] | 520 | if result : |
---|
| 521 | for (groupquotaid, fields) in result : |
---|
| 522 | group = self.getGroup(fields.get("pykotaGroupName")[0]) |
---|
[1133] | 523 | grouppquota = self.getGroupPQuota(group, printer) |
---|
| 524 | groupsandquotas.append((group, grouppquota)) |
---|
[1051] | 525 | groupsandquotas.sort(lambda x, y : cmp(x[0].Name, y[0].Name)) |
---|
[1041] | 526 | return groupsandquotas |
---|
[1016] | 527 | |
---|
| 528 | def addPrinter(self, printername) : |
---|
[1041] | 529 | """Adds a printer to the quota storage, returns it.""" |
---|
[1030] | 530 | fields = { self.info["printerrdn"] : printername, |
---|
| 531 | "objectClass" : ["pykotaObject", "pykotaPrinter"], |
---|
[1041] | 532 | "cn" : printername, |
---|
[1030] | 533 | "pykotaPrinterName" : printername, |
---|
| 534 | "pykotaPricePerPage" : "0.0", |
---|
| 535 | "pykotaPricePerJob" : "0.0", |
---|
| 536 | } |
---|
| 537 | dn = "%s=%s,%s" % (self.info["printerrdn"], printername, self.info["printerbase"]) |
---|
[1041] | 538 | self.doAdd(dn, fields) |
---|
| 539 | return self.getPrinter(printername) |
---|
[1016] | 540 | |
---|
[1041] | 541 | def addUser(self, user) : |
---|
| 542 | """Adds a user to the quota storage, returns it.""" |
---|
[1105] | 543 | newfields = { |
---|
| 544 | "pykotaUserName" : user.Name, |
---|
| 545 | "pykotaLimitBY" : (user.LimitBy or "quota"), |
---|
| 546 | "pykotaBalance" : str(user.AccountBalance or 0.0), |
---|
| 547 | "pykotaLifeTimePaid" : str(user.LifeTimePaid or 0.0), |
---|
| 548 | } |
---|
| 549 | mustadd = 1 |
---|
| 550 | if self.info["newuser"].lower() != 'below' : |
---|
| 551 | result = self.doSearch("(&(objectClass=%s)(%s=%s))" % (self.info["newuser"], self.info["userrdn"], user.Name), None, base=self.info["userbase"]) |
---|
| 552 | if result : |
---|
| 553 | (dn, fields) = result[0] |
---|
| 554 | fields["objectClass"].extend(["pykotaAccount", "pykotaAccountBalance"]) |
---|
| 555 | fields.update(newfields) |
---|
| 556 | self.doModify(dn, fields) |
---|
| 557 | mustadd = 0 |
---|
| 558 | |
---|
| 559 | if mustadd : |
---|
| 560 | fields = { self.info["userrdn"] : user.Name, |
---|
| 561 | "objectClass" : ["pykotaObject", "pykotaAccount", "pykotaAccountBalance"], |
---|
| 562 | "cn" : user.Name, |
---|
| 563 | } |
---|
| 564 | fields.update(newfields) |
---|
| 565 | dn = "%s=%s,%s" % (self.info["userrdn"], user.Name, self.info["userbase"]) |
---|
| 566 | self.doAdd(dn, fields) |
---|
[1041] | 567 | return self.getUser(user.Name) |
---|
[1016] | 568 | |
---|
[1041] | 569 | def addGroup(self, group) : |
---|
| 570 | """Adds a group to the quota storage, returns it.""" |
---|
[1105] | 571 | newfields = { |
---|
| 572 | "pykotaGroupName" : group.Name, |
---|
| 573 | "pykotaLimitBY" : (group.LimitBy or "quota"), |
---|
| 574 | } |
---|
| 575 | mustadd = 1 |
---|
| 576 | if self.info["newgroup"].lower() != 'below' : |
---|
| 577 | result = self.doSearch("(&(objectClass=%s)(%s=%s))" % (self.info["newgroup"], self.info["grouprdn"], group.Name), None, base=self.info["groupbase"]) |
---|
| 578 | if result : |
---|
| 579 | (dn, fields) = result[0] |
---|
| 580 | fields["objectClass"].extend(["pykotaGroup"]) |
---|
| 581 | fields.update(newfields) |
---|
| 582 | self.doModify(dn, fields) |
---|
| 583 | mustadd = 0 |
---|
| 584 | |
---|
| 585 | if mustadd : |
---|
| 586 | fields = { self.info["grouprdn"] : group.Name, |
---|
| 587 | "objectClass" : ["pykotaObject", "pykotaGroup"], |
---|
| 588 | "cn" : group.Name, |
---|
| 589 | } |
---|
| 590 | fields.update(newfields) |
---|
| 591 | dn = "%s=%s,%s" % (self.info["grouprdn"], group.Name, self.info["groupbase"]) |
---|
| 592 | self.doAdd(dn, fields) |
---|
[1041] | 593 | return self.getGroup(group.Name) |
---|
[1016] | 594 | |
---|
[1041] | 595 | def addUserToGroup(self, user, group) : |
---|
| 596 | """Adds an user to a group.""" |
---|
[1141] | 597 | if user.Name not in [u.Name for u in self.getGroupMembers(group)] : |
---|
[1041] | 598 | result = self.doSearch("objectClass=pykotaGroup", None, base=group.ident, scope=ldap.SCOPE_BASE) |
---|
| 599 | if result : |
---|
| 600 | fields = result[0][1] |
---|
[1070] | 601 | if not fields.has_key(self.info["groupmembers"]) : |
---|
| 602 | fields[self.info["groupmembers"]] = [] |
---|
[1041] | 603 | fields[self.info["groupmembers"]].append(user.Name) |
---|
| 604 | self.doModify(group.ident, fields) |
---|
| 605 | group.Members.append(user) |
---|
| 606 | |
---|
| 607 | def addUserPQuota(self, user, printer) : |
---|
| 608 | """Initializes a user print quota on a printer.""" |
---|
[1030] | 609 | uuid = self.genUUID() |
---|
[1041] | 610 | fields = { "cn" : uuid, |
---|
| 611 | "objectClass" : ["pykotaObject", "pykotaUserPQuota"], |
---|
| 612 | "pykotaUserName" : user.Name, |
---|
| 613 | "pykotaPrinterName" : printer.Name, |
---|
| 614 | "pykotaDateLimit" : "None", |
---|
[1030] | 615 | "pykotaPageCounter" : "0", |
---|
| 616 | "pykotaLifePageCounter" : "0", |
---|
| 617 | } |
---|
| 618 | dn = "cn=%s,%s" % (uuid, self.info["userquotabase"]) |
---|
[1031] | 619 | self.doAdd(dn, fields) |
---|
[1041] | 620 | return self.getUserPQuota(user, printer) |
---|
[1016] | 621 | |
---|
[1041] | 622 | def addGroupPQuota(self, group, printer) : |
---|
| 623 | """Initializes a group print quota on a printer.""" |
---|
[1030] | 624 | uuid = self.genUUID() |
---|
[1041] | 625 | fields = { "cn" : uuid, |
---|
| 626 | "objectClass" : ["pykotaObject", "pykotaGroupPQuota"], |
---|
| 627 | "pykotaGroupName" : group.Name, |
---|
| 628 | "pykotaPrinterName" : printer.Name, |
---|
[1030] | 629 | "pykotaDateLimit" : "None", |
---|
| 630 | } |
---|
| 631 | dn = "cn=%s,%s" % (uuid, self.info["groupquotabase"]) |
---|
[1031] | 632 | self.doAdd(dn, fields) |
---|
[1041] | 633 | return self.getGroupPQuota(group, printer) |
---|
[1016] | 634 | |
---|
[1041] | 635 | def writePrinterPrices(self, printer) : |
---|
| 636 | """Write the printer's prices back into the storage.""" |
---|
| 637 | fields = { |
---|
| 638 | "pykotaPricePerPage" : str(printer.PricePerPage), |
---|
| 639 | "pykotaPricePerJob" : str(printer.PricePerJob), |
---|
| 640 | } |
---|
| 641 | self.doModify(printer.ident, fields) |
---|
[1016] | 642 | |
---|
[1041] | 643 | def writeUserLimitBy(self, user, limitby) : |
---|
| 644 | """Sets the user's limiting factor.""" |
---|
[1031] | 645 | fields = { |
---|
[1041] | 646 | "pykotaLimitBy" : limitby, |
---|
[1031] | 647 | } |
---|
[1041] | 648 | self.doModify(user.ident, fields) |
---|
[1016] | 649 | |
---|
[1041] | 650 | def writeGroupLimitBy(self, group, limitby) : |
---|
| 651 | """Sets the group's limiting factor.""" |
---|
[1031] | 652 | fields = { |
---|
[1041] | 653 | "pykotaLimitBy" : limitby, |
---|
[1031] | 654 | } |
---|
[1041] | 655 | self.doModify(group.ident, fields) |
---|
[1016] | 656 | |
---|
[1041] | 657 | def writeUserPQuotaDateLimit(self, userpquota, datelimit) : |
---|
| 658 | """Sets the date limit permanently for a user print quota.""" |
---|
[1031] | 659 | fields = { |
---|
| 660 | "pykotaDateLimit" : "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second), |
---|
| 661 | } |
---|
[1041] | 662 | return self.doModify(userpquota.ident, fields) |
---|
| 663 | |
---|
| 664 | def writeGroupPQuotaDateLimit(self, grouppquota, datelimit) : |
---|
| 665 | """Sets the date limit permanently for a group print quota.""" |
---|
[1031] | 666 | fields = { |
---|
| 667 | "pykotaDateLimit" : "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second), |
---|
| 668 | } |
---|
[1041] | 669 | return self.doModify(grouppquota.ident, fields) |
---|
[1016] | 670 | |
---|
[1041] | 671 | def writeUserPQuotaPagesCounters(self, userpquota, newpagecounter, newlifepagecounter) : |
---|
| 672 | """Sets the new page counters permanently for a user print quota.""" |
---|
| 673 | fields = { |
---|
| 674 | "pykotaPageCounter" : str(newpagecounter), |
---|
| 675 | "pykotaLifePageCounter" : str(newlifepagecounter), |
---|
| 676 | } |
---|
| 677 | return self.doModify(userpquota.ident, fields) |
---|
| 678 | |
---|
| 679 | def writeUserAccountBalance(self, user, newbalance, newlifetimepaid=None) : |
---|
| 680 | """Sets the new account balance and eventually new lifetime paid.""" |
---|
| 681 | fields = { |
---|
| 682 | "pykotaBalance" : str(newbalance), |
---|
| 683 | } |
---|
| 684 | if newlifetimepaid is not None : |
---|
| 685 | fields.update({ "pykotaLifeTimePaid" : str(newlifetimepaid) }) |
---|
| 686 | return self.doModify(user.idbalance, fields) |
---|
| 687 | |
---|
| 688 | def writeLastJobSize(self, lastjob, jobsize) : |
---|
| 689 | """Sets the last job's size permanently.""" |
---|
| 690 | fields = { |
---|
| 691 | "pykotaJobSize" : str(jobsize), |
---|
| 692 | } |
---|
| 693 | self.doModify(lastjob.ident, fields) |
---|
| 694 | |
---|
[1200] | 695 | def writeJobNew(self, printer, user, jobid, pagecounter, action, jobsize=None, filename=None, title=None, copies=None, options=None) : |
---|
[1041] | 696 | """Adds a job in a printer's history.""" |
---|
[1149] | 697 | if (not self.disablehistory) or (not printer.LastJob.Exists) : |
---|
| 698 | uuid = self.genUUID() |
---|
| 699 | dn = "cn=%s,%s" % (uuid, self.info["jobbase"]) |
---|
| 700 | else : |
---|
| 701 | uuid = printer.LastJob.ident[3:].split(",")[0] |
---|
| 702 | dn = printer.LastJob.ident |
---|
[1032] | 703 | fields = { |
---|
| 704 | "objectClass" : ["pykotaObject", "pykotaJob"], |
---|
| 705 | "cn" : uuid, |
---|
[1041] | 706 | "pykotaUserName" : user.Name, |
---|
| 707 | "pykotaPrinterName" : printer.Name, |
---|
[1032] | 708 | "pykotaJobId" : jobid, |
---|
| 709 | "pykotaPrinterPageCounter" : str(pagecounter), |
---|
| 710 | "pykotaAction" : action, |
---|
[1200] | 711 | "pykotaFileName" : str(filename), |
---|
| 712 | "pykotaTitle" : str(title), |
---|
| 713 | "pykotaCopies" : str(copies), |
---|
| 714 | "pykotaOptions" : str(options), |
---|
[1032] | 715 | } |
---|
[1149] | 716 | if (not self.disablehistory) or (not printer.LastJob.Exists) : |
---|
| 717 | if jobsize is not None : |
---|
| 718 | fields.update({ "pykotaJobSize" : str(jobsize) }) |
---|
| 719 | self.doAdd(dn, fields) |
---|
| 720 | else : |
---|
| 721 | # here we explicitly want to reset jobsize to 'None' if needed |
---|
[1032] | 722 | fields.update({ "pykotaJobSize" : str(jobsize) }) |
---|
[1149] | 723 | self.doModify(dn, fields) |
---|
| 724 | |
---|
[1041] | 725 | if printer.LastJob.Exists : |
---|
[1032] | 726 | fields = { |
---|
| 727 | "pykotaLastJobIdent" : uuid, |
---|
| 728 | } |
---|
[1041] | 729 | self.doModify(printer.LastJob.lastjobident, fields) |
---|
[1032] | 730 | else : |
---|
| 731 | lastjuuid = self.genUUID() |
---|
[1067] | 732 | lastjdn = "cn=%s,%s" % (lastjuuid, self.info["lastjobbase"]) |
---|
[1032] | 733 | fields = { |
---|
| 734 | "objectClass" : ["pykotaObject", "pykotaLastJob"], |
---|
| 735 | "cn" : lastjuuid, |
---|
[1041] | 736 | "pykotaPrinterName" : printer.Name, |
---|
[1032] | 737 | "pykotaLastJobIdent" : uuid, |
---|
| 738 | } |
---|
| 739 | self.doAdd(lastjdn, fields) |
---|
[1041] | 740 | |
---|
| 741 | def writeUserPQuotaLimits(self, userpquota, softlimit, hardlimit) : |
---|
| 742 | """Sets soft and hard limits for a user quota.""" |
---|
| 743 | fields = { |
---|
| 744 | "pykotaSoftLimit" : str(softlimit), |
---|
| 745 | "pykotaHardLimit" : str(hardlimit), |
---|
| 746 | } |
---|
| 747 | self.doModify(userpquota.ident, fields) |
---|
| 748 | |
---|
| 749 | def writeGroupPQuotaLimits(self, grouppquota, softlimit, hardlimit) : |
---|
[1084] | 750 | """Sets soft and hard limits for a group quota on a specific printer.""" |
---|
[1041] | 751 | fields = { |
---|
| 752 | "pykotaSoftLimit" : str(softlimit), |
---|
| 753 | "pykotaHardLimit" : str(hardlimit), |
---|
| 754 | } |
---|
| 755 | self.doModify(grouppquota.ident, fields) |
---|
| 756 | |
---|
| 757 | def deleteUser(self, user) : |
---|
| 758 | """Completely deletes an user from the Quota Storage.""" |
---|
| 759 | # TODO : What should we do if we delete the last person who used a given printer ? |
---|
| 760 | # TODO : we can't reassign the last job to the previous one, because next user would be |
---|
| 761 | # TODO : incorrectly charged (overcharged). |
---|
| 762 | result = self.doSearch("(&(objectClass=pykotaLastJob)(pykotaUserName=%s))" % user.Name, base=self.info["lastjobbase"]) |
---|
| 763 | for (ident, fields) in result : |
---|
| 764 | self.doDelete(ident) |
---|
| 765 | result = self.doSearch("(&(objectClass=pykotaJob)(pykotaUserName=%s))" % user.Name, base=self.info["jobbase"]) |
---|
| 766 | for (ident, fields) in result : |
---|
| 767 | self.doDelete(ident) |
---|
| 768 | result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaUserName=%s))" % user.Name, ["pykotaUserName"], base=self.info["userquotabase"]) |
---|
| 769 | for (ident, fields) in result : |
---|
| 770 | self.doDelete(ident) |
---|
| 771 | result = self.doSearch("objectClass=pykotaAccount", None, base=user.ident, scope=ldap.SCOPE_BASE) |
---|
[1032] | 772 | if result : |
---|
[1041] | 773 | fields = result[0][1] |
---|
| 774 | for k in fields.keys() : |
---|
| 775 | if k.startswith("pykota") : |
---|
| 776 | del fields[k] |
---|
| 777 | elif k.lower() == "objectclass" : |
---|
| 778 | todelete = [] |
---|
| 779 | for i in range(len(fields[k])) : |
---|
| 780 | if fields[k][i].startswith("pykota") : |
---|
| 781 | todelete.append(i) |
---|
| 782 | todelete.sort() |
---|
| 783 | todelete.reverse() |
---|
| 784 | for i in todelete : |
---|
| 785 | del fields[k][i] |
---|
[1119] | 786 | if fields.get("objectClass") or fields.get("objectclass") : |
---|
[1041] | 787 | self.doModify(user.ident, fields, ignoreold=0) |
---|
| 788 | else : |
---|
| 789 | self.doDelete(user.ident) |
---|
| 790 | result = self.doSearch("(&(objectClass=pykotaAccountBalance)(pykotaUserName=%s))" % user.Name, ["pykotaUserName"], base=self.info["balancebase"]) |
---|
| 791 | for (ident, fields) in result : |
---|
| 792 | self.doDelete(ident) |
---|
| 793 | |
---|
| 794 | def deleteGroup(self, group) : |
---|
| 795 | """Completely deletes a group from the Quota Storage.""" |
---|
| 796 | result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaGroupName=%s))" % group.Name, ["pykotaGroupName"], base=self.info["groupquotabase"]) |
---|
| 797 | for (ident, fields) in result : |
---|
| 798 | self.doDelete(ident) |
---|
| 799 | result = self.doSearch("objectClass=pykotaGroup", None, base=group.ident, scope=ldap.SCOPE_BASE) |
---|
[1016] | 800 | if result : |
---|
[1027] | 801 | fields = result[0][1] |
---|
[1041] | 802 | for k in fields.keys() : |
---|
| 803 | if k.startswith("pykota") : |
---|
| 804 | del fields[k] |
---|
| 805 | elif k.lower() == "objectclass" : |
---|
| 806 | todelete = [] |
---|
| 807 | for i in range(len(fields[k])) : |
---|
| 808 | if fields[k][i].startswith("pykota") : |
---|
| 809 | todelete.append(i) |
---|
| 810 | todelete.sort() |
---|
| 811 | todelete.reverse() |
---|
| 812 | for i in todelete : |
---|
| 813 | del fields[k][i] |
---|
[1119] | 814 | if fields.get("objectClass") or fields.get("objectclass") : |
---|
[1041] | 815 | self.doModify(group.ident, fields, ignoreold=0) |
---|
| 816 | else : |
---|
| 817 | self.doDelete(group.ident) |
---|
| 818 | |
---|