-
Notifications
You must be signed in to change notification settings - Fork 21
/
ability.php
117 lines (98 loc) · 2.45 KB
/
ability.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?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
*
**/
use Laravel\Config;
use Laravel\Auth;
abstract class Ability {
protected static $_rules = array();
protected static $_action_aliases = array();
public static function can($action, $resource, $resource_val = null)
{
// See if the action has been aliased to somethign else
$true_action = static::determine_action($action);
$matches = static::find_matches($true_action, $resource);
if(count($matches) === 0)
{
return false;
}
$rule = end($matches);
return $rule->allowed($resource_val);
}
public static function cannot($action, $resource, $resource_val = null)
{
return ! static::can($action, $resource, $resource_val);
}
public static function allow($action, $resource, \Closure $callback = null)
{
static::$_rules[] = new Rule(true, $action, $resource, $callback);
}
public static function deny($action, $resource, \Closure $callback = null)
{
static::$_rules[] = new Rule(false, $action, $resource, $callback);
}
public static function action_alias($action, Array $aliases)
{
static::$_action_aliases[$action] = $aliases;
}
public static function dealias($action)
{
return static::$_action_aliases[$action] ?: $action;
}
protected static function determine_action($action)
{
$actions = array();
if ( ! empty(static::$_action_aliases))
{
foreach (static::$_action_aliases as $aliased_action => $aliases)
{
if ( ! empty($aliases) && in_array($action, $aliases))
{
$actions[] = $aliased_action;
}
}
}
if (empty($actions))
{
return $action;
}
else
{
$actions[] = $action;
return $actions;
}
}
protected static function find_matches($action, $resource)
{
$matches = array();
if ( ! empty(static::$_rules))
{
foreach(static::$_rules as $rule)
{
if ($rule->relevant($action, $resource))
{
$matches[] = $rule;
}
}
}
return $matches;
}
public static function reset()
{
static::$_rules = array();
static::$_action_aliases = array();
}
}