public UpdateResult SynchronizeUser(string userName) { CPUser user = this.GetUser(userName); if (user == null) { return(new UpdateResult(null, UpdateResultState.MembershipUserNotFound)); } UpdateResultState state = UpdateResultState.Success; // find source entity Entity source = null; switch (user.SourceType) { case EntityType.Candidate: source = CPProviders.DataProvider.GetEntity(user.Cid); break; case EntityType.Treasurer: source = CPProviders.DataProvider.GetEntity(user.Cid, user.SourceCommitteeID, user.SourceElectionCycle); break; case EntityType.Liaison: case EntityType.Consultant: source = CPProviders.DataProvider.GetEntity(user.Cid, user.SourceCommitteeID, liaisonID: user.SourceLiaisonID); break; } if (source == null) { state |= UpdateResultState.CfisContactNotFound; } else { // update email/name MembershipUserCollection conflicts = Membership.FindUsersByEmail(source.Email); foreach (MembershipUser u in conflicts) { if (string.Equals(user.Cid, GetCid(u.UserName), StringComparison.OrdinalIgnoreCase) && !string.Equals(user.UserName, u.UserName, StringComparison.OrdinalIgnoreCase)) { state |= UpdateResultState.EmailAddressInUse; break; } } if (string.Equals(user.DisplayName, source.Name, StringComparison.OrdinalIgnoreCase)) // update only name or email address, but not both simultaneously { if (!string.IsNullOrEmpty(source.Email)) { MembershipUser u = Membership.GetUser(user.UserName); if (u != null) { user.Email = u.Email = source.Email; try { Membership.UpdateUser(u); state |= UpdateResultState.EmailAddressChanged; } catch { state |= UpdateResultState.AspNetMembershipFailure; } } else { state |= UpdateResultState.MembershipUserNotFound; } } } else { // update name if (!string.IsNullOrEmpty(source.Name)) { user.DisplayName = source.Name; state |= UpdateResultState.DisplayNameChanged; } } } // persist changes UpdateResult result = new UpdateResult(user, state); if (result.Succeeded) { result = UpdateUser(user); } return(result); }
/// <summary> /// Creates a new <see cref="UpdateResult"/> structure that contains the specified results of a user update operation. /// </summary> /// <param name="user">The updated user.</param> /// <param name="state">The result state of the update.</param> public UpdateResult(CPUser user, UpdateResultState state) { _user = user; _state = state; }
public UpdateResult UpdateUser(CPUser user) { UpdateResultState state = UpdateResultState.Success; // .NET Membership properties MembershipUser muser = user == null ? null : Membership.GetUser(user.UserName); if (muser == null) { state |= UpdateResultState.MembershipUserNotFound; } else { bool dirty = false; if (muser.Email != user.Email) { muser.Email = user.Email; dirty = true; } if (muser.IsApproved != user.Enabled) { muser.IsApproved = user.Enabled; dirty = true; } if (muser.IsLockedOut && !user.LockedOut) { muser.UnlockUser(); dirty = true; } if (dirty) { try { Membership.UpdateUser(muser); } catch { state |= UpdateResultState.AspNetMembershipFailure; } } // C-Access Security using (CPSecurityEntities context = new CPSecurityEntities()) { var match = context.SecurityUserProfiles.FirstOrDefault(u => u.UserName == user.UserName); if (match == null) { state |= UpdateResultState.ProfileNotFound; } else { // basic properties match.CandidateId = user.Cid; match.DisplayName = user.DisplayName; match.CfisType = (byte)user.SourceType; match.CfisCommitteeID = user.SourceCommitteeID.HasValue ? user.SourceCommitteeID.Value.ToString() : null; match.CfisCommitteeContactID = user.SourceLiaisonID.HasValue ? user.SourceLiaisonID.Value.ToString() : user.SourceElectionCycle; // election cycles var currentCycles = context.SecurityUserElectionCycles.Where(c => c.UserName == user.UserName); var updateCycles = user.ElectionCycles; if (user.ImplicitElectionCycles == updateCycles.Any()) // implicit EC access requires both an empty EC collection AND a set flag { state |= UpdateResultState.ElectionCycleNotFound; } else { // delete old cycles foreach (var cycle in currentCycles.Where(c => !updateCycles.Contains(c.ElectionCycle))) { context.SecurityUserElectionCycles.DeleteObject(cycle); } // add new cycles foreach (var cycle in updateCycles.Except(currentCycles.Select(c => c.ElectionCycle))) { context.SecurityUserElectionCycles.AddObject(SecurityUserElectionCycle.CreateSecurityUserElectionCycle(user.UserName, cycle)); } } // save changes try { context.SaveChanges(); } catch (OptimisticConcurrencyException) { context.Refresh(RefreshMode.ClientWins, match); context.SaveChanges(); } catch (ConstraintException) { state |= UpdateResultState.ProfileFailure; } } } } return(new UpdateResult(GetUser(user.UserName), state)); }