-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSrbacModule.php
351 lines (323 loc) · 11.2 KB
/
SrbacModule.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
<?php
/**
* SrbacModule class file.
*
* @author Spyros Soldatos <[email protected]>
* @link http://code.google.com/p/srbac/
*/
/**
* SrbacModule is the module that loads the srbac module in the application
*
* @author Spyros Soldatos <[email protected]>
* @package srbac
* @since 1.0.0
*/
class SrbacModule extends CWebModule {
//Constants
const ICON_PACKS = "noia,tango";
const PRIVATE_ATTRIBUTES = "_icons,_cssPublished,_imagesPublished,defaultController,controllerMap,preload,behaviors";
const TABLE_NAMES_ERROR = "Srbac is installed but the CDBAuthManger table names in the database are different from those
in the CDBAuthManager configuration.<br />A common mistake is that names in database are in lowercase.<br />Srbac may not work correctly!!!";
//Private attributes
/* @var $_icons String The path to the icons */
private $_icons;
/* @var $_yiiSupportedVersion String The yii version tha srbac supports */
private $_yiiSupportedVersion = "1.1.0";
/* @var $_version Srbac version */
private $_version = "1.3beta";
/* @var $_cssPublished boolean If css file exists and is published */
private $_cssPublished = false;
/* @var $_imagesPublished boolean If images files exists and are published */
private $_imagesPublished = false;
// Srbac Attributes
/* @var $debug If srbac is in debug mode */
private $_debug = false;
/* @var $pagesize int The number of items displayed in each page*/
private $_pageSize = 15;
/* @var $alwaysAllowed mixed The actions that are always allowed*/
private $_alwaysAllowed = array();
/* @var $userActions mixed Operations assigned to users by default*/
private $_userActions = array();
/* @var $listBoxNumberOfLines integer The number of lines in the assign tabview listboxes */
private $_listBoxNumberOfLines = 10;
/* @var $iconText boolean Display text next to the icons */
private $_iconText = false;
/* @var $_useHeader boolean Use header or not */
private $_showHeader = false;
/* @var $_useFooter boolean Use footer or not */
private $_showFooter = false;
/* @var $_cssUrl The url of the css file to register */
private $_cssUrl;
/* @deprecated $useAlwaysAllowedGui boolean */
public $useAlwaysAllowedGui;
/* @var $_message A warning/error message displayed in the top of each page */
private $_message ="";
/* @var $userid String The primary column of the users table*/
public $userid = "userid";
/* @var $username String The username column of the users table*/
public $username = "username";
/* @var $userclass String The name of the users Class*/
public $userclass = "User";
/* @var $username1 String The username column of the users table*/
public $username1 = "";
/* @var $groupid String The primary column of the groups table*/
public $groupid = "groupid";
/* @var $groupname String The groupname column of the groups table*/
public $groupname = "groupname";
/* @var $groupclass String The name of the groups Class*/
public $groupclass = "Group";
/* @var $superUser String The name of the superuser */
public $superUser = "Authorizer";
/* @var $css string The css to use */
public $css = "srbac.css";
/* @var $notAuthorizedView String The view to render when unathorized access*/
public $notAuthorizedView = "srbac.views.authitem.unauthorized";
/* @var $imagesPath string The path to srbac images*/
public $imagesPath = "srbac.images";
/* @var $imagesPack String The images theme to use*/
public $imagesPack = "noia";
/* @var $header String Srbac header*/
public $header = "srbac.views.authitem.header";
/* @var $footer String Srbac footer*/
public $footer = "srbac.views.authitem.footer";
/* @var $alwaysAllowedPath String */
public $alwaysAllowedPath = "srbac.components";
/* @var $delimeter The delimeter used in modules between moduleId and itemId */
public $delimeter = "-";
/**
* this method is called when the module is being created you may place code
* here to customize the module or the application
*/
public function init() {
// import the module-level models and components
$this->setImport(array(
//'application.models.Group',
'srbac.models.*',
'srbac.components.Helper',
'srbac.components.SHtml',
'srbac.controllers.SBaseController'
));
//Set layout to main
if($this->layout =="") {
$this->layout = "application.views.layouts.main";
}
//Publish css
$this->_cssPublished = Helper::publishCss($this->css);
//Publish images
$this->setIconsPath(Helper::publishImages($this->imagesPath,$this->imagesPack));
$this->_imagesPublished = $this->getIconsPath() == "" ? false : true;
//Create the translation component
$this->setComponents(
array(
'tr'=>array(
'class'=>'CPhpMessageSource',
'basePath'=> dirname(__FILE__).DIRECTORY_SEPARATOR.'messages',
'onMissingTranslation'=>"Helper::markWords"
),
)
);
}
// SETTERS & GETTERS
public function setCssUrl($cssUrl) {
$this->_cssUrl = $cssUrl;
}
public function getCssUrl() {
return $this->_cssUrl;
}
public function setDebug($debug) {
if(is_bool($debug)) {
$this->_debug = $debug;
} else {
throw new CException("Wrong value for srbac attribute debug in srbac configuration.
'".$debug."' is not a boolean.");
}
}
public function getDebug() {
return $this->_debug;
}
public function setPageSize($pageSize) {
if(is_numeric($pageSize)) {
$this->_pageSize = (int) $pageSize;
} else {
throw new CException("Wrong value for srbac attribute pageSize in srbac configuration.
'".$pageSize."' is not an integer.");
}
}
public function getPageSize() {
return $this->_pageSize;
}
public function setAlwaysAllowed($alwaysAllowed) {
$this->_alwaysAllowed = $alwaysAllowed;
}
public function getAlwaysAllowed() {
$paramAllowed = array();
if(!is_file($this->getAlwaysAllowedFile())) {
$handle = fopen($this->getAlwaysAllowedFile(), "wb");
fwrite($handle, "<?php\n return array();\n?>");
fclose($handle);
}
$guiAllowed = include($this->getAlwaysAllowedFile());
if(!is_array($guiAllowed)){
$guiAllowed = array();
}
if(is_array($this->_alwaysAllowed)) {
$paramAllowed = $this->_alwaysAllowed;
}else if(is_file(Yii::getPathOfAlias($this->_alwaysAllowed).".php")) {
$paramAllowed = include(Yii::getPathOfAlias($this->_alwaysAllowed).".php");
} else if(is_string($this->_alwaysAllowed)) {
$paramAllowed = split(",", $this->_alwaysAllowed);
}
return array_merge($guiAllowed, $paramAllowed);
}
public function getAlwaysAllowedFile() {
return Yii::getPathOfAlias($this->alwaysAllowedPath).DIRECTORY_SEPARATOR."allowed.php";
}
public function setUserActions($userActions) {
if(is_array($userActions)) {
$this->_userActions = $userActions;
} else {
$this->_userActions = explode(",",$userActions);
}
}
public function getUserActions() {
return $this->_userActions;
}
public function setListBoxNumberOfLines($size) {
if(is_numeric($size)) {
$this->_listBoxNumberOfLines = (int) $size;
} else {
throw new CException("Wrong value for srbac attribute listBoxNumberOfLines in srbac configuration.
'".$size."' is not an integer.");
}
}
public function getListBoxNumberOfLines() {
return $this->_listBoxNumberOfLines;
}
public function setIconText($iconText) {
if(is_bool($iconText)) {
$this->_iconText = $iconText;
} else {
throw new CException("Wrong value for srbac attribute iconText in srbac configuration.
'".$iconText."' is not a boolean.");
}
}
public function getIconText() {
return $this->_iconText;
}
public function setShowHeader($useHeader) {
if(is_bool($useHeader)) {
$this->_showHeader = $useHeader;
} else {
throw new CException("Wrong value for srbac attribute useHeader in srbac configuration.
'".$useHeader."' is not a boolean.");
}
}
public function getShowHeader() {
return $this->_showHeader;
}
public function setShowFooter($useFooter) {
if(is_bool($useFooter)) {
$this->_showFooter = $useFooter;
} else {
throw new CException("Wrong value for srbac attribute footer in srbac configuration.
'".$useFooter."' is not a boolean.");
}
}
public function getShowFooter() {
return $this->_showFooter;
}
/**
* Checks if srbac is installed by checking if Auth items table exists.
* @return boolean Whether srbac is installed or not
*/
public function isInstalled() {
try {
$tables = Yii::app()->authManager->db->schema->tableNames;
$itemTableName = Yii::app()->authManager->itemTable;
$itemChildTableName = Yii::app()->authManager->itemChildTable ;
$assignmentTableName = Yii::app()->authManager->assignmentTable ;
$tablePrefix = AuthItem::model()->getDbConnection()->tablePrefix;
if(!is_null($tablePrefix)) {
$itemTableName = preg_replace('/{{(.*?)}}/',$tablePrefix.'\1',$itemTableName);
$itemChildTableName = preg_replace('/{{(.*?)}}/',$tablePrefix.'\1',$itemChildTableName);
$assignmentTableName = preg_replace('/{{(.*?)}}/',$tablePrefix.'\1',$assignmentTableName);
}
if(in_array($itemTableName, $tables) &&
in_array($itemChildTableName, $tables) &&
in_array($assignmentTableName, $tables)) {
return true;
}else {
$tables = array_map('strtolower', $tables);
if(in_array(strtolower($itemTableName), $tables) &&
in_array(strtolower($itemChildTableName), $tables) &&
in_array(strtolower($assignmentTableName), $tables)) {
$this->_message = self::TABLE_NAMES_ERROR;
return true;
}
}
return false;
} catch (CDbException $ex ) {
return false;
}
}
/**
* Gets the user's class
* @return userclass
*/
public function getUserModel() {
return new $this->userclass;
}
/**
* Gets the group's class
* @return groupclass
*/
public function getGroupModel() {
return new $this->groupclass;
}
/**
* this method is called before any module controller action is performed
* you may place customized code here
* @param CController $controller
* @param CAction $action
* @return boolean
*/
public function beforeControllerAction($controller, $action) {
if(parent::beforeControllerAction($controller, $action)) {
return true;
}
else
return false;
}
/**
* Gets the path to the icon files
* @return String The path to the icons
*/
public function getIconsPath() {
return $this->_icons;
}
public function setIconsPath($path) {
$this->_icons = $path;
}
public function getSupportedYiiVersion() {
return $this->_yiiSupportedVersion;
}
public function getVersion() {
return $this->_version;
}
public function isCssPublished() {
return $this->_cssPublished;
}
public function isImagesPublished() {
return $this->_imagesPublished;
}
public function getAttributes() {
return get_object_vars($this);
}
public function getMessage() {
if($this->_message != ""){
return Helper::translate("srbac",$this->_message);
} else {
return "";
}
}
}