Changeset 32

Show
Ignore:
Timestamp:
08/22/06 16:13:51 (2 years ago)
Author:
johnbywater
Message:

Inlined access control commands.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/dm/accesscontrol.py

    r26 r32  
    1 from dm.ioc import * 
     1from dm.ioc import RequiredFeature 
    22from dm.exceptions import * 
    3 from dm.command.accesscontrol import * 
    43 
    54class AbstractAccessController(object): 
     
    5453                ) 
    5554            return False 
     55        if not self.actionName in self.registry.actions: 
     56            if self.debug: 
     57                self.logger.debug( 
     58                    "Action name '%s' not registered." % self.actionName 
     59                ) 
     60            return False 
     61        self.action = self.registry.actions[self.actionName] 
     62        if not self.action: 
     63            if self.debug: 
     64                self.logger.debug( 
     65                    "No action for access controller." 
     66                ) 
     67            return False 
    5668        if not self.protectedObject: 
    5769            if self.debug: 
     
    6779             
    6880    def isRoleAuthorised(self, role): 
    69         cmd = AuthoriseAccess(role, self.actionName, self.protectedObject) 
    70         try: 
    71             cmd.execute() 
    72             if self.debug: 
    73                 msg = "Access by role authorised: '%s' to '%s' with '%s'." % ( 
    74                     role.name, self.actionName, self.protectedObject 
    75                 ) 
    76                 self.logger.debug(msg) 
    77             return True 
    78         except KforgeCommandError, inst: 
    79             if self.debug: 
    80                 self.logger.debug(str(inst)) 
    81             return False 
    82  
     81        for grant in role.grants: 
     82            permission = grant.permission 
     83            if permission.action == self.action: 
     84                protectionObject = permission.protectionObject 
     85                if protectionObject.isProtector(self.protectedObject): 
     86                    if self.debug: 
     87                        msg = "Access by role authorised: '%s' to '%s' with '%s'." % (role.name, self.actionName, self.protectedObject) 
     88                        self.logger.debug(msg) 
     89                    return True 
     90        return False 
     91         
    8392 
    8493class SystemAccessController(AbstractAccessController): 
     
    8695 
    8796    def hasAuthorisedRole(self): 
    88         if self.isPersonBarred(self.person): 
     97        if self.isPersonBarred(): 
    8998            return False 
    90         if self.isPersonAuthorised(self.person): 
     99        if self.isPersonAuthorised(): 
    91100            return True 
    92101        if self.isSystemRoleAuthorised(): 
    93102            return True 
    94         # todo: also use administration project? 
    95103        return False 
    96104 
    97     def isPersonBarred(self, person): 
    98         cmd = IsPersonBarred( 
    99             person.name, self.actionName, self.protectedObject 
    100         ) 
    101         try: 
    102             cmd.execute() 
    103             if self.debug: 
    104                 message = "Access by person barred: '%s' to '%s' with '%s'." % ( 
    105                     person.name, self.actionName, self.protectedObject 
    106                 ) 
    107                 self.logger.debug(message) 
    108             return True 
    109         except KforgeCommandError, inst: 
    110             if self.debug: 
    111                 self.logger.debug(str(inst)) 
    112             return False 
    113  
    114     def isPersonAuthorised(self, person): 
    115         cmd = AuthorisePersonalAccess( 
    116             person.name, self.actionName, self.protectedObject 
    117         ) 
    118         try: 
    119             cmd.execute() 
    120             if self.debug: 
    121                 msg = "Access by person authorised: '%s' to '%s' with '%s'." % ( 
    122                     person.name, self.actionName, self.protectedObject 
    123                 ) 
    124                 self.logger.debug(msg) 
    125             return True 
    126         except KforgeCommandError, inst: 
    127             if self.debug: 
    128                 self.logger.debug(str(inst)) 
    129             return False 
     105    def isPersonBarred(self): 
     106        for bar in self.person.bars: 
     107            permission = bar.permission 
     108            if permission.action == self.action: 
     109                protectionObject = permission.protectionObject 
     110                if protectionObject.isProtector(self.protectedObject): 
     111                    if self.debug: 
     112                        msg = "Access by person barred: '%s' to '%s' with '%s'." % (self.person.name, self.actionName, self.protectedObject) 
     113                        self.logger.debug(msg) 
     114                    return True 
     115        return False 
     116         
     117    def isPersonAuthorised(self): 
     118        for grant in self.person.grants: 
     119            permission = grant.permission 
     120            if permission.action == self.action: 
     121                protectionObject = permission.protectionObject 
     122                if protectionObject.isProtector(self.protectedObject): 
     123                    if self.debug: 
     124                        msg = "Access by person authorised: '%s' to '%s' with '%s'." % (self.person.name, self.actionName, self.protectedObject) 
     125                        self.logger.debug(msg) 
     126                    return True 
     127        return False 
    130128 
    131129    def isSystemRoleAuthorised(self): 
  • trunk/src/dm/accesscontroltest.py

    r31 r32  
    6464        self.person.role = oldRole 
    6565 
    66     def test_personBarred(self): 
     66    def test_isAuthorised_visitor_create_person(self): 
    6767        self.person = self.registry.persons['visitor'] 
    6868        self.actionName = 'Create' 
     
    7070        self.ac.actionName = self.actionName 
    7171        self.ac.protectedObject = self.object 
    72         self.failIf(self.isPersonBarred()) 
    7372        self.failUnless(self.isAuthorised()) 
    7473 
    75  
  • trunk/src/dm/command/accesscontrol.py

    r2 r32  
    11from dm.command import Command 
    2 import dm.command.person 
    32from dm.exceptions import * 
    43 
     
    3837                if protectionObject.isProtector(self.protectedObject): 
    3938                    self.grant = grant 
    40                     return True 
    41         return False 
    42  
    43  
    44 class AuthoriseAccess(AccessControlCommand): 
    45     "General role based authorise access command." 
    46      
    47     def execute(self): 
    48         super(AuthoriseAccess, self).execute() 
    49         if not self.findGrant(): 
    50             error = "No grant on role '%s' to '%s' object '%s'." % ( 
    51                 self.role.name, self.action.name, self.protectedObject 
    52             ) 
    53             self.raiseError(error) 
    54     
    55     
    56 class PersonalAccessControlCommand(Command): 
    57     "General person based authorise access command." 
    58      
    59     def __init__(self, personName=None, actionName=None, protectedObject=None): 
    60         super(PersonalAccessControlCommand, self).__init__( 
    61             personName=personName, 
    62             actionName=actionName, 
    63             protectedObject=protectedObject 
    64         ) 
    65         self.personName = personName 
    66         self.actionName = actionName 
    67         self.protectedObject = protectedObject 
    68  
    69     def execute(self): 
    70         super(PersonalAccessControlCommand, self).execute() 
    71         self.validateInput() 
    72  
    73     def validateInput(self): 
    74         if not self.personName: 
    75             raise KforgeCommandError("No person name.") 
    76         else: 
    77             self.person = self.registry.persons[self.personName] 
    78         if not self.actionName: 
    79             raise KforgeCommandError("No action name.") 
    80         else: 
    81             self.action = self.registry.actions[self.actionName] 
    82         if not self.protectedObject: 
    83             raise KforgeCommandError("No protectedObject.") 
    84  
    85  
    86 class AuthorisePersonalAccess(PersonalAccessControlCommand): 
    87  
    88     def __init__(self, *args, **kwds): 
    89         super(AuthorisePersonalAccess, self).__init__(*args, **kwds) 
    90         self.grant = None 
    91      
    92     def execute(self): 
    93         super(AuthorisePersonalAccess, self).execute() 
    94         if not self.findGrant(): 
    95             error = "No grant on person '%s' to '%s' object '%s'." % ( 
    96                 self.person.name, self.action.name, self.protectedObject 
    97             ) 
    98             self.raiseError(error) 
    99  
    100     def findGrant(self): 
    101         for grant in self.person.grants: 
    102             permission = grant.permission 
    103             if permission.action == self.action: 
    104                 protectionObject = permission.protectionObject 
    105                 if protectionObject.isProtector(self.protectedObject): 
    106                     self.grant = grant 
    107                     return True 
    108         return False 
    109  
    110  
    111 class IsPersonBarred(PersonalAccessControlCommand): 
    112  
    113     def __init__(self, *args, **kwds): 
    114         super(IsPersonBarred, self).__init__(*args, **kwds) 
    115         self.bar = None 
    116          
    117     def execute(self): 
    118         super(IsPersonBarred, self).execute() 
    119         if not self.findBar(): 
    120             error = "No bar on person '%s' to '%s' object '%s'." % ( 
    121                 self.person.name, self.action.name, self.protectedObject 
    122             ) 
    123             self.raiseError(error) 
    124  
    125     def findBar(self): 
    126         for bar in self.person.bars: 
    127             permission = bar.permission 
    128             if permission.action == self.action: 
    129                 protectionObject = permission.protectionObject 
    130                 if protectionObject.isProtector(self.protectedObject): 
    131                     self.bar = bar 
    13239                    return True 
    13340        return False 
  • trunk/src/dm/command/accesscontroltest.py

    r12 r32  
    66def suite(): 
    77    suites = [ 
    8         unittest.makeSuite(TestAuthoriseAccess), 
    9         unittest.makeSuite(TestAuthorisePersonalAccess), 
    108        unittest.makeSuite(TestGrantAccess), 
    119        unittest.makeSuite(TestRevokeAccess), 
     
    5654                    return bar 
    5755        return None 
    58  
    59  
    60 class TestAuthoriseAccess(TestAccessControlCommand): 
    61  
    62     def setUp(self): 
    63         super(TestAuthoriseAccess, self).setUp() 
    64         self.setRole('Developer') 
    65         self.setAction('Read') 
    66         self.setProtectedObject('Person') 
    67  
    68     def test_execute_no_role(self): 
    69         cmd = AuthoriseAccess( 
    70             None, self.actionName, self.protectedObject 
    71          
    72         ) 
    73         self.failUnlessRaises(KforgeCommandError, cmd.execute) 
    74  
    75     def test_execute_no_action(self): 
    76         self.action = None 
    77         cmd = AuthoriseAccess( 
    78             self.roleName, '', self.protectedObject 
    79         ) 
    80         self.failUnlessRaises(KforgeCommandError, cmd.execute) 
    81  
    82     def test_execute_no_protectedObject(self): 
    83         self.protectedObject = None 
    84         cmd = AuthoriseAccess( 
    85             self.roleName, self.actionName 
    86         ) 
    87         self.failUnlessRaises(KforgeCommandError, cmd.execute) 
    88  
    89     def test_execute_error(self): 
    90         self.setRole('Visitor') 
    91         self.setAction('Delete') 
    92         self.setProtectedObject('Person') 
    93         cmd = AuthoriseAccess( 
    94             self.role, self.actionName, self.protectedObject 
    95         ) 
    96         self.failUnlessRaises(KforgeCommandError, cmd.execute) 
    97  
    98     def test_execute(self): 
    99         cmd = AuthoriseAccess( 
    100             self.role, self.actionName, self.protectedObject 
    101         ) 
    102         cmd.execute() 
    103  
    104  
    105 class TestAuthorisePersonalAccess(TestAccessControlCommand): 
    106  
    107     def setUp(self): 
    108         super(TestAuthorisePersonalAccess, self).setUp() 
    109         self.personName = '' 
    110         self.person = None 
    111  
    112     def setPerson(self, name): 
    113         self.personName = name 
    114         self.person = self.registry.roles[name] 
    11556 
    11657