Skip to content

Commit 3191830

Browse files
author
Richard Jones
committed
fix permission handling around rego
1 parent e421e64 commit 3191830

File tree

3 files changed

+65
-31
lines changed

3 files changed

+65
-31
lines changed

roundup/backends/sessions.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#$Id: sessions.py,v 1.7 2004-02-11 23:55:09 richard Exp $
1+
#$Id: sessions.py,v 1.8 2004-02-19 02:39:05 richard Exp $
22
"""This module defines a very basic store that's used by the CGI interface
33
to store session and one-time-key information.
44
@@ -59,7 +59,10 @@ def get(self, infoid, value):
5959
def getall(self, infoid):
6060
db = self.opendb('c')
6161
try:
62-
return marshal.loads(db[infoid])
62+
try:
63+
return marshal.loads(db[infoid])
64+
except KeyError:
65+
raise KeyError, 'No such One Time Key "%s"'%infoid
6366
finally:
6467
db.close()
6568

roundup/cgi/actions.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -619,19 +619,17 @@ def handle(self):
619619
# pull the rego information out of the otk database
620620
self.userid = self.db.confirm_registration(self.form['otk'].value)
621621
except (ValueError, KeyError), message:
622-
# XXX: we need to make the "default" page be able to display errors!
623622
self.client.error_message.append(str(message))
624623
return
625624

626625
# log the new user in
627626
self.client.user = self.db.user.get(self.userid, 'username')
628627
# re-open the database for real, using the user
629628
self.client.opendb(self.client.user)
630-
self.db = client.db
631629

632630
# if we have a session, update it
633631
if hasattr(self, 'session'):
634-
self.db.sessions.set(self.session, user=self.user,
632+
self.client.db.sessions.set(self.session, user=self.user,
635633
last_use=time.time())
636634
else:
637635
# new session cookie
@@ -642,7 +640,7 @@ def handle(self):
642640

643641
# redirect to the user's page
644642
raise Redirect, '%suser%s?@ok_message=%s'%(self.base,
645-
self.userid, urllib.quote(message))
643+
self.userid, urllib.quote(message))
646644

647645
class RegisterAction(Action):
648646
name = 'register'
@@ -654,16 +652,18 @@ def handle(self):
654652
655653
Return 1 on successful login.
656654
"""
657-
props = self.client.parsePropsFromForm()[0][('user', None)]
655+
props = self.client.parsePropsFromForm(create=1)[0][('user', None)]
658656

659657
# registration isn't allowed to supply roles
660658
if props.has_key('roles'):
661-
raise Unauthorised, _("It is not permitted to supply roles at registration.")
659+
raise Unauthorised, _("It is not permitted to supply roles "
660+
"at registration.")
662661

