Overview

Classes

  • Ws_logged_inModel
  • Ws_permissionsModel
  • Ws_role_permModel
  • Ws_rolesModel
  • Ws_user_roleModel
  • Ws_userModel
  • WsAuth
  • WsauthController
  • WsChart
  • WsConfig
  • WsController
  • WsDatabase
  • WsForm
  • WsImage
  • WsLocalize
  • WsModel
  • WsModelForm
  • WsModelGridView
  • WsUrl

Functions

  • __autoload
  • callHook
  • WsErrorHandler
  • Overview
  • Class
  1: <?php
  2: 
  3: 
  4: /**
  5:  * WsAuthController
  6:  * Controller for WsAuth module. It contains next views:
  7:  *
  8:  * * register
  9:  * * login
 10:  * * logout
 11:  * * admin
 12:  * * rolePerms
 13:  * * userRoles
 14:  * * edit
 15:  * * verify
 16:  *
 17:  * @see WsController
 18:  * @see WsAuth
 19:  *
 20:  */
 21: class WsauthController
 22: {
 23:     /**
 24:      * @var string $layout Name of layout file located in '/public/layouts'
 25:      *
 26:      */
 27:     public $layout;
 28:     /**
 29:      * @var string $title Web page title
 30:      *
 31:      */
 32:     public $title;
 33:     /**
 34:      * @var array $breadcrumbs List of links that indicate position in Web app
 35:      *
 36:      */
 37:     public $breadcrumbs;
 38:     /**
 39:      * @var string $_action Name of controller action
 40:      *
 41:      */
 42:     private $_action;
 43:     /**
 44:      * @var array $_params List of parameters that would be passed to the action
 45:      *
 46:      */
 47:     private $_params = array();
 48:     /**
 49:      * @var WsAuth $_auth WsAuth instance
 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:      * Read 'view' and return its contet.
 67:      *
 68:      * @return string content of 'view'.
 69:      *
 70:      */
 71:     private function renderView()
 72:     {
 73:         // file to render
 74:         $fileName = WsROOT.'/protected/library/views/'
 75:             .$this->_action.'.php';
 76: 
 77:         // extract parameters so that they can be used in view
 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:      * Renders controller action in web brovser
101:      *
102:      * @param $action string Optional. Name of action.If it is not set then calls 'index' action.
103:      * @param array $params {
104:      *     Optional. List of key=>value parameters that are passed to action
105:      * }
106:      *
107:      */
108:     private function render($action = 'admin', $params = array())
109:     {
110:         // name of action
111:         $this->_action = $action;
112: 
113:         $this->_params = $params;
114:         // $this->params = array_unique($this->params);
115: 
116:         // page title
117:         $WsTitle = $this->title;
118:         // breadcrumbs
119:         $WsBreadcrumbs = $this->breadcrumbs;
120:         // content to show
121:         $WsContent = self::renderView();
122: 
123:         // layout file
124:         $layoutFile = WsROOT.'/public/layouts/'.$this->layout;
125:         if (is_file($layoutFile)) {
126:             // show view in layaout if exists
127:             include($layoutFile);
128:         } else {
129:             // or if not exists, show the content of view, only
130:             echo $WsContent;
131:         }
132:     }
133: 
134: 
135:     /**
136:      * display registration form
137:      *
138:      */
139:     public function register($email=null, $password=null)
140:     {
141:         // breadcrumbs
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:      * Try to login user by it's email address and password and shows login
159:      * form if it fail or if login informations are not provided
160:      *
161:      * @param string $email User email address
162:      * @param string $password User password
163:      *
164:      */
165:     public function login($email=null, $password=null)
166:     {
167:         // breadcrumbs
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:      * logout current loged in user
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:      * administer user accounts
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:         // breadcrumbs
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:      * manage permissions for roles
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:      * manage permissions for roles
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:      * edit user account for currently loged in user
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:      * verify new user account
290:      *
291:      * @param string $verification_code Verification code
292:      *
293:      */
294:     public function verify($verification_code=null)
295:     {
296:         $user_model = new Ws_userModel();
297: 
298:         // check if verification code exists in database
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:             // verify account
309:             $user_model->is_verified = true;
310:             $user_model->is_active = true;
311:             $user_model->save();
312: 
313:             // login new user
314:             $auth = new WsAuth();
315:             $auth->login($user_model->email, $user_model->password);
316:         }
317: 
318:         $this->render('wsauth_verifyed');
319:     }
320: }
321: 
API documentation generated by ApiGen