Jump to:

10990 Posts in 2727 Topics by 1818 members

All other Modules

SilverStripe Forums » All other Modules » SOLVED - CustomLoginForm - GroupDecorator - SS 2.4.2

Discuss all other Modules here.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 902 Views
  • borriej
    Avatar
    Community Member
    267 Posts

    SOLVED - CustomLoginForm - GroupDecorator - SS 2.4.2 Link to this post

    Hello,

    Using SS 242

    I'm using the GroupDecorator + CustomLoginForm - modified the _config..
    dev/build..
    works..
    Security-> new group
    redirect to page 'downloads'
    created new member
    admin group -> checked the box 'go to admin area'
    -> saved.

    But when i log-in with the redirecter account.. i get the message:
    -> I'm sorry, but you can't access that part of the CMS. If you want to log in as someone else, do so below

    Does this script malfunction with SS 2.4.2 ?

    GroupDecorator

    <?php
    class GroupDecorator extends DataObjectDecorator {
       
       function augmentSQL(SQLQuery &$query) {}
       
       public function extraStatics(){
          
          return array(
             'db' => array(
                "GoToAdmin" => "Boolean"
             ),
             'has_one' => array(
                "LinkPage" => "SiteTree"
             ),
          );

       }   

       public function updateCMSFields(FieldSet &$fields) {
        $fields->addFieldToTab("Root.Members", new CheckboxField("GoToAdmin", " Go to Admin area"), 'Members');
        $fields->addFieldToTab("Root.Members", new TreeDropdownField("LinkPageID", "Or select a Page to redirect to", "SiteTree"), 'Members');
       }

    }

    CustomLoginForm

    <?php

    class CustomLoginForm extends MemberLoginForm {

       // this function is overloaded on our sublcass (this) to do something different
       public function dologin($data) {
          if($this->performLogin($data)) {
           if(!$this->redirectByGroup($data))
                   Director::redirect(Director::baseURL());
                echo 'done';
          } else {
             if($badLoginURL = Session::get("BadLoginURL")) {
                Director::redirect($badLoginURL);
             } else {
                Director::redirectBack();
             }
          }
       }

       public function redirectByGroup($data) {    

          // gets the current member that is logging in.
          $member = Member::currentUser();
          // gets all the groups.
          $Groups = DataObject::get("Group");
          
          //cycle through each group   
          foreach($Groups as $Group){
             //if the member is in the group and that group has GoToAdmin checked
             if($member->inGroup($Group->ID) && $Group->GoToAdmin == 1)
             {   
                //redirect to the admin page
                 Director::redirect(Director::baseURL() . 'admin' );
                return true;
             }
             //otherwise if the member is in the group and that group has a page link defined
             elseif($member->inGroup($Group->ID) && $Group->LinkPageID != 0)
             {   
                //Get the page that is referenced in the group      
                $Link = DataObject::get_by_id("SiteTree", "{$Group->LinkPageID}")->URLSegment;
                //direct to that page
                Director::redirect(Director::baseURL() . $Link);
                return true;
             }
             
          }
          //otherwise if none of the above worked return fase
          return false;
                
       }
                   
       
    }   

    ?>

    _config

    Object::add_extension('Group', 'GroupDecorator');

    Thx!

  • borriej
    Avatar
    Community Member
    267 Posts

    Re: SOLVED - CustomLoginForm - GroupDecorator - SS 2.4.2 Link to this post

    My bad...
    Forgot to add
    Object::useCustomClass('MemberLoginForm', 'CustomLoginForm');
    in the config...

    902 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.