2012-07-12 75 views
1

我想更改控制器的默認動作取決於哪個用戶登錄。 Ex。在我的網站中有兩個用戶:發佈者和作者,我想在發佈者登錄時將發佈者操作設置爲默認操作,對於作者也是如此。如何在Yii中動態設置默認動作

我應該怎麼辦?我什麼時候可以檢查我的角色並設置相關操作?

回答

6

另一種方法是將defaultAction property設置在控制器的init() method中。有點像這樣:

<?php 
class MyAwesomeController extends Controller{ // or extends CController depending on your code 
    public function init(){ 
     parent::init(); // no need for this call if you don't have anything in your parent init() 

     if(array_key_exists('RolePublisher', Yii::app()->authManager->getRoles(Yii::app()->user->id))) 
      $this->defaultAction='publisher'; // name of your action 

     else if (array_key_exists('RoleAuthor', Yii::app()->authManager->getRoles(Yii::app()->user->id))) 
      $this->defaultAction='author'; // name of your action 
    } 
    // ... rest of your code 
} 
?> 

退房CAuthManager's getRoles(),看到返回的數組將有'role'=>CAuthItem object格式,這就是爲什麼我用array_key_exists()檢查。

因爲你不知道,動作名稱將只有沒有動作部分的名稱,例如,如果你有public function actionPublisher(){...}那麼動作名稱應該是:publisher

+0

如果你有不同的角色含義(即不使用yii的rbac),那麼你必須修改支票以使用你的角色含義。 – 2012-07-12 07:59:51

0

我想你可以在用戶表中保存「第一個用戶頁面」。當用戶通過身份驗證時,您可以從數據庫加載此頁面。你可以在哪裏做到這一點?我認爲最好的地方是UserIdentity類。之後,你可以在SiteController :: actionLogin()中獲得這個值;

您可以獲取或設置「第一頁」值:

  if (null === $user->first_page) { 
       $firstPage = 'site/index'; 
      } else { 
       $firstPage = $user->first_page; 
      } 

這是一個完整的類:

class UserIdentity extends CUserIdentity 
{ 
    private $_id; 

    public function authenticate() 
    { 
     $user = User::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; 

      if (null === $user->first_page) { 
       $firstPage = 'site/index'; 
      } else { 
       $firstPage = $user->first_page; 
      } 

      $this->errorCode = self::ERROR_NONE; 
     } 
     return !$this->errorCode; 
    } 

    public function getId() 
    { 
     return $this->_id; 
    } 

} 



/** 
* Displays the login page 
*/ 
public function actionLogin() 
{ 
    $model = new LoginForm; 

    // if it is ajax validation request 
    if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') { 
     echo CActiveForm::validate($model); 
     Yii::app()->end(); 
    } 

    // collect user input data 
    if (isset($_POST['LoginForm'])) { 
     $model->attributes = $_POST['LoginForm']; 
     // validate user input and redirect to the previous page if valid 
     if ($model->validate() && $model->login()) 
      $this->redirect(Yii::app()->user->first_page); 
    } 

    // display the login form 
    $this->render('login', array('model' => $model)); 
} 

另外,你可以只寫正確的代碼只有在這個文件中。在SiteController文件中。

+0

這是將用戶重定向到他們的地址的好主意。但我不需要將他們的地址保存到數據庫,我可以爲userIdentity中的每個角色分配地址。非常感謝。 – JahangirAhmad 2012-07-12 06:04:14

1

你可以做的另一個更簡單的事情是保持默認動作相同,但是這個默認動作只是簡單地調用一個額外的動作函數,這取決於用戶登錄的類型。例如,你有indexAction函數有條件地調用this->userActionthis->publisherAction取決於檢查誰已登錄。