Exemplo n.º 1
0
        protected override void PersistNewItem(IUser entity)
        {
            ((User)entity).AddingEntity();

            //ensure security stamp if non
            if (entity.SecurityStamp.IsNullOrWhiteSpace())
            {
                entity.SecurityStamp = Guid.NewGuid().ToString();
            }

            var userDto = UserFactory.BuildDto(entity);

            //Check if we have a known config, we only want to store config for hashing
            //TODO: This logic will need to be updated when we do http://issues.umbraco.org/issue/U4-10089
            if (_passwordConfiguration != null && _passwordConfiguration.Count > 0)
            {
                var json = JsonConvert.SerializeObject(_passwordConfiguration);
                userDto.PasswordConfig = json;
            }

            var id = Convert.ToInt32(Database.Insert(userDto));

            entity.Id = id;

            if (entity.IsPropertyDirty("StartContentIds") || entity.IsPropertyDirty("StartMediaIds"))
            {
                if (entity.IsPropertyDirty("StartContentIds"))
                {
                    AddingOrUpdateStartNodes(entity, Enumerable.Empty <UserStartNodeDto>(), UserStartNodeDto.StartNodeTypeValue.Content, entity.StartContentIds);
                }
                if (entity.IsPropertyDirty("StartMediaIds"))
                {
                    AddingOrUpdateStartNodes(entity, Enumerable.Empty <UserStartNodeDto>(), UserStartNodeDto.StartNodeTypeValue.Media, entity.StartMediaIds);
                }
            }

            if (entity.IsPropertyDirty("Groups"))
            {
                //lookup all assigned
                var assigned = entity.Groups == null || entity.Groups.Any() == false
                    ? new List <UserGroupDto>()
                    : Database.Fetch <UserGroupDto>("SELECT * FROM umbracoUserGroup WHERE userGroupAlias IN (@aliases)", new { aliases = entity.Groups.Select(x => x.Alias) });

                foreach (var groupDto in assigned)
                {
                    var dto = new User2UserGroupDto
                    {
                        UserGroupId = groupDto.Id,
                        UserId      = entity.Id
                    };
                    Database.Insert(dto);
                }
            }

            entity.ResetDirtyProperties();
        }
Exemplo n.º 2
0
 /// <summary>
 ///     Adds a set of users to a group
 /// </summary>
 /// <param name="groupId">Id of group</param>
 /// <param name="userIds">Ids of users</param>
 private void AddUsersToGroup(int groupId, int[] userIds)
 {
     foreach (var userId in userIds)
     {
         var dto = new User2UserGroupDto {
             UserGroupId = groupId, UserId = userId
         };
         Database.Insert(dto);
     }
 }
 /// <summary>
 /// Adds a set of users to a group
 /// </summary>
 /// <param name="groupId">Id of group</param>
 /// <param name="userIds">Ids of users</param>
 private void AddUsersToGroup(int groupId, int[] userIds)
 {
     //TODO: Check if the user exists?
     foreach (var userId in userIds)
     {
         var dto = new User2UserGroupDto
         {
             UserGroupId = groupId,
             UserId      = userId,
         };
         Database.Insert(dto);
     }
 }
Exemplo n.º 4
0
        protected override void PersistNewItem(IUser entity)
        {
            entity.AddingEntity();

            // ensure security stamp if missing
            if (entity.SecurityStamp.IsNullOrWhiteSpace())
            {
                entity.SecurityStamp = Guid.NewGuid().ToString();
            }

            UserDto userDto = UserFactory.BuildDto(entity);

            // check if we have a user config else use the default
            userDto.PasswordConfig = entity.PasswordConfiguration ?? DefaultPasswordConfigJson;

            var id = Convert.ToInt32(Database.Insert(userDto));

            entity.Id = id;

            if (entity.IsPropertyDirty("StartContentIds"))
            {
                AddingOrUpdateStartNodes(entity, Enumerable.Empty <UserStartNodeDto>(), UserStartNodeDto.StartNodeTypeValue.Content, entity.StartContentIds);
            }

            if (entity.IsPropertyDirty("StartMediaIds"))
            {
                AddingOrUpdateStartNodes(entity, Enumerable.Empty <UserStartNodeDto>(), UserStartNodeDto.StartNodeTypeValue.Media, entity.StartMediaIds);
            }

            if (entity.IsPropertyDirty("Groups"))
            {
                // lookup all assigned
                var assigned = entity.Groups == null || entity.Groups.Any() == false
                    ? new List <UserGroupDto>()
                    : Database.Fetch <UserGroupDto>("SELECT * FROM umbracoUserGroup WHERE userGroupAlias IN (@aliases)", new { aliases = entity.Groups.Select(x => x.Alias) });

                foreach (var groupDto in assigned)
                {
                    var dto = new User2UserGroupDto
                    {
                        UserGroupId = groupDto.Id,
                        UserId      = entity.Id
                    };
                    Database.Insert(dto);
                }
            }

            entity.ResetDirtyProperties();
        }
