Mohon bantuannya buat para master semua. Saya kebingungan mau bikin login dengan level tapi halamannya berbeda.
jadi gini, kalau misal saya login dengan level admin, maka halaman index yang tampil akan masuk ke halaman admin, kalau masuk ke level agent bakal masuk ke halaman agent. Aku sudah cari2 referensi, tapi kebanyakan cuma akses menunya doang. Mohon pencerahannya master.
jadi saya punya components seperti ini AdminLogin.php
<?php
class AdminLogin extends CUserIdentity {
private $_id;
public function authenticate() {
$user = TbUser::model()->findByAttributes(
array('username' => $this -> username));
if ($user === null){
$this -> errorCode = self::ERROR_USERNAME_INVALID;
} else {
if($user -> password !== $user
->encrypt($this -> password)) {
$this -> errorCode = self::ERROR_PASSWORD_INVALID;
}else {
$this -> _id = $user -> id;
$this -> setState('username', $user -> username);
$this -> errorCode = self::ERROR_NONE;
}
}
return !$this -> errorCode;
}
public function getId(){
return $this -> _id;
}
}
?>
Ini controllernya LoginController.php
<?php
class LoginController extends Controller {
public $layout = "testing";
public function actionIndex(){
$model = new AdminLoginForm;
if(isset($_POST['ajax']) && $_POST['ajax'] === 'login-form'){
echo CActiveForm::validate($model);
Yii::app() -> end();
}
if (isset($_POST['AdminLoginForm'])){
$model -> attributes = $_POST['AdminLoginForm'];
if($model -> validate() && $model -> login()){
$this -> redirect(array('/admin'));
}
}
$this -> render('index', array('model' => $model));
}
public function actionLogout(){
Yii::app() -> user -> logout();
$this -> redirect(array('login/index'));
}
}
?>
Terus ini modelnya gan AdminLoginForm.php
<?php
class AdminLoginForm extends CFormModel {
public $username;
public $password;
public $rememberMe;
private $_identity;
public function rules(){
return array(
array('username, password', 'required'),
array('rememberMe', 'boolean'),
array('password', 'authenticate'),
);
}
public function attributeLabels(){
return array(
'rememberMe' => 'Ingatkan Saya Nanti',
);
}
public function authenticate($attribute, $params){
if(!$this -> hasErrors()){
$this -> _identity = new AdminLogin($this ->
username, $this -> password);
if(!$this -> _identity -> authenticate()){
$this -> addError('password', 'Incorrect
username or password.');
}
}
}
public function login(){
if ($this -> _identity === null){
$this -> _identity = new AdminLogin($this ->
username, $this -> password);
$this -> _identity -> authenticate();
}
if($this -> _identity -> errorCode ===
AdminLogin::ERROR_NONE){
$duration = $this -> rememberMe ? 3600 * 24 * 30 : 0;
Yii::app()->user -> login($this -> _identity, $duration);
TbUser::model() -> updateByPk($this -> _identity ->
id, array('last_login_time' => new
CDbExpression('NOW()')));
return true;
}else {
return false;
}
}
}
?>
ini sudah berhasil, cuma ga sesuai sama level yang ada mohon bantuannya plisss
untuk tampilannya saya pake bootstrap. 6387