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