forked from na3r/yii-rights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RightsModule.php
218 lines (201 loc) · 5.2 KB
/
RightsModule.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
<?php
/**
* Rights module class file.
*
* @author Christoffer Niska <[email protected]>
* @copyright Copyright © 2010 Christoffer Niska
* @version 1.3.0
*
* DO NOT CHANGE THE DEFAULT CONFIGURATION VALUES!
*
* You may overload the module configuration values in your rights-module
* configuration like so:
*
* 'modules'=>array(
* 'rights'=>array(
* 'userNameColumn'=>'name',
* 'flashSuccessKey'=>'success',
* 'flashErrorKey'=>'error',
* ),
* ),
*/
class RightsModule extends CWebModule
{
/**
* @property string the name of the role with superuser privileges.
*/
public $superuserName = 'Admin';
/**
* @property string the name of the guest role.
*/
public $authenticatedName = 'Authenticated';
/**
* @property string the name of the user model class.
*/
public $userClass = 'User';
/**
* @property string the name of the id column in the user table.
*/
public $userIdColumn = 'id';
/**
* @property string the name of the username column in the user table.
*/
public $userNameColumn = 'username';
/**
* @property boolean whether to enable business rules.
*/
public $enableBizRule = true;
/**
* @property boolean whether to enable data for business rules.
*/
public $enableBizRuleData = false;
/**
* @property boolean whether to display authorization items description
* instead of name it is set.
*/
public $displayDescription = true;
/**
* @property string the flash message key to use for success messages.
*/
public $flashSuccessKey = 'RightsSuccess';
/**
* @property string the flash message key to use for error messages.
*/
public $flashErrorKey = 'RightsError';
/**
* @property boolean whether to install rights when accessed.
*/
public $install = false;
/**
* @property string the base url to Rights. Override when module is nested.
*/
public $baseUrl = '/rights';
/**
* @property string the path to the layout file to use for displaying Rights.
*/
public $layout = 'rights.views.layouts.main';
/**
* @property string the path to the application layout file.
*/
public $appLayout = 'application.views.layouts.main';
/**
* @property string the style sheet file to use for Rights.
*/
public $cssFile;
/**
* @property boolean whether to enable debug mode.
*/
public $debug = false;
private $_assetsUrl;
/**
* Initializes the "rights" module.
*/
public function init()
{
// Set required classes for import.
$this->setImport(array(
'rights.components.*',
'rights.components.behaviors.*',
'rights.components.dataproviders.*',
'rights.controllers.*',
'rights.models.*',
));
// Set the required components.
$this->setComponents(array(
'authorizer'=>array(
'class'=>'RAuthorizer',
'superuserName'=>$this->superuserName,
),
'generator'=>array(
'class'=>'RGenerator',
),
));
// Normally the default controller is Assignment.
$this->defaultController = 'assignment';
// Set the installer if necessary.
if( $this->install===true )
{
$this->setComponents(array(
'installer'=>array(
'class'=>'RInstaller',
'superuserName'=>$this->superuserName,
'authenticatedName'=>$this->authenticatedName,
'guestName'=>Yii::app()->user->guestName,
'defaultRoles'=>Yii::app()->authManager->defaultRoles,
),
));
// When installing we need to set the default controller to Install.
$this->defaultController = 'install';
}
}
/**
* Registers the necessary scripts.
*/
public function registerScripts()
{
// Get the url to the module assets
$assetsUrl = $this->getAssetsUrl();
// Register the necessary scripts
$cs = Yii::app()->getClientScript();
$cs->registerCoreScript('jquery');
$cs->registerCoreScript('jquery.ui');
$cs->registerScriptFile($assetsUrl.'/js/rights.js');
$cs->registerCssFile($assetsUrl.'/css/core.css');
// Make sure we want to register a style sheet.
if( $this->cssFile!==false )
{
// Default style sheet is used unless one is provided.
if( $this->cssFile===null )
$this->cssFile = $assetsUrl.'/css/default.css';
else
$this->cssFile = Yii::app()->request->baseUrl.$this->cssFile;
// Register the style sheet
$cs->registerCssFile($this->cssFile);
}
}
/**
* Publishes the module assets path.
* @return string the base URL that contains all published asset files of Rights.
*/
public function getAssetsUrl()
{
if( $this->_assetsUrl===null )
{
$assetsPath = Yii::getPathOfAlias('rights.assets');
// We need to republish the assets if debug mode is enabled.
if( $this->debug===true )
$this->_assetsUrl = Yii::app()->getAssetManager()->publish($assetsPath, false, -1, true);
else
$this->_assetsUrl = Yii::app()->getAssetManager()->publish($assetsPath);
}
return $this->_assetsUrl;
}
/**
* @return RightsAuthorizer the authorizer component.
*/
public function getAuthorizer()
{
return $this->getComponent('authorizer');
}
/**
* @return RightsInstaller the installer component.
*/
public function getInstaller()
{
return $this->getComponent('installer');
}
/**
* @return RightsGenerator the generator component.
*/
public function getGenerator()
{
return $this->getComponent('generator');
}
/**
* @return the current version.
*/
public function getVersion()
{
return '1.3.0';
}
}