1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-07-02 23:16:49 +08:00
xiaochong0302 43adee9ceb 1.重写view provider,数组赋值自动转对象
2.重写response provider,优化json输出格式
2020-04-25 18:36:32 +08:00

73 lines
1.2 KiB
PHP

<?php
namespace App\Traits;
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();
}
$userRepo = new UserRepo();
return $userRepo->findById($authUser['id']);
}
/**
* @return UserModel
*/
public function getLoginUser()
{
$authUser = $this->getAuthUser();
$validator = new AppValidator();
$validator->checkAuthUser($authUser);
$userRepo = new UserRepo();
return $userRepo->findById($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();
}
}