Yii:会员专区的共享密码



在Yii中设置共享密码保护区的最佳方法是什么?

我想要一个群组模型的视图,它可以通过该群组所有者创建的共享密码访问——群组成员不应该登录,只需要输入这个密码。

这还应该用Yii的内置身份验证工具来完成吗?-或者有没有一个更简单的解决方案,考虑到有人可能想要访问几个组。

您可以使用PHP中内置的标准会话机制来实现这一点。当有人试图查看密码保护区时,请检查会话变量,如果用户尚未输入密码,则使用密码表单将其重定向到某个页面(例如,您可以使用控制器过滤器进行检查)。

表单提交后,检查密码的正确性,如果一切正常,则将其写入会话。您可以通过组ID来区分会话密钥。

您可以使用Yii过滤器功能在执行控制器操作之前激发代码,并阻止您不想允许的操作。

我会为你的所有群组页面创建一个公共控制器,如果需要的话,从这个控制器继承其他控制器

在过滤器中,我会设置代码来检查/提示密码,并将其保存在会话中。

例如,我们有一个过滤器设置来检测用户是否接受了我们修改后的条款和条件。过滤器将检测并阻止访问控制器,直到用户没有确认。

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) {
    }
}

最新更新