예제 #1
0
        public void Can_Perform_Add_With_Group()
        {
            var group = MockedUserGroup.CreateUserGroup();

            // Arrange
            var provider   = new PetaPocoUnitOfWorkProvider(Logger);
            var unitOfWork = provider.GetUnitOfWork();

            using (var repository = CreateUserGroupRepository(unitOfWork))
            {
                repository.AddOrUpdate(group);
                unitOfWork.Commit();
            }

            using (var repository = CreateRepository(unitOfWork))
            {
                IUser user = MockedUser.CreateUser();
                user.AddGroup(group.ToReadOnlyGroup());

                // Act
                repository.AddOrUpdate(user);
                unitOfWork.Commit();

                user = repository.Get(user.Id);

                // Assert
                Assert.That(user.HasIdentity, Is.True);
                Assert.AreEqual(1, user.Groups.Count());
                Assert.AreEqual(group.Alias, user.Groups.ElementAt(0).Alias);
            }
        }
예제 #2
0
        private IUser CreateTestUser(out IUserGroup userGroup)
        {
            userGroup = CreateTestUserGroup();

            IUser user = UserService.CreateUserWithIdentity("test1", "*****@*****.**");

            user.AddGroup(userGroup.ToReadOnlyGroup());

            UserService.Save(user);

            return(user);
        }
예제 #3
0
        /// <summary>
        /// Adds a group to the list of groups for the user, ensure to call Save() afterwords
        /// </summary>
        public void AddGroup(string groupAlias)
        {
            if (_lazyId.HasValue)
            {
                SetupUser(_lazyId.Value);
            }
            var group = ApplicationContext.Current.Services.UserService.GetUserGroupByAlias(groupAlias);

            if (group != null)
            {
                UserEntity.AddGroup(group.ToReadOnlyGroup());
            }
        }
예제 #4
0
        // Umbraco.Code.MapAll -CreateDate -UpdateDate -DeleteDate
        // Umbraco.Code.MapAll -Id -TourData -StartContentIds -StartMediaIds -Language -Username
        // Umbraco.Code.MapAll -PasswordQuestion -SessionTimeout -EmailConfirmedDate -InvitedDate
        // Umbraco.Code.MapAll -SecurityStamp -Avatar -ProviderUserKey -RawPasswordValue
        // Umbraco.Code.MapAll -RawPasswordAnswerValue -Comments -IsApproved -IsLockedOut -LastLoginDate
        // Umbraco.Code.MapAll -LastPasswordChangeDate -LastLockoutDate -FailedPasswordAttempts
        private void Map(UserInvite source, IUser target, MapperContext context)
        {
            target.Email      = source.Email;
            target.Key        = source.Key;
            target.Name       = source.Name;
            target.IsApproved = false;

            target.ClearGroups();
            var groups = _userService.GetUserGroupsByAlias(source.UserGroups.ToArray());

            foreach (var group in groups)
            {
                target.AddGroup(group.ToReadOnlyGroup());
            }
        }
예제 #5
0
        // Umbraco.Code.MapAll -CreateDate -UpdateDate -DeleteDate
        // Umbraco.Code.MapAll -TourData -SessionTimeout -EmailConfirmedDate -InvitedDate -SecurityStamp -Avatar
        // Umbraco.Code.MapAll -ProviderUserKey -RawPasswordValue -RawPasswordAnswerValue -PasswordQuestion -Comments
        // Umbraco.Code.MapAll -IsApproved -IsLockedOut -LastLoginDate -LastPasswordChangeDate -LastLockoutDate
        // Umbraco.Code.MapAll -FailedPasswordAttempts
        private void Map(UserSave source, IUser target, MapperContext context)
        {
            target.Name            = source.Name;
            target.StartContentIds = source.StartContentIds ?? Array.Empty <int>();
            target.StartMediaIds   = source.StartMediaIds ?? Array.Empty <int>();
            target.Language        = source.Culture;
            target.Email           = source.Email;
            target.Key             = source.Key;
            target.Username        = source.Username;
            target.Id = source.Id;

            target.ClearGroups();
            var groups = _userService.GetUserGroupsByAlias(source.UserGroups.ToArray());

            foreach (var group in groups)
            {
                target.AddGroup(group.ToReadOnlyGroup());
            }
        }