Exemplo n.º 5
0
        protected override void PersistUpdatedItem(IUser entity)
        {
            // updates Modified date
            ((User)entity).UpdatingEntity();

            // ensure security stamp if missing
            if (entity.SecurityStamp.IsNullOrWhiteSpace())
            {
                entity.SecurityStamp = Guid.NewGuid().ToString();
            }

            var userDto = UserFactory.BuildDto(entity);

            // build list of columns to check for saving - we don't want to save the password if it hasn't changed!
            // list the columns to save, NOTE: would be nice to not have hard coded strings here but no real good way around that
            var colsToSave = new Dictionary <string, string>
            {
                { "userDisabled", "IsApproved" },
                { "userNoConsole", "IsLockedOut" },
                { "startStructureID", "StartContentId" },
                { "startMediaID", "StartMediaId" },
                { "userName", "Name" },
                { "userLogin", "Username" },
                { "userEmail", "Email" },
                { "userLanguage", "Language" },
                { "securityStampToken", "SecurityStamp" },
                { "lastLockoutDate", "LastLockoutDate" },
                { "lastPasswordChangeDate", "LastPasswordChangeDate" },
                { "lastLoginDate", "LastLoginDate" },
                { "failedLoginAttempts", "FailedPasswordAttempts" },
                { "createDate", "CreateDate" },
                { "updateDate", "UpdateDate" },
                { "avatar", "Avatar" },
                { "emailConfirmedDate", "EmailConfirmedDate" },
                { "invitedDate", "InvitedDate" },
                { "tourData", "TourData" }
            };

            // create list of properties that have changed
            var changedCols = colsToSave
                              .Where(col => entity.IsPropertyDirty(col.Value))
                              .Select(col => col.Key)
                              .ToList();

            // DO NOT update the password if it has not changed or if it is null or empty
            if (entity.IsPropertyDirty("RawPasswordValue") && entity.RawPasswordValue.IsNullOrWhiteSpace() == false)
            {
                changedCols.Add("userPassword");

                // special case - when using ASP.Net identity the user manager will take care of updating the security stamp, however
                // when not using ASP.Net identity (i.e. old membership providers), we'll need to take care of updating this manually
                // so we can just detect if that property is dirty, if it's not we'll set it manually
                if (entity.IsPropertyDirty("SecurityStamp") == false)
                {
                    userDto.SecurityStampToken = entity.SecurityStamp = Guid.NewGuid().ToString();
                    changedCols.Add("securityStampToken");
                }

                // check if we have a known config, we only want to store config for hashing
                //TODO: This logic will need to be updated when we do http://issues.umbraco.org/issue/U4-10089
                if (PasswordConfigJson != null)
                {
                    userDto.PasswordConfig = PasswordConfigJson;
                    changedCols.Add("passwordConfig");
                }
            }

            //only update the changed cols
            if (changedCols.Count > 0)
            {
                Database.Update(userDto, changedCols);
            }

            if (entity.IsPropertyDirty("StartContentIds") || entity.IsPropertyDirty("StartMediaIds"))
            {
                var assignedStartNodes = Database.Fetch <UserStartNodeDto>("SELECT * FROM umbracoUserStartNode WHERE userId = @userId", new { userId = entity.Id });
                if (entity.IsPropertyDirty("StartContentIds"))
                {
                    AddingOrUpdateStartNodes(entity, assignedStartNodes, UserStartNodeDto.StartNodeTypeValue.Content, entity.StartContentIds);
                }
                if (entity.IsPropertyDirty("StartMediaIds"))
                {
                    AddingOrUpdateStartNodes(entity, assignedStartNodes, UserStartNodeDto.StartNodeTypeValue.Media, entity.StartMediaIds);
                }
            }

            if (entity.IsPropertyDirty("Groups"))
            {
                //lookup all assigned
                var assigned = entity.Groups == null || entity.Groups.Any() == false
                    ? new List <UserGroupDto>()
                    : Database.Fetch <UserGroupDto>("SELECT * FROM umbracoUserGroup WHERE userGroupAlias IN (@aliases)", new { aliases = entity.Groups.Select(x => x.Alias) });

                //first delete all
                //TODO: We could do this a nicer way instead of "Nuke and Pave"
                Database.Delete <User2UserGroupDto>("WHERE UserId = @UserId", new { UserId = entity.Id });

                foreach (var groupDto in assigned)
                {
                    var dto = new User2UserGroupDto
                    {
                        UserGroupId = groupDto.Id,
                        UserId      = entity.Id
                    };
                    Database.Insert(dto);
                }
            }

            entity.ResetDirtyProperties();
        }
