PHP code example of bwiechnik / domy-house-base
1. Go to this page and download the library: Download bwiechnik/domy-house-base 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/ */
bwiechnik / domy-house-base example snippets
$this->serviceManager()->get(\Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger::class);
$this->serviceManager()->get(\Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger::class);
namespace Application\Services\Auth;
class AuthenticationServiceFactory extends \Base\Services\Auth\AuthenticationServiceFactory
{
public function __invoke(\Interop\Container\ContainerInterface $container, $requestedName, array $options = null)
{
$service = parent::__invoke($container, $requestedName, $options);
$adapter = $service->getAdapter();
/* @var $adapter \Base\Services\Auth\AuthAdapter */
// nazwa modelu
$adapter->setModelName(\Application\Model\UsersTable::class);
$adapter->setRowPreConditions(['NOT ghost']);
$adapter->setRowPostConditions([
[
'condition' => ['is_locked' => false],
'message' => 'User is locked',
],
]);
$adapter->addCallable(\Base\Services\Auth\AuthAdapter::EVENT_LOGIN_SUCCESS, function(\Base\Services\Auth\AuthAdapter $adapter) {
$model = $adapter->getModel();
$row = $adapter->getUserRow();
$remoteAddress = new \Laminas\Http\PhpEnvironment\RemoteAddress();
$model->update([
'last_login_time' => new \Laminas\Db\Sql\Expression("NOW()"),
'last_login_ip_address' => $remoteAddress->getIpAddress(),
], [
'id' => $row->id,
]);
});
return $service;
}
}
$authManager = $this->getServiceManager()->get(\Base\Services\Auth\AuthManager::class);
/* @var $authManager \Base\Services\Auth\AuthManager */
namespace Application\Services\Rbac;
class RbacManagerFactory implements \Laminas\ServiceManager\Factory\FactoryInterface
{
public function __invoke(\Interop\Container\ContainerInterface $container, $requestedName, array $options = null)
{
$rbacManager = new \Base\Services\Rbac\RbacManager();
$rbacManager->setServiceManager($container);
$rbacRolesManager = new RbacRolesManager();
$rbacRolesManager->setServiceManager($container);
$rbacManager->setRolesManager($rbacRolesManager);
$assertionManager = $container->get(\Base\Services\Rbac\RbacAssertionManager::class);
/* @var $assertionManager \Base\Services\Rbac\RbacAssertionManager */
$assertionManager->setServiceManager($container);
$rbacManager->addAssertionManager($assertionManager);
return $rbacManager;
}
}
public function onBootstrap(\Laminas\Mvc\MvcEvent $event)
{
$application = $event->getApplication();
$eventManager = $application->getEventManager();
$eventManager->attach(\Laminas\Mvc\MvcEvent::EVENT_DISPATCH, [$this, 'onDispatch']);
}
public function onDispatch(\Laminas\Mvc\MvcEvent $event)
{
$application = $event->getApplication();
$serviceManager = $application->getServiceManager();
$controller = $event->getTarget();
$routeName = $event->getRouteMatch()->getMatchedRouteName();
$controllerName = $event->getRouteMatch()->getParam('controller', null);
$actionName = $event->getRouteMatch()->getParam('action', null);
$authManager = $serviceManager->get(\Base\Services\Auth\AuthManager::class);
/* @var $authManager \Base\Services\Auth\AuthManager */
// sprawdzenie czy użytkownik ma dostęp do zasobu
$result = $authManager->filterAccess($routeName, $actionName);
switch ($result) {
case \Base\Services\Auth\AuthManager::ACCESS_DENIED:
if ($routeName !== 'auth' || $actionName !== 'notauthorized') {
return $controller->redirect()->toRoute('auth', ['action' => 'notauthorized']);
}
break;
case \Base\Services\Auth\AuthManager::AUTH_REQUIRED:
if ($routeName !== 'auth' || $actionName !== 'login') {
$controller->flashMessenger()->addErrorMessage('Musisz się zalogować by uzyskać dostęp do tego zasobu');
return $controller->redirect()->toRoute('auth', ['action' => 'login']);
}
break;
}
}