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

Php 根据成员所在的组,如何在成员登录后将其重定向到特定页面?,php,silverstripe,silverstripe-4,Php,Silverstripe,Silverstripe 4,我想有两个不同类型的成员。我已经创建了2个不同的组,如下所示。每个组登录后将重定向到特定页面 分销商->重定向到website.com/Resellers 架构师->重定向到website.com/Architects 这些成员登录后将只能看到此页面,该页面将只是一个包含PDF列表的前端页面 我从挂接:afterMemberLoggedIn()方法开始 use SilverStripe\ORM\DataExtension; use SilverStripe\Security\Security

我想有两个不同类型的成员。我已经创建了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

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

这是我在SS3项目中使用的代码。你可以把它转换成SS4。这是基于多年前一篇关于ssbits的帖子

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

mysite/\u 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();
    }
}
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;
    }
}
mysite/extensions/mysiteloginfo.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;
    }
}

谢谢你的代码,我很感激。我已经将代码转换为SS4,只是有点纠结于如何在.yml文件中钩住SS4中的自定义表单扩展名,你能帮我吗?@ifusion看一下谢谢,看了一下,但似乎遗漏了一些东西,这就是我的atm:-看起来
\u-construct
需要一些东西吗?我被困在这个问题上了-我在这里发布了一个类似的问题:你有解决这个问题的方法吗?如果是的话,你能分享你的方向吗?非常感谢。