Exemplo n.º 6
0
        protected override void PersistNewItem(IUser entity)
        {
            // the use may have no identity, ie ID is zero, and be v7 super
            // user - then it has been marked - and we must not persist it
            // as new, as we do not want to create a new user - instead, persist
            // it as updated
            // see also: UserFactory.BuildEntity
            if (((User)entity).AdditionalData.ContainsKey("IS_V7_ZERO"))
            {
                PersistUpdatedItem(entity);
                return;
            }

            entity.AddingEntity();

            // ensure security stamp if missing
            if (entity.SecurityStamp.IsNullOrWhiteSpace())
            {
                entity.SecurityStamp = Guid.NewGuid().ToString();
            }

            var userDto = UserFactory.BuildDto(entity);

            // check if we have a known config, we only want to store config for hashing
            // TODO: This logic will need to be updated when we do http://issues.umbraco.org/issue/U4-10089
            if (PasswordConfigJson != null)
            {
                userDto.PasswordConfig = PasswordConfigJson;
            }

            var id = Convert.ToInt32(Database.Insert(userDto));

            entity.Id = id;

            if (entity.IsPropertyDirty("StartContentIds"))
            {
                AddingOrUpdateStartNodes(entity, Enumerable.Empty <UserStartNodeDto>(), UserStartNodeDto.StartNodeTypeValue.Content, entity.StartContentIds);
            }

            if (entity.IsPropertyDirty("StartMediaIds"))
            {
                AddingOrUpdateStartNodes(entity, Enumerable.Empty <UserStartNodeDto>(), UserStartNodeDto.StartNodeTypeValue.Media, entity.StartMediaIds);
            }

            if (entity.IsPropertyDirty("Groups"))
            {
                // lookup all assigned
                var assigned = entity.Groups == null || entity.Groups.Any() == false
                    ? new List <UserGroupDto>()
                    : Database.Fetch <UserGroupDto>("SELECT * FROM umbracoUserGroup WHERE userGroupAlias IN (@aliases)", new { aliases = entity.Groups.Select(x => x.Alias) });

                foreach (var groupDto in assigned)
                {
                    var dto = new User2UserGroupDto
                    {
                        UserGroupId = groupDto.Id,
                        UserId      = entity.Id
                    };
                    Database.Insert(dto);
                }
            }

            entity.ResetDirtyProperties();
        }
