Jump to:

23379 Posts in 18298 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » Easy way to check the group of currentMember?

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 2950 Views
  • Bereusei
    Avatar
    Community Member
    94 Posts

    Easy way to check the group of currentMember? Link to this post

    He guys,

    I have some trouble to check if currentMember is in a group, of not.
    I use this function:

    function canEdit($Member = null){
       if(Member::currentUser()->inGroup(8)){
          return true;
       }else{
          return false;
       }
    }

    Problem is to get the right group id. If you often create and delete groups, it takes a long time to find the right number.
    if(Member::currentUser()->inGroup('Foo')) doesn´t work why ever.
    Does anybody know a good solution to check the right number or to use the official name of the group?

    It really freaks me out.

  • danzzz
    Avatar
    Community Member
    175 Posts

    Re: Easy way to check the group of currentMember? Link to this post

    try

    $Group = DataObject::get_one('Group', "Code = 'Foo'");

    if(Member::currentUser()->inGroup($Group->ID)){

    ...

  • Bereusei
    Avatar
    Community Member
    94 Posts

    Re: Easy way to check the group of currentMember? Link to this post

    No, unfortunately this doesn´t work:

    Error: Member::inGroup(): Wrong format for $group parameter
    At line 731 in /home/reisueber/domains/reisueber.eu/public_html/projekte/cms-silverstripe/sapphire/security/Member.php

    user_error(Member::inGroup(): Wrong format for $group parameter,256) line 731 of Member.php Member->inGroup() line 33 of Workshop.php Workshop->canEdit() line 1240 of TableListField.php TableListField::permissions_for_object(Workshop) line 736 of ModelAdmin.php ModelAdmin_CollectionController->getResultsTable(Array) line 759 of ModelAdmin.php ModelAdmin_CollectionController->ResultsForm(Array) line 652 of ModelAdmin.php ModelAdmin_CollectionController->search(Array,Form,SS_HTTPRequest) line 329 of Form.php Form->httpSubmission(SS_HTTPRequest) line 143 of RequestHandler.php RequestHandler->handleRequest(SS_HTTPRequest) line 161 of RequestHandler.php RequestHandler->handleRequest(SS_HTTPRequest) line 147 of Controller.php Controller->handleRequest(SS_HTTPRequest) line 161 of RequestHandler.php RequestHandler->handleRequest(SS_HTTPRequest) line 147 of Controller.php Controller->handleRequest(SS_HTTPRequest) line 282 of Director.php Director::handleRequest(SS_HTTPRequest,Session) line 125 of Director.php Director::direct(/admin/workshops/Workshop/SearchForm) line 127 of main.php

  • Bereusei
    Avatar
    Community Member
    94 Posts

    Re: Easy way to check the group of currentMember? Link to this post

    Got it!

    function canEdit($Member = null){
       $Group = DataObject::get_one('Group', "\"Title\" = 'Test'");
       if(Member::currentUser()->inGroup($Group->ID)){
          return true;
       }else{
          return false;
       }
    }

    In place of Code you must use Title! Great thanks for your hint!

    ... "Test" is the name of the group, by the way.

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Easy way to check the group of currentMember? Link to this post

    Though note the CMS user can alter the title and therefore break your code. Using the 'code' field it isn't going to break if they update the title.

    A more flexible option is to check for permissions, you can then assign permissions to groups.

    Your dataobject must implement the PermissionProvider class and return an array with your new permission codes, then you can assign these in the Security -> Permissions tab.

    if(Permission::check('FOO_PERMISSION')) {}

    2950 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.