Skip to content

Commit f9c2692

Browse files
committed
refactoring, typos
1 parent 6ceb2c2 commit f9c2692

14 files changed

+166
-63
lines changed

src/CurrentNamespaceProxy.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
namespace Librette\SecurityNamespaces;
3+
4+
use Nette\Object;
5+
6+
/**
7+
* @author David Matejka
8+
*/
9+
class CurrentNamespaceProxy extends Object implements ISecurityNamespace
10+
{
11+
12+
/** @var \Librette\SecurityNamespaces\INamespaceDetector */
13+
protected $namespaceDetector;
14+
15+
16+
/**
17+
* @param INamespaceDetector $namespaceDetector
18+
*/
19+
public function __construct(INamespaceDetector $namespaceDetector)
20+
{
21+
$this->namespaceDetector = $namespaceDetector;
22+
}
23+
24+
25+
public function getName()
26+
{
27+
return $this->namespaceDetector->getNamespace()->getName();
28+
}
29+
30+
31+
public function getAuthenticator()
32+
{
33+
return $this->namespaceDetector->getNamespace()->getAuthenticator();
34+
}
35+
36+
37+
public function hasAuthenticator()
38+
{
39+
return $this->namespaceDetector->getNamespace()->hasAuthenticator();
40+
}
41+
42+
43+
public function getAuthorizator()
44+
{
45+
return $this->namespaceDetector->getNamespace()->getAuthorizator();
46+
}
47+
48+
49+
public function hasAuthorizator()
50+
{
51+
return $this->namespaceDetector->getNamespace()->hasAuthorizator();
52+
}
53+
54+
55+
public function getIdentityInitializer()
56+
{
57+
return $this->namespaceDetector->getNamespace()->getIdentityInitializer();
58+
}
59+
60+
61+
public function hasIdentityInitializer()
62+
{
63+
return $this->namespaceDetector->getNamespace()->hasIdentityInitializer();
64+
}
65+
66+
}