662+
username = props['username']
663663
try:
664-
self.db.user.lookup(props['username'])
665-
self.client.error_message.append('Error: A user with the username "%s" '
666-
'already exists'%props['username'])
664+
self.db.user.lookup(username)
665+
self.client.error_message.append(_('Error: A user with the '
666+
'username "%(username)s" already exists')%props)
667667
return
668668
except KeyError:
669669
pass
@@ -686,7 +686,7 @@ def handle(self):
686686
# send the email
687687
tracker_name = self.db.config.TRACKER_NAME
688688
tracker_email = self.db.config.TRACKER_EMAIL
689-
subject = 'Complete your registration to %s -- key %s' % (tracker_name,
689+
subject = 'Complete your registration to %s -- key %s'%(tracker_name,
690690
otk)
691691
body = """To complete your registration of the user "%(name)s" with
692692
%(tracker)s, please do one of the following:

roundup/cgi/templating.py

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,10 @@ class utils(client.instance.interfaces.TemplatingUtils, utils):
205205
c['context'] = HTMLItem(client, classname, client.nodeid,
206206
anonymous=1)
207207
elif client.db.classes.has_key(classname):
208-
c['context'] = HTMLClass(client, classname, anonymous=1)
208+
if classname == 'user':
209+
c['context'] = HTMLUserClass(client, classname, anonymous=1)
210+
else:
211+
c['context'] = HTMLClass(client, classname, anonymous=1)
209212
return c
210213

211214
def render(self, client, classname, request, **options):
@@ -253,6 +256,8 @@ def __getitem__(self, item, desre=re.compile(r'(?P<cl>\w+)(?P<id>[-\d]+)')):
253256
return HTMLItem(self._client, m.group('cl'), m.group('id'))
254257
else:
255258
self._client.db.getclass(item)
259+
if item == 'user':
260+
return HTMLUserClass(self._client, item)
256261
return HTMLClass(self._client, item)
257262

258263
def __getattr__(self, attr):
@@ -264,7 +269,12 @@ def __getattr__(self, attr):
264269
def classes(self):
265270
l = self._client.db.classes.keys()
266271
l.sort()
267-
return [HTMLClass(self._client, cn) for cn in l]
272+
r = []
273+
for item in l:
274+
if item == 'user':
275+
m.append(HTMLUserClass(self._client, item))
276+
m.append(HTMLClass(self._client, item))
277+
return r
268278

269279
def lookupIds(db, prop, ids, num_re=re.compile('-?\d+')):
270280
cl = db.getclass(prop.classname)
@@ -850,7 +860,44 @@ def renderQueryForm(self):
850860
# use our fabricated request
851861
return pt.render(self._client, req.classname, req)
852862

853-
class HTMLUser(HTMLItem):
863+
class HTMLUserPermission:
864+
865+
def is_edit_ok(self):
866+
''' Is the user allowed to Edit the current class?
867+
Also check whether this is the current user's info.
868+
'''
869+
return self._user_perm_check('Edit')
870+
871+
def is_view_ok(self):
872+
''' Is the user allowed to View the current class?
873+
Also check whether this is the current user's info.
874+
'''
875+
return self._user_perm_check('View')
876+
877+
def _user_perm_check(self, type):
878+
# some users may view / edit all users
879+
s = self._db.security
880+
userid = self._client.userid
881+
if s.hasPermission(type, userid, self._classname):
882+
return 1
883+
884+
# users may view their own info
885+
is_anonymous = self._db.user.get(userid, 'username') == 'anonymous'
886+
if getattr(self, '_nodeid', None) == userid and not is_anonymous:
887+
return 1
888+
889+
# may anonymous users register?
890+
if (is_anonymous and s.hasPermission('Web Registration', userid,
891+
self._classname)):
892+
return 1
893+
894+
# nope, no access here
895+
return 0
896+
897+
class HTMLUserClass(HTMLUserPermission, HTMLClass):
898+
pass
899+
900+
class HTMLUser(HTMLUserPermission, HTMLItem):
854901
''' Accesses through the *user* (a special case of item)
855902
'''
856903
def __init__(self, client, classname, nodeid, anonymous=0):
@@ -871,22 +918,6 @@ def hasPermission(self, permission, classname=_marker):
871918
classname = self._default_classname
872919
return self._security.hasPermission(permission, self._nodeid, classname)
873920

874-
def is_edit_ok(self):
875-
''' Is the user allowed to Edit the current class?
876-
Also check whether this is the current user's info.
877-
'''
878-
return self._db.security.hasPermission('Edit', self._client.userid,
879-
self._classname) or (self._nodeid == self._client.userid and
880-
self._db.user.get(self._client.userid, 'username') != 'anonymous')
881-
882-
def is_view_ok(self):
883-
''' Is the user allowed to View the current class?
884-
Also check whether this is the current user's info.
885-
'''
886-
return self._db.security.hasPermission('View', self._client.userid,
887-
self._classname) or (self._nodeid == self._client.userid and
888-
self._db.user.get(self._client.userid, 'username') != 'anonymous')
889-
890921
class HTMLProperty(HTMLInputMixin, HTMLPermissions):
891922
''' String, Number, Date, Interval HTMLProperty
892923

0 commit comments

Comments
 (0)