1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Cheka - Authorization |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* Copyright (C) 2016 Jake Johns |
8
|
|
|
* |
9
|
|
|
* This software may be modified and distributed under the terms |
10
|
|
|
* of the MIT license. See the LICENSE file for details. |
11
|
|
|
* |
12
|
|
|
* @category Resource |
13
|
|
|
* @package Jnjxp\Cheka |
14
|
|
|
* @author Jake Johns <[email protected]> |
15
|
|
|
* @copyright 2016 Jake Johns |
16
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
17
|
|
|
* @link https://github.com/jnjxp/jnjxp.cheka |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Jnjxp\Cheka; |
21
|
|
|
|
22
|
|
|
use Aura\Router\Route; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Route resource aware trait |
26
|
|
|
* |
27
|
|
|
* @category Resource |
28
|
|
|
* @package Jnjxp\Cheka |
29
|
|
|
* @author Jake Johns <[email protected]> |
30
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
31
|
|
|
* @link https://github.com/jnjxp/jnjxp.cheka |
32
|
|
|
*/ |
33
|
|
|
trait ResourceRouteAwareTrait |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Key on which resourceId is stored in extras |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
* |
40
|
|
|
* @access protected |
41
|
|
|
*/ |
42
|
|
|
protected $resourceIdKey = 'resource'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Key on which privilege is stored in extras |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
* |
49
|
|
|
* @access protected |
50
|
|
|
*/ |
51
|
|
|
protected $privilegeKey = 'privilege'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Set extras key on which resourceId is stored |
55
|
|
|
* |
56
|
|
|
* @param string $key name of offset |
57
|
|
|
* |
58
|
|
|
* @return $this |
59
|
|
|
* |
60
|
|
|
* @access public |
61
|
|
|
*/ |
62
|
1 |
|
public function setResourceIdKey($key) |
63
|
|
|
{ |
64
|
1 |
|
$this->resourceIdKey = $key; |
65
|
1 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set extras key on which privilege is stored |
70
|
|
|
* |
71
|
|
|
* @param string $key name of offset |
72
|
|
|
* |
73
|
|
|
* @return $this |
74
|
|
|
* |
75
|
|
|
* @access public |
76
|
|
|
*/ |
77
|
1 |
|
public function setPrivilegeKey($key) |
78
|
|
|
{ |
79
|
1 |
|
$this->privilegeKey = $key; |
80
|
1 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get resourceId from Route |
85
|
|
|
* |
86
|
|
|
* @param Route $route Route |
87
|
|
|
* |
88
|
|
|
* @return string|null |
89
|
|
|
* |
90
|
|
|
* @access protected |
91
|
|
|
*/ |
92
|
7 |
|
protected function getResourceIdFromRoute(Route $route) |
93
|
|
|
{ |
94
|
7 |
|
$extras = $route->extras; |
95
|
|
|
|
96
|
7 |
|
return isset($extras[$this->resourceIdKey]) |
97
|
7 |
|
? $extras[$this->resourceIdKey] |
98
|
7 |
|
: null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get privilege from route |
103
|
|
|
* |
104
|
|
|
* @param Route $route Route |
105
|
|
|
* |
106
|
|
|
* @return null|string |
107
|
|
|
* |
108
|
|
|
* @access protected |
109
|
|
|
*/ |
110
|
5 |
|
protected function getPrivilegeFromRoute(Route $route) |
111
|
|
|
{ |
112
|
5 |
|
$extras = $route->extras; |
113
|
|
|
|
114
|
5 |
|
return isset($extras[$this->privilegeKey]) |
115
|
5 |
|
? $extras[$this->privilegeKey] |
116
|
5 |
|
: null; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|