cannot('viewAny', User::class)) { return $this->errFobidden(__('Access is denied')); } $users = $this->userRepository->getUsers( $userBuilderDto, $querySettingsDto->getQueryWith() )->pagination( $querySettingsDto->getLimit(), $querySettingsDto->getPage() ); return $this->result([ 'users' => $users, ]); } public function create(User $user): ServiceResultError | ServiceResultArray { if ($user->cannot('create', User::class)) { return $this->errFobidden(__('Access is denied')); } return $this->result([ 'user' => new User(), 'roles' => $this->roleRepository->getRolesForSelect(), 'userRoles' => [], ]); } public function edit(int $id, User $user): ServiceResultError | ServiceResultArray { $modelUser = $this->userRepository->getUserById($id); if (is_null($modelUser)) { return $this->errNotFound(__('Not Found')); } if ($user->cannot('view', $modelUser)) { return $this->errFobidden(__('Access is denied')); } $userRoles = $modelUser->roles()->withTrashed()->pluck('id')->toArray(); return $this->result([ 'user' => $modelUser, 'roles' => $this->roleRepository->getRolesForSelect($userRoles), 'userRoles' => $userRoles, ]); } public function store(StoreUpdate $data, User $user): ServiceResultError | StoreUpdateResult { if ($user->cannot('create', User::class)) { return $this->errFobidden(__('Access is denied')); } if ($this->userRepository->isExistsEmail($data->getEmail())) { return $this->errValidate( __('validation.unique', ['attribute' => __('validation.attributes.email')]), ['code' => __('validation.unique', ['attribute' => __('validation.attributes.email')])] ); } try { $modelUser = DB::transaction(function () use ($data) { $dataUser = $this->getDataUser($data); $modelUser = $this->userCommandHandler->handleStore($dataUser, $data->getPassword()); $this->userCommandHandler->handleConfirmationByEmail($modelUser); $this->userCommandHandler->handleSyncRoles($modelUser, $data->getRoles()); return $modelUser; }); } catch (\Throwable $e) { report($e); return $this->errService(__('Server Error')); } return $this->resultStoreUpdateModel($modelUser, __('The user was successfully created')); } public function update(int $id, StoreUpdate $data, User $user): ServiceResultError | StoreUpdateResult { $modelUser = $this->userRepository->getUserById($id); if (is_null($modelUser)) { return $this->errNotFound(__('Not Found')); } if ($user->cannot('update', $modelUser)) { return $this->errFobidden(__('Access is denied')); } if ($this->userRepository->isExistsEmail($data->getEmail(), $modelUser->id)) { return $this->errValidate( __('validation.unique', ['attribute' => __('validation.attributes.email')]), ['code' => __('validation.unique', ['attribute' => __('validation.attributes.email')])] ); } try { $modelUser = DB::transaction(function () use ($data, $modelUser) { $dataUser = $this->getDataUser($data); $modelUser = $this->userCommandHandler->handleUpdate($modelUser, $dataUser); $this->userCommandHandler->handleSyncRoles($modelUser, $data->getRoles()); return $modelUser; }); } catch (\Throwable $e) { report($e); return $this->errService(__('Server Error')); } return $this->resultStoreUpdateModel($modelUser, __('The user was successfully updated')); } public function updatePassword(int $id, UpdatePassword $data, User $user): ServiceResultError | StoreUpdateResult { $modelUser = $this->userRepository->getUserById($id); if (is_null($modelUser)) { return $this->errNotFound(__('Not Found')); } if ($user->cannot('update', $modelUser)) { return $this->errFobidden(__('Access is denied')); } try { $this->userCommandHandler->handleUpdatePassword($modelUser, $data->getPassword()); } catch (\Throwable $e) { report($e->getMessage()); return $this->errService($e->getMessage()); } return $this->resultStoreUpdateModel($modelUser, __('The password has been changed')); } public function destroy(int $id, User $user): ServiceResultError | ServiceResultSuccess { $modelUser = $this->userRepository->getUserById($id); if (is_null($modelUser)) { return $this->errNotFound(__('Not Found')); } if ($user->cannot('delete', $modelUser)) { return $this->errFobidden(__('Access is denied')); } try { DB::transaction(function () use ($modelUser) { $this->userCommandHandler->handleDestroy($modelUser); }); } catch (\Throwable $e) { report($e); return $this->errService(__('Server Error')); } return $this->ok(__('The user has been deleted')); } private function getDataUser(StoreUpdate $data): array { return [ 'is_active' => $data->isActive(), 'name' => $data->getName(), 'email' => $data->getEmail(), ]; } }