Editing User Permissions by code

Permalink 1 user found helpful
Hi!
I need to edit User Permissions by code. I want to allow to "Access User Search" to a user group.
I have been analysing a lot of C5 core classes (TaskPermission, Concrete5_Model_PermissionKey, etc), but I have not seen any method similar to $page->assignPermissions.

1 Attachment

Arequal
 
synlag replied on at Permalink Reply
synlag
Hi,

hope I'm getting you right, do you want to put a user into a group that has access to user search?
If so, this might help:

$u = User::getByUserID($userID); 
$group = Group::getByName($groupThatHasAccess);
$u->enterGroup($group);


Greets
Arequal replied on at Permalink Reply
Arequal
This is a good piece of code, but I'm afraid that it's not useful for me.
If you look at the attachment in my first post, there is a "User permissions" panel where you can add or remove permissions to users and groups. I want to manage that "User Permissions" panel by code, and enable its first functionality "Access User Search" to a given set of users or groups.

Anyway, I appreciate your help. A lot of thanks
synlag replied on at Permalink Reply
synlag
Am i'm right that you want to give access to .../dashboard/users/search/ ?
At the sitemap, you can activate display system pages under options and edit the permission to the user search. Add a group that you want have access to it, then the code posted above would work.
Arequal replied on at Permalink Reply
Arequal
I have that code:
$form = Loader::helper('form/user_selector');
echo $form->selectMultipleUsers("recipients", $uID = false);


It works, but, when the popup appears and tries to load users, it raises a "You have no access to users" error.

Then, I have to login as Administrator, "System & Settings", "User Permissions", and enable "Access User Search" for a special user group that I have added. And this is what I want to get by code.
Kiesel replied on at Permalink Reply
Before 5.6 it was like this:
$g4 = Group::getByID($yourgroupid); 
$tp = TaskPermission::getByHandle('access_user_search');
$tp->addAccess($g4);


But I'm not sure how it is now.
JohntheFish replied on at Permalink Reply
JohntheFish
Kiesel replied on at Permalink Reply
It doesn't show how to handle Task Permissions though.
mkly replied on at Permalink Reply
mkly
TaskPermissions are kind of legacy now. In this case this one is UserPermissionKey. Here's a snippet that might help you sort this one out.

$groupName = 'Superstars';  // change to the group you need
$taskPermissionHandle = 'access_user_search';
$group = Group::getByName($groupName);
$pk    = UserPermissionKey::getByHandle($taskPermissionHandle);
$pao   = $pk->getPermissionAssignmentObject();
$pa    = PermissionAccess::getByID($pk->getPermissionAccessID(), $pk)->duplicate();
$pae   = GroupPermissionAccessEntity::getOrCreate($group);
$pa->addListItem($pae, false, PermissionKey::ACCESS_TYPE_INCLUDE);
$pa->save(array('paID' => $pa->getPermissionAccessID()));
$pao->assignPermissionAccess($pa);


Let me know if that one works for you.

Best Wishes,
Mike
Kiesel replied on at Permalink Reply
That's exactly what is needed for > 5.6. I tried it out and it works.

Thanks a million Mike :)