Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

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

Easy way to check the group of currentMember?


Reply

5 Posts   3128 Views

Avatar
Bereusei

14 January 2012 at 11:28pm Community Member, 94 Posts

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.

Avatar
danzzz

15 January 2012 at 2:03am Community Member, 175 Posts

try

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

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

...

Avatar
Bereusei

15 January 2012 at 8:55am Community Member, 94 Posts

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

Avatar
Bereusei

15 January 2012 at 9:10am (Last edited: 15 January 2012 9:11am), Community Member, 94 Posts

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.

Avatar
Willr

16 January 2012 at 11:22pm Forum Moderator, 5511 Posts

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')) {}