예제 #6
0
        private List <IUser> CreateTestUsers(int[] startContentIds, IUserGroup userGroup, int numberToCreate)
        {
            var users = new List <IUser>();

            for (int i = 0; i < numberToCreate; i++)
            {
                IUser user = UserService.CreateUserWithIdentity($"test{i}", $"test{i}@test.com");
                user.AddGroup(userGroup.ToReadOnlyGroup());

                var updateable = (User)user;
                updateable.StartContentIds = startContentIds;

                UserService.Save(user);

                users.Add(user);
            }

            return(users);
        }
예제 #7
0
        private bool UpdateMemberProperties(IUser user, BackOfficeIdentityUser identityUser)
        {
            var anythingChanged = false;

            // don't assign anything if nothing has changed as this will trigger the track changes of the model
            if (identityUser.IsPropertyDirty(nameof(BackOfficeIdentityUser.LastLoginDateUtc)) ||
                (user.LastLoginDate != default && identityUser.LastLoginDateUtc.HasValue == false) ||
                (identityUser.LastLoginDateUtc.HasValue && user.LastLoginDate.ToUniversalTime() != identityUser.LastLoginDateUtc.Value))
            {
                anythingChanged = true;

                // if the LastLoginDate is being set to MinValue, don't convert it ToLocalTime
                DateTime dt = identityUser.LastLoginDateUtc == DateTime.MinValue ? DateTime.MinValue : identityUser.LastLoginDateUtc.Value.ToLocalTime();
                user.LastLoginDate = dt;
            }

            if (identityUser.IsPropertyDirty(nameof(BackOfficeIdentityUser.InviteDateUtc)) ||
                (user.InvitedDate?.ToUniversalTime() != identityUser.InviteDateUtc))
            {
                anythingChanged  = true;
                user.InvitedDate = identityUser.InviteDateUtc?.ToLocalTime();
            }

            if (identityUser.IsPropertyDirty(nameof(BackOfficeIdentityUser.LastPasswordChangeDateUtc)) ||
                (user.LastPasswordChangeDate != default && identityUser.LastPasswordChangeDateUtc.HasValue == false) ||
                (identityUser.LastPasswordChangeDateUtc.HasValue && user.LastPasswordChangeDate.ToUniversalTime() != identityUser.LastPasswordChangeDateUtc.Value))
            {
                anythingChanged             = true;
                user.LastPasswordChangeDate = identityUser.LastPasswordChangeDateUtc.Value.ToLocalTime();
            }

            if (identityUser.IsPropertyDirty(nameof(BackOfficeIdentityUser.EmailConfirmed)) ||
                (user.EmailConfirmedDate.HasValue && user.EmailConfirmedDate.Value != default && identityUser.EmailConfirmed == false) ||
                ((user.EmailConfirmedDate.HasValue == false || user.EmailConfirmedDate.Value == default) && identityUser.EmailConfirmed))
            {
                anythingChanged         = true;
                user.EmailConfirmedDate = identityUser.EmailConfirmed ? (DateTime?)DateTime.Now : null;
            }

            if (identityUser.IsPropertyDirty(nameof(BackOfficeIdentityUser.Name)) &&
                user.Name != identityUser.Name && identityUser.Name.IsNullOrWhiteSpace() == false)
            {
                anythingChanged = true;
                user.Name       = identityUser.Name;
            }

            if (identityUser.IsPropertyDirty(nameof(BackOfficeIdentityUser.Email)) &&
                user.Email != identityUser.Email && identityUser.Email.IsNullOrWhiteSpace() == false)
            {
                anythingChanged = true;
                user.Email      = identityUser.Email;
            }

            if (identityUser.IsPropertyDirty(nameof(BackOfficeIdentityUser.AccessFailedCount)) &&
                user.FailedPasswordAttempts != identityUser.AccessFailedCount)
            {
                anythingChanged             = true;
                user.FailedPasswordAttempts = identityUser.AccessFailedCount;
            }

            if (user.IsApproved != identityUser.IsApproved)
            {
                anythingChanged = true;
                user.IsApproved = identityUser.IsApproved;
            }

            if (user.IsLockedOut != identityUser.IsLockedOut)
            {
                anythingChanged  = true;
                user.IsLockedOut = identityUser.IsLockedOut;

                if (user.IsLockedOut)
                {
                    // need to set the last lockout date
                    user.LastLockoutDate = DateTime.Now;
                }
            }

            if (identityUser.IsPropertyDirty(nameof(BackOfficeIdentityUser.UserName)) &&
                user.Username != identityUser.UserName && identityUser.UserName.IsNullOrWhiteSpace() == false)
            {
                anythingChanged = true;
                user.Username   = identityUser.UserName;
            }

            if (identityUser.IsPropertyDirty(nameof(BackOfficeIdentityUser.PasswordHash)) &&
                user.RawPasswordValue != identityUser.PasswordHash && identityUser.PasswordHash.IsNullOrWhiteSpace() == false)
            {
                anythingChanged            = true;
                user.RawPasswordValue      = identityUser.PasswordHash;
                user.PasswordConfiguration = identityUser.PasswordConfig;
            }

            if (identityUser.IsPropertyDirty(nameof(BackOfficeIdentityUser.Culture)) &&
                user.Language != identityUser.Culture && identityUser.Culture.IsNullOrWhiteSpace() == false)
            {
                anythingChanged = true;
                user.Language   = identityUser.Culture;
            }

            if (identityUser.IsPropertyDirty(nameof(BackOfficeIdentityUser.StartMediaIds)) &&
                user.StartMediaIds.UnsortedSequenceEqual(identityUser.StartMediaIds) == false)
            {
                anythingChanged    = true;
                user.StartMediaIds = identityUser.StartMediaIds;
            }

            if (identityUser.IsPropertyDirty(nameof(BackOfficeIdentityUser.StartContentIds)) &&
                user.StartContentIds.UnsortedSequenceEqual(identityUser.StartContentIds) == false)
            {
                anythingChanged      = true;
                user.StartContentIds = identityUser.StartContentIds;
            }

            if (user.SecurityStamp != identityUser.SecurityStamp)
            {
                anythingChanged    = true;
                user.SecurityStamp = identityUser.SecurityStamp;
            }

            if (identityUser.IsPropertyDirty(nameof(BackOfficeIdentityUser.Roles)))
            {
                var identityUserRoles = identityUser.Roles.Select(x => x.RoleId).ToArray();

                anythingChanged = true;

                // clear out the current groups (need to ToArray since we are modifying the iterator)
                user.ClearGroups();

                // go lookup all these groups
                IReadOnlyUserGroup[] groups = _userService.GetUserGroupsByAlias(identityUserRoles).Select(x => x.ToReadOnlyGroup()).ToArray();

                // use all of the ones assigned and add them
                foreach (IReadOnlyUserGroup group in groups)
                {
                    user.AddGroup(group);
                }

                // re-assign
                identityUser.SetGroups(groups);
            }

            // we should re-set the calculated start nodes
            identityUser.CalculatedMediaStartNodeIds   = user.CalculateMediaStartNodeIds(_entityService, _appCaches);
            identityUser.CalculatedContentStartNodeIds = user.CalculateContentStartNodeIds(_entityService, _appCaches);

            // reset all changes
            identityUser.ResetDirtyProperties(false);

            return(anythingChanged);
        }
