6e3fef67d8e65301b551997fa4d774de5d49565c
[GroupUserFolder.git] / GRUFFolder.py
1 # -*- coding: utf-8 -*-
2 ## GroupUserFolder
3 ## Copyright (C)2006 Ingeniweb
4
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.
9
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.
14
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.
18 """
19
20 """
21 __version__ = "$Revision: $"
22 # $Source: $
23 # $Id: GRUFFolder.py 30098 2006-09-08 12:35:01Z encolpe $
24 __docformat__ = 'restructuredtext'
25
26
27 # fakes a method from a DTML file
28 from Globals import MessageDialog, DTMLFile
29
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
40
41 #XXX PJ DynaList is very hairy - why vs. PerstList?
42 # (see C__ac_roles__ class below for an explanation)
43 import DynaList
44 import AccessControl.Role, webdav.Collection
45 import Products
46 import os
47 import string
48 import shutil
49 import random
50
51
52
53 def manage_addGRUFUsers(self, id="Users", dtself=None,REQUEST=None,**ignored):
54 """ """
55 f=GRUFUsers(id)
56 self=self.this()
57 try: self._setObject(id, f)
58 except: return MessageDialog(
59 title ='Item Exists',
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')
64
65 def manage_addGRUFGroups(self, id="Groups", dtself=None,REQUEST=None,**ignored):
66 """ """
67 f=GRUFGroups(id)
68 self=self.this()
69 try: self._setObject(id, f)
70 except: return MessageDialog(
71 title ='Item Exists',
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')
76
77 class GRUFFolder(ObjectManager.ObjectManager, SimpleItem.Item):
78 isAnObjectManager=1
79 isPrincipiaFolderish=1
80 manage_main=DTMLFile('dtml/GRUFFolder_main', globals())
81 manage_options=( {'label':'Contents', 'action':'manage_main'}, ) + \
82 SimpleItem.Item.manage_options
83
84 security = ClassSecurityInfo()
85 def __creatable_by_emergency_user__(self): return 1
86
87 def __init__(self, id = None):
88 if id:
89 self.id = id
90 else:
91 self.id = self.default_id
92
93 def getId(self,):
94 if self.id:
95 return self.id
96 else:
97 return self.default_id # Used for b/w compatibility
98
99 def getUserSourceId(self,):
100 return self.getId()
101
102 def isValid(self,):
103 """
104 isValid(self,) => Return true if an acl_users is inside
105 """
106 if "acl_users" in self.objectIds():
107 return 1
108 return None
109
110 security.declarePublic('header_text')
111 def header_text(self,):
112 """
113 header_text(self,) => Text that appears in the content's
114 view heading zone
115 """
116 return ""
117
118 def getUserFolder(self,):
119 """
120 getUserFolder(self,) => get the underlying user folder, UNRESTRICTED !
121 """
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')
126
127 def getUserNames(self,):
128 """
129 getUserNames(self,) => None
130
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'.
134 """
135 # Call the "new_getUserNames" method if available
136 if "new_getUserNames" in self.objectIds():
137 return self.unrestrictedTraverse('new_getUserNames')()
138
139 # Return () if nothing is there
140 return ()
141
142
143
144
145 class GRUFUsers(GRUFFolder):
146 """
147 GRUFUsers : GRUFFolder that holds users
148 """
149 meta_type="GRUFUsers"
150 default_id = "Users"
151
152 manage_options = GRUFFolder.manage_options
153
154 class C__ac_roles__(Persistent, Implicit, DynaList.DynaList):
155 """
156 __ac_roles__ dynastring.
157 Do not forget to set _target to class instance.
158
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).
164
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.
170 """
171 def data(self,):
172 return self.userdefined_roles()
173
174
175 # Property setting
176 ac_roles = C__ac_roles__()
177 __ac_roles__ = ac_roles
178
179 enabled = 1 # True if it's enabled, false if not
180
181 def enableSource(self,):
182 """enableSource(self,) => Set enable status to 1
183 """
184 self.enabled = 1
185
186 def disableSource(self,):
187 """disableSource(self,) => explicit ;)
188 """
189 self.enabled = None
190
191 def isEnabled(self,):
192 """
193 Return true if enabled (surprisingly)
194 """
195 return not not self.enabled
196
197 def header_text(self,):
198 """
199 header_text(self,) => Text that appears in the content's view
200 heading zone
201 """
202 if not "acl_users" in self.objectIds():
203 return "Please put an acl_users here before ever " \
204 "starting to use this object."
205
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 />"""
209
210 return ret
211
212
213 def listGroups(self,):
214 """
215 listGroups(self,) => return a list of groups defined as roles
216 """
217 return self.Groups.restrictedTraverse('listGroups')()
218
219
220 def userdefined_roles(self):
221 "Return list of user-defined roles"
222 return self.listGroups()
223
224
225 class GRUFGroups(GRUFFolder):
226 """
227 GRUFGroups : GRUFFolder that holds groups
228 """
229 meta_type="GRUFGroups"
230 default_id = "Groups"
231
232 _group_prefix = "group_"
233
234
235 class C__ac_roles__(Persistent, Implicit, DynaList.DynaList):
236 """
237 __ac_roles__ dynastring.
238 Do not forget to set _target to class instance.
239
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).
245
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.
251 """
252 def data(self,):
253 return self.userdefined_roles()
254
255
256 ac_roles = C__ac_roles__()
257 __ac_roles__ = ac_roles
258
259
260 def header_text(self,):
261 """
262 header_text(self,) => Text that appears in the content's
263 view heading zone
264 """
265 ret = ""
266 if not "acl_users" in self.objectIds():
267 return "Please put an acl_users here before ever " \
268 "starting to use this object."
269 return ret
270
271 def _getGroup(self, id):
272 """
273 _getGroup(self, id) => same as getUser() but... with a group :-)
274 This method will return an UNWRAPPED object
275 """
276 return self.acl_users.getUser(id)
277
278
279 def listGroups(self, prefixed = 1):
280 """
281 Return a list of available groups.
282 Group names are prefixed !
283 """
284 if not prefixed:
285 return self.acl_users.getUserNames()
286 else:
287 ret = []
288 for grp in self.acl_users.getUserNames():
289 ret.append("%s%s" % (self._group_prefix, grp))
290 return ret
291
292
293 def userdefined_roles(self):
294 "Return list of user-defined roles"
295 return self.listGroups()
296
297
298 InitializeClass(GRUFUsers)
299 InitializeClass(GRUFGroups)