mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-29 13:51:37 +08:00
64 lines
1.1 KiB
PHP
64 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Traits;
|
|
|
|
use App\Models\User as UserModel;
|
|
use App\Repos\User as UserRepo;
|
|
use App\Services\AuthUser as AuthUserService;
|
|
use App\Validators\Validator as AppValidator;
|
|
|
|
trait Auth
|
|
{
|
|
|
|
public function getCurrentUser()
|
|
{
|
|
$authUser = $this->getAuthUser();
|
|
|
|
if (!$authUser) {
|
|
return $this->getGuestUser();
|
|
}
|
|
|
|
$userRepo = new UserRepo();
|
|
|
|
$user = $userRepo->findById($authUser->id);
|
|
|
|
return $user;
|
|
}
|
|
|
|
public function getLoginUser()
|
|
{
|
|
$authUser = $this->getAuthUser();
|
|
|
|
$validator = new AppValidator();
|
|
|
|
$validator->checkAuthUser($authUser);
|
|
|
|
$userRepo = new UserRepo();
|
|
|
|
$user = $userRepo->findById($authUser->id);
|
|
|
|
return $user;
|
|
}
|
|
|
|
public function getGuestUser()
|
|
{
|
|
$user = new UserModel();
|
|
|
|
$user->id = 0;
|
|
$user->name = 'guest';
|
|
|
|
return $user;
|
|
}
|
|
|
|
public function getAuthUser()
|
|
{
|
|
/**
|
|
* @var AuthUserService $auth
|
|
*/
|
|
$auth = $this->getDI()->get('auth');
|
|
|
|
return $auth->getAuthInfo();
|
|
}
|
|
|
|
}
|