Skip to content

Commit

Permalink
Null check fix - required for old trees, fixes backwards compat shim …
Browse files Browse the repository at this point in the history
…for AddApplication
  • Loading branch information
Shazwazza committed Aug 18, 2017
1 parent 7b61070 commit 2817a77
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
50 changes: 27 additions & 23 deletions src/Umbraco.Core/Models/Membership/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,34 +403,38 @@ void IUser.AddAllowedSection(string sectionAlias)
var groups = Groups.ToArray();
var admin = groups.FirstOrDefault(x => x.Alias == Constants.Security.AdminGroupAlias);
if (admin != null)
{
//if the group isn't IUserGroup we'll need to look it up
var realGroup = admin as IUserGroup ?? ApplicationContext.Current.Services.UserService.GetUserGroupById(admin.Id);
realGroup.AddAllowedSection(sectionAlias);
//now we need to flag this for saving (hack!)
GroupsToSave.Add(realGroup);
}

//now we'll check if the user has a special 1:1 user group created for itself. This will occur if this method is used and also during an upgrade.
//this comes in the alias form of userName + 'Group'
var customUserGroup = groups.FirstOrDefault(x => x.Alias == (Username + "Group"));
if (customUserGroup != null)
{
//if the group isn't IUserGroup we'll need to look it up
var realGroup = customUserGroup as IUserGroup ?? ApplicationContext.Current.Services.UserService.GetUserGroupById(customUserGroup.Id);
var realGroup = admin as IUserGroup ?? ApplicationContext.Current.Services.UserService.GetUserGroupById(admin.Id);
realGroup.AddAllowedSection(sectionAlias);
//now we need to flag this for saving (hack!)
GroupsToSave.Add(realGroup);
}

//ok, so the user doesn't have a 1:1 group, we'll need to flag it for creation
var newUserGroup = new UserGroup
{
Alias = Username + "Group",
Name = "Group for " + Username
};
newUserGroup.AddAllowedSection(sectionAlias);
GroupsToSave.Add(newUserGroup);
}
else
{
//now we'll check if the user has a special 1:1 user group created for itself. This will occur if this method is used and also during an upgrade.
//this comes in the alias form of userName + 'Group'
var customUserGroup = groups.FirstOrDefault(x => x.Alias == (Username + "Group"));
if (customUserGroup != null)
{
//if the group isn't IUserGroup we'll need to look it up
var realGroup = customUserGroup as IUserGroup ?? ApplicationContext.Current.Services.UserService.GetUserGroupById(customUserGroup.Id);
realGroup.AddAllowedSection(sectionAlias);
//now we need to flag this for saving (hack!)
GroupsToSave.Add(realGroup);
}

//ok, so the user doesn't have a 1:1 group, we'll need to flag it for creation
var newUserGroup = new UserGroup
{
Alias = Username + "Group",
Name = "Group for " + Username
};
newUserGroup.AddAllowedSection(sectionAlias);
//add this user to this new group
AddGroup(newUserGroup);
GroupsToSave.Add(newUserGroup);
}
}

/// <summary>
Expand Down
13 changes: 7 additions & 6 deletions src/Umbraco.Core/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,9 @@ public void Save(IUser entity, bool raiseEvents = true)
{
throw new ArgumentException("Cannot save user with empty name.");
}

var repository = RepositoryFactory.CreateUserRepository(uow);
repository.AddOrUpdate(entity);

//Now we have to check for backwards compat hacks

//Now we have to check for backwards compat hacks, we'll need to process any groups
//to save first before we update the user since these groups might be new groups.
var explicitUser = entity as User;
if (explicitUser != null && explicitUser.GroupsToSave.Count > 0)
{
Expand All @@ -328,7 +326,10 @@ public void Save(IUser entity, bool raiseEvents = true)
{
groupRepository.AddOrUpdate(userGroup);
}
}
}

var repository = RepositoryFactory.CreateUserRepository(uow);
repository.AddOrUpdate(entity);

try
{
Expand Down
4 changes: 4 additions & 0 deletions src/Umbraco.Web/Trees/LegacyTreeParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public LegacyTreeParams()

public LegacyTreeParams(IEnumerable<KeyValuePair<string, string>> formCollection)
{
if (formCollection == null)
{
return;
}
var p = TreeRequestParams.FromDictionary(formCollection.ToDictionary(x => x.Key, x => x.Value));
NodeKey = p.NodeKey;
StartNodeID = p.StartNodeID;
Expand Down

0 comments on commit 2817a77

Please sign in to comment.