Skip to content

Commit 11e946d

Browse files
committed
Merge pull request umbraco#614 from Alain-es/patch-2
Fix bug U4-6071: Feature request - Add a specific section to all users
2 parents 3412396 + f3b23a1 commit 11e946d

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

src/Umbraco.Core/Services/IUserService.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ public interface IUserService : IMembershipUserService
5353
/// <param name="sectionAlias">Alias of the section to remove</param>
5454
void DeleteSectionFromAllUsers(string sectionAlias);
5555

56+
/// <summary>
57+
/// Add a specific section to all users or those specified as parameters
58+
/// </summary>
59+
/// <remarks>This is useful when a new section is created to allow specific users accessing it</remarks>
60+
/// <param name="sectionAlias">Alias of the section to add</param>
61+
/// <param name="userIds">Specifiying nothing will add the section to all user</param>
62+
void AddSectionToAllUsers(string sectionAlias, params int[] userIds);
63+
5664
/// <summary>
5765
/// Get permissions set for a user and optional node ids
5866
/// </summary>
@@ -117,4 +125,4 @@ public interface IUserService : IMembershipUserService
117125

118126
#endregion
119127
}
120-
}
128+
}

src/Umbraco.Core/Services/UserService.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,36 @@ public void DeleteSectionFromAllUsers(string sectionAlias)
672672
uow.Commit();
673673
}
674674
}
675+
676+
/// <summary>
677+
/// Add a specific section to all users or those specified as parameters
678+
/// </summary>
679+
/// <remarks>This is useful when a new section is created to allow specific users accessing it</remarks>
680+
/// <param name="sectionAlias">Alias of the section to add</param>
681+
/// <param name="userIds">Specifiying nothing will add the section to all user</param>
682+
public void AddSectionToAllUsers(string sectionAlias, params int[] userIds)
683+
{
684+
var uow = _uowProvider.GetUnitOfWork();
685+
using (var repository = _repositoryFactory.CreateUserRepository(uow))
686+
{
687+
IEnumerable<IUser> users;
688+
if (userIds.Any())
689+
{
690+
users = repository.GetAll(userIds);
691+
}
692+
else
693+
{
694+
users = repository.GetAll();
695+
}
696+
foreach (var user in users.Where(u => !u.AllowedSections.InvariantContains(sectionAlias)))
697+
{
698+
//now add the section for each user and commit
699+
user.AddAllowedSection(sectionAlias);
700+
repository.AddOrUpdate(user);
701+
}
702+
uow.Commit();
703+
}
704+
}
675705

676706
/// <summary>
677707
/// Get permissions set for a user and optional node ids
@@ -745,4 +775,4 @@ public IEnumerable<EntityPermission> GetPermissions(IUser user, params int[] nod
745775
/// </summary>
746776
public static event TypedEventHandler<IUserService, DeleteEventArgs<IUserType>> DeletedUserType;
747777
}
748-
}
778+
}

src/Umbraco.Tests/Services/UserServiceTests.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,43 @@ public void Can_Remove_Section_From_All_Assigned_Users()
451451

452452
}
453453

454+
[Test]
455+
public void Can_Add_Section_To_All_Users()
456+
{
457+
var userType = ServiceContext.UserService.GetUserTypeByAlias("admin");
458+
459+
var user1 = ServiceContext.UserService.CreateUserWithIdentity("test1", "test1@test.com", userType);
460+
var user2 = ServiceContext.UserService.CreateUserWithIdentity("test2", "test2@test.com", userType);
461+
var user3 = ServiceContext.UserService.CreateUserWithIdentity("test3", "test3@test.com", userType);
462+
var user4 = ServiceContext.UserService.CreateUserWithIdentity("test4", "test4@test.com", userType);
463+
464+
//now add the section to specific users
465+
ServiceContext.UserService.AddSectionToAllUsers("test", (int)user1.Id, (int)user2.Id);
466+
467+
//assert
468+
var result1 = ServiceContext.UserService.GetUserById((int)user1.Id);
469+
var result2 = ServiceContext.UserService.GetUserById((int)user2.Id);
470+
var result3 = ServiceContext.UserService.GetUserById((int)user3.Id);
471+
var result4 = ServiceContext.UserService.GetUserById((int)user4.Id);
472+
Assert.IsTrue(result1.AllowedSections.Contains("test"));
473+
Assert.IsTrue(result2.AllowedSections.Contains("test"));
474+
Assert.IsFalse(result3.AllowedSections.Contains("test"));
475+
Assert.IsFalse(result4.AllowedSections.Contains("test"));
476+
477+
//now add the section to all users
478+
ServiceContext.UserService.AddSectionToAllUsers("test");
479+
480+
//assert
481+
result1 = ServiceContext.UserService.GetUserById((int)user1.Id);
482+
result2 = ServiceContext.UserService.GetUserById((int)user2.Id);
483+
result3 = ServiceContext.UserService.GetUserById((int)user3.Id);
484+
result4 = ServiceContext.UserService.GetUserById((int)user4.Id);
485+
Assert.IsTrue(result1.AllowedSections.Contains("test"));
486+
Assert.IsTrue(result2.AllowedSections.Contains("test"));
487+
Assert.IsTrue(result3.AllowedSections.Contains("test"));
488+
Assert.IsTrue(result4.AllowedSections.Contains("test"));
489+
}
490+
454491
[Test]
455492
public void Get_By_Profile_Username()
456493
{
@@ -512,4 +549,4 @@ public void Get_User_By_Username()
512549
Assert.That(updatedItem.AllowedSections.Count(), Is.EqualTo(0));
513550
}
514551
}
515-
}
552+
}

0 commit comments

Comments
 (0)