private IUser CreateTestUser()
    {
        using var scope = ScopeProvider.CreateScope(autoComplete: true);
        using var _     = scope.Notifications.Suppress();

        var globalSettings = new GlobalSettings();
        var user           = new User(globalSettings)
        {
            Name = "Test user", Username = "******", Email = "*****@*****.**"
        };

        UserService.Save(user);

        var userGroupA = new UserGroup(ShortStringHelper)
        {
            Alias = "GroupA", Name = "Group A"
        };

        userGroupA.AddAllowedSection("media");
        userGroupA.AddAllowedSection("settings");

        // TODO: This is failing the test
        UserService.Save(userGroupA, new[] { user.Id });

        var userGroupB = new UserGroup(ShortStringHelper)
        {
            Alias = "GroupB", Name = "Group B"
        };

        userGroupB.AddAllowedSection("settings");
        userGroupB.AddAllowedSection("member");
        UserService.Save(userGroupB, new[] { user.Id });

        return(UserService.GetUserById(user.Id));
    }
        private IUser CreateTestUser()
        {
            var user = new User
            {
                Name     = "Test user",
                Username = "******",
                Email    = "*****@*****.**",
            };

            ServiceContext.UserService.Save(user, false);

            var userGroupA = new UserGroup
            {
                Alias = "GroupA",
                Name  = "Group A"
            };

            userGroupA.AddAllowedSection("media");
            userGroupA.AddAllowedSection("settings");
            // TODO: This is failing the test
            ServiceContext.UserService.Save(userGroupA, new[] { user.Id }, false);

            var userGroupB = new UserGroup
            {
                Alias = "GroupB",
                Name  = "Group B"
            };

            userGroupB.AddAllowedSection("settings");
            userGroupB.AddAllowedSection("member");
            ServiceContext.UserService.Save(userGroupB, new[] { user.Id }, false);

            return(ServiceContext.UserService.GetUserById(user.Id));
        }
Esempio n. 3
0
        public void Can_Remove_Section_From_All_Assigned_UserGroups()
        {
            var userGroup1 = new UserGroup
            {
                Alias = "Group1",
                Name  = "Group 1"
            };
            var userGroup2 = new UserGroup
            {
                Alias = "Group2",
                Name  = "Group 2"
            };

            ServiceContext.UserService.Save(userGroup1);
            ServiceContext.UserService.Save(userGroup2);

            //adds some allowed sections
            userGroup1.AddAllowedSection("test");
            userGroup2.AddAllowedSection("test");
            ServiceContext.UserService.Save(userGroup1);
            ServiceContext.UserService.Save(userGroup2);

            //now clear the section from all users
            ServiceContext.UserService.DeleteSectionFromAllUserGroups("test");

            //assert
            var result1 = ServiceContext.UserService.GetUserGroupById(userGroup1.Id);
            var result2 = ServiceContext.UserService.GetUserGroupById(userGroup2.Id);

            Assert.IsFalse(result1.AllowedSections.Contains("test"));
            Assert.IsFalse(result2.AllowedSections.Contains("test"));
        }
Esempio n. 4
0
        private UserGroup CreateTestUserGroup(string alias = "testGroup", string name = "Test Group")
        {
            var userGroup = new UserGroup
            {
                Alias       = alias,
                Name        = name,
                Permissions = "ABCDEFGHIJ1234567".ToCharArray().Select(x => x.ToString())
            };

            userGroup.AddAllowedSection("content");
            userGroup.AddAllowedSection("media");

            ServiceContext.UserService.Save(userGroup);

            return(userGroup);
        }
Esempio n. 5
0
        public static IUserGroup BuildEntity(UserGroupDto dto)
        {
            var userGroup = new UserGroup(dto.UserCount, dto.Alias, dto.Name,
                                          dto.DefaultPermissions.IsNullOrWhiteSpace()
                    ? Enumerable.Empty <string>()
                    : dto.DefaultPermissions.ToCharArray().Select(x => x.ToString(CultureInfo.InvariantCulture)).ToList(),
                                          dto.Icon);

            try
            {
                userGroup.DisableChangeTracking();
                userGroup.Id             = dto.Id;
                userGroup.CreateDate     = dto.CreateDate;
                userGroup.UpdateDate     = dto.UpdateDate;
                userGroup.StartContentId = dto.StartContentId;
                userGroup.StartMediaId   = dto.StartMediaId;
                if (dto.UserGroup2AppDtos != null)
                {
                    foreach (var app in dto.UserGroup2AppDtos)
                    {
                        userGroup.AddAllowedSection(app.AppAlias);
                    }
                }

                userGroup.ResetDirtyProperties(false);
                return(userGroup);
            }
            finally
            {
                userGroup.EnableChangeTracking();
            }
        }
        public UserGroup CreateUserGroup(TenantGroup tenantGroup)
        {
            ValidateGroup(tenantGroup);
            var       groupAlias = tenantGroup.Name.Sanitize();
            UserGroup group      = (UserGroup)userService.GetUserGroupByAlias(groupAlias);

            try
            {
                group = new UserGroup(1, groupAlias, tenantGroup.Name, Enumerable.Empty <string>(), "icon-umb-members")
                {
                    Permissions    = tenantGroup.Permissions,
                    StartContentId = tenantGroup.StartContentId,
                    StartMediaId   = tenantGroup.StartMediaId
                };

                foreach (var section in tenantGroup.AllowedSectionAliases)
                {
                    group.AddAllowedSection(section);
                }
                userService.Save(group);
                ConnectorContext.AuditService.Add(AuditType.New, -1, group.Id, "User Group", $"User Group {group.Name} has been created");
                return(group);
            }
            catch (Exception ex)
            {
                logger.Error(typeof(UserGroupService), ex.Message);
                logger.Error(typeof(UserGroupService), ex.StackTrace);
                throw;
            }
        }
