private void FixupActivation(AssociationUserGroupMemberActivation previousValue) { // This is the principal end in an association that performs cascade deletes. // Update the event listener to refer to the new dependent. if (previousValue != null) { ChangeTracker.ObjectStateChanging -= previousValue.HandleCascadeDelete; } if (Activation != null) { ChangeTracker.ObjectStateChanging += Activation.HandleCascadeDelete; } if (IsDeserializing) { return; } if (Activation != null) { Activation.MemberId = Id; } if (ChangeTracker.ChangeTrackingEnabled) { if (ChangeTracker.OriginalValues.ContainsKey("Activation") && (ChangeTracker.OriginalValues["Activation"] == Activation)) { ChangeTracker.OriginalValues.Remove("Activation"); } else { ChangeTracker.RecordOriginalValue("Activation", previousValue); // This is the principal end of an identifying association, so the dependent must be deleted when the relationship is removed. // If the current state of the dependent is Added, the relationship can be changed without causing the dependent to be deleted. if (previousValue != null && previousValue.ChangeTracker.State != ObjectState.Added) { previousValue.MarkAsDeleted(); } } if (Activation != null && !Activation.ChangeTracker.ChangeTrackingEnabled) { Activation.StartTracking(); } } }
public bool Equals(AssociationUserGroupMemberActivation other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; if (other.MemberId == 0 && MemberId == 0) return false; else return other.MemberId == MemberId; }
public AssociationUserGroup SaveUserGroup(AssociationUserGroup userGroup, bool isTravelAgent, int agencyId) { using (TransactionScope scope = new TransactionScope()) using (var db = new LomsContext()) { AssociationUser manager = null; var userCache = new Dictionary<int, AssociationUser>(); bool newEntity = false; if (userGroup.Id == 0) newEntity = true; db.AssociationUserGroups.ApplyChanges(userGroup); db.SaveChanges(); var existedMembers = (from m in db.AssociationUserGroupMembers where m.GroupId == userGroup.Id select m) .ToList(); bool hasErrors = false; foreach (var member in userGroup.Members) { AssociationUser user = null; var existed = existedMembers.FirstOrDefault(m => m.UserId == member.UserId); if (existed != null) { if (!userCache.TryGetValue(member.UserId, out user)) { user = (from u in db.AssociationUsers where u.Id == member.UserId select u) .SingleOrDefault(); if (user != null) userCache[user.Id] = user; } if (user != null && user.Email == member.Email) { if (member.IsManager) manager = user; existedMembers.Remove(existed); continue; } } user = (from u in db.AssociationUsers where u.AssociationId == CurrentAssociationId && u.Email == member.Email select u) .SingleOrDefault(); if (user != null) userCache[user.Id] = user; if (user == null || !user.HasOnlineAccess) member.Errors.AddError("Email", "User is not registered!"); else if (user.IsTravelAgency && !isTravelAgent) member.Errors.AddError("Email", "User is a travel agent!"); else if (!user.IsTravelAgency && isTravelAgent) member.Errors.AddError("Email", "User is not a travel agent!"); //check if user belong to another travel agency AssociationUserTravelAgencyRole role = null; if (isTravelAgent && user != null) { role = (from r in db.AssociationUserTravelAgencyRoles where r.UserId == user.Id select r).SingleOrDefault(); if (role != null && role.AgencyId != agencyId) member.Errors.AddError("Email", "User is already associated with another travel agency!"); } if (member.Errors.Count() > 0 || hasErrors) { hasErrors = true; continue; } if (member.IsManager) manager = user; //add member.GroupId = userGroup.Id; member.UserId = user.Id; member.Status = member.IsManager ? AssociationUserGroupStatus.Accepted : AssociationUserGroupStatus.Pending; member.Email = user.Email; member.FirstName = user.FirstName; member.LastName = user.LastName; db.AssociationUserGroupMembers.ApplyChanges(member); db.SaveChanges(); if (member.Status == AssociationUserGroupStatus.Pending) { //start activation process AssociationUserGroupMemberActivation activation = new AssociationUserGroupMemberActivation(); activation.MemberId = member.Id; activation.Guid = Guid.NewGuid(); activation.ExpiryTime = DateTime.UtcNow.AddDays(7.0); db.AssociationUserGroupMemberActivations.ApplyChanges(activation); if (user.IsTravelAgency) { if (role == null) { role = new AssociationUserTravelAgencyRole(); role.UserId = user.Id; role.AgencyId = agencyId; role.Status = TravelAgencyStatus.Pending; role.Role = TravelAgencyRole.Consultant; db.AssociationUserTravelAgencyRoles.ApplyChanges(role); } } db.SaveChanges(); //send email var emailProvider = (from e in db.AssociationEmails where e.AssociationId == CurrentAssociationId select e) .FirstOrDefault(); if (emailProvider != null) { var association = (from a in db.Associations where a.Id == CurrentAssociationId select a) .SingleOrDefault(); var request = HttpContext.Current.Request; var uri = request.Url; string baseUrl = String.Format("{0}://{1}:{2}", uri.Scheme, uri.Host ?? "80", uri.Port); string activtionLink = Path.Combine(baseUrl + string.Format("/#GroupMemberActivation/{0}", activation.Guid.ToString("D"))); string contactUsLink = Path.Combine(baseUrl + "/#Contact"); string termAndConditionsLink = Path.Combine(baseUrl + "/terms"); string txtContent, htmlContent; if (!isTravelAgent) { txtContent = MailTemplateHelper.GetByGeneralUserToGroupActivationTxtContent(association.Name.ToUpper(), user.FullName.ToUpper(), activtionLink, contactUsLink, userGroup.Name, manager.FullName, manager.Email, termAndConditionsLink); htmlContent = MailTemplateHelper.GetByGeneralUserToGroupActivationHtmlContent(association.Name.ToUpper(), user.FullName.ToUpper(), activtionLink, contactUsLink, userGroup.Name, manager.FullName, manager.Email, termAndConditionsLink); } else { txtContent = MailTemplateHelper.GetByTravelAgentToGroupActivationTxtContent(association.Name.ToUpper(), user.FullName.ToUpper(), activtionLink, contactUsLink, "TRAVEL AGENCY", userGroup.Name, manager.FullName, manager.Email, termAndConditionsLink); htmlContent = MailTemplateHelper.GetByTravelAgentToGroupActivationHtmlContent(association.Name.ToUpper(), user.FullName.ToUpper(), activtionLink, contactUsLink, "TRAVEL AGENCY", userGroup.Name, manager.FullName, manager.Email, termAndConditionsLink); } var avBody = AlternateView.CreateAlternateViewFromString(htmlContent, null, MediaTypeNames.Text.Html); emailProvider.SendMail(user.Email, association.Name.ToUpper() + " Invitation To Group", txtContent, null, avBody, true); } } } if (hasErrors) { userGroup.AddError("Members", "Invalid email addresses"); if (newEntity) userGroup.Id = 0; return userGroup; } else { foreach (var existed in existedMembers) db.AssociationUserGroupMembers.DeleteObject(existed); db.SaveChanges(); } scope.Complete(); return userGroup; } }