領収証発行サービス
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.

117 lines
3.4KB

  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\AdminUserManager;
  10. use App\Logic\User\LoginUserManager;
  11. use App\Logic\User\UserManager;
  12. use App\Models\User;
  13. use App\Repositories\LoginUserRepository;
  14. use Illuminate\Http\JsonResponse;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\Auth;
  17. class ChangePasswordController extends WebController
  18. {
  19. use LoginUser;
  20. private UserManager $manager;
  21. public function name(): string
  22. {
  23. return "ログインユーザーパスワード変更";
  24. }
  25. public function description(): string
  26. {
  27. return "ログインユーザーのパスワードを変更する";
  28. }
  29. public function __construct(
  30. protected ChangePasswordParam $param,
  31. ) {
  32. parent::__construct();
  33. $this->roleAllow(UserRole::NORMAL_ADMIN);
  34. }
  35. protected function getParam(): IParam
  36. {
  37. return $this->param;
  38. }
  39. protected function run(Request $request): JsonResponse
  40. {
  41. $param = $this->param;
  42. $targetUserId = $this->getTartgetUserId();
  43. $targetUser = User::findOrFail($targetUserId);
  44. try {
  45. $this->transaction->beginTransaction();
  46. $currentContract = $this->loginUser()->getCurrentContract();
  47. if (!$currentContract && $this->loginUser()->user()->role !== UserRole::SUPER_ADMIN) {
  48. throw new AppCommonException("認証不正");
  49. }
  50. $this->manager = UserManager::getManager($targetUser);
  51. if ($currentContract) {
  52. $this->manager->initForModify($currentContract, $targetUserId);
  53. } else {
  54. $this->manager->initForModifyAdmin($targetUserId);
  55. }
  56. if (!$this->param->checkTimestamp($this->manager->getTimestamp())) {
  57. logger(sprintf(
  58. "request:%s origin:%s",
  59. $this->param->timestamp->format('Y/m/d H:i:s'),
  60. $this->manager->getTimestamp()->format(('Y/m/d H:i:s'))
  61. ));
  62. throw new ExclusiveException();
  63. }
  64. $messages = $this->manager
  65. ->fill($param->toArray())
  66. ->update();
  67. if (count($messages) !== 0) {
  68. $this->transaction->rollBack();
  69. return $this->validateErrorResponse($messages);
  70. }
  71. $this->transaction->commit();
  72. } catch (Exception $e) {
  73. $this->transaction->rollBack();
  74. throw $e;
  75. }
  76. return $this->successResponse();
  77. }
  78. private function getTartgetUserId(): string
  79. {
  80. /*
  81. 変更対象のユーザーIDを指定できるのは管理者のみとする
  82. 管理者以外が指定した場合はNG
  83. 指定しない場合は自身を変更対象とする
  84. */
  85. $loginUserRole = $this->loginUser()->user()->role;
  86. if ($this->param->id !== null) {
  87. if ($loginUserRole === UserRole::CONTRACT_ADMIN || $loginUserRole === UserRole::SUPER_ADMIN) {
  88. return $this->param->id;
  89. } else {
  90. throw new AppCommonException('認証不正');
  91. }
  92. }
  93. return $this->loginUser()->user()->id;
  94. }
  95. }