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