1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
""" Basic member data tool.
$Id: MemberDataTool.py,v 1.15.8.6 2003/04/03 20:47:26 efge Exp $
"""
import string
from utils import UniqueObject, getToolByName, _dtmldir
from OFS.SimpleItem import SimpleItem
from OFS.PropertyManager import PropertyManager
from Globals import Acquisition, Persistent, DTMLFile
import Globals
from AccessControl.Role import RoleManager
from BTrees.OOBTree import OOBTree
from ZPublisher.Converters import type_converters
from Acquisition import aq_inner, aq_parent, aq_base
from AccessControl import ClassSecurityInfo
from CMFCorePermissions import ViewManagementScreens
import CMFCorePermissions
from ActionProviderBase import ActionProviderBase
_marker = [] # Create a new marker object.
class MemberDataTool (UniqueObject, SimpleItem, PropertyManager, ActionProviderBase):
'''This tool wraps user objects, making them act as Member objects.
'''
id = 'portal_memberdata'
meta_type = 'CMF Member Data Tool'
_actions = []
_v_temps = None
_properties = ()
security = ClassSecurityInfo()
manage_options=( ActionProviderBase.manage_options +
({ 'label' : 'Overview'
, 'action' : 'manage_overview'
}
, { 'label' : 'Contents'
, 'action' : 'manage_showContents'
}
)
+ PropertyManager.manage_options
+ SimpleItem.manage_options
)
#
# ZMI methods
#
security.declareProtected( CMFCorePermissions.ManagePortal
, 'manage_overview' )
manage_overview = DTMLFile( 'explainMemberDataTool', _dtmldir )
security.declareProtected( CMFCorePermissions.ViewManagementScreens
, 'manage_showContents')
manage_showContents = DTMLFile('memberdataContents', _dtmldir )
security.declareProtected( CMFCorePermissions.ViewManagementScreens
, 'getContentsInformation',)
def __init__(self):
self._members = OOBTree()
# Create the default properties.
self._setProperty('email', '', 'string')
self._setProperty('portal_skin', '', 'string')
self._setProperty('listed', '', 'boolean')
self._setProperty('login_time', '2000/01/01', 'date')
self._setProperty('last_login_time', '2000/01/01', 'date')
#
# 'portal_memberdata' interface methods
#
security.declarePrivate('listActions')
def listActions(self, info=None):
"""
Return actions provided via tool.
"""
return self._actions
security.declarePrivate('getMemberDataContents')
def getMemberDataContents(self):
'''
Return the number of members stored in the _members
BTree and some other useful info
'''
membertool = getToolByName(self, 'portal_membership')
members = self._members
user_list = membertool.listMemberIds()
member_list = members.keys()
member_count = len(members)
orphan_count = 0
for member in member_list:
if member not in user_list:
orphan_count = orphan_count + 1
return [{ 'member_count' : member_count,
'orphan_count' : orphan_count }]
security.declarePrivate( 'searchMemberDataContents' )
def searchMemberDataContents( self, search_param, search_term ):
""" Search members """
res = []
if search_param == 'username':
search_param = 'id'
for user_wrapper in self._members.values():
searched = getattr( user_wrapper, search_param, None )
if searched is not None and string.find( searched, search_term ) != -1:
res.append( { 'username' : getattr( user_wrapper, 'id' )
, 'email' : getattr( user_wrapper, 'email', '' )
}
)
return res
security.declarePrivate('pruneMemberDataContents')
def pruneMemberDataContents(self):
'''
Compare the user IDs stored in the member data
tool with the list in the actual underlying acl_users
and delete anything not in acl_users
'''
membertool= getToolByName(self, 'portal_membership')
members = self._members
user_list = membertool.listMemberIds()
for tuple in members.items():
member_name = tuple[0]
member_obj = tuple[1]
if member_name not in user_list:
del members[member_name]
security.declarePrivate('wrapUser')
def wrapUser(self, u):
'''
If possible, returns the Member object that corresponds
to the given User object.
'''
id = u.getUserName()
members = self._members
if not members.has_key(id):
# Get a temporary member that might be
# registered later via registerMemberData().
temps = self._v_temps
if temps is not None and temps.has_key(id):
m = temps[id]
else:
base = aq_base(self)
m = MemberData(base, id)
if temps is None:
self._v_temps = {id:m}
if hasattr(self, 'REQUEST'):
# No REQUEST during tests.
self.REQUEST._hold(CleanupTemp(self))
else:
temps[id] = m
else:
m = members[id]
# Return a wrapper with self as containment and
# the user as context.
return m.__of__(self).__of__(u)
security.declarePrivate('registerMemberData')
def registerMemberData(self, m, id):
'''
Adds the given member data to the _members dict.
This is done as late as possible to avoid side effect
transactions and to reduce the necessary number of
entries.
'''
self._members[id] = m
Globals.InitializeClass(MemberDataTool)
class CleanupTemp:
"""Used to cleanup _v_temps at the end of the request."""
def __init__(self, tool):
self._tool = tool
def __del__(self):
try:
del self._tool._v_temps
except (AttributeError, KeyError):
# The object has already been deactivated.
pass
class MemberData (SimpleItem):
security = ClassSecurityInfo()
def __init__(self, tool, id):
self.id = id
# Make a temporary reference to the tool.
# The reference will be removed by notifyModified().
self._tool = tool
security.declarePrivate('notifyModified')
def notifyModified(self):
# Links self to parent for full persistence.
tool = getattr(self, '_tool', None)
if tool is not None:
del self._tool
tool.registerMemberData(self, self.getId())
security.declarePublic('getUser')
def getUser(self):
# The user object is our context, but it's possible for
# restricted code to strip context while retaining
# containment. Therefore we need a simple security check.
parent = aq_parent(self)
bcontext = aq_base(parent)
bcontainer = aq_base(aq_parent(aq_inner(self)))
if bcontext is bcontainer or not hasattr(bcontext, 'getUserName'):
raise 'MemberDataError', "Can't find user data"
# Return the user object, which is our context.
return parent
def getTool(self):
return aq_parent(aq_inner(self))
security.declarePublic('getMemberId')
def getMemberId(self):
return self.getUser().getUserName()
security.declareProtected(CMFCorePermissions.SetOwnProperties, 'setProperties')
def setProperties(self, properties=None, **kw):
'''Allows the authenticated member to set his/her own properties.
Accepts either keyword arguments or a mapping for the "properties"
argument.
'''
if properties is None:
properties = kw
membership = getToolByName(self, 'portal_membership')
registration = getToolByName(self, 'portal_registration', None)
if not membership.isAnonymousUser():
member = membership.getAuthenticatedMember()
if registration:
failMessage = registration.testPropertiesValidity(properties, member)
if failMessage is not None:
raise 'Bad Request', failMessage
member.setMemberProperties(properties)
else:
raise 'Bad Request', 'Not logged in.'
security.declarePrivate('setMemberProperties')
def setMemberProperties(self, mapping):
'''Sets the properties of the member.
'''
# Sets the properties given in the MemberDataTool.
tool = self.getTool()
for id in tool.propertyIds():
if mapping.has_key(id):
if not self.__class__.__dict__.has_key(id):
value = mapping[id]
if type(value)==type(''):
proptype = tool.getPropertyType(id) or 'string'
if type_converters.has_key(proptype):
value = type_converters[proptype](value)
setattr(self, id, value)
# Hopefully we can later make notifyModified() implicit.
self.notifyModified()
# XXX: s.b., getPropertyForMember(member, id, default)?
security.declarePublic('getProperty')
def getProperty(self, id, default=_marker):
tool = self.getTool()
base = aq_base( self )
# First, check the wrapper (w/o acquisition).
value = getattr( base, id, _marker )
if value is not _marker:
return value
# Then, check the tool and the user object for a value.
tool_value = tool.getProperty( id, _marker )
user_value = getattr( self.getUser(), id, _marker )
# If the tool doesn't have the property, use user_value or default
if tool_value is _marker:
if user_value is not _marker:
return user_value
elif default is not _marker:
return default
else:
raise ValueError, 'The property %s does not exist' % id
# If the tool has an empty property and we have a user_value, use it
if not tool_value and user_value is not _marker:
return user_value
# Otherwise return the tool value
return tool_value
security.declarePrivate('getPassword')
def getPassword(self):
"""Return the password of the user."""
return self.getUser()._getPassword()
security.declarePrivate('setSecurityProfile')
def setSecurityProfile(self, password=None, roles=None, domains=None):
"""Set the user's basic security profile"""
u = self.getUser()
# This is really hackish. The Zope User API needs methods
# for performing these functions.
if password is not None:
u.__ = password
if roles is not None:
u.roles = roles
if domains is not None:
u.domains = domains
def __str__(self):
return self.getMemberId()
### User object interface ###
security.declarePublic('getUserName')
def getUserName(self):
"""Return the username of a user"""
return self.getUser().getUserName()
security.declarePublic('getId')
def getId(self):
"""Get the ID of the user. The ID can be used, at least from
Python, to get the user from the user's
UserDatabase"""
return self.getUser().getId()
security.declarePublic('getRoles')
def getRoles(self):
"""Return the list of roles assigned to a user."""
return self.getUser().getRoles()
security.declarePublic('getRolesInContext')
def getRolesInContext(self, object):
"""Return the list of roles assigned to the user,
including local roles assigned in context of
the passed in object."""
return self.getUser().getRolesInContext(object)
security.declarePublic('getDomains')
def getDomains(self):
"""Return the list of domain restrictions for a user"""
return self.getUser().getDomains()
security.declarePublic('has_role')
def has_role(self, roles, object=None):
"""Check to see if a user has a given role or roles."""
return self.getUser().has_role(roles, object)
# There are other parts of the interface but they are
# deprecated for use with CMF applications.
Globals.InitializeClass(MemberData)
|