PHP code example of secgin / phalcon-auth-plugin
1. Go to this page and download the library: Download secgin/phalcon-auth-plugin library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
secgin / phalcon-auth-plugin example snippets
$di->register(new AuthProvider());
class AuthDataService implements AuthDataServiceInterface
{
private array $permissions = [
'100' => 7,
'101' => 9,
'user' => [
'100' => 9,
'101' => 9
],
'customer' => [
'100' => 9,
'101' => 3
]
];
private array $allowedIpAddresses = [
'127.0.0.1',
'::1',
'192.168.1.42'
];
public function getPermissionLevel(string $permissionCode, ?string $moduleName = null): ?int
{
return $moduleName != ''
? $this->permissions[$moduleName][$permissionCode] ?? null
: $this->permissions[$permissionCode] ?? null;
}
public function isIpAddressAllowed(string $ipAddress): bool
{
return in_array($ipAddress, $this->allowedIpAddresses);
}
}
$container->setShared('authDataService', AuthDataService::class);
class DispatcherEventHandler extends Injectable
{
public function beforeExecuteRoute(Event $event, Dispatcher $dispatcher): bool
{
$result = $this->auth->hasAllowed(
$dispatcher->getControllerClass(),
$dispatcher->getActiveMethod(),
$dispatcher->getModuleName());
switch ((string)$result)
{
case AuthInterface::NOT_LOGGED_IN:
$this->response
->redirect('user');
return false;
case AuthInterface::NOT_ALLOWED_IP_ADDRESS:
$this->response
->redirect('user/ip');
return false;
case AuthInterface::NOT_ALLOWED_RESOURCE:
$dispatcher->forward([
'controller' => 'error',
'action' => 'show401'
]);
return false;
}
return true;
}
}
try
{
$auth->login([
'id' => '1',
'usernamea' => 'admin',
'name' => 'Admin',
]);
}
catch (LoginRequiredFieldException $e)
{
echo $e->getMessage();
}
catch (Exception $e)
{
echo 'Giriş yapılmadı.';
}
/**
* @Private(100)
*/
class UserController extends Controller
{
/**
* @Private(3, 100)
*/
public function listAction()
{
}
/**
* @Private(5)
*/
public function newAction()
{
}
/**
* @Private(7)
*/
public function editAction()
{
}
/**
* @Private(9)
*/
public function deleteAction()
{
}
}
new Config([
'application' => [
'cacheDir' => BASE_PATH . '/var/cache/',
],
'auth' => [
'useAllowedIpAddress' => true,
'defaultAction' => 0,
'allowMultipleLogin' => true
]
]);