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

时间:2018-08-03 21:32:52

标签: php silverstripe silverstripe-4

我想拥有2倍不同类型的会员。我按照下面创建了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
       }
    }

}

app.yml:

SilverStripe\Security\Member:
  extensions:
    - MemberExtension

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

1 个答案:

答案 0 :(得分:1)

这是我在SS3项目上使用的代码。您可以将其转换为SS4。它是基于几年前关于ssbits的帖子。

基本上,发生的事情是用2个字段扩展组。一个是用于重定向到您将为管理员组设置的管理员的复选框,另一个字段 LinkPage 允许您选择成功登录后要重定向到的页面。将为转销商和建筑师组设置。

mysite / _config / extensions.yml

---
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();
    }
}

mysite / extensions / MysiteLoginForm.php

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;
    }
}