findByPhone($phone); if ($account) { throw new BadRequestException('account.phone_taken'); } } public function checkIfEmailTaken($email) { $accountRepo = new AccountRepo(); $account = $accountRepo->findByEmail($email); if ($account) { throw new BadRequestException('account.email_taken'); } } public function checkLoginAccount($name) { $accountRepo = new AccountRepo(); $account = null; if (CommonValidator::email($name)) { $account = $accountRepo->findByEmail($name); } elseif (CommonValidator::phone($name)) { $account = $accountRepo->findByPhone($name); } if (!$account) { throw new BadRequestException('account.not_found'); } return $account; } public function checkOriginPassword($account, $password) { $hash = PasswordUtil::hash($password, $account->salt); if ($hash != $account->password) { throw new BadRequestException('account.origin_password_incorrect'); } } public function checkUserLogin($name, $password) { $accountRepo = new AccountRepo(); $account = null; if (CommonValidator::email($name)) { $account = $accountRepo->findByEmail($name); } elseif (CommonValidator::phone($name)) { $account = $accountRepo->findByPhone($name); } if (!$account) { throw new BadRequestException('account.login_account_incorrect'); } $hash = PasswordUtil::hash($password, $account->salt); if ($hash != $account->password) { throw new BadRequestException('account.login_password_incorrect'); } $userRepo = new UserRepo(); $user = $userRepo->findById($account->id); return $user; } public function checkAdminLogin($name, $password) { $user = $this->checkUserLogin($name, $password); if ($user->admin_role == 0) { throw new ForbiddenException('account.admin_not_authorized'); } return $user; } }