Skip to content

Commit a49aeb0

Browse files
committed
First commit.
1 parent c32b076 commit a49aeb0

File tree

8 files changed

+183
-0
lines changed

8 files changed

+183
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ data/DoctrineORMModule/cache/
2222
# Legacy ZF1
2323
demos/
2424
extras/documentation
25+
.idea

src/Acl/Auth/Authorizator.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Zend\Mvc\Acl\Auth;
4+
5+
use Zend\Db\Acl\Custom\AuthorizatorInterface;
6+
use Zend\Http\Request;
7+
use Zend\Mvc\Acl\Enum\ServiceEnum;
8+
use Zend\Mvc\Acl\Exceptions\AuthException;
9+
use Zend\ServiceManager\ServiceLocatorInterface;
10+
use Zend\ServiceManager\ServiceManager;
11+
12+
/**
13+
* Authorizator
14+
*
15+
* @package Zend\Mvc\Acl\Auth
16+
*/
17+
class Authorizator
18+
{
19+
/**
20+
* @var array
21+
*/
22+
private $whitelist = [];
23+
24+
/**
25+
* @var ServiceManager
26+
*/
27+
private $serviceManager;
28+
29+
/**
30+
* Authorizator constructor.
31+
*
32+
* @param array $config
33+
* @param ServiceLocatorInterface $serviceManager
34+
*/
35+
public function __construct(array $config, ServiceLocatorInterface $serviceManager)
36+
{
37+
$this->serviceManager = $serviceManager;
38+
39+
if (isset($config['router']) && isset($config['router']['whitelist'])) {
40+
$this->whitelist = $config['router']['whitelist'];
41+
}
42+
43+
}
44+
45+
/**
46+
* @param Request $request
47+
* @return bool
48+
* @throws AuthException
49+
*/
50+
public function authorize(Request $request): bool
51+
{
52+
if (in_array($request->getUriString(), $this->whitelist)) {
53+
return true;
54+
}
55+
56+
if (!$this->serviceManager->has(ServiceEnum::AUTHORIZATOR)) {
57+
throw new AuthException('Authorization service not defined.');
58+
}
59+
60+
/** @var AuthorizatorInterface $authorizator */
61+
$authorizator = $this->serviceManager->get(ServiceEnum::AUTHORIZATOR);
62+
$authorizator->authorize($request);
63+
}
64+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Zend\Db\Acl\Custom;
4+
5+
use Zend\Http\Request;
6+
7+
interface AuthorizatorInterface
8+
{
9+
public function authorize(Request $request);
10+
}

src/Acl/Enum/ServiceEnum.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Zend\Mvc\Acl\Enum;
4+
5+
/**
6+
* ServiceEnum
7+
*
8+
* @package Zend\Mvc\Acl\Enum
9+
*/
10+
interface ServiceEnum
11+
{
12+
const AUTHORIZATOR = 'Zend\Mvc\Acl\Custom\AuthorizatorInterface';
13+
}

src/Acl/Exceptions/AuthException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Zend\Mvc\Acl\Exceptions;
4+
5+
/**
6+
* AuthException
7+
*
8+
* @package Zend\Mvc\Acl\Exceptions
9+
*/
10+
class AuthException extends \Exception { }

src/Acl/Interfaces/AuthInterface.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Zend\Mvc\Acl\Interfaces;
4+
5+
use Zend\Stdlib\Request;
6+
7+
/**
8+
* AuthInterface
9+
*
10+
* @package Zend\Mvc\Acl\Interfaces
11+
*/
12+
interface AuthInterface
13+
{
14+
/**
15+
* @param Request $request
16+
*/
17+
public function validate(Request $request): void;
18+
}

src/Acl/Module/AbstractModule.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Zend\Mvc\Acl\Module;
4+
5+
use Zend\ModuleManager\ModuleManager;
6+
use Zend\Mvc\Acl\Auth\Authorizator;
7+
use Zend\Mvc\MvcEvent;
8+
9+
/**
10+
* AbstractModule
11+
*
12+
* @package Zend\Mvc\Acl\Module
13+
*/
14+
abstract class AbstractModule implements ModuleInterface
15+
{
16+
/**
17+
* @param ModuleManager $manager
18+
*/
19+
public function init(ModuleManager $manager): void
20+
{
21+
$eventManager = $manager->getEventManager();
22+
$sharedEventManager = $eventManager->getSharedManager();
23+
$sharedEventManager->attach(__NAMESPACE__, 'dispatch', [$this, 'onDispatch'], 100);
24+
}
25+
26+
/**
27+
* @param MvcEvent $event
28+
* @throws \Zend\Mvc\Acl\Exceptions\AuthException
29+
*/
30+
public function onDispatch(MvcEvent $event): void
31+
{
32+
$config = $this->getConfig();
33+
$serviceManager = $event->getApplication()->getServiceManager();
34+
$request = $event->getRequest();
35+
36+
$authorizator = new Authorizator($config, $serviceManager);
37+
$authorizator->authorize($request);
38+
}
39+
}

src/Acl/Module/ModuleInterface.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Zend\Mvc\Acl\Module;
4+
use Zend\ModuleManager\ModuleManager;
5+
use Zend\Mvc\MvcEvent;
6+
7+
/**
8+
* ModuleInterface
9+
*
10+
* @package Zend\Mvc\Acl\Module
11+
*/
12+
interface ModuleInterface
13+
{
14+
/**
15+
* @return array
16+
*/
17+
public function getConfig(): array;
18+
19+
/**
20+
* @param ModuleManager $manager
21+
*/
22+
public function init(ModuleManager $manager): void;
23+
24+
/**
25+
* @param MvcEvent $event
26+
*/
27+
public function onDispatch(MvcEvent $event): void;
28+
}

0 commit comments

Comments
 (0)