1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-27 13:00:23 +08:00
2020-06-02 18:54:42 +08:00

73 lines
1.2 KiB
PHP

<?php
namespace App\Traits;
use App\Caches\User as UserCache;
use App\Models\User as UserModel;
use App\Services\Auth as AuthService;
use App\Validators\Validator as AppValidator;
use Phalcon\Di;
trait Auth
{
/**
* @return UserModel
*/
public function getCurrentUser()
{
$authUser = $this->getAuthUser();
if (!$authUser) {
return $this->getGuestUser();
}
$userCache = new UserCache();
return $userCache->get($authUser['id']);
}
/**
* @return UserModel
*/
public function getLoginUser()
{
$authUser = $this->getAuthUser();
$validator = new AppValidator();
$validator->checkAuthUser($authUser);
$userCache = new UserCache();
return $userCache->get($authUser['id']);
}
/**
* @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();
}
}