Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

How can I access user roles from my plugin?

  • mhladun
  • mhladun's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
1 year 11 months ago #227726 by mhladun
I am developing a plugin that should check a user's role(s) before allowing them access to certain survey level plugin settings.

There doesn't appear to be a getUserRole method  here , so I decided to use getUserGroups instead, but that doesn't give me a list of users in a user group - user group objects only have the group's ugid, name, description, and owner id.

Please Log in to join the conversation.

  • mhladun
  • mhladun's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
1 year 11 months ago - 1 year 11 months ago #227730 by mhladun
Replied by mhladun on topic How can I access user roles from my plugin?
I figured out a way of achieving what I wanted. For some reason, User Groups don't allow the owner of the group to be part of the group, and that adds an extra condition you must check.

private function getUserGroupAttributeByUserGroupName($user_group_name, $attribute)
{
    foreach ($this->api->getUserGroups() as $i => $group){
        if($group === $user_group_name){
        return $group[$attribute];
        }
    }
    return NULL;
}
// survey level settings for plugin
public function beforeSurveySettings()
{
    $current_user_uid = $this->api->getCurrentUser();
    $admin_ugid = $this->getUserGroupAttributeByUserGroupName('Admin', 'ugid');
    $user_is_admin = $this->api->getUserInGroup($admin_ugid, $current_user_uid);
    $admin_group_owner_id = $this->getUserGroupAttributeByUserGroupName('Admin', 'owner_id');
    if(!is_null($user_is_admin) || $current_user_uid == $admin_group_owner_id){
        // Only these users can access this block:
        // - users in Admin user group
        // - Admin user group owner
    }
}

It would be nice to achieve this with an API call like
Code:
if(     in_array('Admin',  $this->api->getUserRoles($current_user_id))    ) { // code here }
Last edit: 1 year 11 months ago by mhladun.

Please Log in to join the conversation.

  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 11 months ago #228110 by DenisChenu
Replied by DenisChenu on topic How can I access user roles from my plugin?

It would be nice to achieve this with an API call like
Code:
if(     in_array('Admin',  $this->api->getUserRoles($current_user_id))    ) { // code here }
 
Sincerely,
If i have to create API function for all usage i have : i didn't have a lot of plugin.

Else : feature + pull request are welcome.

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.

Please Log in to join the conversation.

Lime-years ahead

Online-surveys for every purse and purpose