Event Handler for Deleting a Role

Topics: Writing modules
Aug 8, 2012 at 6:37 PM

I'm trying to attach an event when someone deletes a role.  I haven't been able to do this.  I've tried adding the following: 

OnRemoved<UserRolesPart>((context, part) => {
     //code here
});

But that doesn't execute when someone deletes a role.  Looking at the RoleService.cs file, it's deleting a RoleRecord.

_roleRepository.Delete(GetRole(id));

Could anyone shed some light onto how this can be achieved?

Thanks in advance.

Developer
Aug 8, 2012 at 9:21 PM

The first code snippet wouldn't work because that event will only be fired when a content item whose content type has the UserRolesPart attached (which the USer type has, so the event should fire when you remove a user).

Repositories don't fire events, so I'm afraid you're out of luck.

However, there are options:

  1. File a bug, explain your scenario and why firing an event when deleting a role would be useful
  2. Create a fork and implement an event yourself. Then create a pull request and perhaps it will be included.

 

Jan 20, 2014 at 9:18 AM
Edited Jan 20, 2014 at 9:28 AM
I'm working on a Groups module where roles are assigned to Groups and Users are added to groups. Users assigned to the group will gain all the permissions granted for that group. Here i need to have some mechanism of knowing when a Role gets deleted so that i could delete the corresponding assigned roles for the group as well.

Currently, the Roles module is deleting User Roles records in RoleService.DeleteRole but there should also be some other way of achieving this in order to isolate the two modules.
var currentUserRoleRecords = _userRolesRepository.Fetch(x => x.Role.Id == id);
foreach(var userRoleRecord in currentUserRoleRecords) {
    _userRolesRepository.Delete(userRoleRecord);
}