Skip to content
This repository has been archived by the owner on Aug 8, 2019. It is now read-only.

Commit

Permalink
Merging develop into master for deployment.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdrydn committed Apr 1, 2015
2 parents a41b33a + c37cd2c commit 5c1834e
Show file tree
Hide file tree
Showing 21 changed files with 381 additions and 113 deletions.
123 changes: 88 additions & 35 deletions classes/acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* @author: KentProjects <[email protected]>
* @license: Copyright KentProjects
* @link: http://kentprojects.com
*
* Class ACL
* This is the glorious ACL class that ensures a user can and can't do some action.
*/
final class ACL implements Countable
{
Expand All @@ -11,13 +14,33 @@ final class ACL implements Countable
const UPDATE = "acl:update";
const DELETE = "acl:delete";

/**
* A base template for each set of ACLs.
* @var array
*/
protected static $template = array("create" => 0, "read" => 0, "update" => 0, "delete" => 0);

/**
* The current list of ACLs.
* @var array
*/
protected $acl;
/**
* The current user we're handling ACLs for.
* @var Model_User
*/
protected $user;

/**
* Build a new ACLs object.
*
* @param Model_User $user
*/
public function __construct(Model_User $user = null)
{
/**
* If we don't have user, just return, because it will just return false for everything.
*/
if (empty($user))
{
return;
Expand All @@ -28,41 +51,8 @@ public function __construct(Model_User $user = null)
}

/**
* @param string $entity
* @return array
*/
public function get($entity)
{
if (empty($this->user))
{
return array();
}

$values = static::$template;

if (empty($this->acl))
{
return $values;
}

$range = explode("/", $entity);
$rangeString = "";

foreach ($range as $i => $piece)
{
$rangeString .= ($i == 0 ? "" : "/") . $piece;
$values = array_merge($values, $this->checkMatch($rangeString));
}

foreach ($values as $key => $value)
{
$values[$key] = boolval($value);
}

return $values;
}

/**
* Check that a particular entity is a match.
*
* @param string $entry
* @return array
*/
Expand All @@ -79,21 +69,34 @@ protected function checkMatch($entry)
}

/**
* Count the number of ACLs.
* @return int
*/
public function count()
{
return empty($this->acl) ? 0 : count($this->acl);
}

/**
* Remove a particular ACL from this list.
* @param string $entity
* @return void
*/
public function delete($entity)
{
unset($this->acl[$entity]);
ksort($this->acl);
}

/**
* Build the list of ACLs for the current user.
* @return void
*/
public function fetch()
{
/**
* If we were not passed a user, then stop.
*/
if (empty($this->user))
{
return;
Expand All @@ -115,6 +118,53 @@ public function fetch()
}

/**
* Get the permissions for a particular entity.
* This "recursively" splits the entity to ensure that all global variants are handled.
*
* Thus,
* group
* group/1
*
* project
* project/22
*
* @param string $entity
* @return array
*/
public function get($entity)
{
if (empty($this->user))
{
return array();
}

$values = static::$template;

if (empty($this->acl))
{
return $values;
}

$range = explode("/", $entity);
$rangeString = "";

foreach ($range as $i => $piece)
{
$rangeString .= ($i == 0 ? "" : "/") . $piece;
$values = array_merge($values, $this->checkMatch($rangeString));
}

foreach ($values as $key => $value)
{
$values[$key] = boolval($value);
}

return $values;
}

/**
* Return the current user in question.
*
* @return Model_User
*/
public function getUser()
Expand Down Expand Up @@ -181,6 +231,9 @@ public function set($entity, $create = false, $read = false, $update = false, $d
}

/**
* Validate a particular entity and action.
* This is mostly used in the controllers when validating a user's permission to do a certain action.
*
* @param string $entity
* @param string $action
* @throws InvalidArgumentException
Expand Down
11 changes: 11 additions & 0 deletions classes/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* @author: KentProjects <[email protected]>
* @license: Copyright KentProjects
* @link: http://kentprojects.com
*
* Class Auth
* Handles the API authentication.
*/
final class Auth
{
Expand Down Expand Up @@ -45,6 +48,10 @@ public function __construct(Request_Internal &$request, Response &$response, $le

if ($this->level !== self::NONE)
{
/**
* This code runs when authentication is required.
* If something seems wrong, then the API will reject the request.
*/
if ($this->request->query("key", null) === null)
{
throw new HttpStatusException(400, "Missing application key.");
Expand Down Expand Up @@ -116,6 +123,10 @@ function (&$v)
}
else
{
/**
* If this endpoint does not require authentication, and we have the relevant query string values, then we
* may as well authenticate the request.
*/
if ($this->request->query("key", null) !== null)
{
$this->application = Model_Application::getByKey($this->request->query("key"));
Expand Down
8 changes: 8 additions & 0 deletions classes/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* @author: KentProjects <[email protected]>
* @license: Copyright KentProjects
* @link: http://kentprojects.com
*
* Class Controller
* This represents a basic controller that takes requests in and returns responses out.
*/
abstract class Controller
{
Expand Down Expand Up @@ -53,6 +56,7 @@ public function __construct(Request_Internal &$request, Response &$response)

/**
* To be run BEFORE the main action.
* @return void
*/
public function before()
{
Expand All @@ -61,6 +65,7 @@ public function before()

/**
* To be run AFTER the main action.
* @return void
*/
public function after()
{
Expand All @@ -77,6 +82,9 @@ public function after()
}

/**
* This is our custom render function that goes through each result, ensuring it's in a format that JSON-encode
* will accept.
*
* @param mixed $body
* @return mixed
*/
Expand Down
Loading

0 comments on commit 5c1834e

Please sign in to comment.