1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file Permission/AbstractPermission.php |
5
|
|
|
* @brief This file contains the AbstractPermission class. |
6
|
|
|
* @details |
7
|
|
|
* @author Filippo F. Fadda |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Daikengo\Permission; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use Daikengo\Role\IRole; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @brief Abstract class that implements the IPermission interface. Since abstract, this class cannot be instantiated. |
19
|
|
|
* @nosubgrouping |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractPermission implements IPermission { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var IRole $role |
25
|
|
|
*/ |
26
|
|
|
protected $role; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string $name |
31
|
|
|
*/ |
32
|
|
|
protected $name; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @brief Constructor is protected so it can't call explicitly from outside. |
37
|
|
|
* @attention Subclasses must override this method and make it public. |
38
|
|
|
*/ |
39
|
|
|
protected function __construct() { |
40
|
|
|
$this->name = lcfirst(preg_replace('/Permission$/', '', get_class($this))); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @brief Calls is triggered when invoking inaccessible methods in an object context. |
46
|
|
|
* @param string $name The name of the method being called. |
47
|
|
|
* @param array $arguments An enumerated array containing the parameters passed to the method. |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
public function __call($name, array $arguments) { |
51
|
|
|
if (is_callable($this->$name)) |
52
|
|
|
return call_user_func($this->$name, $arguments); |
53
|
|
|
else |
54
|
|
|
throw new \RuntimeException("Method {$name} does not exist."); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @brief Sometime a programmer needs to define a new special role, and eventually a set of permissions to check the |
60
|
|
|
* access, for this particular role, to any existent resource. Through this technique the programmer may define |
61
|
|
|
* dynamic methods to check the permissions for a particular role. |
62
|
|
|
* @param string $name The name of the method being called. |
63
|
|
|
* @param callable $value A closure. |
64
|
|
|
* @details In the following example we define the method `checkForGodRole`, to extend the `ImpersonatePermission` |
65
|
|
|
* class such as God will be able to impersonate anyone. |
66
|
|
|
@code |
67
|
|
|
// We assume `$member` is an instance of the `Member` class. |
68
|
|
|
$permission = new ImpersonatePermission($member); |
69
|
|
|
|
70
|
|
|
$permission->checkForGodRole = function() { |
71
|
|
|
return TRUE; |
72
|
|
|
}; |
73
|
|
|
|
74
|
|
|
// Prints `true`. |
75
|
|
|
print $permission->checkForGodRole(); |
76
|
|
|
@endcode |
77
|
|
|
*/ |
78
|
|
|
public function __set($name, $value) { |
79
|
|
|
$this->$name = is_callable($value) ? $value->bindTo($this, $this) : $value; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
public function setRole(IRole $role) { |
84
|
|
|
$this->role = $role; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
public function getRole() { |
89
|
|
|
return $this->role; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
} |