From e4999717fe97b2840a9c2323dae00cd877b22c8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rald=20Plusquellec?= Date: Mon, 12 Jun 2017 15:50:16 +0200 Subject: [PATCH] authentication --- lib/admin.php | 7 +-- lib/auth.php | 103 +------------------------------ lib/authentication.php | 135 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 107 deletions(-) create mode 100644 lib/authentication.php diff --git a/lib/admin.php b/lib/admin.php index 9bf1b91..a3283e7 100644 --- a/lib/admin.php +++ b/lib/admin.php @@ -1,10 +1,7 @@ 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 {} diff --git a/lib/authentication.php b/lib/authentication.php new file mode 100644 index 0000000..6596154 --- /dev/null +++ b/lib/authentication.php @@ -0,0 +1,135 @@ +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); + } + }