Esempio n. 7
0
    // Umbraco.Code.MapAll -CreateDate -UpdateDate -DeleteDate
    private static void Map(UserGroupSave source, UserGroup target)
    {
        target.StartMediaId   = source.StartMediaId;
        target.StartContentId = source.StartContentId;
        target.Icon           = source.Icon;
        target.Alias          = source.Alias;
        target.Name           = source.Name;
        target.Permissions    = source.DefaultPermissions;
        target.Key            = source.Key;

        var id = GetIntId(source.Id);

        if (id > 0)
        {
            target.Id = id;
        }

        target.ClearAllowedSections();
        if (source.Sections is not null)
        {
            foreach (var section in source.Sections)
            {
                target.AddAllowedSection(section);
            }
        }
    }
Esempio n. 8
0
        public void Can_Remove_Section_From_All_Assigned_UserGroups()
        {
            var userGroup1 = new UserGroup(ShortStringHelper)
            {
                Alias = "Group1",
                Name  = "Group 1"
            };
            var userGroup2 = new UserGroup(ShortStringHelper)
            {
                Alias = "Group2",
                Name  = "Group 2"
            };

            UserService.Save(userGroup1);
            UserService.Save(userGroup2);

            // adds some allowed sections
            userGroup1.AddAllowedSection("test");
            userGroup2.AddAllowedSection("test");
            UserService.Save(userGroup1);
            UserService.Save(userGroup2);

            // now clear the section from all users
            UserService.DeleteSectionFromAllUserGroups("test");

            // Assert
            IUserGroup result1 = UserService.GetUserGroupById(userGroup1.Id);
            IUserGroup result2 = UserService.GetUserGroupById(userGroup2.Id);

            Assert.IsFalse(result1.AllowedSections.Contains("test"));
            Assert.IsFalse(result2.AllowedSections.Contains("test"));
        }
Esempio n. 9
0
        public void Can_Add_Section_To_All_UserGroups()
        {
            var userGroup1 = new UserGroup
            {
                Alias = "Group1",
                Name  = "Group 1"
            };

            userGroup1.AddAllowedSection("test");

            var userGroup2 = new UserGroup
            {
                Alias = "Group2",
                Name  = "Group 2"
            };

            userGroup2.AddAllowedSection("test");

            var userGroup3 = new UserGroup
            {
                Alias = "Group3",
                Name  = "Group 3"
            };

            ServiceContext.UserService.Save(userGroup1);
            ServiceContext.UserService.Save(userGroup2);
            ServiceContext.UserService.Save(userGroup3);

            //assert
            var result1 = ServiceContext.UserService.GetUserGroupById(userGroup1.Id);
            var result2 = ServiceContext.UserService.GetUserGroupById(userGroup2.Id);
            var result3 = ServiceContext.UserService.GetUserGroupById(userGroup3.Id);

            Assert.IsTrue(result1.AllowedSections.Contains("test"));
            Assert.IsTrue(result2.AllowedSections.Contains("test"));
            Assert.IsFalse(result3.AllowedSections.Contains("test"));

            //now add the section to all groups
            foreach (var userGroup in new[] { userGroup1, userGroup2, userGroup3 })
            {
                userGroup.AddAllowedSection("test");
                ServiceContext.UserService.Save(userGroup);
            }

            //assert
            result1 = ServiceContext.UserService.GetUserGroupById(userGroup1.Id);
            result2 = ServiceContext.UserService.GetUserGroupById(userGroup2.Id);
            result3 = ServiceContext.UserService.GetUserGroupById(userGroup3.Id);
            Assert.IsTrue(result1.AllowedSections.Contains("test"));
            Assert.IsTrue(result2.AllowedSections.Contains("test"));
            Assert.IsTrue(result3.AllowedSections.Contains("test"));
        }
