| | 8 | class TracForgePermissionModule(DefaultPermissionStore): |
|---|
| | 9 | """Enhanced permission module to allow for central management.""" |
|---|
| | 10 | |
|---|
| | 11 | master_path = Option('tracforge', 'master_path', |
|---|
| | 12 | doc='Path to master Trac') |
|---|
| | 13 | |
|---|
| | 14 | def get_user_permissions(self, username): |
|---|
| | 15 | subjects = [username] |
|---|
| | 16 | for provider in self.group_providers: |
|---|
| | 17 | subjects += list(provider.get_permission_groups(username)) |
|---|
| | 18 | |
|---|
| | 19 | actions = [] |
|---|
| | 20 | db = self.env.get_db_cnx() |
|---|
| | 21 | cursor = db.cursor() |
|---|
| | 22 | cursor.execute("SELECT username,action FROM permission") |
|---|
| | 23 | rows = cursor.fetchall() |
|---|
| | 24 | master_cursor = Environment(self.master_path).get_db_cnx().cursor() |
|---|
| | 25 | master_cursor.execute("SELECT username,action FROM tracforge_permission") |
|---|
| | 26 | rows += master_cursor.fetchall() |
|---|
| | 27 | while True: |
|---|
| | 28 | num_users = len(subjects) |
|---|
| | 29 | num_actions = len(actions) |
|---|
| | 30 | for user, action in rows: |
|---|
| | 31 | if user in subjects: |
|---|
| | 32 | if not action.islower() and action not in actions: |
|---|
| | 33 | actions.append(action) |
|---|
| | 34 | if action.islower() and action not in subjects: |
|---|
| | 35 | # action is actually the name of the permission group |
|---|
| | 36 | # here |
|---|
| | 37 | subjects.append(action) |
|---|
| | 38 | if num_users == len(subjects) and num_actions == len(actions): |
|---|
| | 39 | break |
|---|
| | 40 | return [action for action in actions if not action.islower()] |
|---|
| | 41 | |
|---|
| | 42 | def get_all_permissions(self): |
|---|
| | 43 | """Return all permissions for all users. |
|---|
| | 44 | |
|---|
| | 45 | The permissions are returned as a list of (subject, action) |
|---|
| | 46 | formatted tuples.""" |
|---|
| | 47 | db = self.env.get_db_cnx() |
|---|
| | 48 | cursor = db.cursor() |
|---|
| | 49 | cursor.execute("SELECT username,action FROM permission") |
|---|
| | 50 | rows = cursor.fetchall() |
|---|
| | 51 | if not self._extract_req().path_info.startswith('/admin/general/perm'): |
|---|
| | 52 | master_cursor = Environment(self.master_path).get_db_cnx().cursor() |
|---|
| | 53 | master_cursor.execute("SELECT username,action FROM tracforge_permission") |
|---|
| | 54 | rows += master_cursor.fetchall() |
|---|
| | 55 | return [(row[0], row[1]) for row in rows] |
|---|
| | 56 | |
|---|
| | 57 | def _extract_req(self): |
|---|
| | 58 | """Truly evil magic to scan for a variable called req in the stack.""" |
|---|
| | 59 | import inspect |
|---|
| | 60 | for record in inspect.stack(): |
|---|
| | 61 | locals = record[0].f_locals |
|---|
| | 62 | if 'req' in locals: |
|---|
| | 63 | return locals['req'] |
|---|
| | 64 | raise Exception, "Error: Penguins On Fire. Can't isolate a req." |
|---|