public bool ChangePassword(string username, string oldPassword, string newPassword) { MembershipUser user = Membership.GetUser(username); if (user != null && user.ChangePassword(oldPassword, newPassword)) { using (CPSecurityEntities context = new CPSecurityEntities()) { var match = context.SecurityUserProfiles.FirstOrDefault(p => p.UserName == username); if (match != null) { match.PasswordExpired = false; try { context.SaveChanges(); } catch (OptimisticConcurrencyException) { context.Refresh(RefreshMode.ClientWins, match); context.SaveChanges(); } } } return(true); } return(false); }
public CPGroup UpdateGroup(CPGroup group) { if (group == null) { return(null); } using (CPSecurityEntities context = new CPSecurityEntities()) { var match = context.SecurityGroups.FirstOrDefault(g => g.GroupId == group.ID); if (match == null) { return(null); } match.GroupName = group.Name; match.UserRights = (int)group.UserRights; try { context.SaveChanges(); } catch (OptimisticConcurrencyException) { context.Refresh(RefreshMode.ClientWins, match); context.SaveChanges(); } catch (ConstraintException) { } return(GetGroup(match.GroupName)); } }
/// <summary> /// Deletes a security user profile from the datastore. /// </summary> /// <param name="context">The datastore entity context to use.</param> /// <param name="username">The username of the profile to delete.</param> public static void DeleteSecurityUserProfile(this CPSecurityEntities context, string username) { var match = context.SecurityUserProfiles.FirstOrDefault(p => p.UserName == username); if (match != null) { try { context.DeleteObject(match); context.SaveChanges(); } catch (OptimisticConcurrencyException) { context.Refresh(RefreshMode.ClientWins, match); context.DeleteObject(match); } } }
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)); }