Preventing Deletion of certain users in a plugin

I have a requirement to make sure certain users are never deleted from the Community.

I was wondering if it would be possible to create an plugin with a whitelist config and then monitor the UserBeforeDeleteEvent and check the whitelist and if the users username is present then cancel the delete even and log something out.

Is this possible and if so how would I go about cancelling the delete itself?

void UserBeforeDeleteEventHandler(UserBeforeDeleteEventArgs e);

Thanks

Adam

Parents
  • It would be possible to monitor the User.BeforeDelete event and throw an exception when a protected user is attempted to be deleted. This will stop the deletion from proceeding.

  • Hi Ben, I noticed one issue with this which is that the user is left in the 'disapproved' state after the delete fails. It's better than them being deleted but not ideal as it will involve an manual process to update their status. I tried to resolve this using the update method to change the users account status but it doesn't work. Any ideas on how I can resolve this?

    _eventLogService.Log($"User is protected so will not be deleted: {user.Username}.");
    
    
    //change user status back to approved after failed delete 
    Apis.Get<IUsers>().RunAsUser("admin", () =>
    {
    Apis.Get<IUsers>().Update(new UsersUpdateOptions
    {
    Id = user.Id,
    AccountStatus = "Approved"
    });
    });
    
    throw new Exception("Stopping User Delete as user is protected");

  • If you are trying to do it in the before event, you should just need to change the event args value, not run the update.   However  the disapproval will potentially override your change later in the pipeline per our business logic.  You will have to manage it somehow using a combination of before then handling the after update event while preserving state.

Reply Children
No Data