@@ -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
269279def 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-
890921class HTMLProperty (HTMLInputMixin , HTMLPermissions ):
891922 ''' String, Number, Date, Interval HTMLProperty
892923
0 commit comments