-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from dtkahl/1.0
merge changes from 1.0 to master
- Loading branch information
Showing
11 changed files
with
561 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php namespace Dtkahl\AccessControl; | ||
|
||
use Dtkahl\ArrayTools\Map; | ||
|
||
class AccessObject | ||
{ | ||
private $identifier; | ||
private $object_roles; | ||
|
||
/** | ||
* AccessObject constructor. | ||
* @param string $identifier | ||
* @param AccessRole[] $object_roles | ||
*/ | ||
public function __construct($identifier, array $object_roles) | ||
{ | ||
$this->identifier = $identifier; | ||
$this->object_roles = new Map(); | ||
|
||
foreach ($object_roles as $role) { | ||
$this->registerRole($role); | ||
} | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getIdentifier() | ||
{ | ||
return $this->identifier; | ||
} | ||
|
||
/** | ||
* @param AccessRole $role | ||
* @return $this | ||
*/ | ||
public function registerRole(AccessRole $role) | ||
{ | ||
$this->object_roles->set($role->getIdentifier(), $role); | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return Map | ||
*/ | ||
public function getRoles() | ||
{ | ||
return $this->object_roles; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php namespace Dtkahl\AccessControl; | ||
|
||
class AccessRole | ||
{ | ||
private $identifier; | ||
private $rights = []; | ||
private $extended_role; | ||
|
||
/** | ||
* AccessRole constructor. | ||
* @param string $identifier | ||
* @param array $rights | ||
* @param AccessRole|null $extended_role | ||
*/ | ||
public function __construct($identifier, array $rights, AccessRole $extended_role = null) | ||
{ | ||
$this->identifier = $identifier; | ||
$this->rights = $this->prepareRights($rights); | ||
$this->extended_role = $extended_role; | ||
} | ||
|
||
private function prepareRights($rights) | ||
{ | ||
$prepared = []; | ||
foreach ($rights as $key=>$value) { | ||
if (is_array($value)) { | ||
foreach ($value as $item) { | ||
$prepared[] = "$key.$item"; | ||
} | ||
} else { | ||
$prepared[] = $value; | ||
} | ||
} | ||
return $prepared; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getIdentifier() | ||
{ | ||
return $this->identifier; | ||
} | ||
|
||
/** | ||
* @param $rights | ||
* @param AccessObject|null $access_object | ||
* @throws AllowedException | ||
* @throws NotAllowedException | ||
*/ | ||
public function checkRight($rights, AccessObject $access_object = null) | ||
{ | ||
$rights = (array) $rights; | ||
|
||
if (count(array_intersect($rights, $this->rights)) == count($rights)) { | ||
throw new AllowedException; | ||
} | ||
|
||
if ($this->extended_role instanceof AccessRole) { | ||
$this->extended_role->checkRight($rights, $access_object); | ||
} | ||
} | ||
|
||
public function getExtendedRole() | ||
{ | ||
return $this->extended_role; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php namespace Dtkahl\AccessControl; | ||
|
||
class AllowedException extends \Exception | ||
{ | ||
/** | ||
* No reason to start sweating honey :) | ||
* | ||
* If this "exception" is thrown everything is alright. The Judge catches it, so it should never get outside. | ||
* | ||
* I know this is sort of an anti pattern but it seams to be the best solution for this specific use case. | ||
* Lets call it a "Sucception" - credits to Michael Betka for this name ;) | ||
*/ | ||
} |
Oops, something went wrong.