forked from bizley/yii2-podium
/
Module.php
366 lines (329 loc) · 14.8 KB
/
Module.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
/**
* TODO:
* -----------------------------------------------------------------------------
* panel glowny admina
* grupy userow
* maintance mode
* rss
* -----------------------------------------------------------------------------
*
* Podium Module
* Yii 2 Forum Module
*
* @author Paweł Bizley Brzozowski <pb@human-device.com>
* @version 0.1 (beta)
* @license TBA
*
* https://github.com/bizley-code/yii2-podium
* Please report all issues at GitHub
* https://github.com/bizley-code/yii2-podium/issues
*
* Podium requires Yii 2.0.4 or newer
* http://www.yiiframework.com
* https://github.com/yiisoft/yii2
*/
namespace bizley\podium;
use bizley\podium\components\Cache;
use bizley\podium\components\Config;
use bizley\podium\components\DbTarget;
use bizley\podium\components\Installation;
use bizley\podium\components\PodiumUser;
use bizley\podium\models\Activity;
use Yii;
use yii\base\BootstrapInterface;
use yii\base\InvalidConfigException;
use yii\base\Module as BaseModule;
use yii\console\Application as ConsoleApplication;
use yii\web\Application as WebApplication;
use yii\web\GroupUrlRule;
/**
* Podium Module
* @author Paweł Bizley Brzozowski <pb@human-device.com>
* @since 0.1
* requires 'bootstrap' => ['log', 'podium'],
*/
class Module extends BaseModule implements BootstrapInterface
{
const MODE_BAU = 'BAU';
const MODE_INSTALL = 'INSTALL';
const USER_INHERIT = 'inherit';
const USER_OWN = 'own';
const RBAC_INHERIT = 'inherit';
const RBAC_OWN = 'own';
const ROUTE_DEFAULT = '/podium/default/index';
const ROUTE_LOGIN = '/podium/account/login';
const ROUTE_REGISTER = '/podium/account/register';
const MAIN_LAYOUT = 'main';
/**
* @var null|integer Admin account ID if $user is set to 'inherit'.
*/
public $adminId;
/**
* @var string Controller namespace
*/
public $controllerNamespace = 'bizley\podium\controllers';
/**
* @var string|array the URL for user login.
* If this property is null login link is not provided in menu.
*/
public $loginUrl = [self::ROUTE_LOGIN];
/**
* @var string Module mode
*/
public $mode = self::MODE_BAU;
/**
* @var string Module RBAC component
*/
public $rbacComponent = self::RBAC_OWN;
/**
* @var string|array the URL for new user registering.
* If this property is null registration link is not provided in menu.
*/
public $registerUrl = [self::ROUTE_REGISTER];
/**
* @var string Module user component
*/
public $userComponent = self::USER_OWN;
/**
* @var Config Module configuration instance
*/
protected $_config;
/**
* @var boolean Installation flag
*/
protected $_installed = false;
/**
* @var string Module version
*/
protected $_version = '0.1';
/**
* Registers user activity after every action.
* @see \bizley\podium\models\Activity
*
* @param \yii\base\Action $action the action just executed.
* @param mixed $result the action return result.
* @return mixed the processed action result.
*/
public function afterAction($action, $result)
{
$parentResult = parent::afterAction($action, $result);
if (Yii::$app instanceof WebApplication && !in_array($action->id, ['import', 'run'])) {
Activity::add();
}
return $parentResult;
}
/**
* Bootstrap method to be called during application bootstrap stage.
* Adding routing rules and log target.
*
* @param \yii\base\Application $app the application currently running
*/
public function bootstrap($app)
{
if ($app instanceof WebApplication) {
$app->getUrlManager()->addRules([
new GroupUrlRule([
'prefix' => 'podium',
'rules' => [
'activate/<token:[\w\-]+>' => 'account/activate',
'admin/ban/<id:\d+>' => 'admin/ban',
'admin/contents/<name:[\w\-]+>' => 'admin/contents',
'admin/delete/<id:\d+>' => 'admin/delete',
'admin/delete-category/<id:\d+>' => 'admin/delete-category',
'admin/delete-forum/<cid:\d+>/<id:\d+>' => 'admin/delete-forum',
'admin/edit-category/<id:\d+>' => 'admin/edit-category',
'admin/edit-forum/<cid:\d+>/<id:\d+>' => 'admin/edit-forum',
'admin/forums/<cid:\d+>' => 'admin/forums',
'admin/mod/<uid:\d+>/<fid:\d+>' => 'admin/mod',
'admin/mods/<id:\d+>' => 'admin/mods',
'admin/new-forum/<cid:\d+>' => 'admin/new-forum',
'admin/pm/<id:\d+>' => 'admin/pm',
'admin/update/<id:\d+>' => 'admin/update',
'admin/view/<id:\d+>' => 'admin/view',
'admin' => 'admin/index',
'category/<id:\d+>/<slug:[\w\-]+>' => 'default/category',
'delete/<cid:\d+>/<fid:\d+>/<id:\d+>/<slug:[\w\-]+>' => 'default/delete',
'deletepost/<cid:\d+>/<fid:\d+>/<tid:\d+>/<pid:\d+>' => 'default/deletepost',
'deleteposts/<cid:\d+>/<fid:\d+>/<id:\d+>/<slug:[\w\-]+>' => 'default/deleteposts',
'demote/<id:\d+>' => 'admin/demote',
'edit/<cid:\d+>/<fid:\d+>/<tid:\d+>/<pid:\d+>' => 'default/edit',
'forum/<cid:\d+>/<id:\d+>/<slug:[\w\-]+>' => 'default/forum',
'home' => 'default/index',
'install' => 'install/run',
'last/<id:\d+>' => 'default/last',
'lock/<cid:\d+>/<fid:\d+>/<id:\d+>/<slug:[\w\-]+>' => 'default/lock',
'login' => 'account/login',
'logout' => 'profile/logout',
'members/posts/<id:\d+>/<slug:[\w\-]+>' => 'members/posts',
'members/threads/<id:\d+>/<slug:[\w\-]+>' => 'members/threads',
'members/view/<id:\d+>/<slug:[\w\-]+>' => 'members/view',
'members' => 'members/index',
'members/ignore/<id:\d+>' => 'members/ignore',
'messages/delete/<id:\d+>' => 'messages/delete',
'messages/new/<user:\d+>' => 'messages/new',
'messages/reply/<id:\d+>' => 'messages/reply',
'messages/view/<id:\d+>' => 'messages/view',
'move/<cid:\d+>/<fid:\d+>/<id:\d+>/<slug:[\w\-]+>' => 'default/move',
'moveposts/<cid:\d+>/<fid:\d+>/<id:\d+>/<slug:[\w\-]+>' => 'default/moveposts',
'new-email/<token:[\w\-]+>' => 'account/new-email',
'new-thread/<cid:\d+>/<fid:\d+>' => 'default/new-thread',
'pin/<cid:\d+>/<fid:\d+>/<id:\d+>/<slug:[\w\-]+>' => 'default/pin',
'post/<cid:\d+>/<fid:\d+>/<tid:\d+>/<pid:\d+>' => 'default/post',
'post/<cid:\d+>/<fid:\d+>/<tid:\d+>' => 'default/post',
'profile' => 'profile/index',
'profile/add/<id:\d+>' => 'profile/add',
'profile/delete/<id:\d+>' => 'profile/delete',
'profile/mark/<id:\d+>' => 'profile/mark',
'promote/<id:\d+>' => 'admin/promote',
'reactivate' => 'account/reactivate',
'register' => 'account/register',
'report/<cid:\d+>/<fid:\d+>/<tid:\d+>/<pid:\d+>/<slug:[\w\-]+>' => 'default/report',
'reset' => 'account/reset',
'rss' => 'default/rss',
'search' => 'default/search',
'show/<id:\d+>' => 'default/show',
'thread/<cid:\d+>/<fid:\d+>/<id:\d+>/<slug:[\w\-]+>' => 'default/thread',
],
])], false);
$dbTarget = new DbTarget;
$dbTarget->logTable = '{{%podium_log}}';
$dbTarget->categories = ['bizley\podium\*'];
$dbTarget->logVars = [];
$app->getLog()->targets['podium'] = $dbTarget;
}
elseif ($app instanceof ConsoleApplication) {
$this->controllerNamespace = 'bizley\podium\console';
}
}
/**
* Gets Podium configuration instance.
*
* @return Config configuration instance
*/
public function getConfig()
{
if (!$this->_config) {
$this->_config = Config::getInstance();
}
return $this->_config;
}
/**
* Checks wheter Podium has been already installed.
*
* @return boolean
*/
public function getInstalled()
{
return $this->_installed;
}
/**
* Redirects to Podium main controller's action.
*
* @return \yii\web\Response
*/
public function goPodium()
{
return Yii::$app->getResponse()->redirect([self::ROUTE_DEFAULT]);
}
/**
* Initializes the module for web app.
* Sets Podium alias and layout.
* Registers user identity, authorization, translations and formatter.
* Verifies the installation.
*/
public function init()
{
parent::init();
if (in_array($this->mode, [self::MODE_BAU, self::MODE_INSTALL])) {
if (in_array($this->userComponent, [self::USER_INHERIT, self::USER_OWN])) {
if (in_array($this->rbacComponent, [self::RBAC_INHERIT, self::RBAC_OWN])) {
$this->setAliases(['@podium' => '@vendor/bizley/podium']);
if (Yii::$app instanceof WebApplication) {
if ($this->userComponent == self::USER_OWN) {
$this->registerIdentity();
}
if ($this->rbacComponent == self::RBAC_OWN) {
$this->registerAuthorization();
}
$this->registerTranslations();
$this->registerFormatter();
$this->layout = self::MAIN_LAYOUT;
$this->_installed = Installation::check();
}
}
else {
throw InvalidConfigException('Invalid value for the rbac parameter.');
}
}
else {
throw InvalidConfigException('Invalid value for the user parameter.');
}
}
else {
throw InvalidConfigException('Invalid value for the mode parameter.');
}
}
/**
* Registers user authorization.
* @see \bizley\podium\components\Installation
*/
public function registerAuthorization()
{
Yii::$app->setComponents([
'authManager' => [
'class' => 'yii\rbac\DbManager',
'itemTable' => '{{%podium_auth_item}}',
'itemChildTable' => '{{%podium_auth_item_child}}',
'assignmentTable' => '{{%podium_auth_assignment}}',
'ruleTable' => '{{%podium_auth_rule}}',
'cache' => Cache::getInstance()->cache
],
]);
}
/**
* Registers formatter for signed users with chosen timezone.
*/
public function registerFormatter()
{
Yii::$app->setComponents([
'formatter' => [
'class' => 'yii\i18n\Formatter',
'timeZone' => Yii::$app->user->isGuest ? 'UTC' : (new PodiumUser)->getTimeZone(),
],
]);
}
/**
* Registers user identity.
* @see \bizley\podium\models\User
*/
public function registerIdentity()
{
Yii::$app->setComponents([
'user' => [
'class' => 'yii\web\User',
'identityClass' => 'bizley\podium\models\User',
'enableAutoLogin' => true,
'loginUrl' => ['/podium/account/login'],
'identityCookie' => ['name' => 'podium', 'httpOnly' => true],
'idParam' => '__id_podium',
],
]);
}
/**
* Registers translations.
*/
public function registerTranslations()
{
Yii::$app->i18n->translations['podium/*'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en-US',
'basePath' => '@podium/messages',
'fileMap' => [
'podium/flash' => 'flash.php',
'podium/layout' => 'layout.php',
'podium/view' => 'view.php',
],
];
}
}