/
user_interface.php
106 lines (92 loc) · 2.74 KB
/
user_interface.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
<?php
namespace Concrete\Controller\Backend;
use Concrete\Core\Controller\Controller;
use Concrete\Core\Error\UserMessageException;
use Concrete\Core\Support\Facade\Application;
use Concrete\Core\View\DialogView;
use Concrete\Core\Http\Request;
abstract class UserInterface extends Controller
{
/** @var \Concrete\Core\Application\Application */
protected $app;
/**
* The current errors container.
*
* @var \Concrete\Core\Error\ErrorList\ErrorList
*/
protected $error;
/**
* An identifier to be used when checking tokens.
*
* @var string|null
*/
protected $validationToken;
public function __construct()
{
$this->app = Application::getFacadeApplication();
$this->error = $this->app->make('error');
$this->view = new DialogView($this->viewPath);
if (preg_match('/Concrete\\\Package\\\(.*)\\\Controller/i', get_class($this), $matches)) {
$pkgHandle = uncamelcase($matches[1]);
$this->view->setPackageHandle($pkgHandle);
}
$this->view->setController($this);
$this->request = Request::getInstance();
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Controller\AbstractController::shouldRunControllerTask()
*/
public function shouldRunControllerTask()
{
return $this->canAccess();
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Controller\Controller::getViewObject()
*/
public function getViewObject()
{
if ($this->canAccess()) {
return parent::getViewObject();
}
throw new UserMessageException(t('Access Denied'));
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Controller\Controller::action()
*/
public function action()
{
$token = isset($this->validationToken) ? $this->validationToken : get_class($this);
$url = (string) call_user_func_array('parent::action', func_get_args());
$url .= (strpos($url, '?') === false ? '?' : '&') . $this->app->make('token')->getParameter($token);
return $url;
}
/**
* Can the current page be accessed?
*
* @return bool
*/
abstract protected function canAccess();
/**
* Check whether the token is valid and if the current page be accessed.
*
* @return bool
*/
protected function validateAction()
{
$token = (isset($this->validationToken)) ? $this->validationToken : get_class($this);
if (!$this->app->make('token')->validate($token)) {
$this->error->add($this->app->make('token')->getErrorMessage());
return false;
}
if (!$this->canAccess()) {
return false;
}
return true;
}
}