def test_retrieve_checker(self): def func(): pass PermissionsManager.register_checker('func_name', func) self.assertEquals(PermissionsManager.retrieve_checker('func_name'), func)
def test_register_checker(self): def func(): pass PermissionsManager.register_checker('func_name', func) self.assertIn('func_name', PermissionsManager._checkers) self.assertEquals(PermissionsManager._checkers['func_name'], func)
def test_register_function_with_diferent_name(self): @register_object_checker('new_name') def function_name(a, b, c): return True self.assertIn('new_name', PermissionsManager.get_checkers()) restore_function = PermissionsManager.retrieve_checker('new_name') self.assertTrue(restore_function('', '', ''))
def has_object_permission(checker_name, user, obj): if user.is_superuser: return True checker = PermissionsManager.retrieve_checker(checker_name) role = get_user_role(user) return checker(role, user, obj)
def has_object_permission(checker_name, user, obj): """Check if a user has permission to perform an action on an object.""" if user and user.is_superuser: return True checker = PermissionsManager.retrieve_checker(checker_name) user_roles = get_user_roles(user) return any([checker(user_role, user, obj) for user_role in user_roles])
def has_object_permission(checker_name, user, obj): """Check if a user has permission to perform an action on an object.""" if user and user.is_superuser: return True checker = PermissionsManager.retrieve_checker(checker_name) user_roles = get_user_roles(user) if not user_roles: user_roles = [None] return any([checker(user_role, user, obj) for user_role in user_roles])
def has_object_permission(checker_name, user, obj): """Check if a user has permission to perform an action on an object.""" if _check_superpowers(user): return True checker = PermissionsManager.retrieve_checker(checker_name) user_roles = get_user_roles(user) if not user_roles: user_roles = [None] return any([checker(user_role, user, obj) for user_role in user_roles])
def test_restore_unregistered_function(self): with self.assertRaises(CheckerNotRegistered): PermissionsManager.retrieve_checker('func_name')
def test_get_checkers(self): self.assertEquals(PermissionsManager.get_checkers(), {})