src/DI/SecurityExtension.php renamed to src/DI/SecurityNamespacesExtension.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@
77
/**
88
* @author David Matejka
99
*/
10-
class SecurityExtension extends CompilerExtension
10+
class SecurityNamespacesExtension extends CompilerExtension
1111
{
1212

1313
const SECURITY_NAMESPACE_TAG = 'librette.security.namespace';
1414

1515

16-
1716
public function loadConfiguration()
1817
{
1918
$builder = $this->getContainerBuilder();
2019

2120
$builder->addDefinition($this->prefix('namespaceDetector'))
2221
->setClass('Librette\SecurityNamespaces\NamespaceDetector');
2322

23+
$builder->addDefinition($this->prefix('namespaceProxy'))
24+
->setClass('\Librette\SecurityNamespaces\CurrentNamespaceProxy');
25+
2426
$builder->addDefinition($this->prefix('authenticator'))
2527
->setClass('Librette\SecurityNamespaces\NamespaceAwareAuthenticator');
2628

@@ -59,6 +61,7 @@ public function beforeCompile()
5961
$namespaceManager = $builder->getDefinition($this->prefix('namespaceManager'));
6062
foreach ($builder->findByTag(self::SECURITY_NAMESPACE_TAG) as $name => $null) {
6163
$nsDefinition = $builder->getDefinition($name);
64+
$nsDefinition->setAutowired(FALSE);
6265
$this->normalizeNamespaceArguments($nsDefinition);
6366
$namespaceManager->addSetup('addNamespace', array($nsDefinition));
6467
}
@@ -73,9 +76,9 @@ public function beforeCompile()
7376
*/
7477
private function normalizeNamespaceArguments(ServiceDefinition $namespaceDefinition)
7578
{
76-
$args = $namespaceDefinition->factory->arguments + array(1 => NULL, NULL, $this->prefix('@dummyIdentityInitializer'));
79+
$args = $namespaceDefinition->getFactory()->arguments + array(1 => NULL, NULL, $this->prefix('@dummyIdentityInitializer'));
7780
ksort($args);
78-
$namespaceDefinition->factory->arguments = $args;
81+
$namespaceDefinition->getFactory()->arguments = $args;
7982
$this->disableAutowiring($args);
8083
}
8184

src/INamespaceDetector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface INamespaceDetector
99
{
1010

1111
/**
12-
* @return SecurityNamespace
12+
* @return SecurityNamespace current namespace
1313
* @throws InvalidStateException
1414
*/
1515
public function getNamespace();

src/ISecurityNamespace.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
namespace Librette\SecurityNamespaces;
3+
4+
use Librette\SecurityNamespaces\Identity\IIdentityInitializer;
5+
use Nette\Security\IAuthenticator;
6+
use Nette\Security\IAuthorizator;
7+
8+
9+
/**
10+
* @author David Matejka
11+
*/
12+
interface ISecurityNamespace
13+
{
14+
15+
/**
16+
* @return IAuthenticator
17+
* @throws InvalidStateException
18+
*/
19+
public function getAuthenticator();
20+
21+
22+
/**
23+
* @return string
24+
*/
25+
public function getName();
26+
27+
28+
/**
29+
* @return bool
30+
*/
31+
public function hasAuthenticator();
32+
33+
34+
/**
35+
* @return IAuthorizator
36+
* @throws InvalidStateException
37+
*/
38+
public function getAuthorizator();
39+
40+
41+
/**
42+
* @return bool
43+
*/
44+
public function hasAuthorizator();
45+
46+
47+
/**
48+
* @throws InvalidStateException
49+
* @return IIdentityInitializer
50+
*/
51+
public function getIdentityInitializer();
52+
53+
54+
/**
55+
* @return bool
56+
*/
57+
public function hasIdentityInitializer();
58+
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace Librette\SecurityNamespaces\Identity;
33

4-
use Librette\SecurityNamespaces\INamespaceDetector;
4+
use Librette\SecurityNamespaces\ISecurityNamespace;
55
use Nette\Object;
66
use Nette\Security\IIdentity;
77

@@ -11,22 +11,22 @@
1111
class NamespaceAwareIdentityInitializer extends Object implements IIdentityInitializer
1212
{
1313

14-
/** @var INamespaceDetector */
15-
protected $namespaceDetector;
14+
/** @var ISecurityNamespace */
15+
protected $namespace;
1616

1717

1818
/**
19-
* @param INamespaceDetector $namespaceDetector
19+
* @param ISecurityNamespace $namespace
2020
*/
21-
public function __construct(INamespaceDetector $namespaceDetector)
21+
public function __construct(ISecurityNamespace $namespace)
2222
{
23-
$this->namespaceDetector = $namespaceDetector;
23+
$this->namespace = $namespace;
2424
}
2525

2626

2727
public function initialize(IIdentity $identity)
2828
{
29-
return $this->namespaceDetector->getNamespace()->getIdentityInitializer()->initialize($identity);
29+
return $this->namespace->getIdentityInitializer()->initialize($identity);
3030
}
3131

3232
}

src/NamespaceAwareAuthenticator.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,22 @@
1010
class NamespaceAwareAuthenticator extends Object implements IAuthenticator
1111
{
1212

13-
/** @var INamespaceDetector */
14-
protected $namespaceDetector;
13+
/** @var ISecurityNamespace */
14+
protected $namespace;
1515

1616

1717
/**
18-
* @param INamespaceDetector $namespaceDetector
18+
* @param ISecurityNamespace $namespace
1919
*/
20-
public function __construct(INamespaceDetector $namespaceDetector)
20+
public function __construct(ISecurityNamespace $namespace)
2121
{
22-
$this->namespaceDetector = $namespaceDetector;
22+
$this->namespace = $namespace;
2323
}
2424

2525

2626
function authenticate(array $credentials)
2727
{
28-
$namespace = $this->namespaceDetector->getNamespace();
29-
30-
return $namespace->getAuthenticator()->authenticate($credentials);
28+
return $this->namespace->getAuthenticator()->authenticate($credentials);
3129
}
3230

3331
}

src/NamespaceAwareAuthorizator.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,22 @@
1010
class NamespaceAwareAuthorizator extends Object implements IAuthorizator
1111
{
1212

13-
/** @var INamespaceDetector */
14-
protected $namespaceDetector;
13+
/** @var ISecurityNamespace */
14+
protected $namespace;
1515

1616

1717
/**
18-
* @param INamespaceDetector $namespaceDetector
18+
* @param ISecurityNamespace $namespace
1919
*/
20-
public function __construct(INamespaceDetector $namespaceDetector)
20+
public function __construct(ISecurityNamespace $namespace)
2121
{
22-
$this->namespaceDetector = $namespaceDetector;
22+
$this->namespace = $namespace;
2323
}
2424

2525

2626
function isAllowed($role, $resource, $privilege)
2727
{
28-
$securityNamespace = $this->namespaceDetector->getNamespace();
29-
30-
return $securityNamespace->getAuthorizator()->isAllowed($role, $resource, $privilege);
28+
return $this->namespace->getAuthorizator()->isAllowed($role, $resource, $privilege);
3129
}
3230

3331
}

src/NamespaceDetector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function __construct(INamespaceUserStorage $userStorage, NamespaceManager
2626
$this->namespaceManager = $namespaceManager;
2727
}
2828

29+
2930
public function getNamespace()
3031
{
3132
return $this->namespaceManager->getNamespace($this->userStorage->getNamespace());

src/NamespaceManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function addNamespace(SecurityNamespace $namespace)
1818

1919
public function getNamespace($name)
2020
{
21-
if(!isset($this->namespaces[$name])) {
21+
if (!isset($this->namespaces[$name])) {
2222
throw new InvalidStateException("Security namespace $name not found");
2323
}
2424

src/SecurityNamespace.php

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* @author David Matejka
1111
*/
12-
class SecurityNamespace extends Object
12+
class SecurityNamespace extends Object implements ISecurityNamespace
1313
{
1414

1515
/** @var IAuthenticator */
@@ -40,42 +40,28 @@ function __construct($name, IAuthenticator $authenticator = NULL, IAuthorizator
4040
}
4141

4242

43-
/**
44-
* @return mixed
45-
*/
4643
public function getName()
4744
{
4845
return $this->name;
4946
}
5047

5148

52-
/**
53-
* @return IAuthenticator
54-
* @throws InvalidStateException
55-
*/
5649
public function getAuthenticator()
5750
{
58-
if(!$this->authenticator) {
51+
if (!$this->authenticator) {
5952
throw new InvalidStateException("Authenticator has not been set.");
6053
}
6154

6255
return $this->authenticator;
6356
}
6457

6558

66-
/**
67-
* @return bool
68-
*/
6959
public function hasAuthenticator()
7060
{
7161
return (bool) $this->authenticator;
7262
}
7363

7464

75-
/**
76-
* @return IAuthorizator
77-
* @throws InvalidStateException
78-
*/
7965
public function getAuthorizator()
8066
{
8167
if (!$this->authorizator) {
@@ -86,32 +72,22 @@ public function getAuthorizator()
8672
}
8773

8874

89-
/**
90-
* @return bool
91-
*/
9275
public function hasAuthorizator()
9376
{
9477
return (bool) $this->authorizator;
9578
}
9679

9780

98-
/**
99-
* @throws InvalidStateException
100-
* @return IIdentityInitializer
101-
*/
10281
public function getIdentityInitializer()
10382
{
104-
if(!$this->identityInitializer) {
83+
if (!$this->identityInitializer) {
10584
throw new InvalidStateException("EntityIdentity initializer has not been set.");
10685
}
10786

10887
return $this->identityInitializer;
10988
}
11089

11190

112-
/**
113-
* @return bool
114-
*/
11591
public function hasIdentityInitializer()
11692
{
11793
return (bool) $this->identityInitializer;

src/exceptions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ interface Exception
66

77
}
88

9+
910
class InvalidStateException extends \RuntimeException implements Exception
1011
{
1112

0 commit comments

Comments
 (0)