1: <?php
2:
3:
4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
21: class WsauthController
22: {
23: 24: 25: 26:
27: public $layout;
28: 29: 30: 31:
32: public $title;
33: 34: 35: 36:
37: public $breadcrumbs;
38: 39: 40: 41:
42: private $_action;
43: 44: 45: 46:
47: private $_params = array();
48: 49: 50: 51:
52: private $_auth;
53:
54:
55: public function __construct()
56: {
57: $this->layout = WsConfig::get('html_layout');
58: $this->title = WsConfig::get('app_name');
59: $this->breadcrumbs = array();
60:
61: $this->_auth = new WsAuth();
62: }
63:
64:
65: 66: 67: 68: 69: 70:
71: private function renderView()
72: {
73:
74: $fileName = WsROOT.'/protected/library/views/'
75: .$this->_action.'.php';
76:
77:
78: if (!empty($this->_params)) {
79: extract($this->_params);
80: }
81:
82: ob_start();
83:
84: if (is_file($fileName)) {
85: include($fileName);
86: } else {
87: ob_get_clean();
88: header('HTTP/1.1 500 Internal Server Error');
89: trigger_error('The view file <strong>'.$fileName.
90: '</strong> is not available.', E_USER_ERROR);
91: }
92:
93: $content = ob_get_clean();
94:
95: return $content;
96: }
97:
98: 99: 100: 101: 102: 103: 104: 105: 106: 107:
108: private function render($action = 'admin', $params = array())
109: {
110:
111: $this->_action = $action;
112:
113: $this->_params = $params;
114:
115:
116:
117: $WsTitle = $this->title;
118:
119: $WsBreadcrumbs = $this->breadcrumbs;
120:
121: $WsContent = self::renderView();
122:
123:
124: $layoutFile = WsROOT.'/public/layouts/'.$this->layout;
125: if (is_file($layoutFile)) {
126:
127: include($layoutFile);
128: } else {
129:
130: echo $WsContent;
131: }
132: }
133:
134:
135: 136: 137: 138:
139: public function register($email=null, $password=null)
140: {
141:
142: $this->breadcrumbs = array(
143: WsLocalize::msg('home') => array(
144: 'site',
145: 'index'
146: ),
147: WsLocalize::msg('register') => array(
148: 'wsauth',
149: 'register'
150: ),
151: );
152:
153: $this->render('wsauth_register');
154: }
155:
156:
157: 158: 159: 160: 161: 162: 163: 164:
165: public function login($email=null, $password=null)
166: {
167:
168: $this->breadcrumbs = array(
169: WsLocalize::msg('home') => array(
170: 'site',
171: 'index'
172: ),
173: WsLocalize::msg('login') => array(
174: 'wsauth',
175: 'login'
176: ),
177: );
178:
179: $this->render('wsauth_login');
180: }
181:
182:
183: 184: 185: 186:
187: public function logout()
188: {
189: if ($this->_auth->checkSession()) {
190: if ($this->_auth->logout()) {
191: $this->render('wsauth_logout');
192: }
193: }
194: }
195:
196:
197: 198: 199: 200:
201: public function admin()
202: {
203: if ($this->_auth->hasPermission('admin') != true) {
204: trigger_error('Access denied', E_USER_ERROR);
205: return;
206: }
207:
208:
209: $this->breadcrumbs = array(
210: WsLocalize::msg('home') => array(
211: 'site',
212: 'index'
213: ),
214: WsLocalize::msg('auth') => array(
215: 'wsauth',
216: 'admin'
217: ),
218: );
219:
220: $user_model = new Ws_userModel();
221: $roles_model = new Ws_rolesModel();
222: $perms_model = new Ws_permissionsModel();
223:
224: $this->render('wsauth_admin', array(
225: 'user_model' => $user_model,
226: 'roles_model' => $roles_model,
227: 'perms_model' => $perms_model,
228: ));
229: }
230:
231:
232: 233: 234: 235:
236: public function rolePerms()
237: {
238: if ($this->_auth->hasPermission('admin') != true) {
239: trigger_error('Access denied', E_USER_ERROR);
240: return;
241: }
242:
243: $this->render('wsauth_rolePerms');
244: }
245:
246:
247: 248: 249: 250:
251: public function userRoles()
252: {
253: if ($this->_auth->hasPermission('admin') != true) {
254: trigger_error('Access denied', E_USER_ERROR);
255: return;
256: }
257:
258: $this->render('wsauth_userRoles');
259: }
260:
261:
262: 263: 264: 265:
266: public function edit()
267: {
268: if (!$this->_auth->checkSession()) {
269: return $user_model = null;
270: } else {
271: $user_model = new Ws_userModel();
272:
273: $condition = 'email=\''.$_SESSION['ws_auth_user_email'].'\'';
274: $res = $user_model->search($condition);
275:
276: if ($res == false or $user_model->nRows != 1) {
277: $user_model = null;
278: }
279: }
280:
281: $this->render('wsauth_edit', array(
282: 'user_model' => $user_model,
283: 'user_email' => $_SESSION['ws_auth_user_email']
284: ));
285: }
286:
287:
288: 289: 290: 291: 292: 293:
294: public function verify($verification_code=null)
295: {
296: $user_model = new Ws_userModel();
297:
298:
299: $condition = 'verification_code=\''.$verification_code.'\' AND '
300: .'is_verified = \'f\'';
301: $res = $user_model->search($condition);
302:
303: if ($res == false or $user_model->nRows != 1) {
304: header('HTTP/1.1 401 Unauthorized');
305: trigger_error(WsLocalize::msg('Invalid verification code'),
306: E_USER_ERROR);
307: } else {
308:
309: $user_model->is_verified = true;
310: $user_model->is_active = true;
311: $user_model->save();
312:
313:
314: $auth = new WsAuth();
315: $auth->login($user_model->email, $user_model->password);
316: }
317:
318: $this->render('wsauth_verifyed');
319: }
320: }
321: