Skip to content

Commit

Permalink
custom guards
Browse files Browse the repository at this point in the history
  • Loading branch information
sinbadxiii committed Sep 9, 2021
1 parent 37f8e3c commit df54be9
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ $di->setShared('auth', function () {
});
```

or
or

```php
declare(strict_types=1);
Expand Down
1 change: 0 additions & 1 deletion config/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
'guard' => 'web',
'passwords' => 'users',
],

'guards' => [
'web' => [
'driver' => 'session',
Expand Down
22 changes: 21 additions & 1 deletion src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Sinbadxiii\PhalconAuth;

use Closure;
use InvalidArgumentException;
use Phalcon\Config;
use Phalcon\Di;

/**
Expand All @@ -14,6 +16,7 @@
class Auth
{
protected $config;
protected $customGuards = [];
protected $guards = [];

public function __construct($config = null)
Expand All @@ -28,7 +31,7 @@ public function __construct($config = null)
* @param $name
* @return array
*/
protected function getConfigGuard(string $name): object
protected function getConfigGuard(string $name)
{
return $this->config->guards->{$name};
}
Expand All @@ -50,12 +53,17 @@ public function guard($name = null)
*/
protected function resolve($name)
{

$configGuard = $this->getConfigGuard($name);

if (is_null($configGuard)) {
throw new InvalidArgumentException("Auth guard [{$name}] is not defined.");
}

if (isset($this->customGuards[$configGuard['driver']])) {
return $this->callCustomGuard($name, $configGuard);
}

$className = sprintf("\\%s\\Guards\\%sGuard",
__NAMESPACE__,
ucfirst($configGuard->driver));
Expand Down Expand Up @@ -103,4 +111,16 @@ public function __call($method, $params)
{
return $this->guard()->{$method}(...$params);
}

public function extend($driver, Closure $callback)
{
$this->customGuards[$driver] = $callback;

return $this;
}

protected function callCustomGuard($name, Config $config)
{
return $this->customGuards[$config['driver']]($name, $config);
}
}
15 changes: 15 additions & 0 deletions src/Contracts/Guard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Sinbadxiii\PhalconAuth\Contracts;

use Sinbadxiii\PhalconAuth\User\AuthenticatableInterface;

interface Guard
{
public function check();
public function user();
public function setUser(AuthenticatableInterface $user);
public function id();
public function guest();
public function validate(array $credentials = []);
}
15 changes: 15 additions & 0 deletions src/Contracts/GuardStateful.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Sinbadxiii\PhalconAuth\Contracts;

use Sinbadxiii\PhalconAuth\User\AuthenticatableInterface;

interface GuardStateful
{
public function attempt(array $credentials = [], $remember = false);
public function login(AuthenticatableInterface $user, $remember = false);
public function loginById($id, $remember = false);
public function once(array $credentials = []);
public function viaRemember();
public function logout();
}
1 change: 0 additions & 1 deletion src/Events/AfterLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

class AfterLogin extends EventAbstract
{

}
1 change: 0 additions & 1 deletion src/Events/BeforeLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

class BeforeLogin extends EventAbstract
{

}
7 changes: 7 additions & 0 deletions src/Guards/GuardHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,11 @@ public function guest()
{
return ! $this->check();
}

public function authenticate()
{
if (!is_null($user = $this->user())) {
return $user;
}
}
}
6 changes: 4 additions & 2 deletions src/Guards/SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Sinbadxiii\PhalconAuth\Guards;

use Sinbadxiii\PhalconAuth\Events\BeforeLogin;
use Sinbadxiii\PhalconAuth\Contracts\GuardStateful;
use Sinbadxiii\PhalconAuth\Events\AfterLogin;
use Sinbadxiii\PhalconAuth\Events\BeforeLogin;
use Sinbadxiii\PhalconAuth\Events\Attempt;
use Sinbadxiii\PhalconAuth\Events\EventInterface;
use Sinbadxiii\PhalconAuth\Events\Logout;
use Sinbadxiii\PhalconAuth\User\AuthenticatableInterface;
Expand All @@ -15,7 +17,7 @@
* Class SessionGuard
* @package Sinbadxiii\PhalconAuth\Guards
*/
class SessionGuard
class SessionGuard implements GuardStateful
{
use GuardHelper;

Expand Down
3 changes: 2 additions & 1 deletion src/Guards/TokenGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sinbadxiii\PhalconAuth\Guards;

use Sinbadxiii\PhalconAuth\Contracts\Guard;
use Sinbadxiii\PhalconAuth\Events\EventInterface;
use Phalcon\Helper\Str;
use Phalcon\Di;
Expand All @@ -12,7 +13,7 @@
* Class TokenGuard
* @package Sinbadxiii\PhalconAuth\Guards
*/
class TokenGuard
class TokenGuard implements Guard
{
use GuardHelper;

Expand Down
30 changes: 16 additions & 14 deletions src/Middlewares/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@

use Phalcon\Di\Injectable;

class Authenticate extends Injectable
class Authenticate extends Injectable implements AuthenticatesRequest
{
/**
* @description custom controller property
* for disable auth check into controller
*/
protected $guest = false;
protected $dispatcher;

public function beforeExecuteRoute($event, $dispatcher)
{
if ($this->auth->check() || $this->isGuest()) {
$this->dispatcher = $dispatcher;

$this->authenticate();
}

/**
* @return bool
*/
protected function authenticate()
{
if ($this->auth->authenticate() || $this->isGuest()) {
return true;
}

Expand All @@ -31,14 +37,10 @@ protected function redirectTo()
//custom url
}

protected function setGuest($guest)
{
$this->guest = $guest;
}

protected function isGuest()
{
return $this->guest;
$controller = $this->dispatcher->getControllerClass();

return !(new $controller)->authAccess();
}
}

9 changes: 9 additions & 0 deletions src/Middlewares/AuthenticatesRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Sinbadxiii\PhalconAuth\Middlewares;

interface AuthenticatesRequest
{
//protected function authenticate();
//protected function unauthenticated();
}

0 comments on commit df54be9

Please sign in to comment.