Skip to content

Instantly share code, notes, and snippets.

@Shazwazza
Created May 18, 2017 01:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Shazwazza/1669959f39ce8e4329e4d9163cb47c28 to your computer and use it in GitHub Desktop.
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
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