/
Statuses.php
118 lines (89 loc) · 3.09 KB
/
Statuses.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
<?php
namespace statuses;
use yii\base\ErrorException;
/**
*
* Main class for Statuses module.
*
* @prop string|array signedUp The route to go after successful login
*
*/
class Statuses extends \yii\base\Module
{
/**
* @inherit
*/
public $controllerNamespace = 'statuses\controllers';
/**
* Current project ID
*/
public $project_id = 2;
/**
* Database component to use in the module
*/
public $db;
/**
* Class for access methods, implements StatusesAccessInterface
*/
public $accessClass;
/**
* Class for access rights from DB (and search rights)
*/
public $accessRightsClass;
public $accessRightsSearchClass;
/**
* @inherit
*/
public function init()
{
parent::init();
$this->checkAccessClassConfig();
$this->checkAccessRightsClassConfig();
$this->registerTranslations();
}
/**
* Initialization of the i18n translation module
*/
public function registerTranslations()
{
\Yii::$app->i18n->translations['statuses'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en',
'basePath' => '@statuses/messages',
'fileMap' => [
'statuses' => 'statuses.php',
],
];
}
/**
*
*/
protected function checkAccessClassConfig() {
$reflectionClass = new \ReflectionClass( $this->accessClass );
if( $reflectionClass->implementsInterface('\statuses\StatusesAccessInterface') == false ) {
throw new ErrorException('Statuses::accessClass must implement StatusesAccessInterface.');
}
}
/**
*
*/
protected function checkAccessRightsClassConfig() {
// check property 'accessRightsClass'
$rightsClass = $this->accessRightsClass;
$rights = new $rightsClass();
if( !is_subclass_of( $rights, '\yii\db\ActiveRecord', false ) ) throw new ErrorException('Statuses::accessRightsClass must be extended from class \yii\db\ActiveRecord.');
try {
$rights->id;
$rights->name;
} catch( \yii\base\UnknownPropertyException $e ) {
throw new ErrorException('Statuses::accessRightsClass must have properties: id, name.');
}
// check property 'accessRightsSearchClass'
$rightsSearchClass = $this->accessRightsSearchClass;
$rightsSearch = new $rightsSearchClass();
if( !is_subclass_of( $rightsSearch, $this->accessRightsClass, false ) ) throw new ErrorException('Statuses::accessRightsSearchClass must be extended from class '.$this->accessRightsClass.'.');
$reflectRightsSearch = new \ReflectionClass( $this->accessRightsSearchClass );
if( !$reflectRightsSearch->hasMethod('search') ) throw new ErrorException('Statuses::accessRightsSearchClass must have public method "search( array $params )".');
return true;
}
}