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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Easy way to check the group of currentMember?


Go to End


5 Posts   9286 Views

Avatar
Bereusei

Community Member, 96 Posts

14 January 2012 at 11:28pm

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

Community Member, 175 Posts

15 January 2012 at 2:03am

try


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

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

...

Avatar
Bereusei

Community Member, 96 Posts

15 January 2012 at 8:55am

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

Community Member, 96 Posts

15 January 2012 at 9:10am

Edited: 15/01/2012 9:11am

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

Forum Moderator, 5523 Posts

16 January 2012 at 11:22pm

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