Back to index

plone3  3.1.7
GRUFFolder.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 ## GroupUserFolder
00003 ## Copyright (C)2006 Ingeniweb
00004 
00005 ## This program is free software; you can redistribute it and/or modify
00006 ## it under the terms of the GNU General Public License as published by
00007 ## the Free Software Foundation; either version 2 of the License, or
00008 ## (at your option) any later version.
00009 
00010 ## This program is distributed in the hope that it will be useful,
00011 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 ## GNU General Public License for more details.
00014 
00015 ## You should have received a copy of the GNU General Public License
00016 ## along with this program; see the file COPYING. If not, write to the
00017 ## Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 """
00019 
00020 """
00021 __version__ = "$Revision:  $"
00022 # $Source:  $
00023 # $Id: GRUFFolder.py 30098 2006-09-08 12:35:01Z encolpe $
00024 __docformat__ = 'restructuredtext'
00025 
00026 
00027 # fakes a method from a DTML file
00028 from Globals import MessageDialog, DTMLFile
00029 
00030 from AccessControl import ClassSecurityInfo
00031 from Globals import InitializeClass
00032 from Acquisition import Implicit
00033 from Globals import Persistent
00034 from AccessControl.Role import RoleManager
00035 from OFS.SimpleItem import Item
00036 from OFS.PropertyManager import PropertyManager
00037 from OFS import ObjectManager, SimpleItem
00038 from DateTime import DateTime
00039 from App import ImageFile
00040 
00041 #XXX PJ DynaList is very hairy - why vs. PerstList?
00042 # (see C__ac_roles__ class below for an explanation)
00043 import DynaList
00044 import AccessControl.Role, webdav.Collection
00045 import Products
00046 import os
00047 import string
00048 import shutil
00049 import random
00050 
00051 
00052 
00053 def manage_addGRUFUsers(self, id="Users", dtself=None,REQUEST=None,**ignored):
00054     """ """
00055     f=GRUFUsers(id)
00056     self=self.this()
00057     try:    self._setObject(id, f)
00058     except: return MessageDialog(
00059                    title  ='Item Exists',
00060                    message='This object already contains a GRUFUsers Folder',
00061                    action ='%s/manage_main' % REQUEST['URL1'])
00062     if REQUEST is not None:
00063         REQUEST['RESPONSE'].redirect(self.absolute_url()+'/manage_main')
00064 
00065 def manage_addGRUFGroups(self, id="Groups", dtself=None,REQUEST=None,**ignored):
00066     """ """
00067     f=GRUFGroups(id)
00068     self=self.this()
00069     try:    self._setObject(id, f)
00070     except: return MessageDialog(
00071                    title  ='Item Exists',
00072                    message='This object already contains a GRUFGroups Folder',
00073                    action ='%s/manage_main' % REQUEST['URL1'])
00074     if REQUEST is not None:
00075         REQUEST['RESPONSE'].redirect(self.absolute_url()+'/manage_main')
00076 
00077 class GRUFFolder(ObjectManager.ObjectManager, SimpleItem.Item):
00078     isAnObjectManager=1
00079     isPrincipiaFolderish=1
00080     manage_main=DTMLFile('dtml/GRUFFolder_main', globals())
00081     manage_options=( {'label':'Contents', 'action':'manage_main'}, ) + \
00082                      SimpleItem.Item.manage_options
00083 
00084     security = ClassSecurityInfo()
00085     def __creatable_by_emergency_user__(self): return 1
00086 
00087     def __init__(self, id = None):
00088         if id:
00089             self.id = id
00090         else:
00091             self.id = self.default_id
00092 
00093     def getId(self,):
00094         if self.id:
00095             return self.id
00096         else:
00097             return self.default_id      # Used for b/w compatibility
00098 
00099     def getUserSourceId(self,):
00100         return self.getId()
00101 
00102     def isValid(self,):
00103         """
00104         isValid(self,) => Return true if an acl_users is inside
00105         """
00106         if "acl_users" in self.objectIds():
00107             return 1
00108         return None
00109 
00110     security.declarePublic('header_text')
00111     def header_text(self,):
00112         """
00113         header_text(self,) => Text that appears in the content's
00114                               view heading zone
00115         """
00116         return ""
00117 
00118     def getUserFolder(self,):
00119         """
00120         getUserFolder(self,) => get the underlying user folder, UNRESTRICTED !
00121         """
00122         if not "acl_users" in self.objectIds():
00123             raise "ValueError", "Please put an acl_users in %s " \
00124                                 "before using GRUF" % (self.getId(),)
00125         return self.restrictedTraverse('acl_users')
00126 
00127     def getUserNames(self,):
00128         """
00129         getUserNames(self,) => None
00130 
00131         We override this to prevent SimpleUserFolder to use GRUF's getUserNames() method.
00132         It's, of course, still possible to override a getUserNames method with SimpleUserFolder:
00133         just call it 'new_getUserNames'.
00134         """
00135         # Call the "new_getUserNames" method if available
00136         if "new_getUserNames" in self.objectIds():
00137             return self.unrestrictedTraverse('new_getUserNames')()
00138 
00139         # Return () if nothing is there
00140         return ()
00141 
00142 
00143 
00144 
00145 class GRUFUsers(GRUFFolder):
00146     """
00147     GRUFUsers : GRUFFolder that holds users
00148     """
00149     meta_type="GRUFUsers"
00150     default_id = "Users"
00151 
00152     manage_options = GRUFFolder.manage_options
00153 
00154     class C__ac_roles__(Persistent, Implicit, DynaList.DynaList):
00155         """
00156         __ac_roles__ dynastring.
00157         Do not forget to set _target to class instance.
00158 
00159         XXX DynaList is surely not efficient but it's the only way
00160         I found to do what I wanted easily. Someone should take
00161         a look to PerstList instead to see if it's possible
00162         to do the same ? (ie. having a list which elements are
00163         the results of a method call).
00164 
00165         However, even if DynaList is not performant, it's not
00166         a critical point because this list is meant to be
00167         looked at only when a User object is looked at INSIDE
00168         GRUF (especially to set groups a user belongs to).
00169         So in practice only used within ZMI.
00170         """
00171         def data(self,):
00172             return self.userdefined_roles()
00173 
00174 
00175     # Property setting
00176     ac_roles = C__ac_roles__()
00177     __ac_roles__ = ac_roles
00178 
00179     enabled = 1                         # True if it's enabled, false if not
00180 
00181     def enableSource(self,):
00182         """enableSource(self,) => Set enable status to 1
00183         """
00184         self.enabled = 1
00185 
00186     def disableSource(self,):
00187         """disableSource(self,) => explicit ;)
00188         """
00189         self.enabled = None
00190 
00191     def isEnabled(self,):
00192         """
00193         Return true if enabled (surprisingly)
00194         """
00195         return not not self.enabled
00196 
00197     def header_text(self,):
00198         """
00199         header_text(self,) => Text that appears in the content's view
00200                               heading zone
00201         """
00202         if not "acl_users" in self.objectIds():
00203             return "Please put an acl_users here before ever " \
00204                    "starting to use this object."
00205 
00206         ret = """In this folder, groups are seen as ROLES from user's
00207                  view. To put a user into a group, affect him a role
00208                  that matches his group.<br />"""
00209 
00210         return ret
00211 
00212 
00213     def listGroups(self,):
00214         """
00215         listGroups(self,) => return a list of groups defined as roles
00216         """
00217         return self.Groups.restrictedTraverse('listGroups')()
00218 
00219 
00220     def userdefined_roles(self):
00221         "Return list of user-defined roles"
00222         return self.listGroups()
00223 
00224 
00225 class GRUFGroups(GRUFFolder):
00226     """
00227     GRUFGroups : GRUFFolder that holds groups
00228     """
00229     meta_type="GRUFGroups"
00230     default_id = "Groups"
00231 
00232     _group_prefix = "group_"
00233 
00234 
00235     class C__ac_roles__(Persistent, Implicit, DynaList.DynaList):
00236         """
00237         __ac_roles__ dynastring.
00238         Do not forget to set _target to class instance.
00239 
00240         XXX DynaList is surely not efficient but it's the only way
00241         I found to do what I wanted easily. Someone should take
00242         a look to PerstList instead to see if it's possible
00243         to do the same ? (ie. having a list which elements are
00244         the results of a method call).
00245 
00246         However, even if DynaList is not performant, it's not
00247         a critical point because this list is meant to be
00248         looked at only when a User object is looked at INSIDE
00249         GRUF (especially to set groups a user belongs to).
00250         So in practice only used within ZMI.
00251         """
00252         def data(self,):
00253             return self.userdefined_roles()
00254 
00255 
00256     ac_roles = C__ac_roles__()
00257     __ac_roles__ = ac_roles
00258 
00259 
00260     def header_text(self,):
00261         """
00262         header_text(self,) => Text that appears in the content's
00263                               view heading zone
00264         """
00265         ret = ""
00266         if not "acl_users" in self.objectIds():
00267             return "Please put an acl_users here before ever " \
00268                    "starting to use this object."
00269         return ret
00270 
00271     def _getGroup(self, id):
00272         """
00273         _getGroup(self, id) => same as getUser() but... with a group :-)
00274         This method will return an UNWRAPPED object
00275         """
00276         return self.acl_users.getUser(id)
00277 
00278 
00279     def listGroups(self, prefixed = 1):
00280         """
00281         Return a list of available groups.
00282         Group names are prefixed !
00283         """
00284         if not prefixed:
00285             return self.acl_users.getUserNames()
00286         else:
00287             ret = []
00288             for grp in self.acl_users.getUserNames():
00289                 ret.append("%s%s" % (self._group_prefix, grp))
00290             return ret
00291 
00292 
00293     def userdefined_roles(self):
00294         "Return list of user-defined roles"
00295         return self.listGroups()
00296 
00297 
00298 InitializeClass(GRUFUsers)
00299 InitializeClass(GRUFGroups)