Skip to content

Commit

Permalink
auth
Browse files Browse the repository at this point in the history
  • Loading branch information
schpill committed Jun 16, 2017
1 parent 919b234 commit 0d43b22
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 71 deletions.
20 changes: 10 additions & 10 deletions lib/authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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'];
Expand All @@ -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);
Expand All @@ -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']);
Expand All @@ -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);
}
}
9 changes: 7 additions & 2 deletions lib/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion lib/cacheredis.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function pull($key, $default = null)

public function __construct($ns = 'core')
{

$this->dir = $ns;

$this->id = sha1('redis' . $ns);
Expand Down
92 changes: 41 additions & 51 deletions lib/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -5054,6 +5054,8 @@ function actual()
$actuals[$key] = $value;

Registry::set('core.actuals', $actuals);

return $value;
}

function fire($event, array $args = [])
Expand Down Expand Up @@ -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));
Expand Down
18 changes: 11 additions & 7 deletions lib/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down

0 comments on commit 0d43b22

Please sign in to comment.