-
Notifications
You must be signed in to change notification settings - Fork 21
/
rule.php
75 lines (62 loc) · 1.75 KB
/
rule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php namespace Authority;
/**
* Authority
*
* Authority is an authorization library for CodeIgniter 2+ and PHPActiveRecord
* This library is inspired by, and largely based off, Ryan Bates' CanCan gem
* for Ruby on Rails. It is not a 1:1 port, but the essentials are available.
* Please check out his work at http://github.com/ryanb/cancan/
*
* @package Authority
* @version 0.0.3
* @author Matthew Machuga
* @license MIT License
* @copyright 2011 Matthew Machuga
* @link http://github.com/machuga
*
**/
class Rule {
protected $_allowed = false;
protected $_resource = null;
protected $_action = null;
protected $_callback = null;
public function __construct($allowed, $action, $resource, \Closure $callback = null)
{
$this->_allowed = $allowed;
$this->_action = $action;
$this->_resource = $resource;
$this->_callback = $callback;
}
public function allowed($resource_val)
{
if(is_null($resource_val) && is_null($this->_callback))
{
return $this->_allowed;
}
$allowed = $this->_callback($resource_val);
if($this->_allowed)
{
return $allowed;
}
return ! $allowed;
}
public function matches_action($action)
{
return is_array($action) ? in_array($this->_action, $action)
: $this->_action === $action;
}
public function matches_resource($resource)
{
$resource = is_object($resource) ? get_class($resource) : $resource;
return $this->_resource === $resource || $this->_resource === 'all';
}
public function relevant($action, $resource)
{
return $this->matches_action($action) && $this->matches_resource($resource);
}
// Allow callbacks to be called
public function __call($method, $args)
{
return (isset($this->$method)) ? call_user_func_array($this->$method, $args) : true;
}
}