File: GRUFFolder.py

package info (click to toggle)
zope-groupuserfolder 3.1.1-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,416 kB
  • ctags: 1,037
  • sloc: python: 6,755; sh: 1,365; makefile: 147
file content (262 lines) | stat: -rw-r--r-- 8,034 bytes parent folder | download
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
##############################################################################
#
# Copyright (c) 2002-2003 Ingeniweb SARL
#
##############################################################################

"""GroupUserFolder product"""

# fakes a method from a DTML file
from Globals import MessageDialog, DTMLFile

from AccessControl import ClassSecurityInfo
from Globals import InitializeClass
from Acquisition import Implicit
from Globals import Persistent
from AccessControl.Role import RoleManager
from OFS.SimpleItem import Item
from OFS.PropertyManager import PropertyManager
from OFS import ObjectManager, SimpleItem
from DateTime import DateTime
from App import ImageFile

#XXX PJ DynaList is very hairy - why vs. PerstList?
# (see C__ac_roles__ class below for an explanation)
import DynaList
import AccessControl.Role, webdav.Collection
import Products
import os
import string
import shutil
import random



def manage_addGRUFUsers(self, id="Users", dtself=None,REQUEST=None,**ignored):
    """ """
    f=GRUFUsers(id)
    self=self.this()
    try:    self._setObject(id, f)
    except: return MessageDialog(
                   title  ='Item Exists',
                   message='This object already contains a GRUFUsers Folder',
                   action ='%s/manage_main' % REQUEST['URL1'])
    if REQUEST is not None:
        REQUEST['RESPONSE'].redirect(self.absolute_url()+'/manage_main')

def manage_addGRUFGroups(self, id="Groups", dtself=None,REQUEST=None,**ignored):
    """ """
    f=GRUFGroups(id)
    self=self.this()
    try:    self._setObject(id, f)
    except: return MessageDialog(
                   title  ='Item Exists',
                   message='This object already contains a GRUFGroups Folder',
                   action ='%s/manage_main' % REQUEST['URL1'])
    if REQUEST is not None:
        REQUEST['RESPONSE'].redirect(self.absolute_url()+'/manage_main')

class GRUFFolder(ObjectManager.ObjectManager, SimpleItem.Item):
    isAnObjectManager=1
    isPrincipiaFolderish=1
    manage_main=DTMLFile('dtml/GRUFFolder_main', globals())
    manage_options=( {'label':'Contents', 'action':'manage_main'}, ) + \
                     SimpleItem.Item.manage_options

    security = ClassSecurityInfo()

    def __init__(self, id = None):
        if id:
            self.id = id
        else:
            self.id = self.default_id

    def getId(self,):
        if self.id:
            return self.id
        else:
            return self.default_id      # Used for b/w compatibility

    def getUserSourceId(self,):
        return self.getId()

    def isValid(self,):
        """
        isValid(self,) => Return true if an acl_users is inside
        """
        if "acl_users" in self.objectIds():
            return 1
        return None

    security.declarePublic('header_text')
    def header_text(self,):
        """
        header_text(self,) => Text that appears in the content's
                              view heading zone
        """
        return ""

    def getUserFolder(self,):
        """
        getUserFolder(self,) => get the underlying user folder, UNRESTRICTED !
        """
        if not "acl_users" in self.objectIds():
            raise "ValueError", "Please put an acl_users in %s " \
                                "before using GRUF" % (self.getId(),)
        return self.restrictedTraverse('acl_users')

    def getUserNames(self,):
        """
        getUserNames(self,) => None

        We override this to prevent SimpleUserFolder to use GRUF's getUserNames() method.
        It's, of course, still possible to override a getUserNames method with SimpleUserFolder:
        just call it 'new_getUserNames'.
        """
        # Call the "new_getUserNames" method if available
        if "new_getUserNames" in self.objectIds():
            return self.new_getUserNames()

        # Return () if nothing is there
        return ()



class GRUFUsers(GRUFFolder):
    """
    GRUFUsers : GRUFFolder that holds users
    """
    meta_type="GRUFUsers"
    default_id = "Users"

    manage_options = GRUFFolder.manage_options


    class C__ac_roles__(Persistent, Implicit, DynaList.DynaList):
        """
        __ac_roles__ dynastring.
        Do not forget to set _target to class instance.

        XXX DynaList is surely not efficient but it's the only way
        I found to do what I wanted easily. Someone should take
        a look to PerstList instead to see if it's possible
        to do the same ? (ie. having a list which elements are
        the results of a method call).

        However, even if DynaList is not performant, it's not
        a critical point because this list is meant to be
        looked at only when a User object is looked at INSIDE
        GRUF (especially to set groups a user belongs to).
        So in practice only used within ZMI.
        """
        def data(self,):
            return self.userdefined_roles()


    ac_roles = C__ac_roles__()
    __ac_roles__ = ac_roles


    def header_text(self,):
        """
        header_text(self,) => Text that appears in the content's view
                              heading zone
        """
        if not "acl_users" in self.objectIds():
            return "Please put an acl_users here before ever " \
                   "starting to use this object."

        ret = """In this folder, groups are seen as ROLES from user's
                 view. To put a user into a group, affect him a role
                 that matches his group.<br />"""

        return ret


    def listGroups(self,):
        """
        listGroups(self,) => return a list of groups defined as roles
        """
        return self.Groups.restrictedTraverse('listGroups')()


    def userdefined_roles(self):
        "Return list of user-defined roles"
        return self.listGroups()


class GRUFGroups(GRUFFolder):
    """
    GRUFGroups : GRUFFolder that holds groups
    """
    meta_type="GRUFGroups"
    default_id = "Groups"

    _group_prefix = "group_"


    class C__ac_roles__(Persistent, Implicit, DynaList.DynaList):
        """
        __ac_roles__ dynastring.
        Do not forget to set _target to class instance.

        XXX DynaList is surely not efficient but it's the only way
        I found to do what I wanted easily. Someone should take
        a look to PerstList instead to see if it's possible
        to do the same ? (ie. having a list which elements are
        the results of a method call).

        However, even if DynaList is not performant, it's not
        a critical point because this list is meant to be
        looked at only when a User object is looked at INSIDE
        GRUF (especially to set groups a user belongs to).
        So in practice only used within ZMI.
        """
        def data(self,):
            return self.userdefined_roles()


    ac_roles = C__ac_roles__()
    __ac_roles__ = ac_roles


    def header_text(self,):
        """
        header_text(self,) => Text that appears in the content's
                              view heading zone
        """
        ret = ""
        if not "acl_users" in self.objectIds():
            return "Please put an acl_users here before ever " \
                   "starting to use this object."
        return ret

    def _getGroup(self, id):
        """
        _getGroup(self, id) => same as getUser() but... with a group :-)
        This method will return an UNWRAPPED object
        """
        return self.acl_users.getUser(id)


    def listGroups(self, prefixed = 1):
        """
        Return a list of available groups.
        Group names are prefixed !
        """
        if not prefixed:
            return self.acl_users.getUserNames()
        else:
            ret = []
            for grp in self.acl_users.getUserNames():
                ret.append("%s%s" % (self._group_prefix, grp))
            return ret


    def userdefined_roles(self):
        "Return list of user-defined roles"
        return self.listGroups()


InitializeClass(GRUFUsers)
InitializeClass(GRUFGroups)