public function getUserOpts($op=null) { $ret=User::model() ->active() ->with(array('userTypeVal.userType' => array('alias' => 'ut'))); if(empty($op)) { return $ret->findAll('ut.name="company-official"'); } else { return $ret->find('ut.name="company-official" and t.id=:uid',array(':uid'=>$op)); } }
Category Archives: Yii Framework
How to provide login mechanism with minimal code changes using Yii Framework
Hi all.
I’m going to tell how to provide login mechanism to your sites by using Yii Framework.
Requirements
- Yii Framework 1.x (I used version 1.1.15: https://github.com/yiisoft/yii/releases/download/1.1.15/yii-1.1.15.022a51.tar.gz)
- A web application created by Yii (http://www.yiiframework.com/doc/guide/1.1/en/quickstart.first-app)
1) Authentication
Modify protected/components/UserIdentity.php as the following:
class UserIdentity extends CUserIdentity { /* ... */ public function authenticate() { $user=User::model()->find(array( 'condition'=>'username=:uname', 'params'=>array(':uname'=>$this->username), )); /** * NOTE: You should hash your passwords via CPasswordHelper::hashPassword($this->password) * before saving to DB */ if($user && CPasswordHelper::verifyPassword($this->password,$user->password)) $this->errorCode=self::ERROR_NONE; else $this->errorCode=self::ERROR_PASSWORD_INVALID; return !$this->errorCode; } /* ... */ }
2) Controllers
2.1) Base controller
Modify protected/components/Controller.php as the following:
class Controller extends CController { /* ... */ public function filters() { return array('accessControl'); } public function accessRules() { return array( array('allow','users'=>array('@')), array('deny','users'=>array('?')) ); } /* ... */ }
2.2) SiteController
Modify protected/controllers/SiteController.php as the following:
class SiteController extends Controller { /* ... */ public function accessRules() { return array( array('allow','users'=>array('@')), array('allow','actions'=>array('login','logout'),'users'=>array('*')), array('deny','users'=>array('*')), ); } /* ... */ }
2.3) Other controllers
You should remove accessRules and filters functions from other controllers.
If you want to use filters in your other controllers, you should use it like the following way:
public function filters() { return array_merge(parent::filters(),array( 'postOnly + delete', )); }
That’s all. Happy coding!
Yii – Ajax login required
Ajax ile çalışan sitelerde, oturum süresi dolduğu zaman kullanıcı ajax yanıtlarında hiç bir işlem yapılmadı gibi sonuç görmektedir.
Yii framework tabanlı web sitelerinde bu soruna çözüm olarak ben aşağıdaki yöntemi kullanıyorum:
– main config’den user kısmında Yii FW’nin döndüğü ajax login gerekli mesajını aşağıdaki gibi değişiriyoruz:
'components'=>array( ... 'user'=>array( 'loginRequiredAjaxResponse'=>'{"loginRequired":true,"loginReqText":"LOGIN_REQUIRED"}', ), ... )
– şimdi sunucudan gelen tüm ajax yanıtlarının bunun bir login mesajı olup olmadığını kontrol eden aşağıdaki Javascript kodunu main layout sayfamıza ekliyoruz:
/** * Handler which redirects session-expired users to login after ajax requests. * @require user->loginRequiredAjaxResponse * @author Erman Gülhan * @date 06.06.2013 */ $(function(){ $('body').ajaxComplete( function(event,request,options){ if(request.responseText!==undefined){ var respond=$.parseJSON(request.responseText); if(respond!==null && respond!==undefined && respond.loginRequired) { //window.location.href = options.url; window.location.reload(true); } } } ); });
Hepsi bu kadar. Kolay gelsin.