-
Notifications
You must be signed in to change notification settings - Fork 90
/
AuthModule.php
executable file
·155 lines (142 loc) · 4.51 KB
/
AuthModule.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
<?php
/**
* AuthModule class file.
* @author Christoffer Niska <[email protected]>
* @copyright Copyright © Christoffer Niska 2012-
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @package auth
* @version 1.7.0
*/
/**
* Web module for managing Yii's built-in authorization manager (CAuthManager).
*/
class AuthModule extends CWebModule
{
/**
* @var boolean whether to enable the RBAC strict mode.
* When enabled items cannot be assigned children of the same type.
*/
public $strictMode = true;
/**
* @var string name of the user model class.
* Change this if your user model name is different than the default value.
*/
public $userClass = 'User';
/**
* @var string name of the user id column.
* Change this if the id column in your user table is different than the default value.
*/
public $userIdColumn = 'id';
/**
* @var string name of the user name column.
* Change this if the name column in your user table is different than the default value.
*/
public $userNameColumn = 'name';
/**
* @var string the application layout.
* Change this if you wish to use a different layout with the module.
*/
public $defaultLayout = 'application.views.layouts.main';
/**
* @var array map of flash message keys to use for the module.
*/
public $flashKeys = array();
/**
* @var string string the id of the default controller for this module.
*/
public $defaultController = 'assignment';
/**
* @var boolean whether to force copying of assets.
* Useful during development and when upgrading the module.
*/
public $forceCopyAssets = false;
/**
* @var string path to view files for this module.
* Specify this to use your own views instead of those shipped with the module.
*/
public $viewDir;
private $_assetsUrl;
/**
* Initializes the module.
*/
public function init()
{
$this->setImport(
array(
'auth.components.*',
'auth.controllers.*',
'auth.models.*',
'auth.widgets.*',
)
);
$this->registerCss();
$this->flashKeys = array_merge(
$this->flashKeys,
array(
'error' => 'error',
'info' => 'info',
'success' => 'success',
'warning' => 'warning',
)
);
if (isset($this->viewDir)) {
if (strpos($this->viewDir, '.')) {
$this->viewDir = Yii::getPathOfAlias($this->viewDir);
}
$this->setLayoutPath($this->viewDir . DIRECTORY_SEPARATOR . 'layouts');
$this->setViewPath($this->viewDir);
}
}
/**
* Registers the module CSS.
*/
public function registerCss()
{
Yii::app()->clientScript->registerCssFile($this->getAssetsUrl() . '/css/auth.css');
}
/**
* The pre-filter for controller actions.
* @param CController $controller the controller.
* @param CAction $action the action.
* @return boolean whether the action should be executed.
* @throws CException|CHttpException if user is denied access.
*/
public function beforeControllerAction($controller, $action)
{
if (parent::beforeControllerAction($controller, $action)) {
$user = Yii::app()->getUser();
if ($user instanceof AuthWebUser) {
if ($user->isAdmin) {
return true;
} elseif ($user->isGuest) {
$user->loginRequired();
}
} else {
throw new CException('WebUser component is not an instance of AuthWebUser.');
}
}
throw new CHttpException(401, Yii::t('AuthModule.main', 'Access denied.'));
}
/**
* Returns the URL to the published assets folder.
* @return string the URL.
*/
protected function getAssetsUrl()
{
if (isset($this->_assetsUrl)) {
return $this->_assetsUrl;
} else {
$assetsPath = Yii::getPathOfAlias('auth.assets');
$assetsUrl = Yii::app()->assetManager->publish($assetsPath, false, -1, $this->forceCopyAssets);
return $this->_assetsUrl = $assetsUrl;
}
}
/**
* Returns the module version number.
* @return string the version.
*/
public function getVersion()
{
return '1.7.0';
}
}