public function generateRole($name = false, $desc = "blah desc") { if (!$name) { $name = "role_{$this->current_role_num}"; } $role = Role::create($name, $desc); $role->role_id = ++$this->current_role_num; return $role; }
/** * @expectedException \RBAC\Exception\InsufficientPermission */ public function testRequirePermission() { $p1 = Permission::create("test_1", "", 1); $p2 = Permission::create("test_2", "", 2); $r1 = Role::create("role_1", "", [$p1, $p2]); $subject = new Subject(1, new RoleSet([$r1])); $subject->requirePermission($p1); $subject->requirePermission($p2->name); $subject->requirePermission("bs_perm"); }
public function testPermissions() { $p1 = Permission::create("test_1", "", 1); $p2 = Permission::create("test_2", "", 2); $p3 = Permission::create("test_3", "", 3); $p4 = Permission::create("test_4", "", 4); $r1 = Role::create("role_1", "", [$p1, $p2]); $r2 = Role::create("role_2", "", [$p1, $p2, $p4]); $role_set = new RoleSet([$r1, $r2]); $permissions = $role_set->getPermissions(); $this->assertEquals(3, sizeof($permissions)); }
/** * Creates or updates a single role given a role name and optionally a role description * Use this function if you just want to register a single role. * * @param $role_name string * @param $role_desc string * @return $roles array of objects */ public function register_role($role_name, $role_desc = false) { //check if the role already exists if ($role_object = $this->role_manager->roleFetchByName($role_name)) { //update the existing role (if the role_desc actually exists) $role_object->description = $role_desc ? $role_desc : $role_object->description; } else { //create the new role (if the role_desc is false, pass an empty role desc string) $role_desc = $role_desc ? $role_desc : ''; $role_object = Role::create($role_name, $role_desc); } if (!$this->role_manager->roleSave($role_object)) { throw new RoleSaveException($this->lang('role_save_unsuccessful')); } return $role_object; }
protected function seed() { $default_users = array(array('id' => '1', 'ipAddress' => inet_pton('127.0.0.1'), 'username' => 'administrator', 'password' => '$2y$10$EiqipvSt3lnD//nchj4u9OgOTL9R3J4AbZ5bUVVrh.Tq/gmc5xIvS', 'passwordChange' => '0', 'email' => '*****@*****.**', 'createdOn' => date('Y-m-d H:i:s'), 'lastLogin' => date('Y-m-d H:i:s'), 'active' => '1', 'sharedKey' => '9tThG2rFhZv+LslrrPy0I6QcCn9E3kNcyv5rqb3qKLywvFCfwHiSZ2fH2qQAgQXAkfBIrd0zHP+1v/FJ4W/kMA==')); //default user to roles $default_users_to_roles = array(array('admin', 'member')); //roles to descriptions $default_roles = array('admin' => 'Site Administrators', 'member' => 'General Members'); //roles to permissions to permission descriptions $default_role_permissions = array('admin' => array('admin_create' => 'Creating administration resources.', 'admin_read' => 'Viewing administration resources.', 'admin_update' => 'Editing administration resources.', 'admin_delete' => 'Deleting administration resources.'), 'member' => array('public_read' => 'Viewing public resources.')); //seeding user accounts foreach ($default_users as $user) { $this->db->insert('user_accounts', $user); } //seeding roles and permissions $role_manager = new RoleManager(new MySQLAdapter($this->db->conn_id, new Options())); foreach ($default_role_permissions as $role => $permissions_array) { //create the role $created_role = Role::create($role, $default_roles[$role]); foreach ($permissions_array as $permission => $reason) { //create the permission $created_permission = Permission::create($permission, $reason); //save the permission to the database $role_manager->permissionSave($created_permission); //add the permission to the role $created_role->addPermission($created_permission); } $role_manager->roleSave($created_role); } //assign the role to the users foreach ($default_users_to_roles as $key => $roles) { $user_id = $default_users[$key]['id']; foreach ($roles as $role) { $assignable_role = $role_manager->roleFetchByName($role); $role_manager->roleAddSubjectId($assignable_role, $user_id); } } }
/** * @expectedException \RBAC\Exception\ValidationError */ public function testRolePermissionAddInvalidRole() { $this->rm->rolePermissionAdd(Role::create("blah"), $this->generatePerm()); }
public function testToString() { $this->assertEquals("test_role", (string) Role::create("test_role")); }