Commit 2a8eb4db authored by Jean-Paul Smets's avatar Jean-Paul Smets

initial upload


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@2998 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 3694e0af
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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
#
##############################################################################
""" Information about customizable roles.
$Id$
"""
from AccessControl import ClassSecurityInfo
from Globals import InitializeClass
from Acquisition import aq_inner, aq_parent
from OFS.SimpleItem import SimpleItem
from Products.CMFCore.utils import getToolByName
from Products.CMFCore.Expression import Expression
from Permissions import View
from Permissions import ManagePortal
from types import StringType
class RoleInformation( SimpleItem ):
""" Represent a single selectable role.
Roles generate links to views of content, or to specific methods
of the site. They can be filtered via their conditions.
"""
_isRoleInformation = 1
__allow_access_to_unprotected_subobjects__ = 1
security = ClassSecurityInfo()
def __init__( self
, id
, title=''
, description=''
, category=()
, condition=''
, priority=10
, base_category=()
, user=''
):
""" Set up an instance.
"""
if condition and type( condition ) == type( '' ):
condition = Expression( condition )
if user and type( user ) == type( '' ):
user = Expression( user )
self.id = id
self.title = title
self.description = description
self.category = category
self.condition = condition
self.priority = priority
self.base_category = base_category
self.user = user
security.declareProtected( View, 'Title' )
def Title(self):
""" Return the Role title.
"""
return self.title or self.getId()
security.declareProtected( View, 'Description' )
def Description( self ):
""" Return a description of the role.
"""
return self.description
security.declarePrivate( 'testCondition' )
def testCondition( self, ec ):
""" Evaluate condition using context, 'ec', and return 0 or 1.
"""
if self.condition:
return self.condition(ec)
else:
return 1
security.declarePublic( 'getRole' )
def getRole( self, ec ):
""" Compute the role using context, 'ec'; return a mapping of
info about the role.
"""
info = {}
info['id'] = self.id
info['name'] = self.Title()
expr = self.getUserExpression()
__traceback_info__ = (info['id'], info['name'], expr)
if self.user:
info['user'] = self.user( ec ) or None
else:
info['user'] = 'The Default User XXX'
info['category'] = self.getCategory()
info['base_category'] = self.getBaseCategory()
return info
security.declarePublic( 'getUserExpression' )
def getUserExpression( self ):
""" Return the text of the TALES expression for our URL.
"""
user = getattr(self, 'user', '')
expr = user and user.text or ''
if expr and type( expr ) is StringType:
if not expr.startswith('python:') and not expr.startswith('string:'):
expr = 'string:${object_url}/%s' % expr
self.user = Expression( expr )
return expr
security.declarePrivate( 'setRoleExpression' )
def setUserExpression(self, user):
if user and type( user ) is StringType:
if not user.startswith('python:') and not user.startswith('string:'):
user = 'string:${object_url}/%s' % user
user = Expression( user )
self.user = user
security.declarePublic( 'getCondition' )
def getCondition(self):
""" Return the text of the TALES expression for our condition.
"""
return getattr( self, 'condition', None ) and self.condition.text or ''
security.declarePublic( 'getCategory' )
def getCategory( self ):
""" Return the category
as a tuple (to prevent script from modifying it)
"""
return tuple(filter(lambda x: x, self.category)) or ()
security.declarePublic( 'getBaseCategory' )
def getBaseCategory( self ):
""" Return the base_category
as a tuple (to prevent script from modifying it)
"""
return tuple(getattr(self, 'base_category', ()))
security.declarePrivate( 'base_category' )
def clone( self ):
""" Return a newly-created RI just like us.
"""
return self.__class__( id=self.id
, title=self.title
, description=self.description
, category =self.category
, condition=self.getCondition()
, priority =self.priority
, base_category=self.base_category
, user=self.getUserExpression()
)
InitializeClass( RoleInformation )
class ori:
#Provided for backwards compatability
# Provides information that may be needed when constructing the list of
# available actions.
__allow_access_to_unprotected_subobjects__ = 1
def __init__( self, tool, folder, object=None ):
self.portal = portal = aq_parent(aq_inner(tool))
membership = getToolByName(tool, 'portal_membership')
self.isAnonymous = membership.isAnonymousUser()
self.user_id = membership.getAuthenticatedMember().getId()
self.portal_url = portal.absolute_url()
if folder is not None:
self.folder_url = folder.absolute_url()
self.folder = folder
else:
self.folder_url = self.portal_url
self.folder = portal
self.content = object
if object is not None:
self.content_url = object.absolute_url()
else:
self.content_url = None
def __getitem__(self, name):
# Mapping interface for easy string formatting.
if name[:1] == '_':
raise KeyError, name
if hasattr(self, name):
return getattr(self, name)
raise KeyError, name
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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
#
##############################################################################
""" Implement a shared base for tools which provide roles.
$Id$
"""
from Globals import DTMLFile, InitializeClass
from AccessControl import ClassSecurityInfo
from Products.CMFCore.Expression import Expression
from Products.ERP5Type import _dtmldir
from RoleInformation import RoleInformation
from Permissions import ManagePortal
#from interfaces.portal_roles import RoleProvider as IRoleProvider
class RoleProviderBase:
""" Provide RoleTabs and management methods for RoleProviders
"""
#__implements__ = IRoleProvider
security = ClassSecurityInfo()
_roles = ()
_roles_form = DTMLFile( 'editToolsRoles', _dtmldir )
manage_options = ( { 'label' : 'Roles'
, 'action' : 'manage_editRolesForm'
}
,
)
#
# RoleProvider interface
#
security.declarePrivate( 'getRoleList' )
def getRoleList( self, info=None ):
""" Return all the roles defined by a provider.
"""
return self._roles or ()
#
# ZMI methods
#
security.declareProtected( ManagePortal, 'manage_editRolesForm' )
def manage_editRolesForm( self, REQUEST, manage_tabs_message=None ):
""" Show the 'Roles' management tab.
"""
roles = []
for a in self.getRoleList():
a1 = {}
a1['id'] = a.getId() # The Role Id (ex. Assignor)
a1['name'] = a.Title() # The name of this role definition (ex. Assignor at company X)
a1['category'] = a.getCategory() or [] # Category definition
a1['base_category'] = a.getBaseCategory() # Base Category Definition
a1['user'] = a.getUserExpression()
a1['condition'] = a.getCondition()
roles.append(a1)
return self._roles_form( self
, REQUEST
, roles=roles
, management_view='Roles'
, manage_tabs_message=manage_tabs_message
)
security.declareProtected( ManagePortal, 'addRole' )
def addRole( self
, id
, name
, user
, condition
, category
, base_category=()
, REQUEST=None
):
""" Add an role to our list.
"""
if not name:
raise ValueError('A name is required.')
a_expr = user and Expression(text=str(user)) or ''
c_expr = condition and Expression(text=str(condition)) or ''
new_roles = self._cloneRoles()
new_role = RoleInformation( id=str(id)
, title=str(name)
, user=a_expr
, condition=c_expr
, category=category.split('\n')
, base_category=base_category.split()
)
new_roles.append( new_role )
self._roles = tuple( new_roles )
if REQUEST is not None:
return self.manage_editRolesForm(
REQUEST, manage_tabs_message='Added.')
security.declareProtected( ManagePortal, 'changeRoles' )
def changeRoles( self, properties=None, REQUEST=None ):
""" Update our list of roles.
"""
if properties is None:
properties = REQUEST
roles = []
for index in range( len( self._roles ) ):
roles.append( self._extractRole( properties, index ) )
self._roles = tuple( roles )
if REQUEST is not None:
return self.manage_editRolesForm(REQUEST, manage_tabs_message=
'Roles changed.')
security.declareProtected( ManagePortal, 'deleteRoles' )
def deleteRoles( self, selections=(), REQUEST=None ):
""" Delete roles indicated by indexes in 'selections'.
"""
sels = list( map( int, selections ) ) # Convert to a list of integers.
old_roles = self._cloneRoles()
new_roles = []
for index in range( len( old_roles ) ):
if index not in sels:
new_roles.append( old_roles[ index ] )
self._roles = tuple( new_roles )
if REQUEST is not None:
return self.manage_editRolesForm(
REQUEST, manage_tabs_message=(
'Deleted %d role(s).' % len(sels)))
security.declareProtected( ManagePortal, 'moveUpRoles' )
def moveUpRoles( self, selections=(), REQUEST=None ):
""" Move the specified roles up one slot in our list.
"""
sels = list( map( int, selections ) ) # Convert to a list of integers.
sels.sort()
new_roles = self._cloneRoles()
for idx in sels:
idx2 = idx - 1
if idx2 < 0:
# Wrap to the bottom.
idx2 = len(new_roles) - 1
# Swap.
a = new_roles[idx2]
new_roles[idx2] = new_roles[idx]
new_roles[idx] = a
self._roles = tuple( new_roles )
if REQUEST is not None:
return self.manage_editRolesForm(
REQUEST, manage_tabs_message=(
'Moved up %d role(s).' % len(sels)))
security.declareProtected( ManagePortal, 'moveDownRoles' )
def moveDownRoles( self, selections=(), REQUEST=None ):
""" Move the specified roles down one slot in our list.
"""
sels = list( map( int, selections ) ) # Convert to a list of integers.
sels.sort()
sels.reverse()
new_roles = self._cloneRoles()
for idx in sels:
idx2 = idx + 1
if idx2 >= len(new_roles):
# Wrap to the top.
idx2 = 0
# Swap.
a = new_roles[idx2]
new_roles[idx2] = new_roles[idx]
new_roles[idx] = a
self._roles = tuple( new_roles )
if REQUEST is not None:
return self.manage_editRolesForm(
REQUEST, manage_tabs_message=(
'Moved down %d role(s).' % len(sels)))
#
# Helper methods
#
security.declarePrivate( '_cloneRoles' )
def _cloneRoles( self ):
""" Return a list of roles, cloned from our current list.
"""
return map( lambda x: x.clone(), list( self._roles ) )
security.declarePrivate( '_extractRole' )
def _extractRole( self, properties, index ):
""" Extract an RoleInformation from the funky form properties.
"""
id = str( properties.get( 'id_%d' % index, '' ) )
name = str( properties.get( 'name_%d' % index, '' ) )
user = str( properties.get( 'user_%d' % index, '' ) )
condition = str( properties.get( 'condition_%d' % index, '' ) )
category = properties.get( 'category_%d' % index, '' ).split('\n')
base_category = properties.get( 'base_category_%d' % index, '' ).split()
if not name:
raise ValueError('A name is required.')
if user is not '':
user = Expression( text=user )
if condition is not '':
condition = Expression( text=condition )
return RoleInformation( id=id
, title=name
, user=user
, condition=condition
, category=category
, base_category=base_category
)
InitializeClass(RoleProviderBase)
<dtml-let form_title="'Roles'">
<dtml-if manage_page_header>
<dtml-var manage_page_header>
<dtml-else>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html lang="en">
<head>
<title>&dtml-form_title;</title>
</head>
<body bgcolor="#FFFFFF" link="#000099" vlink="#555555">
<h3>&dtml-form_title;</h3>
</dtml-if>
</dtml-let>
<dtml-var manage_tabs>
<form action="&dtml-absolute_url;" method="POST">
<dtml-if roles>
<table>
<dtml-in roles mapping>
<dtml-let index=sequence-index>
<tr>
<td>
<div class="form-element">
<input type="checkbox" name="selections:list" value="&dtml-index;" />
</div>
</td>
<td>
<div class="form-label">
Name
</div>
</td>
<td>
<div class="form-element">
<input type="text" name="name_&dtml-index;" value="&dtml-name;" />
</div>
</td>
</tr>
<tr>
<td></td>
<td>
<div class="form-label">
Role
</div>
</td>
<td>
<div class="form-element">
<input type="text" name="id_&dtml-index;" value="&dtml-id;" />
</div>
</td>
</tr>
<tr>
<td></td>
<td>
<div class="form-label">
Condition
</div>
</td>
<td>
<div class="form-element">
<input type="text" name="condition_&dtml-index;" value="&dtml-condition;" size="80"/>
</div>
</td>
</tr>
<tr>
<td></td>
<td>
<div class="form-label">
User
</div>
</td>
<td>
<div class="form-element">
<input type="text" name="user_&dtml-index;" value="&dtml-user;" size="80" />
</div>
</td>
</tr>
<tr>
<td></td>
<td>
<div class="form-label">
Base Category
</div>
</td>
<td>
<div class="form-element">
<input type="text" size="40" name="base_category_&dtml-index;" value="<dtml-var "' '.join(base_category)">" />
</div>
</td>
</tr>
<tr>
<td></td>
<td>
<div class="form-label">
Category
</div>
</td>
<td>
<div class="form-element">
<textarea rows="4" cols="80" name="category_&dtml-index;"><dtml-var "'\n'.join(category)"></textarea>
</div>
</td>
</tr>
</dtml-let>
<tr><td colspan="3">
<hr />
</td></tr>
</dtml-in>
</table>
<div class="form-element">
<table>
<tr>
<td><input type="submit" name="changeRoles:method" value="Save" /></td>
<td><input type="submit" name="deleteRoles:method" value="Delete" /></td>
<td><input type="submit" name="moveUpRoles:method" value="Move up" /></td>
<td><input type="submit" name="moveDownRoles:method" value="Move down" /></td>
</tr>
</table>
</div>
</dtml-if>
<h3 class="form-help">
Add a role
</h3>
<table>
<tr>
<td>
</td>
<td>
<div class="form-label">
Name
</div>
</td>
<td>
<div class="form-element">
<input type="text" name="name" />
</div>
</td>
</tr>
<tr>
<td></td>
<td>
<div class="form-label">
Role
</div>
</td>
<td>
<div class="form-element">
<input type="text" name="id" />
</div>
</td>
</tr>
<tr>
<td></td>
<td>
<div class="form-label">
Condition
</div>
</td>
<td>
<div class="form-element">
<input type="text" name="condition" size="80"/>
</div>
</td>
</tr>
<tr>
<td></td>
<td>
<div class="form-label">
User
</div>
</td>
<td>
<div class="form-element">
<input type="text" name="user" size="80" />
</div>
</td>
</tr>
<tr>
<td></td>
<td>
<div class="form-label">
Base Category
</div>
</td>
<td>
<div class="form-element">
<input type="text" size="40" name="base_category" />
</div>
</td>
</tr>
<tr>
<td></td>
<td>
<div class="form-label">
Category
</div>
</td>
<td>
<div class="form-element">
<textarea rows="4" cols="80" name="category"></textarea>
</div>
</td>
</tr>
</table>
<div class="form-element">
<input type="submit" name="addRole:method" value="Add" />
</div>
</form>
<dtml-if manage_page_footer>
<dtml-var manage_page_footer>
<dtml-else>
</body></html>
</dtml-if>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment