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