| 1 | # svntrac
|
|---|
| 2 | #
|
|---|
| 3 | # Copyright (C) 2003 Xyche Software
|
|---|
| 4 | #
|
|---|
| 5 | # svntrac is free software; you can redistribute it and/or
|
|---|
| 6 | # modify it under the terms of the GNU General Public License as
|
|---|
| 7 | # published by the Free Software Foundation; either version 2 of the
|
|---|
| 8 | # License, or (at your option) any later version.
|
|---|
| 9 | #
|
|---|
| 10 | # svntrac is distributed in the hope that it will be useful,
|
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 13 | # General Public License for more details.
|
|---|
| 14 | #
|
|---|
| 15 | # You should have received a copy of the GNU General Public License
|
|---|
| 16 | # along with this program; if not, write to the Free Software
|
|---|
| 17 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|---|
| 18 | #
|
|---|
| 19 | # Author: Jonas Borgström <jonas@xyche.com>
|
|---|
| 20 |
|
|---|
| 21 | from exceptions import StandardError
|
|---|
| 22 | from auth import get_authname
|
|---|
| 23 | from db import get_connection
|
|---|
| 24 |
|
|---|
| 25 | perm_cache = {}
|
|---|
| 26 |
|
|---|
| 27 | # permissions
|
|---|
| 28 | LOG_VIEW = 'LOG_VIEW'
|
|---|
| 29 |
|
|---|
| 30 | FILE_VIEW = 'FILE_VIEW'
|
|---|
| 31 |
|
|---|
| 32 | CHANGESET_VIEW = 'CHANGESET_VIEW'
|
|---|
| 33 |
|
|---|
| 34 | BROWSER_VIEW = 'BROWSER_VIEW'
|
|---|
| 35 |
|
|---|
| 36 | TICKET_VIEW = 'TICKET_VIEW'
|
|---|
| 37 | TICKET_CREATE = 'TICKET_CREATE'
|
|---|
| 38 | TICKET_MODIFY = 'TICKET_MODIFY'
|
|---|
| 39 |
|
|---|
| 40 | REPORT_VIEW = 'REPORT_VIEW'
|
|---|
| 41 | REPORT_CREATE = 'REPORT_CREATE'
|
|---|
| 42 | REPORT_MODIFY = 'REPORT_MODIFY'
|
|---|
| 43 | REPORT_DELETE = 'REPORT_DELETE'
|
|---|
| 44 |
|
|---|
| 45 | WIKI_VIEW = 'WIKI_VIEW'
|
|---|
| 46 | WIKI_CREATE = 'WIKI_CREATE'
|
|---|
| 47 | WIKI_MODIFY = 'WIKI_MODIFY'
|
|---|
| 48 | WIKI_DELETE = 'WIKI_DELETE'
|
|---|
| 49 |
|
|---|
| 50 | TIMELINE_VIEW = 'TIMELINE_VIEW'
|
|---|
| 51 |
|
|---|
| 52 | meta_permission = {
|
|---|
| 53 | 'TICKET_ADMIN': [TICKET_VIEW, TICKET_CREATE, TICKET_MODIFY],
|
|---|
| 54 | 'REPORT_ADMIN': [REPORT_VIEW, REPORT_CREATE, REPORT_MODIFY, REPORT_DELETE],
|
|---|
| 55 | 'WIKI_ADMIN' : [WIKI_VIEW, WIKI_CREATE, WIKI_MODIFY, WIKI_DELETE]
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | class PermissionError (StandardError):
|
|---|
| 59 | """Insufficient svntrac permissions to complete the operation"""
|
|---|
| 60 | def __init__ (self, action):
|
|---|
| 61 | self.action = action
|
|---|
| 62 | def __str__ (self):
|
|---|
| 63 | return '%s privileges required to perform this operation' % self.action
|
|---|
| 64 |
|
|---|
| 65 | def cache_permissions ():
|
|---|
| 66 | global perm_cache, meta_permission
|
|---|
| 67 |
|
|---|
| 68 | cnx = get_connection ()
|
|---|
| 69 | rs = cnx.db.execute ("SELECT action FROM permission "
|
|---|
| 70 | "WHERE user='%s' OR user='anonymous'" %
|
|---|
| 71 | get_authname ())
|
|---|
| 72 | for row in rs.row_list:
|
|---|
| 73 | action = row[0]
|
|---|
| 74 | if meta_permission.has_key(action):
|
|---|
| 75 | map (lambda action: perm_cache.__setitem__(action, 1),
|
|---|
| 76 | meta_permission[action])
|
|---|
| 77 | perm_cache[action] = 1
|
|---|
| 78 |
|
|---|
| 79 | def has_permission (action):
|
|---|
| 80 | global perm_cache
|
|---|
| 81 | return perm_cache.has_key (action)
|
|---|
| 82 |
|
|---|
| 83 | def assert_permission (action):
|
|---|
| 84 | global perm_cache
|
|---|
| 85 | if not perm_cache.has_key (action):
|
|---|
| 86 | raise PermissionError (action)
|
|---|