-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
138 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
<?php | ||
namespace Octo; | ||
|
||
class Admin | ||
class Admin extends Authentication | ||
{ | ||
public static function __callStatic($m, $a) | ||
{ | ||
return call_user_func_array([auth('admin', 'adminUser'), $m], $a); | ||
} | ||
protected $ns = 'admin', $actual = 'admin.user', $entity = 'adminUser'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,4 @@ | ||
<?php | ||
namespace Octo; | ||
|
||
class Auth | ||
{ | ||
public static function get($default = null, $ns = 'web') | ||
{ | ||
if (session_id()) { | ||
$user = session($ns)->getUser(actual('auth.user')); | ||
} else { | ||
$user = actual('auth.user'); | ||
} | ||
|
||
if ($user) { | ||
$user = !is_array($user) ? $user->toArray() : $user; | ||
actual('auth.user', $user); | ||
|
||
return $user; | ||
} | ||
|
||
return $default; | ||
} | ||
|
||
public static function make($user = null, $ns = 'web') | ||
{ | ||
$user = $user ?: static::get($user, $ns); | ||
|
||
if ($user) { | ||
$user = !is_array($user) ? $user->toArray() : $user; | ||
actual('auth.user', $user); | ||
|
||
if (session_id()) { | ||
session($ns)->setUser($user); | ||
} | ||
} | ||
} | ||
|
||
public static function is($ns = 'web') | ||
{ | ||
return 'octodummy' !== static::get('octodummy', $ns); | ||
} | ||
|
||
public static function guest($ns = 'web') | ||
{ | ||
return 'octodummy' === static::get('octodummy', $ns); | ||
} | ||
|
||
public static function login($user, $ns = 'web') | ||
{ | ||
$user = !is_array($user) ? $user->toArray() : $user; | ||
|
||
actual('auth.user', $user); | ||
|
||
if (session_id()) { | ||
session($ns)->setUser($user); | ||
} | ||
} | ||
|
||
public static function logout($ns = 'web') | ||
{ | ||
if (session_id()) { | ||
session($ns)->erase('user'); | ||
} | ||
|
||
actual('auth.user', null); | ||
} | ||
|
||
public static function id($ns = 'web') | ||
{ | ||
$user = static::get(null, $ns); | ||
|
||
if ($user) { | ||
return $user['id']; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static function email($ns = 'web') | ||
{ | ||
$user = static::get(null, $ns); | ||
|
||
if ($user) { | ||
return isAke($user, 'email', null); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static function user($model = true, $ns = 'web', $em = 'user') | ||
{ | ||
$user = static::get(null, $ns); | ||
|
||
if ($user && $model) { | ||
return em($em)->find((int) $user['id']); | ||
} | ||
|
||
return $user; | ||
} | ||
|
||
public static function __callStatic($m, $a) | ||
{ | ||
return call_user_func_array([auth(), $m], $a); | ||
} | ||
} | ||
class Auth extends Authentication {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
namespace Octo; | ||
|
||
class Authentication | ||
{ | ||
protected $ns = 'web', $actual = 'auth.user', $entity = 'user'; | ||
|
||
protected static function called() | ||
{ | ||
return maker(get_called_class()); | ||
} | ||
|
||
public static function get($default = null) | ||
{ | ||
$class = static::called(); | ||
|
||
if (session_id()) { | ||
$user = session($class->ns) | ||
->getUser( | ||
actual($class->actual) | ||
); | ||
} else { | ||
$user = actual($class->actual); | ||
} | ||
|
||
if ($user) { | ||
$user = !is_array($user) ? $user->toArray() : $user; | ||
actual($class->actual, $user); | ||
|
||
return $user; | ||
} | ||
|
||
return $default; | ||
} | ||
|
||
public static function make($user = null) | ||
{ | ||
$class = static::called(); | ||
|
||
$user = $user ?: static::get($user, $class->ns); | ||
|
||
if ($user) { | ||
$user = !is_array($user) ? $user->toArray() : $user; | ||
actual($class->actual, $user); | ||
|
||
if (session_id()) { | ||
session($class->ns)->setUser($user); | ||
} | ||
} | ||
} | ||
|
||
public static function is() | ||
{ | ||
$class = static::called(); | ||
|
||
return 'octodummy' !== static::get('octodummy', $class->ns); | ||
} | ||
|
||
public static function guest() | ||
{ | ||
$class = static::called(); | ||
|
||
return 'octodummy' === static::get('octodummy', $class->ns); | ||
} | ||
|
||
public static function login($user) | ||
{ | ||
$class = static::called(); | ||
|
||
$user = !is_array($user) ? $user->toArray() : $user; | ||
|
||
actual($class->actual, $user); | ||
|
||
if (session_id()) { | ||
session($class->ns)->setUser($user); | ||
} | ||
} | ||
|
||
public static function logout() | ||
{ | ||
$class = static::called(); | ||
|
||
if (session_id()) { | ||
session($class->ns)->erase('user'); | ||
} | ||
|
||
actual($class->actual, null); | ||
} | ||
|
||
public static function id() | ||
{ | ||
$class = static::called(); | ||
|
||
$user = static::get(null, $class->ns); | ||
|
||
if ($user) { | ||
return $user['id']; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static function email() | ||
{ | ||
$class = static::called(); | ||
|
||
$user = static::get(null, $class->ns); | ||
|
||
if ($user) { | ||
return isAke($user, 'email', null); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static function user($model = true) | ||
{ | ||
$class = static::called(); | ||
|
||
$user = static::get(null, $class->ns); | ||
|
||
if ($user && $model) { | ||
return em($class->entity)->find((int) $user['id']); | ||
} | ||
|
||
return $user; | ||
} | ||
|
||
public static function __callStatic($m, $a) | ||
{ | ||
$class = static::called(); | ||
|
||
return call_user_func_array([auth($class->ns, $class->entity), $m], $a); | ||
} | ||
} |