public function save($id, $data) { $role = parent::save($id, $data); //Assing user roles $permissions = strlen($data['permissions']) ? explode(",", $data['permissions']) : []; $permissions = explode(',', trim($data['permissions'])); if (count($permissions) > 0) { $role->permissions()->sync($permissions); } return $role; }
public function save($id, $data) { if (isset($data['password'])) { $data['password'] = bcrypt($data['password']); } $user = parent::save($id, $data); //Assing user roles if (count($data['roles']) > 0) { $user->roles()->sync($data['roles']); } return $user; }
/** * @param $input * @return mixed * @throws GeneralException */ public function changePassword($input) { $user = parent::find(access()->id()); if (Hash::check($input['old_password'], $user->password)) { $user->password = bcrypt($input['password']); return parent::save($user); } throw new GeneralException(trans('exceptions.frontend.auth.password.change_mismatch')); }
/** * @param Model $user * @param $status * @return bool * @throws GeneralException */ public function mark(Model $user, $status) { if (access()->id() == $user->id && $status == 0) { throw new GeneralException(trans('exceptions.backend.access.users.cant_deactivate_self')); } $user->status = $status; switch ($status) { case 0: event(new UserDeactivated($user)); break; case 1: event(new UserReactivated($user)); break; } if (parent::save($user)) { return true; } throw new GeneralException(trans('exceptions.backend.access.users.mark_error')); }
/** * @param Model $role * @param $input * @throws GeneralException * @return bool */ public function update(Model $role, array $input) { //See if the role has all access, administrator always has all access if ($role->id == 1) { $all = true; } else { $all = $input['associated-permissions'] == 'all' ? true : false; } if (!isset($input['permissions'])) { $input['permissions'] = []; } //This config is only required if all is false if (!$all) { //See if the role must contain a permission as per config if (config('access.roles.role_must_contain_permission') && count($input['permissions']) == 0) { throw new GeneralException(trans('exceptions.backend.access.roles.needs_permission')); } } $role->name = $input['name']; $role->sort = isset($input['sort']) && strlen($input['sort']) > 0 && is_numeric($input['sort']) ? (int) $input['sort'] : 0; //See if this role has all permissions and set the flag on the role $role->all = $all; DB::transaction(function () use($role, $input, $all) { if (parent::save($role)) { //If role has all access detach all permissions because they're not needed if ($all) { $role->permissions()->sync([]); } else { //Remove all roles first $role->permissions()->sync([]); //Attach permissions if the role does not have all access $permissions = []; if (is_array($input['permissions']) && count($input['permissions'])) { foreach ($input['permissions'] as $perm) { if (is_numeric($perm)) { array_push($permissions, $perm); } } } $role->attachPermissions($permissions); } event(new RoleUpdated($role)); return true; } throw new GeneralException(trans('exceptions.backend.access.roles.update_error')); }); }