/
Response.php
121 lines (108 loc) · 3.59 KB
/
Response.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
118
119
120
121
<?php
namespace Concrete\Core\Permission\Response;
use Exception;
use User;
use PermissionKeyCategory;
use Core;
class Response
{
/** @var \Concrete\Core\Permission\ObjectInterface */
protected $object;
/** @var PermissionKeyCategory */
protected $category;
public static $cache = array();
/**
* Sets the current permission object to the object provided, this object should implement the Permission ObjectInterface.
*
* @param \Concrete\Core\Permission\ObjectInterface $object
*/
public function setPermissionObject($object)
{
$this->object = $object;
}
/**
* Retrieves the current permission object.
*/
public function getPermissionObject()
{
return $this->object;
}
/**
* Sets the current Permission Category object to an appropriate PermissionKeyCategory.
*
* @param PermissionKeyCategory $category
*/
public function setPermissionCategoryObject($category)
{
$this->category = $category;
}
/**
* Returns an error constant if an error is present, false if there are no errors.
*
* @return bool|int
*/
public function testForErrors()
{
return false;
}
/**
* Passing in any object that implements the ObjectInterface, retrieve the Permission Response object.
*
* @param \Concrete\Core\Permission\ObjectInterface $object
*
* @return \Concrete\Core\Permission\Response\Response
*/
public static function getResponse($object)
{
$cache = Core::make('cache/request');
$identifier = sprintf('permission/response/%s/%s', get_class($object), $object->getPermissionObjectIdentifier());
$item = $cache->getItem($identifier);
if (!$item->isMiss()) {
return $item->get();
}
$className = $object->getPermissionResponseClassName();
/** @var \Concrete\Core\Permission\Response\Response $pr */
$pr = Core::make($className);
if ($object->getPermissionObjectKeyCategoryHandle()) {
$category = PermissionKeyCategory::getByHandle($object->getPermissionObjectKeyCategoryHandle());
$pr->setPermissionCategoryObject($category);
}
$pr->setPermissionObject($object);
$cache->save($item->set($pr));
return $pr;
}
/**
* This function returns true if the user has permission to the object, or false if they do not have access.
*
* @param string $permissionHandle A Permission Key Handle
* @param array $args Arguments to pass to the PermissionKey object's validate function
*
* @return bool
*
* @throws Exception
*/
public function validate($permissionHandle, $args = array())
{
$u = new User();
if ($u->isSuperUser()) {
return true;
}
if (!is_object($this->category)) {
throw new Exception(t('Unable to get category for permission %s', $permissionHandle));
}
$pk = $this->category->getPermissionKeyByHandle($permissionHandle);
if (!$pk) {
throw new Exception(t('Unable to get permission key for %s', $permissionHandle));
}
$pk->setPermissionObject($this->object);
return call_user_func_array(array($pk, 'validate'), $args);
}
public function __call($f, $a)
{
$permission = substr($f, 3);
/** @var \Concrete\Core\Utility\Service\Text $textHelper */
$textHelper = Core::make('helper/text');
$permission = $textHelper->uncamelcase($permission);
return $this->validate($permission, $a);
}
}