forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebController.php
More file actions
159 lines (133 loc) · 3.91 KB
/
WebController.php
File metadata and controls
159 lines (133 loc) · 3.91 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace PHPCensor;
use PHPCensor\Exception\HttpException\ForbiddenException;
use PHPCensor\Http\Response;
use PHPCensor\Http\Request;
use PHPCensor\Store\Factory;
use PHPCensor\Model\User;
use PHPCensor\Store\UserStore;
use PHPCensor\Exception\HttpException;
abstract class WebController extends Controller
{
/**
* @var string
*/
protected $className;
/**
* @var View
*/
protected $view = null;
/**
* @var string
*/
public $layoutName = '';
/**
* @var View
*/
public $layout = null;
/**
* @param Config $config
* @param Request $request
*/
public function __construct(Config $config, Request $request)
{
parent::__construct($config, $request);
$class = explode('\\', get_class($this));
$this->className = substr(array_pop($class), 0, -10);
}
public function init()
{
if (!empty($this->layoutName)) {
$this->layout = new View($this->layoutName);
$this->layout->title = 'PHP Censor';
$this->layout->breadcrumb = [];
$this->layout->version = trim(file_get_contents(ROOT_DIR . 'VERSION.md'));
$groups = [];
$groupStore = Factory::getStore('ProjectGroup');
$groupList = $groupStore->getWhere([], 100, 0, ['title' => 'ASC']);
foreach ($groupList['items'] as $group) {
$thisGroup = ['title' => $group->getTitle()];
$projects = Factory::getStore('Project')->getByGroupId($group->getId(), false);
$thisGroup['projects'] = $projects['items'];
$groups[] = $thisGroup;
}
$archivedProjects = Factory::getStore('Project')->getAll(true);
$this->layout->archivedProjects = $archivedProjects['items'];
$this->layout->groups = $groups;
}
}
/**
* Handle the incoming request.
*
* @param string $action
* @param array $actionParams
*
* @return Response
*/
public function handleAction($action, $actionParams)
{
if (View::exists($this->className . '/' . $action)) {
$this->view = new View($this->className . '/' . $action);
}
$result = parent::handleAction($action, $actionParams);
if ($result instanceof Response) {
return $result;
}
$content = '';
if (is_string($result)) {
$content = $result;
} elseif ($this->view) {
$content = $this->view->render();
}
$response = new Response();
if ($this->layout) {
$this->layout->content = $content;
$response->setContent($this->layout->render());
} else {
$response->setContent($content);
}
return $response;
}
/**
* Require that the currently logged in user is an administrator.
*
* @throws HttpException
*/
protected function requireAdmin()
{
if (!$this->currentUserIsAdmin()) {
throw new ForbiddenException('You do not have permission to do that.');
}
}
/**
* Check if the currently logged in user is an administrator.
*
* @return bool
*
* @return bool
*
* @throws HttpException
*/
protected function currentUserIsAdmin()
{
$user = $this->getUser();
if (!$user) {
return false;
}
return $this->getUser()->getIsAdmin();
}
/**
* @return User|null
*
* @throws HttpException
*/
protected function getUser()
{
if (empty($_SESSION['php-censor-user-id'])) {
return null;
}
/** @var UserStore $userStore */
$userStore = Factory::getStore('User');
return $userStore->getById($_SESSION['php-censor-user-id']);
}
}