private void ValidateUser(User user, UserProperties props) { if (props.HasFlag(UserProperties.Email) && (user.Email == null || !Regex.IsMatch(user.Email, _emailPattern, RegexOptions.IgnoreCase))) { throw new InvalidEmailException(); } if (props.HasFlag(UserProperties.Password) && (user.Password == null || !Regex.IsMatch(user.Password, _passwordPattern, RegexOptions.IgnoreCase))) { throw new InvalidPasswordHashException(); } if (user.DateOfBirth != null && props.HasFlag(UserProperties.DateOfBirth) && user.DateOfBirth >= DateTime.Now) { throw new InvalidDateOfBirthException(); } }
public void UpdateUser(Guid oldUserId, User @new, UserProperties propertiesToChange) { ValidateUser(@new, propertiesToChange); using (var ctx = new DB.CodingIdeasEntities()) { if (ctx.Users.Where(x => x.Username == @new.Username && x.Password == @new.Password).Count() != 0) { throw new InvalidCredentialsException(); } var r = ctx.Users.Include(x => x.UserSkills).Where(x => x.Id == oldUserId).FirstOrDefault(); if (r == null) { throw new UserNotFoundException(); } if (propertiesToChange.HasFlag(UserProperties.DateOfBirth)) { r.DOB = @new.DateOfBirth; } if (propertiesToChange.HasFlag(UserProperties.Email)) { r.Email = @new.Email; } if (propertiesToChange.HasFlag(UserProperties.FirstName)) { r.FirstName = @new.FirstName; } if (propertiesToChange.HasFlag(UserProperties.LastName)) { r.LastName = @new.LastName; } if (propertiesToChange.HasFlag(UserProperties.Password)) { r.Password = @new.Password; } if (propertiesToChange.HasFlag(UserProperties.Username)) { r.Username = @new.Username; } if (propertiesToChange.HasFlag(UserProperties.Skills)) { r.UserSkills.Clear(); foreach (var s in @new.Skills) { r.UserSkills.Add(new DB.UserSkill() { ProgrammingLanguageId = s.ProgrammingLanguage.Id, Proficiency = s.Proficiency, UserId = r.Id }); } } ctx.SaveChanges(); } }
/// <summary> /// Set selected properties of a user. /// </summary> /// <param name="userHandle">Handle to an open SAM user.</param> /// <param name="sourceUser"> /// A <see cref="LocalUser"/> object containing the data to set into the user. /// </param> /// <param name="setFlags"> /// A combination of <see cref="UserProperties"/> values indicating the properties to be set. /// </param> /// <param name="password">A <see cref="System.Security.SecureString"/> /// object containing the new password. /// </param> /// <param name="passwordExpired">One of the /// <see cref="PasswordExpiredState"/> enumeration values indicating /// whether the password-expired state is to be explicitly set or /// left as is. If the <paramref name="password"/> parameter is null, /// this parameter is ignored. /// </param> /// <param name="setPasswordNeverExpires"> /// Nullable value the specifies whether the PasswordNeverExpires bit should be flipped /// </param> private void SetUserData(IntPtr userHandle, LocalUser sourceUser, UserProperties setFlags, System.Security.SecureString password, PasswordExpiredState passwordExpired, bool? setPasswordNeverExpires) { IntPtr buffer = IntPtr.Zero; try { UInt32 which = 0; UInt32 status = 0; UInt32 uac = GetUserAccountControl(userHandle); USER_ALL_INFORMATION info = new USER_ALL_INFORMATION(); if (setFlags.HasFlag(UserProperties.AccountExpires)) { which |= SamApi.USER_ALL_ACCOUNTEXPIRES; info.AccountExpires.QuadPart = sourceUser.AccountExpires.HasValue ? sourceUser.AccountExpires.Value.ToFileTime() : 0L; } if (setFlags.HasFlag(UserProperties.Description)) { which |= SamApi.USER_ALL_ADMINCOMMENT; info.AdminComment = new UNICODE_STRING(sourceUser.Description); } if (setFlags.HasFlag(UserProperties.Enabled)) { which |= SamApi.USER_ALL_USERACCOUNTCONTROL; if (sourceUser.Enabled) uac &= ~SamApi.USER_ACCOUNT_DISABLED; else uac |= SamApi.USER_ACCOUNT_DISABLED; } if (setFlags.HasFlag(UserProperties.FullName)) { which |= SamApi.USER_ALL_FULLNAME; info.FullName = new UNICODE_STRING(sourceUser.FullName); } if (setFlags.HasFlag(UserProperties.PasswordNeverExpires)) { // Only modify the bit if a change was requested if (setPasswordNeverExpires.HasValue) { which |= SamApi.USER_ALL_USERACCOUNTCONTROL; if (setPasswordNeverExpires.Value) uac |= SamApi.USER_DONT_EXPIRE_PASSWORD; else uac &= ~SamApi.USER_DONT_EXPIRE_PASSWORD; } } if (setFlags.HasFlag(UserProperties.PasswordRequired)) { which |= SamApi.USER_ALL_USERACCOUNTCONTROL; if (sourceUser.PasswordRequired) uac &= ~SamApi.USER_PASSWORD_NOT_REQUIRED; else uac |= SamApi.USER_PASSWORD_NOT_REQUIRED; } if (which != 0) { info.WhichFields = which; if ((which & SamApi.USER_ALL_USERACCOUNTCONTROL) != 0) info.UserAccountControl = uac; buffer = Marshal.AllocHGlobal(ClrFacade.SizeOf<USER_ALL_INFORMATION>()); ClrFacade.StructureToPtr<USER_ALL_INFORMATION>(info, buffer, false); status = SamApi.SamSetInformationUser(userHandle, USER_INFORMATION_CLASS.UserAllInformation, buffer); ThrowOnFailure(status); status = SamApi.SamFreeMemory(buffer); buffer = IntPtr.Zero; } if (setFlags.HasFlag(UserProperties.UserMayChangePassword)) SetUserMayChangePassword(userHandle, sourceUser.SID, sourceUser.UserMayChangePassword); if (password != null) SetUserPassword(userHandle, password, passwordExpired); } finally { if (buffer != IntPtr.Zero) { ClrFacade.DestroyStructure<USER_ALL_INFORMATION>(buffer); Marshal.FreeHGlobal(buffer); } } }