From 0d43b2283580021b5854f1498c1a083fe40e1f16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rald=20Plusquellec?= Date: Fri, 16 Jun 2017 12:40:19 +0200 Subject: [PATCH] auth --- lib/authentication.php | 20 ++++----- lib/cache.php | 9 ++++- lib/cacheredis.php | 1 - lib/lib.php | 92 +++++++++++++++++++----------------------- lib/router.php | 18 +++++---- 5 files changed, 69 insertions(+), 71 deletions(-) diff --git a/lib/authentication.php b/lib/authentication.php index 6596154..21a1098 100644 --- a/lib/authentication.php +++ b/lib/authentication.php @@ -7,12 +7,12 @@ class Authentication protected static function called() { - return maker(get_called_class()); + return actual('auth.class', maker(get_called_class())); } - public static function get($default = null) + public static function get($default = null, $class = null) { - $class = static::called(); + $class = $class ?: static::called(); if (session_id()) { $user = session($class->ns) @@ -37,7 +37,7 @@ public static function make($user = null) { $class = static::called(); - $user = $user ?: static::get($user, $class->ns); + $user = $user ?: static::get($user, $class); if ($user) { $user = !is_array($user) ? $user->toArray() : $user; @@ -53,14 +53,14 @@ public static function is() { $class = static::called(); - return 'octodummy' !== static::get('octodummy', $class->ns); + return 'octodummy' !== static::get('octodummy', $class); } public static function guest() { $class = static::called(); - return 'octodummy' === static::get('octodummy', $class->ns); + return 'octodummy' === static::get('octodummy', $class); } public static function login($user) @@ -91,7 +91,7 @@ public static function id() { $class = static::called(); - $user = static::get(null, $class->ns); + $user = static::get(null, $class); if ($user) { return $user['id']; @@ -104,7 +104,7 @@ public static function email() { $class = static::called(); - $user = static::get(null, $class->ns); + $user = static::get(null, $class); if ($user) { return isAke($user, 'email', null); @@ -117,7 +117,7 @@ public static function user($model = true) { $class = static::called(); - $user = static::get(null, $class->ns); + $user = static::get(null, $class); if ($user && $model) { return em($class->entity)->find((int) $user['id']); @@ -130,6 +130,6 @@ public static function __callStatic($m, $a) { $class = static::called(); - return call_user_func_array([auth($class->ns, $class->entity), $m], $a); + return call_user_func_array([guard($class->ns, $class->entity), $m], $a); } } diff --git a/lib/cache.php b/lib/cache.php index 5ef6e5d..6d469a9 100644 --- a/lib/cache.php +++ b/lib/cache.php @@ -224,6 +224,11 @@ public function replace($key, $value, $expire = null) return false; } + public function at($k, $v, $timestamp) + { + return $this->setExpireAt($k, $v, $timestamp); + } + public function setExpireAt($k, $v, $timestamp) { $file = $this->getPath($k); @@ -258,7 +263,7 @@ public function expireAt($k, $timestamp) { $v = $this->get($k); - return $this->set($k, $v, $timestamp); + return $this->setExpireAt($k, $v, $timestamp); } public function get($k, $d = null) @@ -269,7 +274,7 @@ public function get($k, $d = null) $age = filemtime($file); if ($age >= time()) { - return File::value(unserialize(File::read($file))); + return value(unserialize(File::read($file))); } else { File::delete($file); } diff --git a/lib/cacheredis.php b/lib/cacheredis.php index 2a741f8..65cc8cc 100644 --- a/lib/cacheredis.php +++ b/lib/cacheredis.php @@ -25,7 +25,6 @@ public function pull($key, $default = null) public function __construct($ns = 'core') { - $this->dir = $ns; $this->id = sha1('redis' . $ns); diff --git a/lib/lib.php b/lib/lib.php index b491fa9..9b12349 100755 --- a/lib/lib.php +++ b/lib/lib.php @@ -5054,6 +5054,8 @@ function actual() $actuals[$key] = $value; Registry::set('core.actuals', $actuals); + + return $value; } function fire($event, array $args = []) @@ -6246,89 +6248,77 @@ function guard($ns = 'web', $em = 'user') { $class = o(); - $class->macro('policy', function ($policy, callable $callable) use ($class) { - $policies = Registry::get('guard.policies', []); - $policies[$policy] = $callable; - - Registry::set('guard.policies', $policies); - - return $class; - }); - - $class->macro('login', function ($user) use ($ns) { + $class->macro('login', function ($user) use ($class) { $user = !is_array($user) ? $user->toArray() : $user; - session($ns)->setUser($user); + return $class->reveal()->login($user); }); - $class->macro('logout', function () use ($ns) { - session($ns)->erase('user'); + $class->macro('logout', function () use ($class) { + return $class->reveal()->logout(); }); - $class->macro('id', function () use ($ns) { - $user = session($ns)->getUser(); - - if ($user) { - return $user['id']; - } - - return null; + $class->macro('id', function () use ($class) { + return $class->reveal()->id(); }); - $class->macro('email', function () use ($ns) { - $user = session($ns)->getUser(); - - if ($user) { - return isAke($user, 'email', null); - } - - return null; + $class->macro('email', function () use ($class) { + return $class->reveal()->email(); }); $class->macro('on', function () use ($class) { return call_user_func_array([$class, 'policy'], func_get_args()); }); - $class->macro('user', function ($model = true) use ($ns, $em) { - $user = session($ns)->getUser(); - - if ($user && $model) { - return em($em)->find((int) $user['id']); - } - - return $user; + $class->macro('user', function ($model = true) use ($class) { + return $class->reveal()->user($model); }); - $class->macro('logWithId', function ($id, $route = 'home') use ($ns, $em) { - $user = em($em)->find((int) $id); + $class->macro('logWithId', function ($id, $route = 'home') use ($class) { + $auth = $class->reveal(); + $user = em($auth->entity)->find((int) $id); if ($user) { - session($ns)->setUser($user->toArray()); + $auth->login($user); go(urlFor($route)); } else { - ptption('guard', "Unknown id."); + exception('guard', "Unknown id."); } }); - $class->macro('logByUser', function ($user, $route = 'home') use ($ns) { + $class->macro('logByUser', function ($user, $route = 'home') use ($class) { $user = !is_array($user) ? $user->toArray() : $user; - session($ns)->setUser($user); + + $class->reveal()->login($user); + go(urlFor($route)); }); - $class->macro('allows', function () use ($ns) { - $user = session($ns)->getUser(); + $class->macro('reveal', function () { + $auth = actual('auth.class'); - if ($user) { - $user = item($user); + return is_object($auth) ? $auth : new Auth; + }); - $args = func_get_args(); + $class->macro('policy', function ($policy, callable $callable) use ($class) { + $policies = Registry::get('guard.policies', []); + $policies[$policy] = $callable; - $policy = array_shift($args); + Registry::set('guard.policies', $policies); - $policies = Registry::get('guard.policies', []); + return $class; + }); - $policy = isAke($policies, $policy, null); + $class->macro('allows', function () use ($class) { + $auth = $class->reveal(); + $user = $auth->user(); + + if ($user) { + $user = item($user); + $args = func_get_args(); + $policy = array_shift($args); + $policies = Registry::get('guard.policies', []); + $policy = isAke($policies, $policy, null); if (is_callable($policy)) { return call_user_func_array($policy, array_merge([$user], $args)); diff --git a/lib/router.php b/lib/router.php index ffb99d5..676365e 100644 --- a/lib/router.php +++ b/lib/router.php @@ -400,13 +400,17 @@ private function params($route, $params) $params[$p] = maker($cn, [$val]); } } else { - $dv = $parameter->getDefaultValue(); - - if (fnmatch('*Entity', $dv)) { - $cp = maker($dv); - $id = $params[$p]; - $params[$p] = $cp->find((int) $id); - } + try { + $dv = $parameter->getDefaultValue(); + + if (fnmatch('*Entity', $dv)) { + $cp = maker($dv); + $id = $params[$p]; + $params[$p] = $cp->find((int) $id); + } else { + $params[$p] = $dv; + } + } catch (\Exception $e) {} } $p++;