[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Security/Grants/Views/Browser - PrincipalRoleView.py:1.2 configure.zcml:1.4 principal_role_association.pt:1.2

Florent Guillaume fg@nuxeo.com
Tue, 25 Jun 2002 11:27:53 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/Security/Grants/Views/Browser
In directory cvs.zope.org:/tmp/cvs-serv422/lib/python/Zope/App/Security/Grants/Views/Browser

Modified Files:
	PrincipalRoleView.py configure.zcml 
	principal_role_association.pt 
Log Message:
Finish implementing existing Principal Role management views.
Filtering is not really useable now, it should ask for a
prefix to use before displaying the filtered matrix.
Use a Persistent LocalSecurityMap to store the annotations.
Fix the tests.

Add 'Principal Roles' to the Folder menu (XXX should add it
to other (all?) content objects too).


Did the name="foo.bar.baz:records" syntax ever work in publisher ?



=== Zope3/lib/python/Zope/App/Security/Grants/Views/Browser/PrincipalRoleView.py 1.1 => 1.2 ===
 from Zope.App.Security.IRole import IRole
 
+from Zope.App.Security.Settings import Unset
+
+
 class PrincipalRoleView(BrowserView):
 
     index = ViewPageTemplateFile('principal_role_association.pt')
 
     def getAllPrincipals(self):
-
         principals = getattr(self, '_principals', None)
-
         if principals is None:
             principals = self._principals = getService(
                 self.context, 'AuthenticationService'
-                ).getPrincipals()
-
+                ).getPrincipals('')
+            principals = [p.getId() for p in principals]
         return principals
-    
-    def getAllRoles(self):
 
+    def getAllRoles(self):
         roles = getattr(self, '_roles', None)
-
         if roles is None:
             roles = self._roles = getService(self.context, 'RoleService'
                 ).getRoles()
-
+            roles = [r.getId() for r in roles]
         return roles
 
-    def createGrid( self, principals=None, roles=None ):
-
+    def createGrid(self, principals=None, roles=None):
         if not principals:
             principals = self.getAllPrincipals()
-
         if not roles:
             roles = self.getAllRoles()
+        return PrincipalRoleGrid(principals, roles, self.context)
 
-        return PrincipalRoleGrid( principals, roles, self.context )
-        
-    def action(self, principals, roles, mapping, testing=None):
-
-        for row in mapping:
-            pid = row.permission_id
-            roles = row.role_ids
+    def action(self, principals, roles, testing=None):
+        prm = getAdapter(self.context, IPrincipalRoleManager)
+        for role in roles:
+            for principal in principals:
+                name = 'grid.%s.%s' % (role, principal)
+                setting = self.request.get(name, 'Unset')
+                if setting == 'Unset':
+                    prm.unsetRoleForPrincipal(role, principal)
+                elif setting == 'Assign':
+                    prm.assignRoleToPrincipal(role, principal)
+                elif setting == 'Remove':
+                    prm.removeRoleFromPrincipal(role, principal)
+                else:
+                    raise ValueError("Incorrect setting %s" % setting)
 
         if not testing:
-            return self.index( 
+            return self.index(
                 message="Settings changed at %s" % time.ctime(time.time())
                 )
 
 
 class PrincipalRoleGrid:
 
-    def __init__( self, principals, roles, context ):    
-
+    def __init__(self, principals, roles, context):
         self._principals = principals
         self._roles = roles
         self._grid = {}
 
-        map = getAdapter( context, IPrincipalRoleMap )
+        map = getAdapter(context, IPrincipalRoleMap)
 
         for role in roles:
             for principal in principals:
-                setting = map.getSetting( role, principal )
-                self._grid[ ( role, principal ) ] = setting
+                setting = map.getSetting(role, principal)
+                self._grid[(principal, role)] = setting.getName()
 
-    def principals( self ):
+    def principals(self):
         return self._principals
 
-    def roles( self ):
+    def roles(self):
         return self._roles
 
-    def getValue( self, role, principal ):
-        return self._grid[ ( role, principal ) ]
+    def getValue(self, principal, role):
+        return self._grid[(principal, role)]
 
-    def listAvailableValues( self ):
-        return ( 'Unset', 'Assigned', 'Removed' )
+    def listAvailableValues(self):
+        # XXX rather use Assign.getName() & co
+        return ('Unset', 'Assign', 'Remove')
 


=== Zope3/lib/python/Zope/App/Security/Grants/Views/Browser/configure.zcml 1.3 => 1.4 ===
   </browser:view>
 
+
+  <browser:view for="Zope.App.OFS.Annotation.IAnnotatable."
+                permission="Zope.Security"
+                factory=".PrincipalRoleView.">
+    <browser:page name="PrincipalRoles.html"
+                  attribute="index" />
+    <browser:page name="ChangePrincipalRoles.html"
+                  attribute="action" />
+  </browser:view>
+
+
   <content class=".PrincipalPermissionView.">
     <require
         permission="Zope.Security"


=== Zope3/lib/python/Zope/App/Security/Grants/Views/Browser/principal_role_association.pt 1.1 => 1.2 ===
     <style metal:fill-slot="headers" type="text/css"> </style>
   </head>
-  <body metal:fill-slot="main" tal:define="hasFilter python:request.get('Filter', None)">
+  <body metal:fill-slot="body" tal:define="hasFilter python:request.get('Filter', None)">
+
+      <div tal:replace="options/message|nothing" />
+
       <div tal:condition="not: hasFilter">
-        <span class="message"> Apply filter </span>       
-        <form method="POST" action="" tal:attributes="action template/getId">
-           <span tal:define="principals view/getAllPrincipals"> 
-           Principal(s): 
+        <span class="message"> Apply filter </span>
+        <form method="POST" action="@@PrincipalRoles.html">
+           <span tal:define="principals view/getAllPrincipals">
+           Principal(s):
             <select name="principals:list" multiple>
              <option tal:repeat="principal principals" tal:attributes="value principal/getId" tal:content="principal/getTitle">my title</option>
             </select>
            </span>
-            
+
            <span tal:define="roles view/getAllRoles">
-           Role(s): 
+           Role(s):
             <select name="roles:list" multiple>
              <option tal:repeat="role roles" tal:attributes="value role/getId" tal:content="role/getTitle">my title</option>
             </select>
            </span>
-            
+
            <input type="submit" name="Filter" value="Filter" />
         </form>
-        
-      </div>      
-      <!-- WANRING!! SELF POSTING FORM!! -->
-      
+
+      </div>
+      <!-- WARNING!! SELF POSTING FORM!! -->
+
       <div tal:condition="hasFilter">
         <div class="principalRolesGrid"
-             tal:define="principalRoleGrid python:view.createGrid( request.get('roles', None), request.get('principals', None)); " >
+             tal:define="principalRoleGrid python:view.createGrid(request.get('principals', None), request.get('roles', None)); " >
 
-          <span tal:define="global listPrincipals principalRoleGrid/principals;       
+          <span tal:define="global listPrincipals principalRoleGrid/principals;
                             global listRoles principalRoleGrid/roles;
-                            global listValues principalRoleGrid/listAvailabaleValues" />
+                            global listValues principalRoleGrid/listAvailableValues" />
 
-          <form method="POST" action="apply">
+          <form method="POST" action="ChangePrincipalRoles.html">
           <table>
             <tr class="roleHeading">
               <td class="principal">
                 &nbsp;
               </td>
-              <td class="role" tal:repeat="role listRoles" tal:content="role/getId">
-                role id
+              <td class="role" tal:repeat="role listRoles" tal:content="role">
+                Role Id
               </td>
             </tr>
-            
+
             <tr class="principalRoleRow" tal:repeat="principal listPrincipals">
-              <td class="principalLabel" tal:content="principal/getId">
-							  Principal Id
+              <td class="principalLabel" tal:content="principal">
+                Principal Id
               </td>
-							
+
               <td class="principalRole" tal:repeat="role listRoles">
-                <select name="mapping.principal.row" 
-                        tal:attributes="name string:grid.${principal/getId}.${role/getId}:records"
-                        tal:define="selectedValue python:1 or principalRoleGrid.grid(principal, role)" >
-                  <option value="" tal:repeat="defaultValue listValues" 
-                   tal:attributes="selected python:test(defaultValue==selectedValue, 'selected', '');
-									                 value defaultValue;"
+                <select name="grid.role.principal:records"
+                        tal:attributes="name string:grid.${role}.${principal}"
+                        tal:define="selectedValue python:principalRoleGrid.getValue(principal, role)" >
+                  <option value="" tal:repeat="defaultValue listValues"
+                   tal:attributes="selected python:defaultValue==selectedValue;
+                                   value defaultValue;
+                                   debugsel selectedValue;
+"
                    tal:content="defaultValue">
                   &nbsp;
                   </option>
                 </select>
               </td>
-            </tr>  
-            
+            </tr>
+
           </table>
-					<input type="submit" name="apply" value="apply" />        
+          <input type="hidden" name="principals:list"
+            tal:repeat="principal listPrincipals"
+            tal:attributes="value principal" />
+          <input type="hidden" name="roles:list"
+            tal:repeat="role listRoles"
+            tal:attributes="value role" />
+
+          <input type="submit" name="apply" value="apply" />
+
           </form>
-          
-        </div> 
+
+        </div>
       </div>
-       
+
   </body>
 </html>