X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/b94b945fb03e21a1997cfe6e50148967586cb26d..refs/pull/3598/head:/app/Auth/Access/UserTokenService.php diff --git a/app/Auth/Access/UserTokenService.php b/app/Auth/Access/UserTokenService.php index a1defbf62..ffd828ab5 100644 --- a/app/Auth/Access/UserTokenService.php +++ b/app/Auth/Access/UserTokenService.php @@ -1,59 +1,56 @@ -db = $db; - } - /** * Delete all email confirmations that belong to a user. + * * @param User $user + * * @return mixed */ public function deleteByUser(User $user) { - return $this->db->table($this->tokenTable) + return DB::table($this->tokenTable) ->where('user_id', '=', $user->id) ->delete(); } /** * Get the user id from a token, while check the token exists and has not expired. + * * @param string $token - * @return int + * * @throws UserTokenNotFoundException * @throws UserTokenExpiredException + * + * @return int */ - public function checkTokenAndGetUserId(string $token) : int + public function checkTokenAndGetUserId(string $token): int { $entry = $this->getEntryByToken($token); @@ -70,63 +67,74 @@ class UserTokenService /** * Creates a unique token within the email confirmation database. + * * @return string */ - protected function generateToken() : string + protected function generateToken(): string { $token = Str::random(24); while ($this->tokenExists($token)) { $token = Str::random(25); } + return $token; } /** * Generate and store a token for the given user. + * * @param User $user + * * @return string */ - protected function createTokenForUser(User $user) : string + protected function createTokenForUser(User $user): string { $token = $this->generateToken(); - $this->db->table($this->tokenTable)->insert([ - 'user_id' => $user->id, - 'token' => $token, + DB::table($this->tokenTable)->insert([ + 'user_id' => $user->id, + 'token' => $token, 'created_at' => Carbon::now(), - 'updated_at' => Carbon::now() + 'updated_at' => Carbon::now(), ]); + return $token; } /** * Check if the given token exists. + * * @param string $token + * * @return bool */ - protected function tokenExists(string $token) : bool + protected function tokenExists(string $token): bool { - return $this->db->table($this->tokenTable) + return DB::table($this->tokenTable) ->where('token', '=', $token)->exists(); } /** * Get a token entry for the given token. + * * @param string $token + * * @return object|null */ protected function getEntryByToken(string $token) { - return $this->db->table($this->tokenTable) + return DB::table($this->tokenTable) ->where('token', '=', $token) ->first(); } /** * Check if the given token entry has expired. + * * @param stdClass $tokenEntry + * * @return bool */ - protected function entryExpired(stdClass $tokenEntry) : bool + protected function entryExpired(stdClass $tokenEntry): bool { return Carbon::now()->subHours($this->expiryTime) ->gt(new Carbon($tokenEntry->created_at));