代码之家  ›  专栏  ›  技术社区  ›  ifusion

如何在成员登录后根据其所在的组将其重定向到特定页面?

  •  1
  • ifusion  · 技术社区  · 7 年前

    我想有两个不同类型的成员。我已经创建了2个不同的组,如下所示。每个组登录后都将重定向到特定页面。

    • 经销商->重定向到 website.com/resellers
    • website.com/architects

    这些成员一旦登录,将只能看到这个页面,这将只是一个前端页面与PDF的列表。

    我从以下几个方面入手: afterMemberLoggedIn() 方法。

    use SilverStripe\ORM\DataExtension;
    use SilverStripe\Security\Security;
    
    class MemberExtension extends DataExtension {
    
        public function afterMemberLoggedIn()
        {
           if (Security::getCurrentUser()->inGroup('Reseller')) {
               // Redirect to reseller page
           }
        }
    
    }
    

    申请编号:

    SilverStripe\Security\Member:
      extensions:
        - MemberExtension
    

    我觉得这不是正确的方法?实现这一目标的最佳方法是什么?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Barry    7 年前

    这是我在SS3项目中使用的代码。你可以把它转换成SS4。这是根据几年前在ssbits上的一篇文章写的。

    基本上是用两个字段扩展组。一个是用于重定向到将为管理员组设置的管理员的复选框,另一个是 链接页 允许您在成功登录时选择要重定向到的页面。这将为分销商和架构师组设置。

    ---
    name: 'mysiteextensions'
    ---
    Group:
      extensions:
        - 'GroupDecorator'
    
    Injector:
      MemberLoginForm:
        MysiteLoginForm
    

    mysite/extensions/GroupDecorator.php

    class GroupDecorator extends DataExtension {
    
        private static $db = array(
            'GoToAdmin' => 'Boolean'
        );
    
        private static $has_one = array(
            'LinkPage' => 'SiteTree'
        );
    
        public function updateCMSFields(FieldList $fields) {
            $fields->addFieldToTab('Root.Members', CheckboxField::create('GoToAdmin', 'Go to admin?'), 'Members');
            $fields->addFieldToTab('Root.Members', TreeDropdownField::create('LinkPageID', 'Or select a page to redirect to', 'SiteTree'), 'Members');
        }
    
        function __construct() {
            parent::__construct();
        }
    }
    

    class MysiteLoginForm extends MemberLoginForm {
    
        public function dologin($data) {
            if ($this->performLogin($data)) {
                if (!$this->redirectByGroup($data)) {
                    $this->controller->redirect(Director::baseURL());
                }
            } else {
                if ($badLoginURL = Session::get('BadLoginURL')) {
                    $this->controller->redirect($badLoginURL);
                } else {
                    //Director::redirectBack();
    
                    //if we redirect to the admin after a failed login, it will show us the login form.
                    $this->controller->redirect(Director::baseURL().'admin');
                }
            }
        }
    
        public function redirectByGroup($data) {
    
            //gets current member which is logged in.
            $member = Member::currentUser();
    
            //gets all groups
            $groups = DataObject::get('Group');
    
            $backURL = Controller::curr()->getRequest()->getVar('BackURL');
    
            //cycle through the groups
            foreach ($groups as $group) {
    
                //if member is in the group and the group has gotoAdmin checked
                if ($member->inGroup($group->ID) && $group->GoToAdmin == 1) {
    
                    //redirect to the admin page.
                    $this->controller->redirect(Director::baseURL().'admin');
                    return true;
    
                    //member is in the group and the group has a page link defined.
                } elseif ($member->inGroup($group->ID) && $group->LinkPageID != 0) {
                    //get the page.
                    $link = DataObject::get_by_id('SiteTree', $group->LinkPageID)->URLSegment;
    
                    //redirect to page
                    $this->controller->redirect(Director::baseURL() . $link);
    
                    return true;
                }
            }
            //not found.
            return false;
        }
    }
    
    推荐文章