1 # -*- coding: utf-8 -*-
3 ## Copyright (C)2006 Ingeniweb
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 2 of the License, or
8 ## (at your option) any later version.
10 ## This program is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ## GNU General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with this program; see the file COPYING. If not, write to the
17 ## Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 __version__
= "$Revision: $"
23 # $Id: GRUFFolder.py 30098 2006-09-08 12:35:01Z encolpe $
24 __docformat__
= 'restructuredtext'
27 # fakes a method from a DTML file
28 from Globals
import MessageDialog
, DTMLFile
30 from AccessControl
import ClassSecurityInfo
31 from Globals
import InitializeClass
32 from Acquisition
import Implicit
33 from Globals
import Persistent
34 from AccessControl
.Role
import RoleManager
35 from OFS
.SimpleItem
import Item
36 from OFS
.PropertyManager
import PropertyManager
37 from OFS
import ObjectManager
, SimpleItem
38 from DateTime
import DateTime
39 from App
import ImageFile
41 #XXX PJ DynaList is very hairy - why vs. PerstList?
42 # (see C__ac_roles__ class below for an explanation)
44 import AccessControl
.Role
, webdav
.Collection
53 def manage_addGRUFUsers(self
, id="Users", dtself
=None,REQUEST
=None,**ignored
):
57 try: self
._setObject
(id, f
)
58 except: return MessageDialog(
60 message
='This object already contains a GRUFUsers Folder',
61 action
='%s/manage_main' % REQUEST
['URL1'])
62 if REQUEST
is not None:
63 REQUEST
['RESPONSE'].redirect(self
.absolute_url()+'/manage_main')
65 def manage_addGRUFGroups(self
, id="Groups", dtself
=None,REQUEST
=None,**ignored
):
69 try: self
._setObject
(id, f
)
70 except: return MessageDialog(
72 message
='This object already contains a GRUFGroups Folder',
73 action
='%s/manage_main' % REQUEST
['URL1'])
74 if REQUEST
is not None:
75 REQUEST
['RESPONSE'].redirect(self
.absolute_url()+'/manage_main')
77 class GRUFFolder(ObjectManager
.ObjectManager
, SimpleItem
.Item
):
79 isPrincipiaFolderish
=1
80 manage_main
=DTMLFile('dtml/GRUFFolder_main', globals())
81 manage_options
=( {'label':'Contents', 'action':'manage_main'}, ) + \
82 SimpleItem
.Item
.manage_options
84 security
= ClassSecurityInfo()
85 def __creatable_by_emergency_user__(self
): return 1
87 def __init__(self
, id = None):
91 self
.id = self
.default_id
97 return self
.default_id
# Used for b/w compatibility
99 def getUserSourceId(self
,):
104 isValid(self,) => Return true if an acl_users is inside
106 if "acl_users" in self
.objectIds():
110 security
.declarePublic('header_text')
111 def header_text(self
,):
113 header_text(self,) => Text that appears in the content's
118 def getUserFolder(self
,):
120 getUserFolder(self,) => get the underlying user folder, UNRESTRICTED !
122 if not "acl_users" in self
.objectIds():
123 raise "ValueError", "Please put an acl_users in %s " \
124 "before using GRUF" % (self
.getId(),)
125 return self
.restrictedTraverse('acl_users')
127 def getUserNames(self
,):
129 getUserNames(self,) => None
131 We override this to prevent SimpleUserFolder to use GRUF's getUserNames() method.
132 It's, of course, still possible to override a getUserNames method with SimpleUserFolder:
133 just call it 'new_getUserNames'.
135 # Call the "new_getUserNames" method if available
136 if "new_getUserNames" in self
.objectIds():
137 return self
.unrestrictedTraverse('new_getUserNames')()
139 # Return () if nothing is there
145 class GRUFUsers(GRUFFolder
):
147 GRUFUsers : GRUFFolder that holds users
149 meta_type
="GRUFUsers"
152 manage_options
= GRUFFolder
.manage_options
154 class C__ac_roles__(Persistent
, Implicit
, DynaList
.DynaList
):
156 __ac_roles__ dynastring.
157 Do not forget to set _target to class instance.
159 XXX DynaList is surely not efficient but it's the only way
160 I found to do what I wanted easily. Someone should take
161 a look to PerstList instead to see if it's possible
162 to do the same ? (ie. having a list which elements are
163 the results of a method call).
165 However, even if DynaList is not performant, it's not
166 a critical point because this list is meant to be
167 looked at only when a User object is looked at INSIDE
168 GRUF (especially to set groups a user belongs to).
169 So in practice only used within ZMI.
172 return self
.userdefined_roles()
176 ac_roles
= C__ac_roles__()
177 __ac_roles__
= ac_roles
179 enabled
= 1 # True if it's enabled, false if not
181 def enableSource(self
,):
182 """enableSource(self,) => Set enable status to 1
186 def disableSource(self
,):
187 """disableSource(self,) => explicit ;)
191 def isEnabled(self
,):
193 Return true if enabled (surprisingly)
195 return not not self
.enabled
197 def header_text(self
,):
199 header_text(self,) => Text that appears in the content's view
202 if not "acl_users" in self
.objectIds():
203 return "Please put an acl_users here before ever " \
204 "starting to use this object."
206 ret
= """In this folder, groups are seen as ROLES from user's
207 view. To put a user into a group, affect him a role
208 that matches his group.<br />"""
213 def listGroups(self
,):
215 listGroups(self,) => return a list of groups defined as roles
217 return self
.Groups
.restrictedTraverse('listGroups')()
220 def userdefined_roles(self
):
221 "Return list of user-defined roles"
222 return self
.listGroups()
225 class GRUFGroups(GRUFFolder
):
227 GRUFGroups : GRUFFolder that holds groups
229 meta_type
="GRUFGroups"
230 default_id
= "Groups"
232 _group_prefix
= "group_"
235 class C__ac_roles__(Persistent
, Implicit
, DynaList
.DynaList
):
237 __ac_roles__ dynastring.
238 Do not forget to set _target to class instance.
240 XXX DynaList is surely not efficient but it's the only way
241 I found to do what I wanted easily. Someone should take
242 a look to PerstList instead to see if it's possible
243 to do the same ? (ie. having a list which elements are
244 the results of a method call).
246 However, even if DynaList is not performant, it's not
247 a critical point because this list is meant to be
248 looked at only when a User object is looked at INSIDE
249 GRUF (especially to set groups a user belongs to).
250 So in practice only used within ZMI.
253 return self
.userdefined_roles()
256 ac_roles
= C__ac_roles__()
257 __ac_roles__
= ac_roles
260 def header_text(self
,):
262 header_text(self,) => Text that appears in the content's
266 if not "acl_users" in self
.objectIds():
267 return "Please put an acl_users here before ever " \
268 "starting to use this object."
271 def _getGroup(self
, id):
273 _getGroup(self, id) => same as getUser() but... with a group :-)
274 This method will return an UNWRAPPED object
276 return self
.acl_users
.getUser(id)
279 def listGroups(self
, prefixed
= 1):
281 Return a list of available groups.
282 Group names are prefixed !
285 return self
.acl_users
.getUserNames()
288 for grp
in self
.acl_users
.getUserNames():
289 ret
.append("%s%s" % (self
._group
_prefix
, grp
))
293 def userdefined_roles(self
):
294 "Return list of user-defined roles"
295 return self
.listGroups()
298 InitializeClass(GRUFUsers
)
299 InitializeClass(GRUFGroups
)