@hal.littel
To implement authentication in Yii, follow these steps:
'components' => array( 'user' => array( 'class' => 'CWebUser', 'loginUrl' => array('/user/login'), ), 'authManager' => array( 'class' => 'CDbAuthManager', 'connectionID' => 'db', ), ),
yii migrate --migrationPath=application.modules.user.migrations
public function actionLogin() { $model = new LoginForm;
1 2 3 4 5 6 7 8 9 10 11 12 |
if(Yii::app()->user->id) $this->redirect('/'); if(isset($_POST['LoginForm'])) { $model->attributes = $_POST['LoginForm']; if($model->validate() && $model->login()) $this->redirect(Yii::app()->user->returnUrl); } $this->render('login',array('model'=>$model)); |
}
public function actionLogout() { Yii::app()->user->logout(); $this->redirect(Yii::app()->homeUrl); }
yii rbac/init
public function accessRules() { return array( array('allow', 'actions'=>array('update','delete'), 'expression'=>'Yii::app()->user->checkAccess("updateOwnModel", array("model"=>$this->loadModel($_GET["id"])))', ), array('allow', 'controllers'=>array('admin'), 'expression'=>'Yii::app()->user->checkAccess("admin")', ), array('deny', 'users'=>array('*'), ), ); }
1
|
<?php echo CHtml::link('Update',array('update','id'=>$model->id)); ?> |
That's it! You've implemented authentication and authorization in your Yii application using RBAC.