Skip to content

Commit 9d648ed

Browse files
author
Richard Jones
committed
Getting closer to a good framework.
1 parent 6e983eb commit 9d648ed

File tree

1 file changed

+88
-48
lines changed

1 file changed

+88
-48
lines changed

doc/security.txt

Lines changed: 88 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Security Mechanisms
33
===================
44

5-
:Version: $Revision: 1.9 $
5+
:Version: $Revision: 1.10 $
66

77
Current situation
88
=================
@@ -174,62 +174,101 @@ A permission module defines::
174174
- permissions (PermissionClass Multilink)
175175
'''
176176

177-
def hasClassPermission(db, classname, permission, userid):
178-
''' Look through all the Roles, and hence Permissions, and see if
179-
"permission" is there for the specified classname.
177+
class Security:
178+
def __init__(self, db):
179+
''' Initialise the permission and role classes, and add in the
180+
base roles (for admin user).
181+
'''
182+
# use a weak ref to avoid circularity
183+
self.db = weakref.proxy(db)
180184

181-
'''
185+
# create the permission class instance (we only need one))
186+
self.permission = PermissionClass(db, "permission")
182187

183-
def hasNodePermission(db, classname, nodeid, userid, properties):
184-
''' Check the named properties of the given node to see if the userid
185-
appears in them. If it does, then the user is granted this
186-
permission check.
188+
# create the role class instance (we only need one)
189+
self.role = RoleClass(db, "role")
187190

188-
'propspec' consists of a list of property names. The property
189-
names must be the name of a property of classname, or a
190-
KeyError is raised. That property must be a Link or Multilink
191-
property, or a TypeError is raised.
191+
# the default Roles
192+
self.addRole(name="User", description="A regular user, no privs")
193+
self.addRole(name="Admin", description="An admin user, full privs")
194+
self.addRole(name="No Rego",
195+
description="A user who can't register")
192196

193-
If the property is a Link, the userid must match the property
194-
value. If the property is a Multilink, the userid must appear
195-
in the Multilink list.
196-
'''
197+
ee = self.addPermission(name="Edit",
198+
description="User may edit everthing")
199+
self.addPermissionToRole('Admin', ee)
200+
ae = self.addPermission(name="Assign",
201+
description="User may be assigned to anything")
202+
self.addPermissionToRole('Admin', ae)
197203

198-
The instance dbinit module then has in ``open()``::
204+
def hasClassPermission(self, db, classname, permission, userid):
205+
''' Look through all the Roles, and hence Permissions, and see if
206+
"permission" is there for the specified classname.
207+
208+
'''
209+
210+
def hasNodePermission(self, db, classname, nodeid, userid, properties):
211+
''' Check the named properties of the given node to see if the
212+
userid appears in them. If it does, then the user is granted
213+
this permission check.
214+
215+
'propspec' consists of a list of property names. The property
216+
names must be the name of a property of classname, or a
217+
KeyError is raised. That property must be a Link or Multilink
218+
property, or a TypeError is raised.
219+
220+
If the property is a Link, the userid must match the property
221+
value. If the property is a Multilink, the userid must appear
222+
in the Multilink list.
223+
'''
224+
225+
def addPermission(self, **propspec):
226+
''' Create a new Permission with the properties defined in
227+
'propspec'
228+
'''
199229

200-
perm = permission.PermissionClass(db, "permission")
201-
role = permission.RoleClass(db, "role")
230+
def addRole(self, **propspec):
231+
''' Create a new Role with the properties defined in 'propspec'
232+
'''
233+
234+
def addPermissionToRole(self, rolename, permissionid):
235+
''' Add the permission to the role's permission list.
236+
237+
'rolename' is the name of the role to add 'permissionid'.
238+
'''
239+
240+
Modules such as ``cgi_client.py`` and ``mailgw.py`` define their own
241+
permissions like so (this example is ``cgi_client.py``)::
242+
243+
# XXX GAH. If the permissions are instance-db-specific then this can't
244+
# work!
245+
from roundup import permission
202246

203247
# create some Permissions
204-
wa = perm.create(name="Web Access",
205-
description="User may use the web interface")
206-
wr = perm.create(name="Web Registration",
207-
description="User may register through the web")
208-
209-
ma = perm.create(name="Mail Access",
210-
description="User may use the email interface")
211-
mr = perm.create(name="Mail Registration",
212-
description="User may register through email")
213-
214-
ee = perm.create(name="Edit",
215-
description="User may edit everthing")
216-
ei = perm.create(name="Edit", classname="issue",
217-
description="User is allowed to edit issues")
248+
newid = permission.addPermission(name="Web Access",
249+
description="User may use the web interface")
250+
permission.addToRole('User', newid)
251+
permission.addToRole('No Rego', newid)
252+
newid = permission.addPermission(name="Web Registration",
253+
description="User may register through the web")
254+
permission.addToRole('User', newid)
255+
# XXX GAH!
218256

219-
ae = perm.create(name="Assign",
220-
description="User may be assigned to anything")
221-
ai = perm.create(name="Assign", classname="issue",
222-
description="User may be assigned to issues")
257+
The instance dbinit module then has in ``open()``::
223258

224-
# create some Roles that use the Permissions
225-
role.create(name="User", description="A regular user, no privs",
226-
permissions=[wa, wr, ma, mr, ei, ai])
227-
role.create(name="Admin", description="An admin user, full privs",
228-
permissions=[ee, ae])
229-
role.create(name="No Rego", description="A user who can't register",
230-
permissions=[wa, ma])
259+
# open the database - it must be modified to init the Security class
260+
# from permissions.py as db.security
261+
db = Database(instance_config, name)
262+
263+
# add some extra permissions and associate them with roles
264+
ei = db.security.addPermission(name="Edit", classname="issue",
265+
description="User is allowed to edit issues")
266+
db.security.addPermissionToRole('User', ei)
267+
ai = db.security.addPermission(name="Assign", classname="issue",
268+
description="User may be assigned to issues")
269+
db.security.addPermissionToRole('User', ei)
231270

232-
in ``init()``::
271+
In the dbinit ``init()``::
233272

234273
r = db.getclass('role').lookup('Admin')
235274
user.create(username="admin", password=Password(adminpw),
@@ -243,10 +282,11 @@ in ``init()``::
243282
Then in the code that matters, calls to ``hasPermission`` are made to
244283
determine if the user has permission to perform some action::
245284

246-
if security.hasClassPermission('issue', 'Edit', self.user):
285+
if db.security.hasClassPermission('issue', 'Edit', self.user):
247286
# all ok
248287

249-
if security.hasNodePermission('issue', nodeid, self.user, ['assignedto']):
288+
if db.security.hasNodePermission('issue', nodeid, self.user,
289+
['assignedto']):
250290
# all ok
251291

252292
The htmltemplate will implement a new tag, <permission> which has the form::

0 commit comments

Comments
 (0)