Created
May 18, 2017 01:51
-
-
Save Shazwazza/1669959f39ce8e4329e4d9163cb47c28 to your computer and use it in GitHub Desktop.
Using Umbraco events to enforce a specific user to only being allowed to create users but not update them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading; | |
using Umbraco.Core; | |
using Umbraco.Core.Models; | |
using Umbraco.Core.Services; | |
using Umbraco.Core.Security; | |
namespace Test | |
{ | |
public class MyStartup : ApplicationEventHandler | |
{ | |
/// <summary> | |
/// Bind to umbraco events when the application is ready | |
/// </summary> | |
/// <param name="umbracoApplication"></param> | |
/// <param name="applicationContext"></param> | |
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) | |
{ | |
UserService.SavingUser += UserService_SavingUser; | |
} | |
private void UserService_SavingUser(IUserService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.Membership.IUser> e) | |
{ | |
//get the current user | |
var backOfficeIdentity = Thread.CurrentPrincipal.Identity as UmbracoBackOfficeIdentity; | |
if (backOfficeIdentity == null) return; | |
//check if it's the 'special' user | |
if (backOfficeIdentity.Username != "UserAdmin") return; | |
foreach (var user in e.SavedEntities) | |
{ | |
if (user.IsNewEntity() == false) | |
{ | |
//this user cannot save existing users so we need to cancel | |
e.Cancel = true; | |
return; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment