/usr/share/pyshared/zope/app/authentication/browser/rolepermissionview.py is in python-zope.app.authentication 3.9-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ##############################################################################
#
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Role Permission View Classes
$Id: rolepermissionview.py 116801 2010-09-25 09:04:32Z icemac $
"""
from datetime import datetime
from zope.component import getUtilitiesFor, getUtility
from zope.i18n import translate
from zope.interface import implements
from zope.exceptions.interfaces import UserError
from zope.i18nmessageid import ZopeMessageFactory as _
from zope.security.interfaces import IPermission
from zope.securitypolicy.interfaces import Unset, Allow, Deny
from zope.securitypolicy.interfaces import IRole, IRolePermissionManager
class RolePermissionView(object):
_pagetip = _("""For each permission you want to grant (or deny) to a role,
set the entry for that permission and role to a '+' (or '-').
Permissions are shown on the left side, going down.
Roles are shown accross the top.
""")
def pagetip(self):
return translate(self._pagetip, context=self.request)
def roles(self):
roles = getattr(self, '_roles', None)
if roles is None:
roles = [
(translate(role.title, context=self.request).strip(), role)
for name, role in getUtilitiesFor(IRole)]
roles.sort()
roles = self._roles = [role for name, role in roles]
return roles
def permissions(self):
permissions = getattr(self, '_permissions', None)
if permissions is None:
permissions = [
(translate(perm.title, context=self.request).strip(), perm)
for name, perm in getUtilitiesFor(IPermission)
if name != 'zope.Public']
permissions.sort()
permissions = self._permissions = [perm
for name, perm in permissions]
return permissions
def availableSettings(self, noacquire=False):
aq = {'id': Unset.getName(), 'shorttitle': ' ',
'title': _('permission-acquire', 'Acquire')}
rest = [{'id': Allow.getName(), 'shorttitle': '+',
'title': _('permission-allow', 'Allow')},
{'id': Deny.getName(), 'shorttitle': '-',
'title': _('permission-deny', 'Deny')},
]
if noacquire:
return rest
else:
return [aq]+rest
def permissionRoles(self):
context = self.context.__parent__
roles = self.roles()
return [PermissionRoles(permission, context, roles)
for permission in self.permissions()]
def permissionForID(self, pid):
roles = self.roles()
perm = getUtility(IPermission, pid)
return PermissionRoles(perm, self.context.__parent__, roles)
def roleForID(self, rid):
permissions = self.permissions()
role = getUtility(IRole, rid)
return RolePermissions(role, self.context.__parent__, permissions)
def update(self, testing=None):
status = ''
changed = False
if 'SUBMIT' in self.request:
roles = [r.id for r in self.roles()]
permissions = [p.id for p in self.permissions()]
prm = IRolePermissionManager(self.context.__parent__)
for ip in range(len(permissions)):
rperm = self.request.get("p%s" % ip)
if rperm not in permissions: continue
for ir in range(len(roles)):
rrole = self.request.get("r%s" % ir)
if rrole not in roles: continue
setting = self.request.get("p%sr%s" % (ip, ir), None)
if setting is not None:
if setting == Unset.getName():
prm.unsetPermissionFromRole(rperm, rrole)
elif setting == Allow.getName():
prm.grantPermissionToRole(rperm, rrole)
elif setting == Deny.getName():
prm.denyPermissionToRole(rperm, rrole)
else:
raise ValueError("Incorrect setting: %s" % setting)
changed = True
if 'SUBMIT_PERMS' in self.request:
prm = IRolePermissionManager(self.context.__parent__)
roles = self.roles()
rperm = self.request.get('permission_id')
settings = self.request.get('settings', ())
for ir in range(len(roles)):
rrole = roles[ir].id
setting = settings[ir]
if setting == Unset.getName():
prm.unsetPermissionFromRole(rperm, rrole)
elif setting == Allow.getName():
prm.grantPermissionToRole(rperm, rrole)
elif setting == Deny.getName():
prm.denyPermissionToRole(rperm, rrole)
else:
raise ValueError("Incorrect setting: %s" % setting)
changed = True
if 'SUBMIT_ROLE' in self.request:
role_id = self.request.get('role_id')
prm = IRolePermissionManager(self.context.__parent__)
allowed = self.request.get(Allow.getName(), ())
denied = self.request.get(Deny.getName(), ())
for permission in self.permissions():
rperm = permission.id
if rperm in allowed and rperm in denied:
permission_translated = translate(
permission.title, context=self.request)
msg = _('You choose both allow and deny for permission'
' "${permission}". This is not allowed.',
mapping = {'permission': permission_translated})
raise UserError(msg)
if rperm in allowed:
prm.grantPermissionToRole(rperm, role_id)
elif rperm in denied:
prm.denyPermissionToRole(rperm, role_id)
else:
prm.unsetPermissionFromRole(rperm, role_id)
changed = True
if changed:
formatter = self.request.locale.dates.getFormatter(
'dateTime', 'medium')
status = _("Settings changed at ${date_time}",
mapping={'date_time':
formatter.format(datetime.utcnow())})
return status
class PermissionRoles(object):
implements(IPermission)
def __init__(self, permission, context, roles):
self._permission = permission
self._context = context
self._roles = roles
def _getId(self):
return self._permission.id
id = property(_getId)
def _getTitle(self):
return self._permission.title
title = property(_getTitle)
def _getDescription(self):
return self._permission.description
description = property(_getDescription)
def roleSettings(self):
"""
Returns the list of setting names of each role for this permission.
"""
prm = IRolePermissionManager(self._context)
proles = prm.getRolesForPermission(self._permission.id)
settings = {}
for role, setting in proles:
settings[role] = setting.getName()
nosetting = Unset.getName()
return [settings.get(role.id, nosetting) for role in self._roles]
class RolePermissions(object):
implements(IRole)
def __init__(self, role, context, permissions):
self._role = role
self._context = context
self._permissions = permissions
def _getId(self):
return self._role.id
id = property(_getId)
def _getTitle(self):
return self._role.title
title = property(_getTitle)
def _getDescription(self):
return self._role.description
description = property(_getDescription)
def permissionsInfo(self):
prm = IRolePermissionManager(self._context)
rperms = prm.getPermissionsForRole(self._role.id)
settings = {}
for permission, setting in rperms:
settings[permission] = setting.getName()
nosetting = Unset.getName()
return [{'id': permission.id,
'title': permission.title,
'setting': settings.get(permission.id, nosetting)}
for permission in self._permissions]
|