2024-01-29 08:53:46 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Api\v1\Resources;
|
|
|
|
|
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Laravel\Passport\TokenRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property mixed $id
|
|
|
|
* @property string $name
|
|
|
|
* @property string $email
|
|
|
|
* @property string $oauth_provider
|
|
|
|
* @property \Illuminate\Support\Collection<array-key, mixed> $preferences
|
|
|
|
* @property string $is_admin
|
|
|
|
* @property string $last_seen_at
|
|
|
|
* @property string $created_at
|
|
|
|
* @property string $updated_at
|
|
|
|
* @property int|null $twofaccounts_count
|
|
|
|
*/
|
|
|
|
class UserManagerResource extends UserResource
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The "data" wrapper that should be applied.
|
|
|
|
*
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
public static $wrap = 'info';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new resource instance.
|
|
|
|
*
|
|
|
|
* @param mixed $resource
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct($resource)
|
|
|
|
{
|
|
|
|
$this->resource = $resource;
|
|
|
|
$password_reset = null;
|
2024-03-29 09:42:54 +01:00
|
|
|
|
2024-01-29 08:53:46 +01:00
|
|
|
// Password reset token
|
|
|
|
$resetToken = DB::table(config('auth.passwords.users.table'))->where(
|
|
|
|
'email', $this->resource->getEmailForPasswordReset()
|
|
|
|
)->first();
|
|
|
|
|
|
|
|
if ($resetToken) {
|
|
|
|
$password_reset = $this->tokenExpired($resetToken->created_at)
|
|
|
|
? 0
|
|
|
|
: $resetToken->created_at;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Personal Access Tokens (PATs)
|
|
|
|
$tokenRepository = App::make(TokenRepository::class);
|
2024-03-29 09:42:54 +01:00
|
|
|
$tokens = $tokenRepository->forUser($this->resource->getAuthIdentifier());
|
2024-01-29 08:53:46 +01:00
|
|
|
|
|
|
|
$PATs_count = $tokens->load('client')->filter(function ($token) {
|
2024-04-20 19:03:44 +02:00
|
|
|
return $token->client->personal_access_client && ! $token->revoked; /** @phpstan-ignore-line */
|
2024-01-29 08:53:46 +01:00
|
|
|
})->count();
|
|
|
|
|
|
|
|
$this->with = [
|
|
|
|
'password_reset' => $password_reset,
|
|
|
|
'valid_personal_access_tokens' => $PATs_count,
|
2024-03-29 09:42:54 +01:00
|
|
|
'webauthn_credentials' => $this->resource->webAuthnCredentials()->count(),
|
2024-01-29 08:53:46 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the token has expired.
|
|
|
|
*
|
|
|
|
* @param string $createdAt
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function tokenExpired($createdAt)
|
|
|
|
{
|
|
|
|
// See Illuminate\Auth\Passwords\DatabaseTokenRepository
|
2024-03-29 09:42:54 +01:00
|
|
|
return Carbon::parse($createdAt)->addSeconds(config('auth.passwords.users.expires', 60) * 60)->isPast();
|
2024-01-29 08:53:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform the resource into an array.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function toArray($request)
|
|
|
|
{
|
2024-06-26 14:32:18 +02:00
|
|
|
$tz = $request->user()?->preferences['timezone'] ?? config('app.timezone');
|
2024-04-25 16:56:35 +02:00
|
|
|
|
2024-01-29 08:53:46 +01:00
|
|
|
return array_merge(
|
|
|
|
parent::toArray($request),
|
|
|
|
[
|
|
|
|
'twofaccounts_count' => is_null($this->twofaccounts_count) ? 0 : $this->twofaccounts_count,
|
2024-04-25 16:56:35 +02:00
|
|
|
'last_seen_at' => Carbon::parse($this->last_seen_at)->tz($tz)->locale(App::getLocale())->diffForHumans(),
|
|
|
|
'created_at' => Carbon::parse($this->created_at)->tz($tz)->locale(App::getLocale())->diffForHumans(),
|
2024-01-29 08:53:46 +01:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|