2010-09-02 09:16:08 -05:00
|
|
|
# ------------------------------------------------------------------------------
|
2012-05-05 10:04:19 -05:00
|
|
|
from appy.gen import WorkflowOwner
|
2011-12-05 08:11:29 -06:00
|
|
|
from appy.gen.wrappers import AbstractWrapper
|
2010-09-02 09:16:08 -05:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class UserWrapper(AbstractWrapper):
|
2012-05-05 10:04:19 -05:00
|
|
|
workflow = WorkflowOwner
|
2010-10-14 07:43:56 -05:00
|
|
|
|
2010-09-02 09:16:08 -05:00
|
|
|
def showLogin(self):
|
|
|
|
'''When must we show the login field?'''
|
|
|
|
if self.o.isTemporary(): return 'edit'
|
2012-07-10 07:21:08 -05:00
|
|
|
# The manager has the possibility to change the login itself.
|
|
|
|
if self.user.has_role('Manager'): return True
|
2012-05-05 10:04:19 -05:00
|
|
|
return ('view', 'result')
|
2010-09-02 09:16:08 -05:00
|
|
|
|
2012-03-03 16:29:32 -06:00
|
|
|
def showName(self):
|
|
|
|
'''Name and first name, by default, are always shown.'''
|
|
|
|
return True
|
|
|
|
|
2012-02-21 05:09:42 -06:00
|
|
|
def showEmail(self):
|
|
|
|
'''In most cases, email is the login. Show the field only if it is not
|
|
|
|
the case.'''
|
|
|
|
email = self.email
|
|
|
|
return email and (email != self.login)
|
|
|
|
|
|
|
|
def showRoles(self):
|
|
|
|
'''Only the admin can view or edit roles.'''
|
|
|
|
return self.user.has_role('Manager')
|
|
|
|
|
2010-09-02 09:16:08 -05:00
|
|
|
def validateLogin(self, login):
|
|
|
|
'''Is this p_login valid?'''
|
2012-07-10 07:21:08 -05:00
|
|
|
# 2 cases: (1) The user is being created and has no login yet, or
|
|
|
|
# (2) The user is being edited and has already a login, that
|
|
|
|
# can potentially be changed.
|
|
|
|
if not self.login or (login != self.login):
|
|
|
|
# A new p_login is requested. Check if it is valid and free.
|
|
|
|
# Firstly, the login can't be the id of the whole site or "admin".
|
|
|
|
if login == 'admin': return self.translate('login_reserved')
|
|
|
|
# Check that no user or group already uses this login.
|
|
|
|
if self.count('User', noSecurity=True, login=login) or \
|
|
|
|
self.count('Group', noSecurity=True, login=login):
|
|
|
|
return self.translate('login_in_use')
|
2010-09-02 09:16:08 -05:00
|
|
|
return True
|
|
|
|
|
|
|
|
def validatePassword(self, password):
|
|
|
|
'''Is this p_password valid?'''
|
|
|
|
# Password must be at least 5 chars length
|
|
|
|
if len(password) < 5:
|
2012-06-03 11:34:56 -05:00
|
|
|
return self.translate('password_too_short', mapping={'nb':5})
|
2010-09-02 09:16:08 -05:00
|
|
|
return True
|
|
|
|
|
|
|
|
def showPassword(self):
|
|
|
|
'''When must we show the 2 fields for entering a password ?'''
|
2012-02-21 05:09:42 -06:00
|
|
|
# When someone creates the user
|
2010-09-02 09:16:08 -05:00
|
|
|
if self.o.isTemporary(): return 'edit'
|
2012-07-10 07:21:08 -05:00
|
|
|
# When the user itself (we don't check role Owner because a Manager can
|
|
|
|
# also own a User instance) wants to edit information about himself.
|
|
|
|
if self.user.getId() == self.login: return 'edit'
|
2010-09-02 09:16:08 -05:00
|
|
|
|
2012-05-29 13:50:18 -05:00
|
|
|
def setPassword(self, newPassword=None):
|
|
|
|
'''Sets a p_newPassword for self. If p_newPassword is not given, we
|
|
|
|
generate one. This method returns the generated password (or simply
|
|
|
|
p_newPassword if no generation occurred).'''
|
|
|
|
if newPassword:
|
|
|
|
msgPart = 'changed'
|
|
|
|
else:
|
|
|
|
newPassword = self.getField('password1').generatePassword()
|
|
|
|
msgPart = 'generated'
|
|
|
|
login = self.login
|
|
|
|
zopeUser = self.o.acl_users.getUserById(login)
|
|
|
|
tool = self.tool.o
|
|
|
|
zopeUser.__ = tool._encryptPassword(newPassword)
|
|
|
|
if self.user.getId() == login:
|
|
|
|
# The user for which we change the password is the currently logged
|
|
|
|
# user. So update the authentication cookie, too.
|
|
|
|
tool._updateCookie(login, newPassword)
|
|
|
|
self.log('Password %s by "%s" for "%s".' % \
|
|
|
|
(msgPart, self.user.getId(), login))
|
|
|
|
return newPassword
|
|
|
|
|
2012-07-12 10:54:14 -05:00
|
|
|
def checkPassword(self, clearPassword):
|
|
|
|
'''Returns True if p_clearPassword is the correct password for this
|
|
|
|
user.'''
|
|
|
|
encryptedPassword = self.getZopeUser()._getPassword()
|
|
|
|
from AccessControl.AuthEncoding import pw_validate
|
|
|
|
return pw_validate(encryptedPassword, clearPassword)
|
|
|
|
|
2012-07-11 10:27:40 -05:00
|
|
|
def setLogin(self, oldLogin, newLogin):
|
|
|
|
'''Changes the login of this user from p_oldLogin to p_newLogin.'''
|
|
|
|
self.login = newLogin
|
|
|
|
# Update the corresponding Zope-level user
|
|
|
|
aclUsers = self.o.acl_users
|
|
|
|
zopeUser = aclUsers.getUserById(oldLogin)
|
|
|
|
zopeUser.name = newLogin
|
|
|
|
del aclUsers.data[oldLogin]
|
|
|
|
aclUsers.data[newLogin] = zopeUser
|
|
|
|
# Update the email if the email corresponds to the login.
|
|
|
|
email = self.email
|
|
|
|
if email == oldLogin:
|
|
|
|
self.email = newLogin
|
|
|
|
# Update the title, which is the login
|
|
|
|
self.title = newLogin
|
|
|
|
# Browse all objects of the database and update potential local roles
|
|
|
|
# that referred to the old login.
|
|
|
|
context = {'nb': 0, 'old': oldLogin, 'new': newLogin}
|
|
|
|
for className in self.o.getProductConfig().allClassNames:
|
|
|
|
self.compute(className, context=context, noSecurity=True,
|
|
|
|
expression="ctx['nb'] += obj.o.applyUserIdChange(" \
|
|
|
|
"ctx['old'], ctx['new'])")
|
|
|
|
self.log("Login '%s' renamed to '%s' by '%s'." % \
|
|
|
|
(oldLogin, newLogin, self.user.getId()))
|
|
|
|
self.log('Login change: local roles updated in %d object(s).' % \
|
|
|
|
context['nb'])
|
|
|
|
|
2010-09-02 09:16:08 -05:00
|
|
|
def getGrantableRoles(self):
|
|
|
|
'''Returns the list of roles that the admin can grant to a user.'''
|
|
|
|
res = []
|
|
|
|
for role in self.o.getProductConfig().grantableRoles:
|
|
|
|
res.append( (role, self.translate('role_%s' % role)) )
|
|
|
|
return res
|
|
|
|
|
|
|
|
def validate(self, new, errors):
|
|
|
|
'''Inter-field validation.'''
|
|
|
|
page = self.request.get('page', 'main')
|
2012-07-10 07:21:08 -05:00
|
|
|
self.o._oldLogin = None
|
2010-09-02 09:16:08 -05:00
|
|
|
if page == 'main':
|
|
|
|
if hasattr(new, 'password1') and (new.password1 != new.password2):
|
2012-06-03 11:34:56 -05:00
|
|
|
msg = self.translate('passwords_mismatch')
|
2010-09-02 09:16:08 -05:00
|
|
|
errors.password1 = msg
|
|
|
|
errors.password2 = msg
|
2012-07-10 07:21:08 -05:00
|
|
|
# Remember the previous login
|
|
|
|
if self.login: self.o._oldLogin = self.login
|
2011-02-06 10:39:36 -06:00
|
|
|
return self._callCustom('validate', new, errors)
|
2010-09-02 09:16:08 -05:00
|
|
|
|
|
|
|
def onEdit(self, created):
|
2012-05-29 13:50:18 -05:00
|
|
|
self.title = self.login
|
2011-11-25 11:01:20 -06:00
|
|
|
aclUsers = self.o.acl_users
|
|
|
|
login = self.login
|
2010-09-02 09:16:08 -05:00
|
|
|
if created:
|
2011-11-25 11:01:20 -06:00
|
|
|
# Create the corresponding Zope user
|
|
|
|
aclUsers._doAddUser(login, self.password1, self.roles, ())
|
2011-11-28 15:50:01 -06:00
|
|
|
zopeUser = aclUsers.getUser(login)
|
2010-09-02 09:16:08 -05:00
|
|
|
# Remove our own password copies
|
|
|
|
self.password1 = self.password2 = ''
|
2011-11-28 15:50:01 -06:00
|
|
|
from persistent.mapping import PersistentMapping
|
|
|
|
# The following dict will store, for every group, global roles
|
|
|
|
# granted to it.
|
|
|
|
zopeUser.groups = PersistentMapping()
|
|
|
|
else:
|
2012-07-10 07:21:08 -05:00
|
|
|
# Update the login itself if the user has changed it.
|
|
|
|
oldLogin = self.o._oldLogin
|
|
|
|
if oldLogin and (oldLogin != login):
|
2012-07-11 10:27:40 -05:00
|
|
|
self.setLogin(oldLogin, login)
|
2012-07-10 07:21:08 -05:00
|
|
|
del self.o._oldLogin
|
2012-02-21 05:09:42 -06:00
|
|
|
# Update roles at the Zope level.
|
2011-11-28 15:50:01 -06:00
|
|
|
zopeUser = aclUsers.getUserById(login)
|
|
|
|
zopeUser.roles = self.roles
|
2012-02-21 05:09:42 -06:00
|
|
|
# Update the password if the user has entered new ones.
|
|
|
|
rq = self.request
|
2012-06-02 13:55:25 -05:00
|
|
|
if rq.has_key('password1'):
|
|
|
|
self.setPassword(rq['password1'])
|
|
|
|
self.password1 = self.password2 = ''
|
2012-02-16 11:13:51 -06:00
|
|
|
# "self" must be owned by its Zope user.
|
2011-11-25 11:01:20 -06:00
|
|
|
if 'Owner' not in self.o.get_local_roles_for_userid(login):
|
|
|
|
self.o.manage_addLocalRoles(login, ('Owner',))
|
2012-02-16 11:13:51 -06:00
|
|
|
# If the user was created by an Anonymous, Anonymous can't stay Owner
|
|
|
|
# of the object.
|
|
|
|
if None in self.o.__ac_local_roles__:
|
|
|
|
del self.o.__ac_local_roles__[None]
|
2011-02-06 10:39:36 -06:00
|
|
|
return self._callCustom('onEdit', created)
|
2010-11-26 10:30:46 -06:00
|
|
|
|
2011-11-28 15:50:01 -06:00
|
|
|
def getZopeUser(self):
|
|
|
|
'''Gets the Zope user corresponding to this user.'''
|
|
|
|
return self.o.acl_users.getUser(self.login)
|
|
|
|
|
2010-11-26 10:30:46 -06:00
|
|
|
def onDelete(self):
|
2011-11-28 15:50:01 -06:00
|
|
|
'''Before deleting myself, I must delete the corresponding Zope user.'''
|
|
|
|
self.o.acl_users._doDelUsers([self.login])
|
|
|
|
self.log('User "%s" deleted.' % self.login)
|
2010-11-26 10:30:46 -06:00
|
|
|
# Call a custom "onDelete" if any.
|
2011-02-06 10:39:36 -06:00
|
|
|
return self._callCustom('onDelete')
|
2011-11-28 15:50:01 -06:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
try:
|
|
|
|
from AccessControl.PermissionRole import _what_not_even_god_should_do, \
|
|
|
|
rolesForPermissionOn
|
|
|
|
from Acquisition import aq_base
|
|
|
|
except ImportError:
|
|
|
|
pass # For those using Appy without Zope
|
|
|
|
|
|
|
|
class ZopeUserPatches:
|
|
|
|
'''This class is a fake one that defines Appy variants of some of Zope's
|
|
|
|
AccessControl.User methods. The idea is to implement the notion of group
|
|
|
|
of users.'''
|
|
|
|
|
|
|
|
def getRoles(self):
|
|
|
|
'''Returns the global roles that this user (or any of its groups)
|
|
|
|
possesses.'''
|
|
|
|
res = list(self.roles)
|
2012-03-19 11:00:44 -05:00
|
|
|
if 'Anonymous' not in res: res.append('Authenticated')
|
2011-11-28 15:50:01 -06:00
|
|
|
# Add group global roles
|
|
|
|
if not hasattr(aq_base(self), 'groups'): return res
|
|
|
|
for roles in self.groups.itervalues():
|
|
|
|
for role in roles:
|
|
|
|
if role not in res: res.append(role)
|
|
|
|
return res
|
|
|
|
|
|
|
|
def getRolesInContext(self, object):
|
|
|
|
'''Return the list of global and local (to p_object) roles granted to
|
|
|
|
this user (or to any of its groups).'''
|
2012-02-02 10:30:54 -06:00
|
|
|
if isinstance(object, AbstractWrapper): object = object.o
|
2011-11-28 15:50:01 -06:00
|
|
|
object = getattr(object, 'aq_inner', object)
|
|
|
|
# Start with user global roles
|
|
|
|
res = self.getRoles()
|
|
|
|
# Add local roles
|
|
|
|
localRoles = getattr(object, '__ac_local_roles__', None)
|
|
|
|
if not localRoles: return res
|
|
|
|
userId = self.getId()
|
|
|
|
groups = getattr(self, 'groups', ())
|
|
|
|
for id, roles in localRoles.iteritems():
|
|
|
|
if (id != userId) and (id not in groups): continue
|
2012-02-02 10:30:54 -06:00
|
|
|
for role in roles:
|
|
|
|
if role not in res: res.append(role)
|
2011-11-28 15:50:01 -06:00
|
|
|
return res
|
|
|
|
|
|
|
|
def allowed(self, object, object_roles=None):
|
|
|
|
'''Checks whether the user has access to p_object. The user (or one of
|
|
|
|
its groups) must have one of the roles in p_object_roles.'''
|
|
|
|
if object_roles is _what_not_even_god_should_do: return 0
|
|
|
|
# If "Anonymous" is among p_object_roles, grant access.
|
|
|
|
if (object_roles is None) or ('Anonymous' in object_roles): return 1
|
|
|
|
# If "Authenticated" is among p_object_roles, grant access if the user
|
|
|
|
# is not anonymous.
|
|
|
|
if 'Authenticated' in object_roles and \
|
|
|
|
(self.getUserName() != 'Anonymous User'):
|
|
|
|
if self._check_context(object): return 1
|
|
|
|
# Try first to grant access based on global user roles
|
|
|
|
for role in self.getRoles():
|
|
|
|
if role not in object_roles: continue
|
|
|
|
if self._check_context(object): return 1
|
|
|
|
return
|
|
|
|
# Try then to grant access based on local roles
|
|
|
|
innerObject = getattr(object, 'aq_inner', object)
|
|
|
|
localRoles = getattr(innerObject, '__ac_local_roles__', None)
|
|
|
|
if not localRoles: return
|
|
|
|
userId = self.getId()
|
|
|
|
groups = getattr(self, 'groups', ())
|
|
|
|
for id, roles in localRoles.iteritems():
|
|
|
|
if (id != userId) and (id not in groups): continue
|
|
|
|
for role in roles:
|
|
|
|
if role not in object_roles: continue
|
|
|
|
if self._check_context(object): return 1
|
|
|
|
return
|
|
|
|
|
2012-03-03 16:29:32 -06:00
|
|
|
try:
|
|
|
|
from AccessControl.User import SimpleUser
|
|
|
|
SimpleUser.getRoles = getRoles
|
|
|
|
SimpleUser.getRolesInContext = getRolesInContext
|
|
|
|
SimpleUser.allowed = allowed
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2010-09-02 09:16:08 -05:00
|
|
|
# ------------------------------------------------------------------------------
|