/** * @param $input * @param $roles * @throws GeneralException * @return bool */ public function create($input, $roles) { $permission = new Permission(); $permission->name = $input['name']; $permission->display_name = $input['display_name']; $permission->system = isset($input['system']) ? 1 : 0; $permission->group_id = isset($input['group']) && strlen($input['group']) > 0 ? (int) $input['group'] : null; $permission->sort = isset($input['sort']) ? (int) $input['sort'] : 0; if ($permission->save()) { //For each role, load role, collect perms, add perm to perms, flush perms, read perms if (count($roles['permission_roles']) > 0) { foreach ($roles['permission_roles'] as $role_id) { //Get the role, with permissions $role = $this->roles->findOrThrowException($role_id, true); //Get the roles permissions into an array $role_permissions = $role->permissions->lists('id')->all(); if (count($role_permissions) >= 1) { //Role has permissions, gather them first //Add this new permission id to the role array_push($role_permissions, $permission->id); //For some reason the lists() casts as a string, convert all to int $role_permissions_temp = array(); foreach ($role_permissions as $rp) { array_push($role_permissions_temp, (int) $rp); } $role_permissions = $role_permissions_temp; //Sync the permissions to the role $role->permissions()->sync($role_permissions); } else { //Role has no permissions, add the 1 $role->permissions()->sync([$permission->id]); } } } //Add the dependencies of this permission if any if (isset($input['dependencies']) && count($input['dependencies'])) { foreach ($input['dependencies'] as $dependency_id) { $this->dependencies->create($permission->id, $dependency_id); } } return true; } throw new GeneralException(trans('exceptions.backend.access.permissions.create_error')); }
public function testDetachPermissionsFromRoleByObject() { $this->notSeeInDatabase('permission_role', ['permission_id' => 1, 'role_id' => $this->userRole->id]); $this->notSeeInDatabase('permission_role', ['permission_id' => 2, 'role_id' => $this->userRole->id]); $this->userRole->attachPermissions([Permission::findOrFail(1), Permission::findOrFail(2)]); $this->seeInDatabase('permission_role', ['permission_id' => 1, 'role_id' => $this->userRole->id]); $this->seeInDatabase('permission_role', ['permission_id' => 2, 'role_id' => $this->userRole->id]); $this->userRole->detachPermissions([Permission::findOrFail(1), Permission::findOrFail(2)]); $this->notSeeInDatabase('permission_role', ['permission_id' => 1, 'role_id' => $this->userRole->id]); $this->notSeeInDatabase('permission_role', ['permission_id' => 2, 'role_id' => $this->userRole->id]); }
/** * @param string $order_by * @param string $sort * @return mixed */ public function getAllPermissions($order_by = 'sort', $sort = 'asc') { return Permission::orderBy($order_by, $sort)->get(); }