mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-28 05:11:39 +08:00
90 lines
1.8 KiB
PHP
90 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Traits;
|
|
|
|
use App\Caches\User as UserCache;
|
|
use App\Exceptions\Unauthorized as UnauthorizedException;
|
|
use App\Models\User as UserModel;
|
|
use App\Repos\User as UserRepo;
|
|
use App\Services\Auth as AuthService;
|
|
use App\Validators\Validator as AppValidator;
|
|
use Phalcon\Di as Di;
|
|
|
|
trait Auth
|
|
{
|
|
|
|
/**
|
|
* @param bool $cache
|
|
* @return UserModel
|
|
*/
|
|
public function getCurrentUser($cache = false)
|
|
{
|
|
$authUser = $this->getAuthUser();
|
|
|
|
if (!$authUser) {
|
|
return $this->getGuestUser();
|
|
}
|
|
|
|
if ($cache == false) {
|
|
$userRepo = new UserRepo();
|
|
$user = $userRepo->findById($authUser['id']);
|
|
} else {
|
|
$userCache = new UserCache();
|
|
$user = $userCache->get($authUser['id']);
|
|
}
|
|
|
|
return $user;
|
|
}
|
|
|
|
/**
|
|
* @param bool $cache
|
|
* @return UserModel
|
|
* @throws UnauthorizedException
|
|
*/
|
|
public function getLoginUser($cache = false)
|
|
{
|
|
$authUser = $this->getAuthUser();
|
|
|
|
$validator = new AppValidator();
|
|
|
|
$validator->checkAuthUser($authUser['id']);
|
|
|
|
if ($cache == false) {
|
|
$userRepo = new UserRepo();
|
|
$user = $userRepo->findById($authUser['id']);
|
|
} else {
|
|
$userCache = new UserCache();
|
|
$user = $userCache->get($authUser['id']);
|
|
}
|
|
|
|
return $user;
|
|
}
|
|
|
|
/**
|
|
* @return UserModel
|
|
*/
|
|
public function getGuestUser()
|
|
{
|
|
$user = new UserModel();
|
|
|
|
$user->id = 0;
|
|
$user->name = 'guest';
|
|
|
|
return $user;
|
|
}
|
|
|
|
/**
|
|
* @return array|null
|
|
*/
|
|
public function getAuthUser()
|
|
{
|
|
/**
|
|
* @var AuthService $auth
|
|
*/
|
|
$auth = Di::getDefault()->get('auth');
|
|
|
|
return $auth->getAuthInfo();
|
|
}
|
|
|
|
}
|