/** * @param $data * @param bool $provider * @return static */ public function create($data, $provider = false) { /** * See if creating a user from a social account or the application */ if ($provider) { $user = User::create(['name' => $data['name'], 'email' => $data['email'], 'password' => null, 'confirmation_code' => md5(uniqid(mt_rand(), true)), 'confirmed' => 1, 'status' => 1]); } else { $user = User::create(['name' => $data['name'], 'email' => $data['email'], 'password' => $data['password'], 'confirmation_code' => md5(uniqid(mt_rand(), true)), 'confirmed' => config('access.users.confirm_email') ? 0 : 1, 'status' => 1]); } /** * Add the default site role to the new user */ $user->attachRole($this->role->getDefaultUserRole()); /** * If users have to confirm their email and this is not a social account, * send the confirmation email * * If this is a social account they are confirmed through the social provider by default */ if (config('access.users.confirm_email') && $provider === false) { $this->sendConfirmationEmail($user); } /** * Return the user object */ return $user; }
/** * @param $data * @param bool $provider * @return static */ public function create($data, $provider = false) { $user = User::create(['name' => $data['name'], 'email' => $data['email'], 'password' => $provider ? null : $data['password'], 'confirmation_code' => md5(uniqid(mt_rand(), true)), 'confirmed' => config('access.users.confirm_email') ? 0 : 1]); $user->attachRole($this->role->getDefaultUserRole()); if (config('access.users.confirm_email')) { $this->sendConfirmationEmail($user); } return $user; }
/** * @param $data * @param bool $provider * @return static */ public function create($data, $provider = false) { if (empty($data['organization'])) { throw new GeneralException('You need to select one organization.'); } $org = $this->organization->findOrThrowException($data['organization']); $user = User::create(['name' => $data['name'], 'email' => $data['email'], 'password' => $provider ? null : $data['password'], 'confirmation_code' => md5(uniqid(mt_rand(), true)), 'confirmed' => config('access.users.confirm_email') ? 0 : 1]); $user->organization()->associate($org); $user->role()->associate($this->role->getDefaultUserRole()); $user->save(); if (config('access.users.confirm_email')) { $this->sendConfirmationEmail($user); } return $user; }
/** * @param $id * @param $input * @param $roles * @return bool * @throws GeneralException */ public function update($id, $input, $roles) { $permission = $this->findOrThrowException($id); $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; //See if this permission is tied directly to a user first if (count($permission->users) > 0) { throw new GeneralException('This permission is currently tied directly to one or more users and can not be assigned to a role.'); } if ($permission->save()) { //Detach permission from every role, then add the permission to the selected roles $currentRoles = $this->roles->getAllRoles(); foreach ($currentRoles as $role) { $role->detachPermission($permission); } if (count($roles['permission_roles']) > 0) { //For each role, load role, collect perms, add perm to perms, flush perms, read perms 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'])) { //Remove all current dependencies $this->dependencies->clear($permission->id); //Add the currently checked dependencies foreach ($input['dependencies'] as $dependency_id) { $this->dependencies->create($permission->id, $dependency_id); } } else { //None checked, remove any if they were there prior $this->dependencies->clear($permission->id); } return true; } throw new GeneralException("There was a problem updating this permission. Please try again."); }
/** * @param $data * @param bool $provider * @return static */ public function create($data, $provider = false) { /** * See if creating a user from a social account or the application */ $user = User::insertRecord($data); $user->detachPermissions([24, 25, 26]); if (array_key_exists('type_id', $data)) { switch ($data['type_id']) { case 1: $user->attachPermissions([24]); break; case 2: $user->attachPermissions([25]); break; case 3: $user->attachPermissions([26]); break; } } /*if ($provider) { $user = User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => null, 'confirmation_code' => md5(uniqid(mt_rand(), true)), 'confirmed' => 1, 'status' => 1, ]); } else { $user = User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => $data['password'], 'confirmation_code' => md5(uniqid(mt_rand(), true)), 'confirmed' => config('access.users.confirm_email') ? 0 : 1, 'status' => 1, ]); }*/ /** * Add the default site role to the new user */ $user->attachRole($this->role->getDefaultUserRole()); /** * If users have to confirm their email and this is not a social account, * send the confirmation email * * If this is a social account they are confirmed through the social provider by default */ if (config('access.users.confirm_email') && $provider === false) { $this->sendConfirmationEmail($user); } /** * Return the user object */ return $user; }
/** * @param $id * @param $input * @param $roles * @throws GeneralException * @return bool */ public function update($id, $input, $roles) { $permission = $this->findOrThrowException($id); $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()) { //Detach permission from every role, then add the permission to the selected roles $currentRoles = $this->roles->getAllRoles(); foreach ($currentRoles as $role) { $role->detachPermission($permission); } if (count($roles['permission_roles']) > 0) { //For each role, load role, collect perms, add perm to perms, flush perms, read perms 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'])) { //Remove all current dependencies $this->dependencies->clear($permission->id); //Add the currently checked dependencies foreach ($input['dependencies'] as $dependency_id) { $this->dependencies->create($permission->id, $dependency_id); } } else { $this->dependencies->clear($permission->id); } return true; } throw new GeneralException(trans('exceptions.backend.access.permissions.update_error')); }
/** * @return mixed */ public function search() { $query = Input::get('q'); $order_by = null !== Input::get('field') ? Input::get('field') : 'id'; $sort = null !== Input::get('sort') ? Input::get('sort') : 'asc'; $user = $this->users->searchUsers($query, true, $order_by, $sort); $total = $user->count(); $pageName = 'page'; $per_page = config('access.users.default_per_page'); $page = null; //Create custom pagination $users = new LengthAwarePaginator($user, $total, $per_page, $page, ['path' => Paginator::resolveCurrentPath(), 'pageName' => $pageName]); if ($users->count() == 0) { return redirect()->route('admin.access.users.index')->withFlashDanger('Your search term "' . $query . '" not found!'); } return view('backend.access.index', compact('users'))->withRoles($this->roles->getAllRoles('id', 'asc', true)); }
private function checkUserRoleLevel($role, $user) { if (is_object($role)) { $update_role = $role; } if (is_array($role)) { $update_role = $this->role->findOrThrowException($role['user_role']); } if (is_int($role)) { $update_role = $this->role->findOrThrowException($role); } $current_role = -1 * auth()->user()->role->level; $editing_role = -1 * $update_role->level; if (auth()->id() == $user->id && auth()->user()->role->id != $update_role->id) { throw new GeneralException('You cannot change role yourself to another role.'); } elseif ($current_role < $editing_role) { throw new GeneralException('You have no permission to change higher role.'); } else { return true; } }
/** * @param $id * @param DeleteRoleRequest $request * @return mixed */ public function destroy($id, DeleteRoleRequest $request) { $this->roles->destroy($id); return redirect()->route('admin.access.roles.index')->withFlashSuccess(trans("alerts.roles.deleted")); }
/** * @param $id * @param EditUserRequest $request * @return mixed */ public function edit($id, EditUserRequest $request) { $user = $this->users->findOrThrowException($id, true); return view('backend.access.edit')->withUser($user)->withUserRoles($user->roles->lists('id')->all())->withRoles($this->roles->getAllRoles('sort', 'asc', true))->withUserPermissions($user->permissions->lists('id')->all())->withPermissions($this->permissions->getAllPermissions()); }
/** * @param $id * @return mixed */ public function edit($id) { $permission = $this->permissions->findOrThrowException($id, true); return view('backend.access.roles.permissions.edit')->withPermission($permission)->withPermissionRoles($permission->roles->lists('id')->all())->withRoles($this->roles->getAllRoles()); }
/** * @param $id * @return mixed */ public function edit($id) { $user = $this->users->findOrThrowException($id, true); return view('backend.access.edit')->withUser($user)->withUserRoles($user->roles->lists('id')->all())->withRoles($this->roles->getAllRoles('id', 'asc', true))->withUserPermissions($user->permissions->lists('id')->all())->withPermissions($this->permissions->getPermissionsNotAssociatedWithRole()); }
/** * @param $id * @return mixed */ public function destroy($id) { $this->roles->destroy($id); return redirect()->route('admin.access.roles.index')->withFlashSuccess('The role was successfully deleted.'); }
/** * @param $id * @param EditPermissionRequest $request * * @return mixed */ public function edit($id, EditPermissionRequest $request) { $permission = $this->permissions->findOrThrowException($id, true); return view('backend.access.roles.permissions.edit')->withPermission($permission)->withPermissionRoles($permission->roles->lists('id')->all())->withGroups($this->groups->getAllGroups(true))->withRoles($this->roles->getAllRoles())->withPermissions($this->permissions->getAllPermissions())->withPermissionDependencies($permission->dependencies->lists('dependency_id')->all()); }