This commit is contained in:
2019-12-26 14:13:01 -03:00
parent 65b42beb2b
commit 2799988084
2 changed files with 190 additions and 0 deletions

27
app/Contract/Auth.php Normal file
View File

@ -0,0 +1,27 @@
<?php
namespace App\Contract;
use App\Definition\Contract;
use App\Service\Auth as AuthService;
class Auth
{
use Contract;
protected static function newInstance()
{
return new AuthService();
}
public static function __callStatic($name, $params)
{
if (!method_exists(Response::class, $name)) {
$instance = self::getInstance();
if (method_exists($instance, $name)) {
return call_user_func_array([$instance, $name], $params);
}
return null;
}
return call_user_func_array([self, $name], $params);
}
}
?>

163
app/Service/Auth.php Normal file
View File

@ -0,0 +1,163 @@
<?php
namespace App\Service;
class Auth
{
protected $selector;
protected $token;
public function __construct()
{
$this->getCookie();
}
protected function getCookie()
{
if (isset($_COOKIE['rememberMe'])) {
list($s, $t) = \explode(':', $_COOKIE['rememberMe']);
$this->selector = $s;
$this->token = $t;
}
}
protected function saveCookie()
{
$now = \Carbon\Carbon::now(config('app.timezone'));
$exp = $now->addHours(config('app.login_hours'));
\setcookie('rememberMe', $this->selector . ':' . $this->token, $exp->timestamp);
}
protected function clearCookie()
{
\setcookie('rememberMe', '', \Carbon\Carbon::now(config('app.timezone'))->timestamp);
}
protected function generateToken()
{
$this->selector = bin2hex(\random_bytes(12));
$this->token = bin2hex(\random_bytes(20));
}
public function login($username, $password)
{
$user = \Model::factory(\Incoviba\common\User::class)->where('name', $username)->findOne();
if ($user !== false) {
if (\password_verify($password, $user->password) === false) {
$this->clearCookie();
return false;
}
$this->generateToken();
$now = \Carbon\Carbon::now(config('app.timezone'));
$exp = $now->addHours(-config('app.login_hours'));
$auth = \Model::factory(\Incoviba\common\Auth::class)->where('user_id', $user->id)->whereGt('time', $exp->timestamp)->where('status', 1)->findOne();
if ($auth !== false) {
$auth->time('now');
$auth->selector = $this->selector;
$auth->token($this->token);
$auth->save();
$this->saveCookie();
return true;
}
$auth = \Model::factory(\Incoviba\common\Auth::class)->create();
$auth->user_id = $user->id;
$auth->time('now');
$auth->selector = $this->selector;
$auth->token($this->token);
try {
$auth->save();
$this->saveCookie();
return true;
} catch (\Exception $e) {
$this->clearCookie();
return false;
}
}
return false;
}
public function isIn()
{
if ($this->selector == null) {
return false;
}
$auths = \Model::factory(\Incoviba\common\Auth::class)->where('selector', $this->selector)->findMany();
if ($auths === false) {
$this->clearCookie();
return false;
}
foreach ($auths as $auth) {
if (\password_verify($this->token, $auth->token)) {
return $auth->isIn();
}
}
return false;
}
public function User()
{
if ($this->selector == null) {
return false;
}
$auths = \Model::factory(\Incoviba\common\Auth::class)->where('selector', $this->selector)->findMany();
if ($auths === false) {
return false;
}
foreach ($auths as $auth) {
if (\password_verify($this->token, $auth->token)) {
return $auth->user();
}
}
return false;
}
public function hasAccess()
{
if ($this->selector == null) {
return false;
}
$auths = \Model::factory(\Incoviba\common\Auth::class)->where('selector', $this->selector)->findMany();
if ($auths === false) {
return false;
}
foreach ($auths as $auth) {
if (\password_verify($this->token, $auth->token)) {
return $auth->user()->hasAccess();
}
}
return false;
}
public function checkAccess($controller, $action = null)
{
if ($this->selector == null) {
return false;
}
$auths = \Model::factory(\Incoviba\common\Auth::class)->where('selector', $this->selector)->findMany();
if ($auths === false) {
return false;
}
foreach ($auths as $auth) {
if (\password_verify($this->token, $auth->token)) {
return $auth->user()->checkAccess($controller, $action);
}
}
return false;
}
public function logout()
{
$this->clearCookie();
if ($this->selector == null) {
return true;
}
$auths = \Model::factory(\Incoviba\common\Auth::class)->where('selector', $this->selector)->findMany();
if ($auths === false) {
return true;
}
foreach ($auths as $auth) {
if (\password_verify($this->token, $auth->token)) {
$auth->status = 0;
try {
$auth->save();
return true;
} catch (\Exception $e) {
return false;
}
}
}
return true;
}
}