I'd like to Restrict access to Element API endpoint by the current user's group.
Is this possible Using Craft 2?
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communityI'd like to Restrict access to Element API endpoint by the current user's group.
Is this possible Using Craft 2?
Yes it is you can see here how to check permissions
You can get the current user with
$currentUser = craft()->userSession-getUser();
Then you can check the same conditions like in the example link. If the conditions are false you can return an empty array instead of your endpoints
if(!$currentUser->isInGroup('groupHandle')) {
return [];
}
Please make sure to check if the current user is not null
die()
your script out with a message. The clean way would be to create a custom route for each api endpoint and display the message in your controller
– Robin Schambach
Jun 16 '18 at 10:06
die('You cannot access this resource')
is exactly what I need for this. Thank you !!
– Adam Menczykowski
Jun 16 '18 at 10:33