comparison pylons_app/lib/auth.py @ 239:b18f89d6d17f

Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 30 May 2010 19:49:40 +0200
parents a0116e944da1
children 3782a6d698af
comparison
equal deleted inserted replaced
238:a55c17874486 239:b18f89d6d17f
1 from functools import wraps 1 from functools import wraps
2 from pylons import session, url 2 from pylons import session, url, app_globals as g
3 from pylons.controllers.util import abort, redirect 3 from pylons.controllers.util import abort, redirect
4 from pylons_app.model import meta 4 from pylons_app.model import meta
5 from pylons_app.model.db import User 5 from pylons_app.model.db import User
6 from sqlalchemy.exc import OperationalError 6 from sqlalchemy.exc import OperationalError
7 from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound 7 from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
45 permissions = set() 45 permissions = set()
46 group = set() 46 group = set()
47 47
48 def __init__(self): 48 def __init__(self):
49 pass 49 pass
50 50
51
52
53 def set_available_permissions(config):
54 """
55 This function will propagate pylons globals with all available defined
56 permission given in db. We don't wannt to check each time from db for new
57 permissions since adding a new permission also requires application restart
58 ie. to decorate new views with the newly created permission
59 @param config:
60 """
61 from pylons_app.model.meta import Session
62 from pylons_app.model.db import Permission
63 logging.info('getting information about all available permissions')
64 sa = Session()
65 all_perms = sa.query(Permission).all()
66 config['pylons.app_globals'].available_permissions = [x.permission_name for x in all_perms]
67
68
69
51 #=============================================================================== 70 #===============================================================================
52 # DECORATORS 71 # DECORATORS
53 #=============================================================================== 72 #===============================================================================
54 class LoginRequired(object): 73 class LoginRequired(object):
55 """ 74 """
71 logging.info('user %s not authenticated', user.username) 90 logging.info('user %s not authenticated', user.username)
72 logging.info('redirecting to login page') 91 logging.info('redirecting to login page')
73 return redirect(url('login_home')) 92 return redirect(url('login_home'))
74 93
75 return _wrapper 94 return _wrapper
95
96 class PermsDecorator(object):
97
98 def __init__(self, *perms):
99 available_perms = g.available_permissions
100 for perm in perms:
101 if perm not in available_perms:
102 raise Exception("'%s' permission in not defined" % perm)
103 self.required_perms = set(perms)
104 self.user_perms = set([])#propagate this list from somewhere.
105
106 def __call__(self, func):
107 @wraps(func)
108 def _wrapper(*args, **kwargs):
109 logging.info('checking %s permissions %s for %s',
110 self.__class__.__name__[-3:], self.required_perms, func.__name__)
111
112 if self.check_permissions():
113 logging.info('Permission granted for %s', func.__name__)
114 return func(*args, **kwargs)
115
116 else:
117 logging.warning('Permission denied for %s', func.__name__)
118 #redirect with forbidden ret code
119 return redirect(url('access_denied'), 403)
120 return _wrapper
121
122
123 def check_permissions(self):
124 """
125 Dummy function for overiding
126 """
127 raise Exception('You have to write this function in child class')
128
129 class CheckPermissionAll(PermsDecorator):
130 """
131 Checks for access permission for all given predicates. All of them have to
132 be meet in order to fulfill the request
133 """
134
135 def check_permissions(self):
136 if self.required_perms.issubset(self.user_perms):
137 return True
138 return False
139
140
141 class CheckPermissionAny(PermsDecorator):
142 """
143 Checks for access permission for any of given predicates. In order to
144 fulfill the request any of predicates must be meet
145 """
146
147 def check_permissions(self):
148 if self.required_perms.intersection(self.user_perms):
149 return True
150 return False
151
152
153