[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Security - RolePermissionView.py:1.1.2.5

Casey Duncan casey_duncan@yahoo.com
Fri, 1 Feb 2002 12:34:33 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/Security
In directory cvs.zope.org:/tmp/cvs-serv31380

Modified Files:
      Tag: Zope-3x-branch
	RolePermissionView.py 
Log Message:
Added RolePermission Managment form
Added Unittests


=== Zope3/lib/python/Zope/App/Security/RolePermissionView.py 1.1.2.4 => 1.1.2.5 ===
 from Zope.App.Security.IRolePermissionManager import IRolePermissionManager
 from Zope.App.Security.IPermission import IPermission
+from Zope.App.Security.IRole import IRole
 
 class RolePermissionView(AttributePublisher, ContextDependent):
 
     index = PageTemplateFile('pt/manage_access.pt')
     manage_permissionForm = PageTemplateFile('pt/manage_permissionForm.pt')
+    manage_roleForm = PageTemplateFile('pt/manage_roleForm.pt')
     
     def roles(self):
         roles = getattr(self, '_roles', None)
@@ -54,10 +56,18 @@
     def permissionForID(self, pid):
         context = self.getContext()
         roles = self.roles()
-        perm = getService(self.getContext(), 'PermissionService'
+        perm = getService(context, 'PermissionService'
                           ).getPermission(pid)
         return PermissionRoles(perm, context, roles)
 
+    def roleForID(self, rid):
+        context = self.getContext()
+        permissions = self.permissions()
+        role = getService(context, 'RoleService'
+                          ).getRole(rid)
+        return RolePermissions(role, context, permissions)
+
+
     def action(self, REQUEST, testing=None):
         roles       = [r.getId() for r in self.roles()]
         permissions = [p.getId() for p in self.permissions()]
@@ -74,10 +84,40 @@
                     prm.retractPermissionFromRole(rperm, rrole)
 
         if not testing:
-            return self.index(
+            return self.index( REQUEST,
                 message="Settings changed at %s" % time.ctime(time.time())
                 )
 
+    def update_permission(self, REQUEST, permission_id,
+                          roles=(), testing=None):
+        prm = getAdapter(self.getContext(), IRolePermissionManager)
+
+        for ir in [r.getId() for r in self.roles()]:
+            if ir in roles:
+                prm.grantPermissionToRole(permission_id, ir)
+            else:
+                prm.retractPermissionFromRole(permission_id, ir)
+        if not testing:
+            return self.index(REQUEST,
+                              message="Settings changed at %s"
+                                  % time.ctime(time.time())
+                              )
+
+    def update_role(self, REQUEST, role_id,
+                    permissions=(), testing=None):
+        prm = getAdapter(self.getContext(), IRolePermissionManager)
+
+        for ip in [p.getId() for p in self.permissions()]:
+            if ip in permissions:
+                prm.grantPermissionToRole(ip, role_id)
+            else:
+                prm.retractPermissionFromRole(ip, role_id)
+        if not testing:
+            return self.index(REQUEST,
+                              message="Settings changed at %s"
+                                  % time.ctime(time.time())
+                              )        
+
 class PermissionRoles:
 
     __implements__ = IPermission
@@ -86,7 +126,6 @@
         self._permission = permission
         self._context    = context
         self._roles      = roles
-        #self._roles      = [role.getId() for role in roles]
 
     def getId(self):
         return self._permission.getId()
@@ -94,7 +133,7 @@
     def getTitle(self):
         return self._permission.getTitle()
 
-    def getDescription():
+    def getDescription(self):
         return self._permission.getDescription()
 
     def roles(self):
@@ -103,10 +142,39 @@
         return [((role.getId() in proles) and '1' or None)
                 for role in self._roles]
 
-    def rolesinfo(self):
+    def rolesInfo(self):
         prm = getAdapter(self._context, IRolePermissionManager)
         proles = prm.getRolesForPermission(self._permission.getId())
         return [{'id': role.getId(),
                  'title': role.getTitle(),
                  'checked': ((role.getId() in proles) and '1' or None)}
                 for role in self._roles]
+
+class RolePermissions:
+
+    __implements__ = IRole
+
+    def __init__(self, role, context, permissions):
+        self._role = role
+        self._context = context
+        self._permissions = permissions
+
+    def getId(self):
+        return self._role.getId()
+
+    def getTitle(self):
+        return self._role.getTitle()
+
+    def getDescription(self):
+        return self._role.getDescription()
+
+    def permissionsInfo(self):
+        prm = getAdapter(self._context, IRolePermissionManager)
+        rperms = prm.getPermissionsForRole(self._role.getId())
+        return [{'id': permission.getId(),
+                 'title': permission.getTitle(),
+                 'checked': ((permission.getId() in rperms) and '1' or None)}
+                for permission in self._permissions]        
+
+        
+