Exemplo n.º 7
0
        protected override void PersistUpdatedItem(IUser entity)
        {
            // updates Modified date
            entity.UpdatingEntity();

            // ensure security stamp if missing
            if (entity.SecurityStamp.IsNullOrWhiteSpace())
            {
                entity.SecurityStamp = Guid.NewGuid().ToString();
            }

            var userDto = UserFactory.BuildDto(entity);

            // build list of columns to check for saving - we don't want to save the password if it hasn't changed!
            // list the columns to save, NOTE: would be nice to not have hard coded strings here but no real good way around that
            var colsToSave = new Dictionary <string, string>
            {
                //TODO: Change these to constants + nameof
                { "userDisabled", "IsApproved" },
                { "userNoConsole", "IsLockedOut" },
                { "startStructureID", "StartContentId" },
                { "startMediaID", "StartMediaId" },
                { "userName", "Name" },
                { "userLogin", "Username" },
                { "userEmail", "Email" },
                { "userLanguage", "Language" },
                { "securityStampToken", "SecurityStamp" },
                { "lastLockoutDate", "LastLockoutDate" },
                { "lastPasswordChangeDate", "LastPasswordChangeDate" },
                { "lastLoginDate", "LastLoginDate" },
                { "failedLoginAttempts", "FailedPasswordAttempts" },
                { "createDate", "CreateDate" },
                { "updateDate", "UpdateDate" },
                { "avatar", "Avatar" },
                { "emailConfirmedDate", "EmailConfirmedDate" },
                { "invitedDate", "InvitedDate" },
                { "tourData", "TourData" }
            };

            // create list of properties that have changed
            var changedCols = colsToSave
                              .Where(col => entity.IsPropertyDirty(col.Value))
                              .Select(col => col.Key)
                              .ToList();

            if (entity.IsPropertyDirty("SecurityStamp"))
            {
                changedCols.Add("securityStampToken");
            }

            // DO NOT update the password if it has not changed or if it is null or empty
            if (entity.IsPropertyDirty("RawPasswordValue") && entity.RawPasswordValue.IsNullOrWhiteSpace() == false)
            {
                changedCols.Add("userPassword");

                // If the security stamp hasn't already updated we need to force it
                if (entity.IsPropertyDirty("SecurityStamp") == false)
                {
                    userDto.SecurityStampToken = entity.SecurityStamp = Guid.NewGuid().ToString();
                    changedCols.Add("securityStampToken");
                }

                // check if we have a user config else use the default
                userDto.PasswordConfig = entity.PasswordConfiguration ?? DefaultPasswordConfigJson;
                changedCols.Add("passwordConfig");
            }

            // If userlogin or the email has changed then need to reset security stamp
            if (changedCols.Contains("userLogin") || changedCols.Contains("userEmail"))
            {
                userDto.EmailConfirmedDate = null;
                changedCols.Add("emailConfirmedDate");

                // If the security stamp hasn't already updated we need to force it
                if (entity.IsPropertyDirty("SecurityStamp") == false)
                {
                    userDto.SecurityStampToken = entity.SecurityStamp = Guid.NewGuid().ToString();
                    changedCols.Add("securityStampToken");
                }
            }

            //only update the changed cols
            if (changedCols.Count > 0)
            {
                Database.Update(userDto, changedCols);
            }

            if (entity.IsPropertyDirty("StartContentIds") || entity.IsPropertyDirty("StartMediaIds"))
            {
                var assignedStartNodes = Database.Fetch <UserStartNodeDto>("SELECT * FROM umbracoUserStartNode WHERE userId = @userId", new { userId = entity.Id });
                if (entity.IsPropertyDirty("StartContentIds"))
                {
                    AddingOrUpdateStartNodes(entity, assignedStartNodes, UserStartNodeDto.StartNodeTypeValue.Content, entity.StartContentIds);
                }
                if (entity.IsPropertyDirty("StartMediaIds"))
                {
                    AddingOrUpdateStartNodes(entity, assignedStartNodes, UserStartNodeDto.StartNodeTypeValue.Media, entity.StartMediaIds);
                }
            }

            if (entity.IsPropertyDirty("Groups"))
            {
                //lookup all assigned
                var assigned = entity.Groups == null || entity.Groups.Any() == false
                    ? new List <UserGroupDto>()
                    : Database.Fetch <UserGroupDto>("SELECT * FROM umbracoUserGroup WHERE userGroupAlias IN (@aliases)", new { aliases = entity.Groups.Select(x => x.Alias) });

                //first delete all
                // TODO: We could do this a nicer way instead of "Nuke and Pave"
                Database.Delete <User2UserGroupDto>("WHERE UserId = @UserId", new { UserId = entity.Id });

                foreach (var groupDto in assigned)
                {
                    var dto = new User2UserGroupDto
                    {
                        UserGroupId = groupDto.Id,
                        UserId      = entity.Id
                    };
                    Database.Insert(dto);
                }
            }

            entity.ResetDirtyProperties();
        }