Skip to content

Commit

Permalink
Merge pull request #435 from christeredvartsen/issue-434-more-models
Browse files Browse the repository at this point in the history
Issue 434 more models
  • Loading branch information
christeredvartsen committed Mar 4, 2016
2 parents cfbc371 + 2ecf36f commit 51a08ad
Show file tree
Hide file tree
Showing 21 changed files with 920 additions and 32 deletions.
6 changes: 6 additions & 0 deletions ChangeLog.markdown
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog for Imbo
==================

Imbo-2.x.x
----------
__N/A__

* #434: Custom models for group(s) and access rule(s) (Christer Edvartsen)

Imbo-2.0.0
----------
__2016-02-28__
Expand Down
2 changes: 1 addition & 1 deletion docs/installation/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ The adapter is set up using the ``accessControl`` key in your configuration file
return new Imbo\Auth\AccessControl\Adapter\SimpleArrayAdapter([
'some-user' => 'my-super-secret-private-key',
'other-user' => 'other-super-secret-private-key',
])
]);
},
// ...
Expand Down
5 changes: 5 additions & 0 deletions library/Imbo/Auth/AccessControl/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ abstract class AbstractAdapter implements AdapterInterface {
*/
abstract public function getGroups(GroupQuery $query = null, GroupsModel $model);

/**
* {@inheritdoc}
*/
abstract public function groupExists($groupName);

/**
* {@inheritdoc}
*/
Expand Down
9 changes: 9 additions & 0 deletions library/Imbo/Auth/AccessControl/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,18 @@ function hasAccess($publicKey, $resource, $user = null);
*/
function getGroups(GroupQuery $query = null, GroupsModel $model);

/**
* Check whether or not a group exists
*
* @param string $groupName Name of the group
* @return boolean
*/
function groupExists($groupName);

/**
* Fetch a resource group with the given name
*
* @param string $groupName Name of the group
* @return array Array of resources the group consists of
*/
function getGroup($groupName);
Expand Down
7 changes: 7 additions & 0 deletions library/Imbo/Auth/AccessControl/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ public function getGroups(GroupQuery $query = null, GroupsModel $model) {
return array_slice($this->groups, $offset, $query->limit(), true);
}

/**
* {@inheritdoc}
*/
public function groupExists($groupName) {
return isset($this->groups[$groupName]);
}

/**
* {@inheritdoc}
*/
Expand Down
9 changes: 9 additions & 0 deletions library/Imbo/Auth/AccessControl/Adapter/MongoDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ public function getGroups(GroupQuery $query = null, GroupsModel $model) {
return $groups;
}

/**
* {@inheritdoc}
*/
public function groupExists($groupName) {
return (boolean) $this->getGroupsCollection()->findOne([
'name' => $groupName
]);
}

/**
* {@inheritdoc}
*/
Expand Down
22 changes: 19 additions & 3 deletions library/Imbo/Http/Response/Formatter/JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,37 @@ public function formatGroups(Model\Groups $model) {
* {@inheritdoc}
*/
public function formatGroup(Model\Group $model) {
return $this->encode($model->getData());
return $this->encode([
'name' => $model->getName(),
'resources' => $model->getResources(),
]);
}

/**
* {@inheritdoc}
*/
public function formatAccessRule(Model\AccessRule $model) {
return $this->encode($model->getData());
$data = [
'id' => $model->getId(),
'users' => $model->getUsers(),
];

if ($group = $model->getGroup()) {
$data['group'] = $group;
}

if ($resources = $model->getResources()) {
$data['resources'] = $resources;
}

return $this->encode($data);
}

/**
* {@inheritdoc}
*/
public function formatAccessRules(Model\AccessRules $model) {
return $this->encode($model->getData());
return $this->encode($model->getRules());
}

/**
Expand Down
34 changes: 19 additions & 15 deletions library/Imbo/Http/Response/Formatter/XML.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,19 +250,19 @@ public function formatGroups(Model\Groups $model) {
* {@inheritdoc}
*/
public function formatGroup(Model\Group $model) {
$data = $model->getData();

$entries = '';
foreach ($data['resources'] as $resource) {

foreach ($model->getResources() as $resource) {
$entries .= '<resource>' . $this->formatValue($resource) . '</resource>';
}

return <<<DATA
return <<<GROUP
<?xml version="1.0" encoding="UTF-8"?>
<imbo>
<name>{$model->getName()}</name>
<resources>{$entries}</resources>
</imbo>
DATA;
GROUP;
}

/**
Expand All @@ -276,48 +276,52 @@ public function formatStats(Model\Stats $model) {
]);
$custom = $this->formatArray($model->getCustomStats() ?: []);

return <<<STATUS
return <<<STATS
<?xml version="1.0" encoding="UTF-8"?>
<imbo>
<stats>
{$total}
<custom>{$custom}</custom>
</stats>
</imbo>
STATUS;
STATS;
}

/**
* {@inheritdoc}
*/
public function formatAccessRule(Model\AccessRule $model) {
$rule = $this->formatAccessRuleArray($model->getData());
$rule = $this->formatAccessRuleArray([
'id' => $model->getId(),
'users' => $model->getUsers(),
'group' => $model->getGroup(),
'resources' => $model->getResources(),
]);

return <<<DATA
return <<<RULE
<?xml version="1.0" encoding="UTF-8"?>
<imbo>
{$rule}
</imbo>
DATA;
RULE;
}

/**
* {@inheritdoc}
*/
public function formatAccessRules(Model\AccessRules $model) {
$data = $model->getData();

$rules = '';
foreach ($data as $rule) {

foreach ($model->getRules() as $rule) {
$rules .= $this->formatAccessRuleArray($rule);
}

return <<<DATA
return <<<RULES
<?xml version="1.0" encoding="UTF-8"?>
<imbo>
<access>{$rules}</access>
</imbo>
DATA;
RULES;
}

/**
Expand Down
116 changes: 114 additions & 2 deletions library/Imbo/Model/AccessRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,119 @@
/**
* Access rule model
*
* @author Kristoffer Brabrand <kristoffer@brabrand.no>
* @author Christer Edvartsen <cogo@starzinger.net>
* @package Models
*/
class AccessRule extends ArrayModel {}
class AccessRule implements ModelInterface {
/**
* ID of the rule
*
* @var int
*/
private $id;

/**
* Group name
*
* @var string
*/
private $group;

/**
* List of resources
*
* @var string[]
*/
private $resources = [];

/**
* List of users
*
* @var string[]
*/
private $users = [];

/**
* Set the ID
*
* @param int $id
* @return self
*/
public function setId($id) {
$this->id = $id;

return $this;
}

/**
* Get the ID
*
* @return int
*/
public function getId() {
return $this->id;
}

/**
* Set the group
*
* @param string $group
* @return self
*/
public function setGroup($group) {
$this->group = $group;

return $this;
}

/**
* Get the group
*
* @return string
*/
public function getGroup() {
return $this->group;
}

/**
* Set the resources
*
* @param string[] $resources
* @return self
*/
public function setResources(array $resources) {
$this->resources = $resources;

return $this;
}

/**
* Get the resources
*
* @return string[]
*/
public function getResources() {
return $this->resources;
}

/**
* Set the users
*
* @param string[] $users
* @return self
*/
public function setUsers(array $users) {
$this->users = $users;

return $this;
}

/**
* Get the users
*
* @return string[]
*/
public function getUsers() {
return $this->users;
}
}
32 changes: 30 additions & 2 deletions library/Imbo/Model/AccessRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,35 @@
/**
* Access rules model
*
* @author Kristoffer Brabrand <kristoffer@brabrand.no>
* @author Christer Edvartsen <cogo@starzinger.net>
* @package Models
*/
class AccessRules extends ArrayModel {}
class AccessRules implements ModelInterface {
/**
* List of rules
*
* @var array[]
*/
private $rules = [];

/**
* Set the rules
*
* @param array[]
* @return self
*/
public function setRules(array $rules) {
$this->rules = $rules;

return $this;
}

/**
* Get the rules
*
* @return array[]
*/
public function getRules() {
return $this->rules;
}
}

0 comments on commit 51a08ad

Please sign in to comment.