diff -rupN Trac-0.11.2.1-original/trac/perm.py Trac-0.11.2.1/trac/perm.py
|
old
|
new
|
class DefaultPermissionStore(Component): |
| 153 | 153 | permissions and groups. |
| 154 | 154 | """ |
| 155 | 155 | implements(IPermissionStore) |
| | 156 | |
| | 157 | # Number of seconds that the cached permission table is valid for. |
| | 158 | CACHE_EXPIRY = 5 |
| | 159 | # How frequently to clear the permission table cache |
| | 160 | CACHE_REAP_TIME = 60 |
| | 161 | |
| | 162 | def __init__(self): |
| | 163 | self.permission_table_cache = {} |
| | 164 | self.last_reap = time() |
| 156 | 165 | |
| 157 | 166 | group_providers = ExtensionPoint(IPermissionGroupProvider) |
| 158 | 167 | |
| … |
… |
class DefaultPermissionStore(Component): |
| 165 | 174 | the action column: such a record represents a group and not an actual |
| 166 | 175 | permission, and declares that the user is part of that group. |
| 167 | 176 | """ |
| 168 | | subjects = set([username]) |
| | 177 | subjects = set([username]) |
| 169 | 178 | for provider in self.group_providers: |
| 170 | 179 | subjects.update(provider.get_permission_groups(username)) |
| 171 | | |
| 172 | 180 | actions = set([]) |
| 173 | | db = self.env.get_db_cnx() |
| 174 | | cursor = db.cursor() |
| 175 | | cursor.execute("SELECT username,action FROM permission") |
| 176 | | rows = cursor.fetchall() |
| | 181 | |
| | 182 | # Begin caching work |
| | 183 | now = time() |
| | 184 | |
| | 185 | if now - self.last_reap > self.CACHE_REAP_TIME: |
| | 186 | self.permission_table_cache = {} |
| | 187 | self.last_reap = time() |
| | 188 | |
| | 189 | timestamp, rows = self.permission_table_cache.get('query', (0, None)) |
| | 190 | |
| | 191 | # Cache Hit? |
| | 192 | if now - timestamp > self.CACHE_EXPIRY: |
| | 193 | # no, proceed to query the database |
| | 194 | db = self.env.get_db_cnx() |
| | 195 | cursor = db.cursor() |
| | 196 | cursor.execute("SELECT username,action FROM permission") |
| | 197 | rows = cursor.fetchall() |
| | 198 | self.permission_table_cache['query'] = (now, rows) |
| | 199 | |
| 177 | 200 | while True: |
| 178 | 201 | num_users = len(subjects) |
| 179 | 202 | num_actions = len(actions) |
| … |
… |
class DefaultPermissionPolicy(Component) |
| 267 | 290 | # IPermissionPolicy methods |
| 268 | 291 | |
| 269 | 292 | def check_permission(self, action, username, resource, perm): |
| 270 | | now = time() |
| | 293 | now = time() |
| 271 | 294 | |
| 272 | 295 | if now - self.last_reap > self.CACHE_REAP_TIME: |
| 273 | 296 | self.permission_cache = {} |
| … |
… |
class PermissionSystem(Component): |
| 292 | 315 | implements(IPermissionRequestor) |
| 293 | 316 | |
| 294 | 317 | requestors = ExtensionPoint(IPermissionRequestor) |
| | 318 | |
| | 319 | # Number of seconds that cached userlist with a specific |
| | 320 | # permission is valid for. |
| | 321 | CACHE_EXPIRY = 5 |
| | 322 | # How frequently to clear the userlist w/permission cache |
| | 323 | CACHE_REAP_TIME = 60 |
| | 324 | |
| | 325 | def __init__(self): |
| | 326 | self.users_with_perm_cache = {} |
| | 327 | self.last_reap = time() |
| 295 | 328 | |
| 296 | 329 | store = ExtensionOption('trac', 'permission_store', IPermissionStore, |
| 297 | 330 | 'DefaultPermissionStore', |
| … |
… |
class PermissionSystem(Component): |
| 383 | 416 | for action in requestor.get_permission_actions(): |
| 384 | 417 | for child in action[1]: |
| 385 | 418 | parent_map.setdefault(child, []).append(action[0]) |
| 386 | | |
| 387 | | satisfying_perms = {} |
| | 419 | |
| | 420 | satisfying_perms = {} |
| 388 | 421 | def _append_with_parents(action): |
| 389 | 422 | if action in satisfying_perms: |
| 390 | 423 | return # avoid unneccesary work and infinite loops |
| … |
… |
class PermissionSystem(Component): |
| 392 | 425 | if action in parent_map: |
| 393 | 426 | map(_append_with_parents, parent_map[action]) |
| 394 | 427 | _append_with_parents(permission) |
| | 428 | |
| | 429 | # Begin caching work |
| | 430 | now = time() |
| | 431 | |
| | 432 | if now - self.last_reap > self.CACHE_REAP_TIME: |
| | 433 | self.users_with_perm_cache = {} |
| | 434 | self.last_reap = time() |
| | 435 | |
| | 436 | timestamp, userlist = self.users_with_perm_cache.get(permission, (0, None)) |
| | 437 | |
| | 438 | # Cache hit? |
| | 439 | if now - timestamp > self.CACHE_EXPIRY: |
| | 440 | # No, Pull userlist from database. |
| | 441 | userlist = self.store.get_users_with_permissions(satisfying_perms.keys()) |
| | 442 | self.users_with_perm_cache[permission] = (now, userlist) |
| 395 | 443 | |
| 396 | | return self.store.get_users_with_permissions(satisfying_perms.keys()) |
| | 444 | return userlist or None |
| 397 | 445 | |
| 398 | 446 | def expand_actions(self, actions): |
| 399 | 447 | """Helper method for expanding all meta actions.""" |