Skip to content

Commit b0d373b

Browse files
committed
constants are PascalCase
1 parent b4d9456 commit b0d373b

File tree

11 files changed

+77
-57
lines changed

11 files changed

+77
-57
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ $acl->allow('guest', 'poll', 'vote');
305305
$acl->allow('registered', 'comment', 'add');
306306

307307
// the administrator can view and edit anything
308-
$acl->allow('administrator', $acl::ALL, ['view', 'edit', 'add']);
308+
$acl->allow('administrator', $acl::All, ['view', 'edit', 'add']);
309309
```
310310

311311
What if we want to **prevent** someone from accessing a resource?

src/Bridges/SecurityHttp/CookieStorage.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class CookieStorage implements Nette\Security\UserStorage
2121
{
2222
use Nette\SmartObject;
2323

24-
private const MIN_LENGTH = 13;
24+
private const MinLength = 13;
2525

2626
/** @var Http\IRequest */
2727
private $request;
@@ -55,7 +55,7 @@ public function __construct(Http\IRequest $request, Http\IResponse $response)
5555
public function saveAuthentication(IIdentity $identity): void
5656
{
5757
$uid = (string) $identity->getId();
58-
if (strlen($uid) < self::MIN_LENGTH) {
58+
if (strlen($uid) < self::MinLength) {
5959
throw new \LogicException('UID is too short.');
6060
}
6161

@@ -84,7 +84,7 @@ public function getState(): array
8484
{
8585
if ($this->uid === null) {
8686
$uid = $this->request->getCookie($this->cookieName);
87-
$this->uid = is_string($uid) && strlen($uid) >= self::MIN_LENGTH ? $uid : '';
87+
$this->uid = is_string($uid) && strlen($uid) >= self::MinLength ? $uid : '';
8888
}
8989

9090
return $this->uid

src/Bridges/SecurityHttp/SessionStorage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function clearAuthentication(bool $clearIdentity): void
6262
{
6363
$section = $this->getSessionSection();
6464
$section->set('authenticated', false);
65-
$section->set('reason', self::LOGOUT_MANUAL);
65+
$section->set('reason', self::LogoutManual);
6666
$section->set('authTime', null);
6767
if ($clearIdentity === true) {
6868
$section->set('identity', null);
@@ -149,7 +149,7 @@ protected function getSessionSection(): ?SessionSection
149149

150150
if ($section->get('authenticated') && $section->get('expireDelta') > 0) { // check time expiration
151151
if ($section->get('expireTime') < time()) {
152-
$section->set('reason', self::LOGOUT_INACTIVITY);
152+
$section->set('reason', self::LogoutInactivity);
153153
$section->set('authenticated', false);
154154
if ($section->get('expireIdentity')) {
155155
$section->remove('identity');

src/Security/Authenticator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
*/
1616
interface Authenticator extends IAuthenticator
1717
{
18+
/** Exception error code */
19+
public const
20+
IdentityNotFound = 1,
21+
InvalidCredential = 2,
22+
Failure = 3,
23+
NotApproved = 4;
24+
1825
/**
1926
* Performs an authentication.
2027
* @throws AuthenticationException

src/Security/Authorizator.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
interface Authorizator
1818
{
1919
/** Set type: all */
20-
public const ALL = null;
20+
public const All = null;
2121

2222
/** Permission type: allow */
23-
public const ALLOW = true;
23+
public const Allow = true;
2424

2525
/** Permission type: deny */
26-
public const DENY = false;
26+
public const Deny = false;
27+
28+
/** Deprecated */
29+
public const ALL = self::All;
30+
public const ALLOW = self::Allow;
31+
public const DENY = self::Deny;
2732

2833
/**
2934
* Performs a role-based authorization.

src/Security/Permission.php

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Permission implements Authorizator
3232
'allResources' => [
3333
'allRoles' => [
3434
'allPrivileges' => [
35-
'type' => self::DENY,
35+
'type' => self::Deny,
3636
'assert' => null,
3737
],
3838
'byPrivilege' => [],
@@ -388,12 +388,12 @@ public function removeAllResources()
388388
* @return static
389389
*/
390390
public function allow(
391-
$roles = self::ALL,
392-
$resources = self::ALL,
393-
$privileges = self::ALL,
391+
$roles = self::All,
392+
$resources = self::All,
393+
$privileges = self::All,
394394
?callable $assertion = null,
395395
) {
396-
$this->setRule(true, self::ALLOW, $roles, $resources, $privileges, $assertion);
396+
$this->setRule(true, self::Allow, $roles, $resources, $privileges, $assertion);
397397
return $this;
398398
}
399399

@@ -408,12 +408,12 @@ public function allow(
408408
* @return static
409409
*/
410410
public function deny(
411-
$roles = self::ALL,
412-
$resources = self::ALL,
413-
$privileges = self::ALL,
411+
$roles = self::All,
412+
$resources = self::All,
413+
$privileges = self::All,
414414
?callable $assertion = null,
415415
) {
416-
$this->setRule(true, self::DENY, $roles, $resources, $privileges, $assertion);
416+
$this->setRule(true, self::Deny, $roles, $resources, $privileges, $assertion);
417417
return $this;
418418
}
419419

@@ -426,9 +426,9 @@ public function deny(
426426
* @param string|string[]|null $privileges
427427
* @return static
428428
*/
429-
public function removeAllow($roles = self::ALL, $resources = self::ALL, $privileges = self::ALL)
429+
public function removeAllow($roles = self::All, $resources = self::All, $privileges = self::All)
430430
{
431-
$this->setRule(false, self::ALLOW, $roles, $resources, $privileges);
431+
$this->setRule(false, self::Allow, $roles, $resources, $privileges);
432432
return $this;
433433
}
434434

@@ -441,9 +441,9 @@ public function removeAllow($roles = self::ALL, $resources = self::ALL, $privile
441441
* @param string|string[]|null $privileges
442442
* @return static
443443
*/
444-
public function removeDeny($roles = self::ALL, $resources = self::ALL, $privileges = self::ALL)
444+
public function removeDeny($roles = self::All, $resources = self::All, $privileges = self::All)
445445
{
446-
$this->setRule(false, self::DENY, $roles, $resources, $privileges);
446+
$this->setRule(false, self::Deny, $roles, $resources, $privileges);
447447
return $this;
448448
}
449449

@@ -459,8 +459,8 @@ public function removeDeny($roles = self::ALL, $resources = self::ALL, $privileg
459459
protected function setRule(bool $toAdd, bool $type, $roles, $resources, $privileges, ?callable $assertion = null)
460460
{
461461
// ensure that all specified Roles exist; normalize input to array of Roles or null
462-
if ($roles === self::ALL) {
463-
$roles = [self::ALL];
462+
if ($roles === self::All) {
463+
$roles = [self::All];
464464

465465
} else {
466466
if (!is_array($roles)) {
@@ -473,8 +473,8 @@ protected function setRule(bool $toAdd, bool $type, $roles, $resources, $privile
473473
}
474474

475475
// ensure that all specified Resources exist; normalize input to array of Resources or null
476-
if ($resources === self::ALL) {
477-
$resources = [self::ALL];
476+
if ($resources === self::All) {
477+
$resources = [self::All];
478478

479479
} else {
480480
if (!is_array($resources)) {
@@ -487,7 +487,7 @@ protected function setRule(bool $toAdd, bool $type, $roles, $resources, $privile
487487
}
488488

489489
// normalize privileges to array
490-
if ($privileges === self::ALL) {
490+
if ($privileges === self::All) {
491491
$privileges = [];
492492

493493
} elseif (!is_array($privileges)) {
@@ -521,11 +521,11 @@ protected function setRule(bool $toAdd, bool $type, $roles, $resources, $privile
521521
}
522522

523523
if (count($privileges) === 0) {
524-
if ($resource === self::ALL && $role === self::ALL) {
524+
if ($resource === self::All && $role === self::All) {
525525
if ($type === $rules['allPrivileges']['type']) {
526526
$rules = [
527527
'allPrivileges' => [
528-
'type' => self::DENY,
528+
'type' => self::Deny,
529529
'assert' => null,
530530
],
531531
'byPrivilege' => [],
@@ -571,10 +571,10 @@ protected function setRule(bool $toAdd, bool $type, $roles, $resources, $privile
571571
* @param string|null $privilege
572572
* @throws Nette\InvalidStateException
573573
*/
574-
public function isAllowed($role = self::ALL, $resource = self::ALL, $privilege = self::ALL): bool
574+
public function isAllowed($role = self::All, $resource = self::All, $privilege = self::All): bool
575575
{
576576
$this->queriedRole = $role;
577-
if ($role !== self::ALL) {
577+
if ($role !== self::All) {
578578
if ($role instanceof Role) {
579579
$role = $role->getRoleId();
580580
}
@@ -583,7 +583,7 @@ public function isAllowed($role = self::ALL, $resource = self::ALL, $privilege =
583583
}
584584

585585
$this->queriedResource = $resource;
586-
if ($resource !== self::ALL) {
586+
if ($resource !== self::All) {
587587
if ($resource instanceof Resource) {
588588
$resource = $resource->getResourceId();
589589
}
@@ -595,15 +595,15 @@ public function isAllowed($role = self::ALL, $resource = self::ALL, $privilege =
595595
// depth-first search on $role if it is not 'allRoles' pseudo-parent
596596
if (
597597
$role !== null
598-
&& ($result = $this->searchRolePrivileges($privilege === self::ALL, $role, $resource, $privilege)) !== null
598+
&& ($result = $this->searchRolePrivileges($privilege === self::All, $role, $resource, $privilege)) !== null
599599
) {
600600
break;
601601
}
602602

603-
if ($privilege === self::ALL) {
604-
if ($rules = $this->getRules($resource, self::ALL)) { // look for rule on 'allRoles' psuedo-parent
603+
if ($privilege === self::All) {
604+
if ($rules = $this->getRules($resource, self::All)) { // look for rule on 'allRoles' psuedo-parent
605605
foreach ($rules['byPrivilege'] as $privilege => $rule) {
606-
if (($result = $this->getRuleType($resource, null, $privilege)) === self::DENY) {
606+
if (($result = $this->getRuleType($resource, null, $privilege)) === self::Deny) {
607607
break 2;
608608
}
609609
}
@@ -670,8 +670,8 @@ private function searchRolePrivileges(bool $all, $role, $resource, $privilege)
670670
if ($all) {
671671
if ($rules = $this->getRules($resource, $role)) {
672672
foreach ($rules['byPrivilege'] as $privilege2 => $rule) {
673-
if ($this->getRuleType($resource, $role, $privilege2) === self::DENY) {
674-
return self::DENY;
673+
if ($this->getRuleType($resource, $role, $privilege2) === self::Deny) {
674+
return self::Deny;
675675
}
676676
}
677677

@@ -711,7 +711,7 @@ private function getRuleType($resource, $role, $privilege): ?bool
711711
return null;
712712
}
713713

714-
if ($privilege === self::ALL) {
714+
if ($privilege === self::All) {
715715
if (isset($rules['allPrivileges'])) {
716716
$rule = $rules['allPrivileges'];
717717
} else {
@@ -727,14 +727,14 @@ private function getRuleType($resource, $role, $privilege): ?bool
727727
if ($rule['assert'] === null || $rule['assert']($this, $role, $resource, $privilege)) {
728728
return $rule['type'];
729729

730-
} elseif ($resource !== self::ALL || $role !== self::ALL || $privilege !== self::ALL) {
730+
} elseif ($resource !== self::All || $role !== self::All || $privilege !== self::All) {
731731
return null;
732732

733-
} elseif ($rule['type'] === self::ALLOW) {
734-
return self::DENY;
733+
} elseif ($rule['type'] === self::Allow) {
734+
return self::Deny;
735735

736736
} else {
737-
return self::ALLOW;
737+
return self::Allow;
738738
}
739739
}
740740

@@ -748,7 +748,7 @@ private function getRuleType($resource, $role, $privilege): ?bool
748748
private function &getRules($resource, $role, bool $create = false): ?array
749749
{
750750
$null = null;
751-
if ($resource === self::ALL) {
751+
if ($resource === self::All) {
752752
$visitor = &$this->rules['allResources'];
753753
} else {
754754
if (!isset($this->rules['byResource'][$resource])) {
@@ -762,7 +762,7 @@ private function &getRules($resource, $role, bool $create = false): ?array
762762
$visitor = &$this->rules['byResource'][$resource];
763763
}
764764

765-
if ($role === self::ALL) {
765+
if ($role === self::All) {
766766
if (!isset($visitor['allRoles'])) {
767767
if (!$create) {
768768
return $null;

src/Security/SimpleAuthenticator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ public function authenticate(string $username, string $password): IIdentity
5454
if ($this->verifyPassword($password, $pass)) {
5555
return new SimpleIdentity($name, $this->roles[$name] ?? null, $this->data[$name] ?? []);
5656
} else {
57-
throw new AuthenticationException('Invalid password.', self::INVALID_CREDENTIAL);
57+
throw new AuthenticationException('Invalid password.', self::InvalidCredential);
5858
}
5959
}
6060
}
6161

62-
throw new AuthenticationException("User '$username' not found.", self::IDENTITY_NOT_FOUND);
62+
throw new AuthenticationException("User '$username' not found.", self::IdentityNotFound);
6363
}
6464

6565

src/Security/User.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ class User
3535

3636
/** Log-out reason */
3737
public const
38-
LOGOUT_MANUAL = UserStorage::LOGOUT_MANUAL,
39-
LOGOUT_INACTIVITY = UserStorage::LOGOUT_INACTIVITY;
38+
LogoutManual = UserStorage::LogoutManual,
39+
LogoutInactivity = UserStorage::LogoutInactivity;
40+
41+
/** Deprecated */
42+
public const LOGOUT_MANUAL = self::LogoutManual;
43+
public const LOGOUT_INACTIVITY = self::LogoutInactivity;
4044

4145
/** @var string default role for unauthenticated user */
4246
public $guestRole = 'guest';
@@ -335,7 +339,7 @@ final public function isInRole(string $role): bool
335339
* Has a user effective access to the Resource?
336340
* If $resource is null, then the query applies to all resources.
337341
*/
338-
public function isAllowed($resource = Authorizator::ALL, $privilege = Authorizator::ALL): bool
342+
public function isAllowed($resource = Authorizator::All, $privilege = Authorizator::All): bool
339343
{
340344
foreach ($this->getRoles() as $role) {
341345
if ($this->getAuthorizator()->isAllowed($role, $resource, $privilege)) {

src/Security/UserStorage.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ interface UserStorage
1717
{
1818
/** Log-out reason */
1919
public const
20-
LOGOUT_MANUAL = 1,
21-
LOGOUT_INACTIVITY = 2;
20+
LogoutManual = 1,
21+
LogoutInactivity = 2;
22+
23+
/** Deprecated */
24+
public const LOGOUT_MANUAL = self::LogoutManual;
25+
public const LOGOUT_INACTIVITY = self::LogoutInactivity;
2226

2327
/**
2428
* Sets the authenticated state of user.

tests/Security/User.authentication.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ class Authenticator implements Nette\Security\Authenticator
2424
public function authenticate(string $username, string $password): IIdentity
2525
{
2626
if ($username !== 'john') {
27-
throw new Nette\Security\AuthenticationException('Unknown user', self::IDENTITY_NOT_FOUND);
27+
throw new Nette\Security\AuthenticationException('Unknown user', self::IdentityNotFound);
2828

2929
} elseif ($password !== 'xxx') {
30-
throw new Nette\Security\AuthenticationException('Password not match', self::INVALID_CREDENTIAL);
30+
throw new Nette\Security\AuthenticationException('Password not match', self::InvalidCredential);
3131

3232
} else {
3333
return new SimpleIdentity('John Doe', 'admin');

0 commit comments

Comments
 (0)