您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

73 行
1.9KB

  1. <?php
  2. namespace App\Http\Controllers\Web\Auth;
  3. use App\Http\Controllers\Web\WebController;
  4. use App\Kintone\Models\Customer;
  5. use App\Models\User;
  6. use Illuminate\Http\JsonResponse;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Support\Facades\Hash;
  10. class LoginController extends WebController
  11. {
  12. public function name(): string
  13. {
  14. return "ログイン";
  15. }
  16. public function description(): string
  17. {
  18. return "ログインを行う";
  19. }
  20. public function __construct(protected LoginParam $param)
  21. {
  22. parent::__construct();
  23. }
  24. protected function run(Request $request): JsonResponse
  25. {
  26. // 取得したユーザ情報を登録しログインを行う
  27. $param = $this->param;
  28. $access = Customer::getAccess();
  29. $query = Customer::getQuery()->where(Customer::FIELD_EMAIL, $param->email)
  30. ->where(Customer::FIELD_CUSTOMER_CODE, $param->customerCode)
  31. ->whereIn(Customer::FIELD_ALLOW_ACCESS_MY_PAGE, ["許可"]);
  32. $customer = $access->some($query);
  33. if ($customer->count() !== 1) {
  34. return $this->failedResponse();
  35. }
  36. $customer = $customer->first();
  37. $kintoneId = $customer->getRecordId();
  38. $user = User::whereKintoneId($kintoneId)
  39. ->first();
  40. if ($user instanceof User) {
  41. // パスワードチェック
  42. if (!Hash::check($param->password, $user->password)) {
  43. return $this->failedResponse();
  44. }
  45. //データ同期 Email
  46. if ($user->email !== $param->email) {
  47. $user->email = $param->email;
  48. $user->save();
  49. }
  50. Auth::login($user);
  51. return $this->successResponse($customer->toArray());
  52. }
  53. return $this->failedResponse();
  54. }
  55. }