public function run() { $adminEmail = Config::get('madison.seeder.admin_email'); $admin = new Role(); $admin->name = 'Admin'; $admin->save(); $independent_sponsor = new Role(); $independent_sponsor->name = 'Independent Sponsor'; $independent_sponsor->save(); $permIds = array(); foreach ($this->adminPermissions as $permClass => $data) { $perm = new Permission(); foreach ($data as $key => $val) { $perm->{$key} = $val; } $perm->save(); $permIds[] = $perm->id; } $admin->perms()->sync($permIds); $user = User::where('email', '=', $adminEmail)->first(); $user->attachRole($admin); $createDocPerm = new Permission(); $createDocPerm->name = "independent_sponsor_create_doc"; $createDocPerm->display_name = "Independent Sponsoring"; $createDocPerm->save(); $independent_sponsor->perms()->sync(array($createDocPerm->id)); }
/** * Store role * * @param array $roleData * @throws NotFoundException, ValidationException * @return \App\Models\RoleModel */ public function store($roleData) { try { if (array_get($roleData, 'id')) { $role = RoleModel::findOrFail((int) array_get($roleData, 'id'))->fill($roleData); } else { $role = new RoleModel(); $role->fill($roleData); } } catch (Exception $e) { throw new NotFoundException(trans('app.notFound')); } if (!$role->validate()) { throw new ValidationException(trans('app.correctErrors'), $role->errors()->toArray()); } try { $role->save(); // associate permissions if (array_get($roleData, 'permission_id')) { $role->perms()->sync(array_get($roleData, 'permission_id')); } } catch (Exception $e) { throw $e; } return $role; }
/** * Store a newly created resource in storage. * * @return Response */ public function store() { $rules = ['name' => 'required|alpha_dash|min:1|max:255', 'display_name' => 'string|max:255', 'description' => 'string', 'permissions' => 'array|integerInArray|existsInArray:permission,id']; $validator = Validator::make(Input::only(array_keys($rules)), $rules); if ($validator->fails()) { throw new ResourceException($validator->errors()->first()); } $role = new Role(); $fields = ['name']; foreach ($fields as $key => $field) { if (Input::has($field)) { $role->{$field} = Input::get($field); } } //field which can null/empty string $fields = ['description', 'display_name']; foreach ($fields as $key => $field) { if (Input::get($field) === '') { $role->{$field} = null; } elseif (Input::has($field)) { $role->{$field} = Input::get($field); } } $role->save(); $role->perms()->sync(Input::get('permissions', [])); return $this->show($role->id); }
public function createRbacRules() { $this->destroyRbacRules(); $ownerRole = new Role(); $ownerRole->name = "group_{$this->id}_owner"; $ownerRole->save(); $permissions = $this->getPermissionsArray(); $permIds = array(); $permLookup = array(); foreach ($permissions as $perm) { $permModel = new Permission(); foreach ($perm as $key => $val) { $permModel->{$key} = $val; } $permModel->save(); $permIds[] = $permModel->id; switch ($perm['name']) { case "group_{$this->id}_create_document": $permLookup['create'] = $permModel->id; break; case "group_{$this->id}_edit_document": $permLookup['edit'] = $permModel->id; break; case "group_{$this->id}_delete_document": $permLookup['delete'] = $permModel->id; break; case "group_{$this->id}_manage_document": $permLookup['manage'] = $permModel->id; break; } } $ownerRole->perms()->sync($permIds); $editorRole = new Role(); $editorRole->name = "group_{$this->id}_editor"; $editorRole->save(); $editorRole->perms()->sync(array($permLookup['create'], $permLookup['edit'], $permLookup['manage'])); $staffRole = new Role(); $staffRole->name = "group_{$this->id}_staff"; $staffRole->save(); $users = array(static::ROLE_OWNER => $this->findUsersByRole(static::ROLE_OWNER), static::ROLE_EDITOR => $this->findUsersByRole(static::ROLE_EDITOR), static::ROLE_STAFF => $this->findUsersByRole(static::ROLE_STAFF)); foreach ($users as $role => $userList) { foreach ($userList as $userObj) { switch ($role) { case static::ROLE_OWNER: $userObj->attachRole($ownerRole); break; case static::ROLE_EDITOR: $userObj->attachRole($editorRole); break; case static::ROLE_STAFF: $userObj->attachRole($staffRole); break; } } } }
public function setupFoundorAndBaseRolsPermission() { // Create Roles $founder = new Role(); $founder->name = 'Founder'; $founder->save(); $admin = new Role(); $admin->name = 'Admin'; $admin->save(); // Create User $user = new User(); $user->username = '******'; $user->display_name = 'Admin'; $user->email = '*****@*****.**'; $user->password = '******'; if (!$user->save()) { Log::info('Unable to create user ' . $user->username, (array) $user->errors()); } else { Log::info('Created user "' . $user->username . '" <' . $user->email . '>'); } // Attach Roles to user $user->roles()->attach($founder->id); // Create Permissions $manageContent = new Permission(); $manageContent->name = 'manage_contents'; $manageContent->display_name = 'Manage Content'; $manageContent->save(); $manageUsers = new Permission(); $manageUsers->name = 'manage_users'; $manageUsers->display_name = 'Manage Users'; $manageUsers->save(); // Assign Permission to Role $founder->perms()->sync([$manageContent->id, $manageUsers->id]); $admin->perms()->sync([$manageContent->id]); }