Closed (duplicate)
Project:
Group
Version:
3.0.0
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
27 Jul 2023 at 16:23 UTC
Updated:
12 Sep 2023 at 13:33 UTC
Jump to comment: Most recent
At the moment there are not APIs to add or remove a role from a member.
I can add a role doing: $group_relationship->set('group_roles', [role_ids]);
Before that, I need to loop through the member roles and add all the existing role ids in an array and then add the new role.
Then save the Group relationships
$memeber = $group->getMember($account);
$existing_roles = $member->getRoles();
$g_relationship = $member->getGroupRelationship();
if (!isset($rexisting_roles[$new_role_id)) {
$updated_roles = [];
$updated_roles = $new_role;
foreach($existing_roles as $existing_role) {
if ($role->getScope() == 'individual') {
$updated_roles[] = $existing_role->getOriginalId();
}
}
$g_relationship->set('group_roles', $updated_roles);
$g_relationship->save();
}
Is there a better way of doing it?
Comments
Comment #2
vincenzo gambino commentedComment #3
vincenzo gambino commentedComment #4
kristiaanvandeneyndeCurrently no, this has been on my list for a while so will look into addRole and removeRole.
Comment #5
kristiaanvandeneyndeJust saw that this is a duplicate of #3132084: Add/Remove group role for a user programmatically