1- # $Id: client.py,v 1.4 2002-09-01 22:09:20 richard Exp $
1+ # $Id: client.py,v 1.5 2002-09-01 23:57:53 richard Exp $
22
33__doc__ = """
44WWW request handler (also used in the stand-alone server).
@@ -289,7 +289,7 @@ def template(self, name, **kwargs):
289289 return pt .render (** kwargs )
290290 except PageTemplate .PTRuntimeError , message :
291291 return '<strong>%s</strong><ol>%s</ol>' % (message ,
292- cgi . escape ( '<li>' .join (pt ._v_errors ) ))
292+ '<li>' .join (pt ._v_errors ))
293293 except :
294294 # everything else
295295 return cgitb .html ()
@@ -306,9 +306,9 @@ def content(self):
306306 actions = {
307307 'edit' : 'editItemAction' ,
308308 'new' : 'newItemAction' ,
309+ 'register' : 'registerAction' ,
309310 'login' : 'login_action' ,
310311 'logout' : 'logout_action' ,
311- 'register' : 'register_action' ,
312312 'search' : 'searchAction' ,
313313 }
314314 def handle_action (self ):
@@ -319,9 +319,9 @@ def handle_action(self):
319319 actions are defined in the "actions" dictionary on this class:
320320 "edit" -> self.editItemAction
321321 "new" -> self.newItemAction
322+ "register" -> self.registerAction
322323 "login" -> self.login_action
323324 "logout" -> self.logout_action
324- "register" -> self.register_action
325325 "search" -> self.searchAction
326326
327327 '''
@@ -472,17 +472,25 @@ def logout_action(self):
472472 # Let the user know what's going on
473473 self .ok_message .append (_ ('You are logged out' ))
474474
475- def register_action (self ):
475+ def registerAction (self ):
476476 '''Attempt to create a new user based on the contents of the form
477477 and then set the cookie.
478478
479479 return 1 on successful login
480480 '''
481+ # create the new user
482+ cl = self .db .user
483+
484+ # parse the props from the form
485+ try :
486+ props = parsePropsFromForm (self .db , cl , self .form , self .nodeid )
487+ except (ValueError , KeyError ), message :
488+ self .error_message .append (_ ('Error: ' ) + str (message ))
489+ return
490+
481491 # make sure we're allowed to register
482- userid = self .db .user .lookup (self .user )
483- if not self .db .security .hasPermission ('Web Registration' , userid ):
484- raise Unauthorised , _ ("You do not have permission to access" \
485- " %(action)s." )% {'action' : 'registration' }
492+ if not self .registerPermission (props ):
493+ raise Unauthorised , _ ("You do not have permission to register" )
486494
487495 # re-open the database as "admin"
488496 if self .user != 'admin' :
@@ -493,21 +501,33 @@ def register_action(self):
493501 try :
494502 props = parsePropsFromForm (self .db , cl , self .form )
495503 props ['roles' ] = self .instance .NEW_WEB_USER_ROLES
496- uid = cl .create (** props )
504+ self . userid = cl .create (** props )
497505 self .db .commit ()
498506 except ValueError , message :
499507 self .error_message .append (message )
500508
501509 # log the new user in
502- self .user = cl .get (uid , 'username' )
510+ self .user = cl .get (self . userid , 'username' )
503511 # re-open the database for real, using the user
504512 self .opendb (self .user )
505- password = cl . get (uid , 'password' )
513+ password = self . db . user . get (self . userid , 'password' )
506514 self .set_cookie (self .user , password )
507515
508516 # nice message
509517 self .ok_message .append (_ ('You are now registered, welcome!' ))
510518
519+ def registerPermission (self , props ):
520+ ''' Determine whether the user has permission to register
521+
522+ Base behaviour is to check the user has "Web Registration".
523+ '''
524+ # registration isn't allowed to supply roles
525+ if props .has_key ('roles' ):
526+ return 0
527+ if self .db .security .hasPermission ('Web Registration' , self .userid ):
528+ return 1
529+ return 0
530+
511531 def editItemAction (self ):
512532 ''' Perform an edit of an item in the database.
513533
@@ -589,10 +609,9 @@ def editItemPermission(self, props):
589609 # if the item being edited is the current user, we're ok
590610 if self .nodeid == self .userid :
591611 return 1
592- if not self .db .security .hasPermission ('Edit' , self .userid ,
593- self .classname ):
594- return 0
595- return 1
612+ if self .db .security .hasPermission ('Edit' , self .userid , self .classname ):
613+ return 1
614+ return 0
596615
597616 def newItemAction (self ):
598617 ''' Add a new item to the database.
@@ -663,9 +682,9 @@ def newItemPermission(self, props):
663682 if self .classname == 'user' and has ('Web Registration' , self .userid ,
664683 'user' ):
665684 return 1
666- if not has ('Edit' , self .userid , self .classname ):
667- return 0
668- return 1
685+ if has ('Edit' , self .userid , self .classname ):
686+ return 1
687+ return 0
669688
670689 def genericEditAction (self ):
671690 ''' Performs an edit of all of a class' items in one go.
0 commit comments