Skip to content

Commit

Permalink
authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
schpill committed Jun 12, 2017
1 parent 1d4d802 commit e499971
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 107 deletions.
7 changes: 2 additions & 5 deletions lib/admin.php
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';
}
103 changes: 1 addition & 102 deletions lib/auth.php
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 {}
135 changes: 135 additions & 0 deletions lib/authentication.php
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);
}
}

0 comments on commit e499971

Please sign in to comment.