예제 #8
0
        public IActionResult AddGroup([FromBody] GroupModel model)
        {
            _user.AddGroup(model.Name);

            return(Ok());
        }
예제 #9
0
        public void Get_All_User_Permissions_For_All_Nodes_With_Explicit_Permission()
        {
            // Arrange
            UserGroup userGroup1 = CreateTestUserGroup();
            UserGroup userGroup2 = CreateTestUserGroup("test2", "Test 2");
            UserGroup userGroup3 = CreateTestUserGroup("test3", "Test 3");
            IUser     user       = UserService.CreateUserWithIdentity("John Doe", "*****@*****.**");

            int defaultPermissionCount = userGroup3.Permissions.Count();

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

            Template template = TemplateBuilder.CreateTextPageTemplate();

            FileService.SaveTemplate(template);
            ContentType contentType = ContentTypeBuilder.CreateSimpleContentType(defaultTemplateId: template.Id);

            ContentTypeService.Save(contentType);

            Content[] content = new[]
            {
                ContentBuilder.CreateSimpleContent(contentType),
                ContentBuilder.CreateSimpleContent(contentType),
                ContentBuilder.CreateSimpleContent(contentType)
            };
            ContentService.Save(content);

            // assign permissions - we aren't assigning anything explicit for group3 and nothing explicit for content[2] /w group2
            ContentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup1.Id });
            ContentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup1.Id });
            ContentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup2.Id });
            ContentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup1.Id });
            ContentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup2.Id });
            ContentService.SetPermission(content[2], ActionDelete.ActionLetter, new int[] { userGroup1.Id });

            // Act
            // we don't pass in any nodes so it will return all of them
            EntityPermission[] result = UserService.GetPermissions(user).ToArray();
            var permissions           = result
                                        .GroupBy(x => x.EntityId)
                                        .ToDictionary(x => x.Key, x => x.GroupBy(a => a.UserGroupId).ToDictionary(a => a.Key, a => a.ToArray()));

            // Assert

            // there will be 3 since that is how many content items there are
            Assert.AreEqual(3, permissions.Count);

            // test permissions contains content[0]
            Assert.IsTrue(permissions.ContainsKey(content[0].Id));

            // test that this permissions set contains permissions for all groups
            Assert.IsTrue(permissions[content[0].Id].ContainsKey(userGroup1.Id));
            Assert.IsTrue(permissions[content[0].Id].ContainsKey(userGroup2.Id));
            Assert.IsTrue(permissions[content[0].Id].ContainsKey(userGroup3.Id));

            // test that the correct number of permissions are returned for each group
            Assert.AreEqual(2, permissions[content[0].Id][userGroup1.Id].SelectMany(x => x.AssignedPermissions).Count());
            Assert.AreEqual(1, permissions[content[0].Id][userGroup2.Id].SelectMany(x => x.AssignedPermissions).Count());
            Assert.AreEqual(defaultPermissionCount, permissions[content[0].Id][userGroup3.Id].SelectMany(x => x.AssignedPermissions).Count());

            // test permissions contains content[1]
            Assert.IsTrue(permissions.ContainsKey(content[1].Id));

            // test that this permissions set contains permissions for all groups
            Assert.IsTrue(permissions[content[1].Id].ContainsKey(userGroup1.Id));
            Assert.IsTrue(permissions[content[1].Id].ContainsKey(userGroup2.Id));
            Assert.IsTrue(permissions[content[1].Id].ContainsKey(userGroup3.Id));

            // test that the correct number of permissions are returned for each group
            Assert.AreEqual(1, permissions[content[1].Id][userGroup1.Id].SelectMany(x => x.AssignedPermissions).Count());
            Assert.AreEqual(1, permissions[content[1].Id][userGroup2.Id].SelectMany(x => x.AssignedPermissions).Count());
            Assert.AreEqual(defaultPermissionCount, permissions[content[1].Id][userGroup3.Id].SelectMany(x => x.AssignedPermissions).Count());

            // test permissions contains content[2]
            Assert.IsTrue(permissions.ContainsKey(content[2].Id));

            // test that this permissions set contains permissions for all groups
            Assert.IsTrue(permissions[content[2].Id].ContainsKey(userGroup1.Id));
            Assert.IsTrue(permissions[content[2].Id].ContainsKey(userGroup2.Id));
            Assert.IsTrue(permissions[content[2].Id].ContainsKey(userGroup3.Id));

            // test that the correct number of permissions are returned for each group
            Assert.AreEqual(1, permissions[content[2].Id][userGroup1.Id].SelectMany(x => x.AssignedPermissions).Count());
            Assert.AreEqual(defaultPermissionCount, permissions[content[2].Id][userGroup2.Id].SelectMany(x => x.AssignedPermissions).Count());
            Assert.AreEqual(defaultPermissionCount, permissions[content[2].Id][userGroup3.Id].SelectMany(x => x.AssignedPermissions).Count());
        }