Fix for #140, changing the logic to remove or add roles only if neede…#623
Fix for #140, changing the logic to remove or add roles only if neede…#623sadashivdalvi wants to merge 4 commits intocivicrm:7.x-masterfrom
Conversation
…f needed and always use google api instead of delete
|
Can one of the admins verify this patch? |
|
(Standard links)
|
|
@jitendrapurohit can you look over this? |
|
@sadashivdalvi What is the problem you are addressing? You mention '#140' but in which system? The PR that is automatically linked does not look relevant. I don't understand the comment 'always use google api instead of delete' ... the code makes no use of google api's. |
|
I have a system which performs certain operation whenever a role is added or removed, this operation is to sync some external system. What currently happens is, system removes all roles (using db delete which doesn't invoke any hooks, etc) and then adds roles which actually invokes the external system unnecessarily, Instead of that civicrm code should identify whether we need a change in role and use api to save roles. I am using this patch on production since I submitted the PR and it's working fine. "google" was a typing mistake, I meant use api to save role changes. Thanks, |
|
Ok, that makes sense. The changes are smaller than the diff suggests at first sight. I have not tested it, but the code changes look good to me. |
|
|
||
| if (empty($contactMemberships) && !empty($rid)) { | ||
| db_delete('users_roles')->condition('uid', $uid)->condition('rid', $rid, $roleCondition)->execute(); | ||
| } |
There was a problem hiding this comment.
is this no longer happening now? I just replicated the following problem which wasn't happening with the previous code.
- Create 2 member sync rule. Eg role1 => memtype1 & role2 => memtype2.
- Create a contact (with drupal user) and add a membership of type = memtype1.
- Sync the rule. role1 is added to the user (Correct).
- Delete the membership
memtype1from the contact and add the membership of type =memtype2. - Sync the rule again. role2 is added (Correct). But role1 is not removed from the contact (Incorrect).
As memtype1 is not associated with the civi contact, role1 should have been removed after the sync. It was working as per the above condition.
There was a problem hiding this comment.
@jitendrapurohit can you please recheck, I missed that case, which I fixed by adding additional check now.
foreach ($memberroles as $membership_type_id => $rolerule) {
if (user_has_role($rolerule['rid'], $account) && !in_array($rolerule['rid'], $addRoles)) {
// This will only happen if the membership is deleted.
$expRoles[] = $rolerule['rid'];
}
}
Simple check that if the user has a role in the rule and doesn't have it addRoles then remove that role.
Have also improved the saving logic by moving the save out of loops, something I missed last time.
Thanks,
Sadashiv
sadashivdalvi
left a comment
There was a problem hiding this comment.
I did find on my site that we do need a fix from https://issues.civicrm.org/jira/browse/CRM-16000 i.e. #316 Should I include that patch here so that the code becomes complete and functional.
I have this deployed on my production site.
Thanks,
Sadashiv
|
|
||
| if (empty($contactMemberships) && !empty($rid)) { | ||
| db_delete('users_roles')->condition('uid', $uid)->condition('rid', $rid, $roleCondition)->execute(); | ||
| } |
There was a problem hiding this comment.
@jitendrapurohit can you please recheck, I missed that case, which I fixed by adding additional check now.
foreach ($memberroles as $membership_type_id => $rolerule) {
if (user_has_role($rolerule['rid'], $account) && !in_array($rolerule['rid'], $addRoles)) {
// This will only happen if the membership is deleted.
$expRoles[] = $rolerule['rid'];
}
}
Simple check that if the user has a role in the rule and doesn't have it addRoles then remove that role.
Have also improved the saving logic by moving the save out of loops, something I missed last time.
Thanks,
Sadashiv
…d and always use google api instead of delete