領収証発行サービス
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.5KB

  1. <?php
  2. namespace App\Http\Controllers\Web\LoginUser;
  3. use App\Codes\UserRole;
  4. use App\Exceptions\AppCommonException;
  5. use App\Exceptions\ExclusiveException;
  6. use App\Features\LoginUser;
  7. use App\Http\Controllers\Web\IParam;
  8. use App\Http\Controllers\Web\WebController;
  9. use App\Logic\User\LoginUserManager;
  10. use App\Repositories\LoginUserRepository;
  11. use Illuminate\Http\JsonResponse;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\Auth;
  14. class ChangePasswordController extends WebController
  15. {
  16. use LoginUser;
  17. public function name(): string
  18. {
  19. return "ログインユーザーパスワード変更";
  20. }
  21. public function description(): string
  22. {
  23. return "ログインユーザーのパスワードを変更する";
  24. }
  25. public function __construct(
  26. protected ChangePasswordParam $param,
  27. private LoginUserManager $manager
  28. ) {
  29. parent::__construct();
  30. $this->roleAllow(UserRole::NORMAL_ADMIN);
  31. }
  32. protected function getParam(): IParam
  33. {
  34. return $this->param;
  35. }
  36. protected function run(Request $request): JsonResponse
  37. {
  38. $param = $this->param;
  39. try {
  40. $this->transaction->beginTransaction();
  41. $currentContract = $this->loginUser()->getCurrentContract();
  42. if (!$currentContract) {
  43. throw new AppCommonException("認証不正");
  44. }
  45. $this->manager->initForModify($currentContract, $this->getTartgetUserId());
  46. if (!$this->param->checkTimestamp($this->manager->getTimestamp())) {
  47. throw new ExclusiveException();
  48. }
  49. $messages = $this->manager
  50. ->fill($param->toArray())
  51. ->update();
  52. if (count($messages) !== 0) {
  53. $this->transaction->rollBack();
  54. return $this->validateErrorResponse($messages);
  55. }
  56. $this->transaction->commit();
  57. } catch (Exception $e) {
  58. $this->transaction->rollBack();
  59. throw $e;
  60. }
  61. return $this->successResponse();
  62. }
  63. private function getTartgetUserId(): string
  64. {
  65. $loginUserRole = $this->loginUser()->user()->role;
  66. if ($this->param->id !== null) {
  67. if ($loginUserRole === UserRole::CONTRACT_ADMIN || $loginUserRole === UserRole::SUPER_ADMIN) {
  68. return $this->param->id;
  69. } else {
  70. throw new AppCommonException('認証不正');
  71. }
  72. }
  73. return $this->loginUser()->user()->id;
  74. }
  75. }