def test_checkWithKwargs(self): # # Checks with kwargs # guard = Guard() # Create compulsory context elements sm = getSecurityManager() self.site._setObject('dummy', DummyContent('dummy')) ob = self.site.dummy wf_def = self._getDummyWorkflow() # Initialize the guard with an ok guard guard_props = {'guard_permissions':'', 'guard_roles':'', 'guard_expr' :'python:1'} res = guard.changeFromProperties(guard_props) self.assert_(res) self.assert_(guard.check(sm, wf_def, ob, arg1=1, arg2=2)) # Initialize the guard with a not ok guard guard_props = {'guard_permissions':'', 'guard_roles':'', 'guard_expr' :'python:0'} res = guard.changeFromProperties(guard_props) self.assert_(res) self.assert_(not guard.check(sm, wf_def, ob, arg1=1, arg2=2))
def setProperties( self, description, default_value="", default_expr="", for_catalog=0, for_status=0, update_always=0, props=None, REQUEST=None, ): """ """ self.description = str(description) self.default_value = str(default_value) if default_expr: self.default_expr = Expression(default_expr) else: self.default_expr = None g = Guard() if g.changeFromProperties(props or REQUEST): self.info_guard = g else: self.info_guard = None self.for_catalog = bool(for_catalog) self.for_status = bool(for_status) self.update_always = bool(update_always) if REQUEST is not None: return self.manage_properties(REQUEST, "Properties changed.")
def test_checkGuardExpr(self): # # Basic checks. # guard = Guard() # Create compulsory context elements sm = getSecurityManager() self.site._setObject("dummy", DummyContent("dummy")) ob = self.site.dummy wf_def = self._getDummyWorkflow() # Initialize the guard with an ok guard guard_props = {"guard_permissions": "", "guard_roles": "", "guard_expr": "python:1"} res = guard.changeFromProperties(guard_props) self.assert_(res) self.assert_(guard.check(sm, wf_def, ob)) # Initialize the guard with a not ok guard guard_props = {"guard_permissions": "", "guard_roles": "", "guard_expr": "python:0"} res = guard.changeFromProperties(guard_props) self.assert_(res) self.assert_(not guard.check(sm, wf_def, ob))
def test_checkGuardExpr(self): # # Basic checks. # guard = Guard() # Create compulsory context elements sm = getSecurityManager() ob = DummyContent('dummy') wf_def = self._getDummyWorkflow() # Initialize the guard with an ok guard guard_props = {'guard_permissions':'', 'guard_roles':'', 'guard_expr' :'python:1'} res = guard.changeFromProperties(guard_props) self.assert_(res) self.assert_(guard.check(sm, wf_def, ob)) # Initialize the guard with a not ok guard guard_props = {'guard_permissions':'', 'guard_roles':'', 'guard_expr' :'python:0'} res = guard.changeFromProperties(guard_props) self.assert_(res) self.assert_(not guard.check(sm, wf_def, ob))
def setProperties(self, description, default_value='', default_expr='', for_catalog=0, for_status=0, update_always=0, props=None, REQUEST=None): ''' ''' self.description = str(description) self.default_value = str(default_value) if default_expr: self.default_expr = Expression(default_expr) else: self.default_expr = None g = Guard() if g.changeFromProperties(props or REQUEST): self.info_guard = g else: self.info_guard = None self.for_catalog = bool(for_catalog) self.for_status = bool(for_status) self.update_always = bool(update_always) if REQUEST is not None: return self.manage_properties(REQUEST, 'Properties changed.')
def setProperties(self, title, new_state_id, trigger_type=TRIGGER_USER_ACTION, script_name='', after_script_name='', actbox_name='', actbox_url='', actbox_category='workflow', actbox_icon='', props=None, REQUEST=None, description=''): ''' ''' self.title = str(title) self.description = str(description) self.new_state_id = str(new_state_id) self.trigger_type = int(trigger_type) self.script_name = str(script_name) self.after_script_name = str(after_script_name) g = Guard() if g.changeFromProperties(props or REQUEST): self.guard = g else: self.guard = None self.actbox_name = str(actbox_name) self.actbox_url = str(actbox_url) self.actbox_icon = str(actbox_icon) self.actbox_category = str(actbox_category) if REQUEST is not None: return self.manage_properties(REQUEST, 'Properties changed.')
def setProperties(self, title, portal_type_filter=None, portal_type_group_filter=None, trigger_type=TRIGGER_WORKFLOW_METHOD, once_per_transaction=False, temporary_document_disallowed=False, script_name=(), after_script_name=(), before_commit_script_name=(), activate_script_name=(), actbox_name='', actbox_url='', actbox_category='workflow', method_id=(), props=None, REQUEST=None, description=''): """ Update transition properties XXX - then make sure that method_id is WorkflowMethod for portal_type_filter XXX - this will likely require dynamic """ if type(method_id) is type(''): self.method_id = method_id.split() else: self.method_id = method_id if portal_type_filter is not None and 'None' in portal_type_filter: portal_type_filter = None if portal_type_group_filter is not None and 'None' in portal_type_group_filter: portal_type_group_filter = None if 'None' in after_script_name: after_script_name = () if 'None' in activate_script_name: activate_script_name = () if 'None' in script_name: script_name = () if 'None' in before_commit_script_name: before_commit_script_name = () self.portal_type_filter = portal_type_filter self.portal_type_group_filter = portal_type_group_filter self.title = str(title) self.description = str(description) self.trigger_type = int(trigger_type) self.once_per_transaction = bool(once_per_transaction) self.temporary_document_disallowed = bool(temporary_document_disallowed) self.script_name = script_name self.after_script_name = after_script_name self.before_commit_script_name = before_commit_script_name self.activate_script_name = activate_script_name g = Guard() if g.changeFromProperties(props or REQUEST): self.guard = g else: self.guard = None self.actbox_name = str(actbox_name) self.actbox_url = str(actbox_url) self.actbox_category = str(actbox_category) # reset cached methods self.getPortalObject().portal_types.resetDynamicDocuments() if REQUEST is not None: return self.manage_properties(REQUEST, 'Properties changed.')
def manage_setGuard(self, props=None, REQUEST=None): ''' ''' g = Guard() if g.changeFromProperties(props or REQUEST): self.guard = g else: self.guard = None if REQUEST is not None: return self.manage_guardForm(REQUEST, 'Properties changed.')
def _initDCWorkflowCreationGuard(workflow, guard): """Initialize Instance creation conditions guard """ if guard is None: workflow.creation_guard = None else: props = {'guard_roles': ';'.join(guard['roles']), 'guard_permissions': ';'.join(guard['permissions']), 'guard_groups': ';'.join(guard['groups']), 'guard_expr': guard['expression']} g = Guard() g.changeFromProperties(props) workflow.creation_guard = g
def setProperties(self, title, manager_bypass=0, props=None, REQUEST=None, description=''): """Sets basic properties. """ self.title = str(title) self.description = str(description) self.manager_bypass = manager_bypass and 1 or 0 g = Guard() if g.changeFromProperties(props or REQUEST): self.creation_guard = g else: self.creation_guard = None if REQUEST is not None: return self.manage_properties( REQUEST, manage_tabs_message='Properties changed.')
def WorkflowUIMixin_setProperties( self, title , description='' # the only addition to WorkflowUIMixin.setProperties , manager_bypass=0, props=None, REQUEST=None): """Sets basic properties. """ self.title = str(title) self.description = str(description) self.manager_bypass = manager_bypass and 1 or 0 g = Guard() if g.changeFromProperties(props or REQUEST): self.creation_guard = g else: self.creation_guard = None if REQUEST is not None: return self.manage_properties( REQUEST, manage_tabs_message='Properties changed.')
def setUp(self): from Products.CMFCore.TypesTool import FactoryTypeInformation as FTI from Products.CMFCore.tests.base.dummy import DummySite from Products.CMFCore.tests.base.dummy import DummyUserFolder from Products.CMFCore.tests.base.tidata import FTIDATA_DUMMY SecurityTest.setUp(self) self.site = DummySite('site').__of__(self.root) self.acl_users = self.site._setObject('acl_users', DummyUserFolder()) self.ttool = self.site._setObject('portal_types', self._makeOne()) fti = FTIDATA_DUMMY[0].copy() self.ttool._setObject('Dummy Content', FTI(**fti)) # setup workflow tool # to test 'Instance creation conditions' of workflows from Products.CMFCore.WorkflowTool import WorkflowTool self.site._setObject('portal_workflow', WorkflowTool()) wftool = self.site.portal_workflow from Products.DCWorkflow.DCWorkflow import DCWorkflowDefinition wftool._setObject('wf', DCWorkflowDefinition('wf')) wftool.setDefaultChain('wf') wf = wftool.wf wf.states.addState('initial') wf.states.setInitialState('initial') from Products.DCWorkflow.Guard import Guard g = Guard() wf.creation_guard = g
def rename_retract_ar_transition(portal): """Renames retract_ar transition to invalidate """ logger.info("Renaming 'retract_ar' transition to 'invalidate'") wf_tool = api.get_tool("portal_workflow") workflow = wf_tool.getWorkflowById("bika_ar_workflow") if "invalidate" not in workflow.transitions: workflow.transitions.addTransition("invalidate") transition = workflow.transitions.invalidate transition.setProperties( title="Invalidate", new_state_id="invalid", after_script_name="", actbox_name="Invalidate", ) guard = transition.guard or Guard() guard_props = { "guard_permissions": "BIKA: Retract", "guard_roles": "", "guard_expr": "python:here.guard_cancelled_object()" } guard.changeFromProperties(guard_props) transition.guard = guard for state in workflow.states.values(): if 'retract_ar' in state.transitions: trans = filter(lambda id: id != 'retract_ar', state.transitions) trans += ('invalidate', ) state.transitions = trans if "retract_ar" in workflow.transitions: workflow.transitions.deleteTransitions(["retract_ar"])
def update_rejection_permissions_for(portal, workflow_id, permission_id): logger.info("Updating rejection permissions for {}".format(workflow_id)) all_roles = ["Manager", "LabManager", "LabClerk", "Client", "Owner"] roles_mapping = { "sample_registered": all_roles, "scheduled_sampling": all_roles, "to_be_sampled": all_roles, "sampled": all_roles, "to_be_preserved": all_roles, "sample_due": all_roles, "sample_received": ["Manager", "LabManager", "LabClerk"], "attachment_due": ["Manager", "LabManager"], "to_be_verified": ["Manager", "LabManager"], # Those that only apply to sample_workflow below "expired": ["Manager", "LabManager"], "disposed": ["Manager", "LabManager"], } wf_tool = api.get_tool("portal_workflow") workflow = wf_tool.getWorkflowById(workflow_id) if "rejected" not in workflow.states: logger.warning( "rejected state not found for workflow {} [SKIP]".format( workflow.id)) return False if permission_id in workflow.permissions: logger.info("'{}' already in place. [SKIP]".format(permission_id)) return False workflow.permissions += (permission_id, ) for state_id, state in workflow.states.items(): if "reject" not in state.transitions: continue if state_id not in roles_mapping: continue roles = roles_mapping[state_id] state.setPermission(permission_id, False, roles) if "reject" not in workflow.transitions: workflow.transitions.addTransition("reject") transition = workflow.transitions.reject transition.setProperties( title="Reject", new_state_id="rejected", actbox_name="Reject", ) transition = workflow.transitions.reject guard = transition.guard or Guard() guard_props = { "guard_permissions": permission_id, "guard_roles": "", "guard_expr": "python:here.bika_setup.isRejectionWorkflowEnabled()" } guard.changeFromProperties(guard_props) transition.guard = guard return True
def clone_transition(transition, clone): transition.description = clone.description transition.new_state_id = clone.new_state_id transition.trigger_type = clone.trigger_type if clone.guard: guard = Guard() guard.permissions = clone.guard.permissions[:] guard.roles = clone.guard.roles[:] guard.groups = clone.guard.groups[:] transition.guard = guard transition.actbox_name = transition.title transition.actbox_url = clone.actbox_url.replace(clone.id, transition.id) transition.actbox_category = clone.actbox_category transition.var_exprs = clone.var_exprs transition.script_name = clone.script_name transition.after_script_name = clone.after_script_name
def getGuard(self): """Returns the initiation guard. If no init guard has been created, returns a temporary object. """ if self.creation_guard is not None: return self.creation_guard else: return Guard().__of__(self) # Create a temporary guard.
def setProperties(self, description, actbox_name='', actbox_url='', actbox_category='global', actbox_icon='', props=None, REQUEST=None): ''' ''' if props is None: props = REQUEST self.description = str(description) for key in self.getAvailableCatalogVars(): # Populate var_matches. fieldname = 'var_match_%s' % key v = props.get(fieldname, '') if v: if not self.var_matches: self.var_matches = PersistentMapping() if tales_re.match(v).group(1): # Found a TALES prefix self.var_matches[key] = Expression(v) else: # Falling back to formatted string v = [var.strip() for var in v.split(';')] self.var_matches[key] = tuple(v) else: if self.var_matches and key in self.var_matches: del self.var_matches[key] self.actbox_name = str(actbox_name) self.actbox_url = str(actbox_url) self.actbox_category = str(actbox_category) self.actbox_icon = str(actbox_icon) g = Guard() if g.changeFromProperties(props or REQUEST): self.guard = g else: self.guard = None if REQUEST is not None: return self.manage_properties(REQUEST, 'Properties changed.')
def WorkflowUIMixin_setProperties( self, title, description="", # the only addition to WorkflowUIMixin.setProperties manager_bypass=0, props=None, REQUEST=None, ): """Sets basic properties. """ self.title = str(title) self.description = str(description) self.manager_bypass = manager_bypass and 1 or 0 g = Guard() if g.changeFromProperties(props or REQUEST): self.creation_guard = g else: self.creation_guard = None if REQUEST is not None: return self.manage_properties(REQUEST, manage_tabs_message="Properties changed.")
def manage_setGuard(self, props=None, REQUEST=None): ''' ''' g = Guard() if g.changeFromProperties(props or REQUEST): guard = self.guard if guard is None: self.guard = g else: guard._p_activate() if guard.__dict__ != g.__dict__: guard.__dict__.clear() guard.__dict__.update(g.__dict__) guard._p_changed = 1 else: try: del self.guard except AttributeError: pass if REQUEST is not None: return self.manage_guardForm(REQUEST, 'Properties changed.')
def test_checkWithKwargs(self): # # Checks with kwargs # guard = Guard() # Create compulsory context elements sm = getSecurityManager() self.site._setObject('dummy', DummyContent('dummy')) ob = self.site.dummy wf_def = self._getDummyWorkflow() # Initialize the guard with an ok guard guard_props = { 'guard_permissions': '', 'guard_roles': '', 'guard_expr': 'python:1' } res = guard.changeFromProperties(guard_props) self.assert_(res) self.assert_(guard.check(sm, wf_def, ob, arg1=1, arg2=2)) # Initialize the guard with a not ok guard guard_props = { 'guard_permissions': '', 'guard_roles': '', 'guard_expr': 'python:0' } res = guard.changeFromProperties(guard_props) self.assert_(res) self.assert_(not guard.check(sm, wf_def, ob, arg1=1, arg2=2))
def test_checkGuardExpr(self): # # Basic checks. # guard = Guard() # Create compulsory context elements sm = getSecurityManager() ob = DummyContent('dummy') wf_def = self._getDummyWorkflow() # Initialize the guard with an ok guard guard_props = { 'guard_permissions': '', 'guard_roles': '', 'guard_expr': 'python:1' } res = guard.changeFromProperties(guard_props) self.assert_(res) self.assert_(guard.check(sm, wf_def, ob)) # Initialize the guard with a not ok guard guard_props = { 'guard_permissions': '', 'guard_roles': '', 'guard_expr': 'python:0' } res = guard.changeFromProperties(guard_props) self.assert_(res) self.assert_(not guard.check(sm, wf_def, ob))
def setProperties(self, description, actbox_name='', actbox_url='', actbox_category='global', actbox_icon='', props=None, REQUEST=None): ''' ''' if props is None: props = REQUEST self.description = str(description) for key in self.getAvailableCatalogVars(): # Populate var_matches. fieldname = 'var_match_%s' % key v = props.get(fieldname, '') if v: if not self.var_matches: self.var_matches = PersistentMapping() if tales_re.match(v).group(1): # Found a TALES prefix self.var_matches[key] = Expression(v) else: # Falling back to formatted string v = [ var.strip() for var in v.split(';') ] self.var_matches[key] = tuple(v) else: if self.var_matches and self.var_matches.has_key(key): del self.var_matches[key] self.actbox_name = str(actbox_name) self.actbox_url = str(actbox_url) self.actbox_category = str(actbox_category) self.actbox_icon = str(actbox_icon) g = Guard() if g.changeFromProperties(props or REQUEST): self.guard = g else: self.guard = None if REQUEST is not None: return self.manage_properties(REQUEST, 'Properties changed.')
def add_create_partition_transition(portal): logger.info("Adding partitioning workflow") wf_tool = api.get_tool("portal_workflow") workflow = wf_tool.getWorkflowById("bika_ar_workflow") # Transition: create_partitions update_role_mappings = False transition_id = "create_partitions" if transition_id not in workflow.transitions: update_role_mappings = True workflow.transitions.addTransition(transition_id) transition = workflow.transitions.create_partitions transition.setProperties( title="Create partitions", new_state_id='sample_received', after_script_name='', actbox_name="Create partitions", ) guard = transition.guard or Guard() guard_props = { 'guard_permissions': 'BIKA: Edit Results', 'guard_roles': '', 'guard_expr': 'python:here.guard_handler("create_partitions")' } guard.changeFromProperties(guard_props) transition.guard = guard # Add the transition to "sample_received" state state = workflow.states.sample_received if transition_id not in state.transitions: update_role_mappings = True state.transitions += (transition_id, ) if not update_role_mappings: return # Update role mappings if necessary. Analysis Requests transitioned beyond # sample_received state are omitted. states = [ "sampled", "scheduled_sampling", "to_be_preserved", "to_be_sampled", "sample_due", "sample_received" ] query = dict(portal_type="AnalysisRequest", states=states) ars = api.search(query, CATALOG_ANALYSIS_REQUEST_LISTING) total = len(ars) for num, ar in enumerate(ars, start=1): workflow.updateRoleMappingsFor(api.get_object(ar)) if num % 100 == 0: logger.info("Updating role mappings: {0}/{1}".format(num, total))
def update_workflow_transition(workflow, transition_id, settings): logger.info("Updating workflow '{}', transition: '{}'" .format(workflow.id, transition_id)) if transition_id not in workflow.transitions: workflow.transitions.addTransition(transition_id) transition = workflow.transitions.get(transition_id) transition.setProperties( title=settings.get("title"), new_state_id=settings.get("new_state"), after_script_name=settings.get("after_script", ""), actbox_name=settings.get("action", settings.get("title")) ) guard = transition.guard or Guard() guard_props = {"guard_permissions": "", "guard_roles": "", "guard_expr": ""} guard_props = settings.get("guard", guard_props) guard.changeFromProperties(guard_props) transition.guard = guard
def test_BaseGuardAPI(self): # # Test guard basic API # guard = Guard() self.assertNotEqual(guard, None) # Test default values self.assertEqual(guard.getPermissionsText(), '') self.assertEqual(guard.getRolesText(), '') self.assertEqual(guard.getExprText(), '') # Initialize the guard with empty values # not initialization guard_props = {'guard_permissions':'', 'guard_roles':'', 'guard_expr' :''} res = guard.changeFromProperties(guard_props) self.assert_(res==0) # Test default values self.assertEqual(guard.getPermissionsText(), '') self.assertEqual(guard.getRolesText(), '') self.assertEqual(guard.getExprText(), '') # Change guard guard_props = {'guard_roles':'Manager', 'guard_permissions':'', 'guard_expr' :''} res = guard.changeFromProperties(guard_props) self.assert_(res==1) self.assertEqual(guard.getRolesText(), 'Manager') self.assertEqual(guard.getPermissionsText(), '') self.assertEqual(guard.getExprText(), '') # Change guard guard_props = {'guard_roles':'Manager;', 'guard_permissions':'', 'guard_expr' :''} res = guard.changeFromProperties(guard_props) self.assert_(res==1) # With one space after the ';' self.assertEqual(guard.getRolesText(), 'Manager; ') self.assertEqual(guard.getPermissionsText(), '') self.assertEqual(guard.getExprText(), '') # Change guard guard_props = {'guard_roles':'Manager;Member', 'guard_permissions':'', 'guard_expr' :''} res = guard.changeFromProperties(guard_props) self.assert_(res==1) # With one space after the ';' self.assertEqual(guard.getRolesText(), 'Manager; Member') self.assertEqual(guard.getPermissionsText(), '') self.assertEqual(guard.getExprText(), '') # Change guard guard_props = {'guard_roles':'Manager;Member', 'guard_permissions':'', 'guard_expr' :''} res = guard.changeFromProperties(guard_props) self.assert_(res==1) # With one space after the ';' self.assertEqual(guard.getRolesText(), 'Manager; Member') self.assertEqual(guard.getPermissionsText(), '') self.assertEqual(guard.getExprText(), '') # Change guard guard_props = {'guard_roles':'Manager', 'guard_permissions':'', 'guard_expr' :''} res = guard.changeFromProperties(guard_props) self.assert_(res==1) self.assertEqual(guard.getRolesText(), 'Manager') self.assertEqual(guard.getPermissionsText(), '') self.assertEqual(guard.getExprText(), '') # Change guard guard_props = {'guard_roles':'Manager', 'guard_permissions':'ManagePortal;', 'guard_expr' :''} res = guard.changeFromProperties(guard_props) self.assert_(res==1) self.assertEqual(guard.getRolesText(), 'Manager') self.assertEqual(guard.getPermissionsText(), 'ManagePortal; ') self.assertEqual(guard.getExprText(), '') # Change guard guard_props = {'guard_roles':'Manager', 'guard_permissions':'ManagePortal', 'guard_expr' :''} res = guard.changeFromProperties(guard_props) self.assert_(res==1) self.assertEqual(guard.getRolesText(), 'Manager') self.assertEqual(guard.getPermissionsText(), 'ManagePortal') self.assertEqual(guard.getExprText(), '') # Change guard guard_props = {'guard_roles':'Manager', 'guard_permissions':'ManagePortal', 'guard_expr' :'python:1'} res = guard.changeFromProperties(guard_props) self.assert_(res==1) self.assertEqual(guard.getRolesText(), 'Manager') self.assertEqual(guard.getPermissionsText(), 'ManagePortal') self.assertEqual(guard.getExprText(), 'python:1') # Change guard guard_props = {'guard_roles':'Manager', 'guard_permissions':'ManagePortal', 'guard_expr' :'string:'} res = guard.changeFromProperties(guard_props) self.assert_(res==1) self.assertEqual(guard.getRolesText(), 'Manager') self.assertEqual(guard.getPermissionsText(), 'ManagePortal') self.assertEqual(guard.getExprText(), 'string:') # Change guard with wrong TALES guard_props = {'guard_roles':'Manager', 'guard_permissions':'ManagePortal', 'guard_expr' :'python:'} self.assertRaises(CompilerError, guard.changeFromProperties, guard_props) self.assertEqual(guard.getRolesText(), 'Manager') self.assertEqual(guard.getPermissionsText(), 'ManagePortal') self.assertEqual(guard.getExprText(), 'string:') # reinit the guard guard_props = {'guard_permissions':'', 'guard_roles':'', 'guard_expr' :''} res = guard.changeFromProperties(guard_props) self.assert_(res==0) # No API on DCWorkflow guard to reset properly.... guard.permissions = '' guard.roles = '' guard.expr = None # Test default values self.assertEqual(guard.getPermissionsText(), '') self.assertEqual(guard.getRolesText(), '') self.assertEqual(guard.getExprText(), '')
def getGuard(self): guard = getattr(self, 'guard', None) if guard is not None: return guard else: return Guard().__of__(self) # Create a temporary guard.
def test_BaseGuardAPI(self): from zope.tales.tales import CompilerError # # Test guard basic API # guard = Guard() self.assertNotEqual(guard, None) # Test default values self.assertEqual(guard.getPermissionsText(), '') self.assertEqual(guard.getRolesText(), '') self.assertEqual(guard.getExprText(), '') # Initialize the guard with empty values # not initialization guard_props = { 'guard_permissions': '', 'guard_roles': '', 'guard_expr': '' } res = guard.changeFromProperties(guard_props) self.assert_(res == 0) # Test default values self.assertEqual(guard.getPermissionsText(), '') self.assertEqual(guard.getRolesText(), '') self.assertEqual(guard.getExprText(), '') # Change guard guard_props = { 'guard_roles': 'Manager', 'guard_permissions': '', 'guard_expr': '' } res = guard.changeFromProperties(guard_props) self.assert_(res == 1) self.assertEqual(guard.getRolesText(), 'Manager') self.assertEqual(guard.getPermissionsText(), '') self.assertEqual(guard.getExprText(), '') # Change guard guard_props = { 'guard_roles': 'Manager;', 'guard_permissions': '', 'guard_expr': '' } res = guard.changeFromProperties(guard_props) self.assert_(res == 1) # With one space after the ';' self.assertEqual(guard.getRolesText(), 'Manager; ') self.assertEqual(guard.getPermissionsText(), '') self.assertEqual(guard.getExprText(), '') # Change guard guard_props = { 'guard_roles': 'Manager;Member', 'guard_permissions': '', 'guard_expr': '' } res = guard.changeFromProperties(guard_props) self.assert_(res == 1) # With one space after the ';' self.assertEqual(guard.getRolesText(), 'Manager; Member') self.assertEqual(guard.getPermissionsText(), '') self.assertEqual(guard.getExprText(), '') # Change guard guard_props = { 'guard_roles': 'Manager;Member', 'guard_permissions': '', 'guard_expr': '' } res = guard.changeFromProperties(guard_props) self.assert_(res == 1) # With one space after the ';' self.assertEqual(guard.getRolesText(), 'Manager; Member') self.assertEqual(guard.getPermissionsText(), '') self.assertEqual(guard.getExprText(), '') # Change guard guard_props = { 'guard_roles': 'Manager', 'guard_permissions': '', 'guard_expr': '' } res = guard.changeFromProperties(guard_props) self.assert_(res == 1) self.assertEqual(guard.getRolesText(), 'Manager') self.assertEqual(guard.getPermissionsText(), '') self.assertEqual(guard.getExprText(), '') # Change guard guard_props = { 'guard_roles': 'Manager', 'guard_permissions': 'ManagePortal;', 'guard_expr': '' } res = guard.changeFromProperties(guard_props) self.assert_(res == 1) self.assertEqual(guard.getRolesText(), 'Manager') self.assertEqual(guard.getPermissionsText(), 'ManagePortal; ') self.assertEqual(guard.getExprText(), '') # Change guard guard_props = { 'guard_roles': 'Manager', 'guard_permissions': 'ManagePortal', 'guard_expr': '' } res = guard.changeFromProperties(guard_props) self.assert_(res == 1) self.assertEqual(guard.getRolesText(), 'Manager') self.assertEqual(guard.getPermissionsText(), 'ManagePortal') self.assertEqual(guard.getExprText(), '') # Change guard guard_props = { 'guard_roles': 'Manager', 'guard_permissions': 'ManagePortal', 'guard_expr': 'python:1' } res = guard.changeFromProperties(guard_props) self.assert_(res == 1) self.assertEqual(guard.getRolesText(), 'Manager') self.assertEqual(guard.getPermissionsText(), 'ManagePortal') self.assertEqual(guard.getExprText(), 'python:1') # Change guard guard_props = { 'guard_roles': 'Manager', 'guard_permissions': 'ManagePortal', 'guard_expr': 'string:' } res = guard.changeFromProperties(guard_props) self.assert_(res == 1) self.assertEqual(guard.getRolesText(), 'Manager') self.assertEqual(guard.getPermissionsText(), 'ManagePortal') self.assertEqual(guard.getExprText(), 'string:') # Change guard with wrong TALES guard_props = { 'guard_roles': 'Manager', 'guard_permissions': 'ManagePortal', 'guard_expr': 'python:' } self.assertRaises(CompilerError, guard.changeFromProperties, guard_props) self.assertEqual(guard.getRolesText(), 'Manager') self.assertEqual(guard.getPermissionsText(), 'ManagePortal') self.assertEqual(guard.getExprText(), 'string:') # reinit the guard guard_props = { 'guard_permissions': '', 'guard_roles': '', 'guard_expr': '' } res = guard.changeFromProperties(guard_props) self.assert_(res == 0) # No API on DCWorkflow guard to reset properly.... guard.permissions = '' guard.roles = '' guard.expr = None # Test default values self.assertEqual(guard.getPermissionsText(), '') self.assertEqual(guard.getRolesText(), '') self.assertEqual(guard.getExprText(), '')
def getGuard(self): guard = self.guard if guard is None: return Guard().__of__(self) # Create a temporary guard. return guard
def setProperties(self, title, portal_type_filter=None, portal_type_group_filter=None, trigger_type=TRIGGER_WORKFLOW_METHOD, once_per_transaction=False, temporary_document_disallowed=False, script_name=(), after_script_name=(), before_commit_script_name=(), activate_script_name=(), actbox_name='', actbox_url='', actbox_category='workflow', method_id=(), props=None, REQUEST=None, description=''): """ Update transition properties XXX - then make sure that method_id is WorkflowMethod for portal_type_filter XXX - this will likely require dynamic """ if type(method_id) is type(''): self.method_id = method_id.split() else: self.method_id = method_id if portal_type_filter is not None and 'None' in portal_type_filter: portal_type_filter = None if portal_type_group_filter is not None and 'None' in portal_type_group_filter: portal_type_group_filter = None if 'None' in after_script_name: after_script_name = () if 'None' in activate_script_name: activate_script_name = () if 'None' in script_name: script_name = () if 'None' in before_commit_script_name: before_commit_script_name = () self.portal_type_filter = portal_type_filter self.portal_type_group_filter = portal_type_group_filter self.title = str(title) self.description = str(description) self.trigger_type = int(trigger_type) self.once_per_transaction = bool(once_per_transaction) self.temporary_document_disallowed = bool( temporary_document_disallowed) self.script_name = script_name self.after_script_name = after_script_name self.before_commit_script_name = before_commit_script_name self.activate_script_name = activate_script_name g = Guard() if g.changeFromProperties(props or REQUEST): self.guard = g else: self.guard = None self.actbox_name = str(actbox_name) self.actbox_url = str(actbox_url) self.actbox_category = str(actbox_category) # reset cached methods self.getPortalObject().portal_types.resetDynamicDocuments() if REQUEST is not None: return self.manage_properties(REQUEST, 'Properties changed.')
def test_BaseGuardAPI(self): # # Test guard basic API # guard = Guard() self.assertNotEqual(guard, None) # Test default values self.assertEqual(guard.getPermissionsText(), "") self.assertEqual(guard.getRolesText(), "") self.assertEqual(guard.getExprText(), "") # Initialize the guard with empty values # not initialization guard_props = {"guard_permissions": "", "guard_roles": "", "guard_expr": ""} res = guard.changeFromProperties(guard_props) self.assert_(res == 0) # Test default values self.assertEqual(guard.getPermissionsText(), "") self.assertEqual(guard.getRolesText(), "") self.assertEqual(guard.getExprText(), "") # Change guard guard_props = {"guard_roles": "Manager", "guard_permissions": "", "guard_expr": ""} res = guard.changeFromProperties(guard_props) self.assert_(res == 1) self.assertEqual(guard.getRolesText(), "Manager") self.assertEqual(guard.getPermissionsText(), "") self.assertEqual(guard.getExprText(), "") # Change guard guard_props = {"guard_roles": "Manager;", "guard_permissions": "", "guard_expr": ""} res = guard.changeFromProperties(guard_props) self.assert_(res == 1) # With one space after the ';' self.assertEqual(guard.getRolesText(), "Manager; ") self.assertEqual(guard.getPermissionsText(), "") self.assertEqual(guard.getExprText(), "") # Change guard guard_props = {"guard_roles": "Manager;Member", "guard_permissions": "", "guard_expr": ""} res = guard.changeFromProperties(guard_props) self.assert_(res == 1) # With one space after the ';' self.assertEqual(guard.getRolesText(), "Manager; Member") self.assertEqual(guard.getPermissionsText(), "") self.assertEqual(guard.getExprText(), "") # Change guard guard_props = {"guard_roles": "Manager;Member", "guard_permissions": "", "guard_expr": ""} res = guard.changeFromProperties(guard_props) self.assert_(res == 1) # With one space after the ';' self.assertEqual(guard.getRolesText(), "Manager; Member") self.assertEqual(guard.getPermissionsText(), "") self.assertEqual(guard.getExprText(), "") # Change guard guard_props = {"guard_roles": "Manager", "guard_permissions": "", "guard_expr": ""} res = guard.changeFromProperties(guard_props) self.assert_(res == 1) self.assertEqual(guard.getRolesText(), "Manager") self.assertEqual(guard.getPermissionsText(), "") self.assertEqual(guard.getExprText(), "") # Change guard guard_props = {"guard_roles": "Manager", "guard_permissions": "ManagePortal;", "guard_expr": ""} res = guard.changeFromProperties(guard_props) self.assert_(res == 1) self.assertEqual(guard.getRolesText(), "Manager") self.assertEqual(guard.getPermissionsText(), "ManagePortal; ") self.assertEqual(guard.getExprText(), "") # Change guard guard_props = {"guard_roles": "Manager", "guard_permissions": "ManagePortal", "guard_expr": ""} res = guard.changeFromProperties(guard_props) self.assert_(res == 1) self.assertEqual(guard.getRolesText(), "Manager") self.assertEqual(guard.getPermissionsText(), "ManagePortal") self.assertEqual(guard.getExprText(), "") # Change guard guard_props = {"guard_roles": "Manager", "guard_permissions": "ManagePortal", "guard_expr": "python:1"} res = guard.changeFromProperties(guard_props) self.assert_(res == 1) self.assertEqual(guard.getRolesText(), "Manager") self.assertEqual(guard.getPermissionsText(), "ManagePortal") self.assertEqual(guard.getExprText(), "python:1") # Change guard guard_props = {"guard_roles": "Manager", "guard_permissions": "ManagePortal", "guard_expr": "string:"} res = guard.changeFromProperties(guard_props) self.assert_(res == 1) self.assertEqual(guard.getRolesText(), "Manager") self.assertEqual(guard.getPermissionsText(), "ManagePortal") self.assertEqual(guard.getExprText(), "string:") # Change guard with wrong TALES guard_props = {"guard_roles": "Manager", "guard_permissions": "ManagePortal", "guard_expr": "python:"} self.assertRaises(CompilerError, guard.changeFromProperties, guard_props) self.assertEqual(guard.getRolesText(), "Manager") self.assertEqual(guard.getPermissionsText(), "ManagePortal") self.assertEqual(guard.getExprText(), "string:") # reinit the guard guard_props = {"guard_permissions": "", "guard_roles": "", "guard_expr": ""} res = guard.changeFromProperties(guard_props) self.assert_(res == 0) # No API on DCWorkflow guard to reset properly.... guard.permissions = "" guard.roles = "" guard.expr = None # Test default values self.assertEqual(guard.getPermissionsText(), "") self.assertEqual(guard.getRolesText(), "") self.assertEqual(guard.getExprText(), "")
def getGuard(self): if self.guard is not None: return self.guard else: return Guard().__of__(self) # Create a temporary guard.
def setup_bhp_workflow_for(portal, workflow_id): wtool = api.get_tool("portal_workflow") workflow = wtool.getWorkflowById(workflow_id) # STATUSES CREATION # Ordered: Clinic submits the form --[no_sampling_wf]--> sample_ordered sample_ordered = workflow.states.get('sample_ordered') if not sample_ordered: workflow.states.addState('sample_ordered') sample_ordered = workflow.states.sample_ordered sample_ordered.title = "Ordered" roles = ('Manager', 'LabManager', 'LabClerk', 'Owner') sample_ordered.setPermission(AccessContentsInformation, False, roles) sample_ordered.setPermission(ModifyPortalContent, False, roles) sample_ordered.setPermission(View, False, roles) sample_ordered.setPermission(CancelAndReinstate, False, roles) sample_ordered.setPermission(EditFieldResults, False, ()) sample_ordered.setPermission(EditResults, False, ()) sample_ordered.setPermission(EditSample, False, roles) sample_ordered.setPermission(PreserveSample, False, ()) sample_ordered.setPermission(ReceiveSample, False, ()) sample_ordered.setPermission(ScheduleSampling, False, ()) sample_ordered.transitions = ('send_to_lab', ) workflow.transitions.no_sampling_workflow.new_state_id = 'sample_ordered' # Shipped: Clinic sent the sample --[send_to_lab]--> sample_shipped sample_shipped = workflow.states.get('sample_shipped') if not sample_shipped: workflow.states.addState('sample_shipped') sample_shipped = workflow.states.sample_shipped sample_shipped.title = "Shipped" roles = ('Manager', 'LabManager', 'LabClerk', 'Owner') sample_shipped.setPermission(AccessContentsInformation, False, roles) sample_shipped.setPermission(ModifyPortalContent, False, roles) sample_shipped.setPermission(View, False, roles) sample_shipped.setPermission(CancelAndReinstate, False, roles) sample_shipped.setPermission(EditFieldResults, False, ()) sample_shipped.setPermission(EditResults, False, ()) sample_shipped.setPermission(EditSample, False, roles) sample_shipped.setPermission(PreserveSample, False, ()) sample_shipped.setPermission(ReceiveSample, False, ()) sample_shipped.setPermission(ScheduleSampling, False, ()) sample_shipped.transitions = ('deliver', ) # At reception: Sample is delivered --[deliver]--> sample_at_reception at_reception = workflow.states.get('sample_at_reception') if not at_reception: workflow.states.addState('sample_at_reception') at_reception = workflow.states.sample_at_reception at_reception.title = "At reception" roles = ('Manager', 'LabManager', 'LabClerk', 'Owner') at_reception.setPermission(AccessContentsInformation, False, roles) at_reception.setPermission(ModifyPortalContent, False, roles) at_reception.setPermission(View, False, roles) at_reception.setPermission(CancelAndReinstate, False, roles) at_reception.setPermission(EditFieldResults, False, ()) at_reception.setPermission(EditResults, False, ()) at_reception.setPermission(EditSample, False, roles) at_reception.setPermission(PreserveSample, False, ()) at_reception.setPermission(ReceiveSample, False, ()) at_reception.setPermission(ScheduleSampling, False, ()) at_reception.transitions = ('send_to_pot', 'process', 'reject') # Process: Create partitions --[process]--> sample_at_reception # No new state is necessary here # Sent to PoT: Sample is sent to PoT --[send_to_pot]--> sample_due workflow.states.sample_due.title = "Sent to point of testing" # At Point of PoT: Sample is received at PoT --[receive]--> sample_received workflow.states.sample_received.title = "At point of testing" # TRANSITIONS CREATION # Send to lab: ordered --> sample_shipped if not workflow.transitions.get('send_to_lab'): workflow.transitions.addTransition('send_to_lab') send_transition = workflow.transitions.send_to_lab send_transition.setProperties( title='Send to Lab', new_state_id='sample_shipped', after_script_name='', actbox_name="Send to Lab", ) guard_send = send_transition.guard or Guard() guard_props = { 'guard_permissions': 'BIKA: Add Sample', 'guard_roles': '', 'guard_expr': 'python:here.guard_send_to_lab()' } guard_send.changeFromProperties(guard_props) send_transition.guard = guard_send # Deliver: sample_shipped --> sample_at_reception if not workflow.transitions.get('deliver'): workflow.transitions.addTransition('deliver') deliver_transition = workflow.transitions.deliver deliver_transition.setProperties( title="Receive at reception", new_state_id='sample_at_reception', after_script_name='', actbox_name="Receive at reception", ) guard_deliver = deliver_transition.guard or Guard() guard_props = { 'guard_permissions': 'BIKA: Add Sample', 'guard_roles': '', 'guard_expr': 'python:here.guard_deliver()' } guard_deliver.changeFromProperties(guard_props) deliver_transition.guard = guard_deliver # Process: sample_at_reception --> sample_at_reception if not workflow.transitions.get('process'): workflow.transitions.addTransition('process') process_transition = workflow.transitions.process process_transition.setProperties( title="Process", new_state_id='sample_at_reception', after_script_name='', actbox_name="Process", ) guard_process = process_transition.guard or Guard() guard_props = { 'guard_permissions': 'BIKA: Add Sample', 'guard_roles': '', 'guard_expr': 'python:here.guard_process()' } guard_process.changeFromProperties(guard_props) process_transition.guard = guard_process # Send to Pot: sample_at_reception --> sample_due if not workflow.transitions.get('send_to_pot'): workflow.transitions.addTransition('send_to_pot') send2pot_transition = workflow.transitions.send_to_pot send2pot_transition.setProperties( title="Send to point of testing", new_state_id='sample_due', after_script_name='', actbox_name="Send to point of testing", ) guard_send2pot = send2pot_transition.guard or Guard() guard_props = { 'guard_permissions': 'BIKA: Add Sample', 'guard_roles': '', 'guard_expr': 'python:here.guard_send_to_pot()' } guard_send2pot.changeFromProperties(guard_props) send2pot_transition.guard = guard_send2pot # Receive at PoT: sample_due--> sample_received workflow.transitions.receive.title = "Receive at point of testing" workflow.transitions.receive.actbox_name = "Receive at point of testing" # Override guards submit_transition = workflow.transitions.get('submit', None) if submit_transition: guard_submit = submit_transition.guard or Guard() guard_props = { 'guard_permissions': '', 'guard_roles': '', 'guard_expr': 'python:here.guard_submit()' } guard_submit.changeFromProperties(guard_props) submit_transition.guard = guard_submit