1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-07-05 00:11:30 +08:00
2020-05-31 20:09:08 +08:00

78 lines
1.3 KiB
PHP

<?php
namespace App\Traits;
use App\Caches\User as UserCache;
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;
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()
{
$userRepo = new UserRepo();
return $userRepo->findById(100015);
$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();
}
}