2012-07-11 77 views
0

在Yii中設置共享密碼保護區的最佳方式是什麼?Yii:會員區共享密碼

我期待有一個組模型的視圖,可以通過該組所有者創建的共享密碼進行訪問 - 組成員不需要登錄,只需輸入此密碼即可。

Yii的內置驗證工具仍然可以實現嗎? - 還是有一個更簡單的解決方案,銘記有人可能想要訪問幾個組。

回答

0

您可以使用PHP中內置的標準會話機制來執行此操作。當有人試圖查看密碼保護區域時,檢查會話變量,如果用戶還沒有輸入密碼,然後用密碼錶單將他重定向到某個頁面(例如,可以使用控制器過濾器進行檢查)。

表單提交後,請檢查密碼的正確性,如果一切正常,請將其寫入會話中。您可以按組ID標識會話密鑰。

0

您可以使用Yii filter功能在執行控制器操作之前觸發代碼,並阻止您不想允許的操作。

我會爲你所有的組頁面創建一個通用控制器,如果需要的話,可以從這個控件繼承其他控制器。

在過濾器中,我將設置代碼來檢查/提示輸入密碼,並將其保存在會話中。

例如,我們有一個過濾器設置來檢測用戶是否接受了我們修訂的條款和條件。過濾器將檢測並阻止訪問控制器,直到用戶未確認爲止。

class TocConfirmFilter extends CFilter { 

    /** 
    * Initializes the filter. 
    * This method is invoked after the filter properties are initialized 
    * and before {@link preFilter} is called. 
    * You may override this method to include some initialization logic. 
    */ 
    public function init() { 

    } 

    /** 
    * Performs the pre-action filtering. 
    * @param CFilterChain the filter chain that the filter is on. 
    * @return boolean whether the filtering process should continue and the action 
    * should be executed. 
    */ 
    protected function preFilter($filterChain) { 

     // do not perform this filter on this action 
     if ($filterChain->action->controller->id . '/' . $filterChain->action->id == 'public/onePublicPage') { 
      return true; 
     } 


     if (isset(Yii::app()->user->id)) { 
      $user = user::model()->findbyPk(Yii::app()->user->id); 
      if ($user === null) 
       throw new CHttpException(404, 'The requested user does not exist.'); 
      if ($user->tocconfirmed == 0) { 
       Yii::app()->getRequest()->redirect(Yii::app()->createAbsoluteUrl('authorize/confirm')); 
       return false; 
      } 
     } 
     return true; 
    } 

    /** 
    * Performs the post-action filtering. 
    * @param CFilterChain the filter chain that the filter is on. 
    */ 
    protected function postFilter($filterChain) { 

    } 

}