Esempio n. 10
0
        public void Can_Add_Section_To_All_UserGroups()
        {
            var userGroup1 = new UserGroup(ShortStringHelper)
            {
                Alias = "Group1",
                Name  = "Group 1"
            };

            userGroup1.AddAllowedSection("test");

            var userGroup2 = new UserGroup(ShortStringHelper)
            {
                Alias = "Group2",
                Name  = "Group 2"
            };

            userGroup2.AddAllowedSection("test");

            var userGroup3 = new UserGroup(ShortStringHelper)
            {
                Alias = "Group3",
                Name  = "Group 3"
            };

            UserService.Save(userGroup1);
            UserService.Save(userGroup2);
            UserService.Save(userGroup3);

            // Assert
            IUserGroup result1 = UserService.GetUserGroupById(userGroup1.Id);
            IUserGroup result2 = UserService.GetUserGroupById(userGroup2.Id);
            IUserGroup result3 = UserService.GetUserGroupById(userGroup3.Id);

            Assert.IsTrue(result1.AllowedSections.Contains("test"));
            Assert.IsTrue(result2.AllowedSections.Contains("test"));
            Assert.IsFalse(result3.AllowedSections.Contains("test"));

            // now add the section to all groups
            foreach (UserGroup userGroup in new[] { userGroup1, userGroup2, userGroup3 })
            {
                userGroup.AddAllowedSection("test");
                UserService.Save(userGroup);
            }

            // Assert
            result1 = UserService.GetUserGroupById(userGroup1.Id);
            result2 = UserService.GetUserGroupById(userGroup2.Id);
            result3 = UserService.GetUserGroupById(userGroup3.Id);
            Assert.IsTrue(result1.AllowedSections.Contains("test"));
            Assert.IsTrue(result2.AllowedSections.Contains("test"));
            Assert.IsTrue(result3.AllowedSections.Contains("test"));
        }
Esempio n. 11
0
        private UserGroup Build()
        {
            var item = new UserGroup()
            {
                Id             = 3,
                Key            = Guid.NewGuid(),
                UpdateDate     = DateTime.Now,
                CreateDate     = DateTime.Now,
                Name           = "Test",
                Alias          = "alias",
                Icon           = "icon",
                Permissions    = new [] { "a", "b", "c" },
                DeleteDate     = null,
                StartContentId = null,
                StartMediaId   = null,
            };

            item.AddAllowedSection("A");
            item.AddAllowedSection("B");

            return(item);
        }
Esempio n. 12
0
        internal static UserGroup CreateUserGroup(string suffix = "", string[] permissions = null, string[] allowedSections = null)
        {
            var group = new UserGroup
            {
                Alias       = "testUserGroup" + suffix,
                Name        = "TestUserGroup" + suffix,
                Permissions = permissions ?? new[] { "A", "B", "C" }
            };

            if (allowedSections == null)
            {
                group.AddAllowedSection("content");
                group.AddAllowedSection("media");
            }
            else
            {
                foreach (var allowedSection in allowedSections)
                {
                    group.AddAllowedSection(allowedSection);
                }
            }

            return(group);
        }
Esempio n. 13
0
        public void Can_Add_And_Remove_Sections_From_UserGroup()
        {
            var userGroup = new UserGroup
            {
                Alias = "Group1",
                Name  = "Group 1"
            };

            userGroup.AddAllowedSection("content");
            userGroup.AddAllowedSection("mediat");
            ServiceContext.UserService.Save(userGroup);

            var result1 = ServiceContext.UserService.GetUserGroupById(userGroup.Id);

            Assert.AreEqual(2, result1.AllowedSections.Count());

            //adds some allowed sections
            userGroup.AddAllowedSection("test1");
            userGroup.AddAllowedSection("test2");
            userGroup.AddAllowedSection("test3");
            userGroup.AddAllowedSection("test4");
            ServiceContext.UserService.Save(userGroup);

            result1 = ServiceContext.UserService.GetUserGroupById(userGroup.Id);

            Assert.AreEqual(6, result1.AllowedSections.Count());

            //simulate clearing the sections
            foreach (var s in userGroup.AllowedSections)
            {
                result1.RemoveAllowedSection(s);
            }

            //now just re-add a couple
            result1.AddAllowedSection("test3");
            result1.AddAllowedSection("test4");
            ServiceContext.UserService.Save(result1);

            //assert
            //re-get
            result1 = ServiceContext.UserService.GetUserGroupById(userGroup.Id);
            Assert.AreEqual(2, result1.AllowedSections.Count());
        }