Пример #1
0
     public bool Equals(AssociationUserGroup other)
     {
         if (ReferenceEquals(null, other)) return false;
         if (ReferenceEquals(this, other)) return true;
 		if (other.Id == 0 && Id == 0)
 			return false;
 		else
 			return other.Id == Id;
     }
Пример #2
0
        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;
            }
        }
Пример #3
0
        private void FillUserGroupMembers(LomsContext db, AssociationUserGroup userGroup, Dictionary<int, AssociationUser> userCache)
        {
            var members = from m in db.AssociationUserGroupMembers
                          where m.GroupId == userGroup.Id
                          select m;

            foreach (var member in members)
            {
                AssociationUser user = null;
                if (userCache == null || !userCache.TryGetValue(member.UserId, out user))
                {
                    user = (from u in db.AssociationUsers
                            where u.Id == member.UserId
                            select u)
                            .SingleOrDefault();

                    if (user != null && userCache != null)
                        userCache[user.Id] = user;
                }

                if (user != null)
                {
                    member.Email = user.Email;
                    member.FirstName = user.FirstName;
                    member.LastName = user.LastName;
                }
                else
                    member.Email = "USER NOT FOUND!";
            }

            userGroup.Members = new ObservableCollection<AssociationUserGroupMember>(members);
        }
Пример #4
0
 private void OpenAddEditUserGroupsWindow(AssociationUserGroup userGroup)
 {
     CloseEditPanel();
     _emailFieldCount = 4;
     EditingUserGroup = userGroup;
     EditingUserGroup.SupressValidationOnPropertyChanges = true;
     if (EditingUserGroup.Members != null)
     {
         foreach (var groupEmail in EditingUserGroup.Members)
         {
             groupEmail.SupressValidationOnPropertyChanges = true;
         }
     }
     AddEditUserGroupInProgress = true;
 }
Пример #5
0
 private void AddUserGroup()
 {
     var groupToAdd = new AssociationUserGroup
                          {
                              AssociationUserId = SelectedSupervisorId,
                              Members = new ObservableCollection<AssociationUserGroupMember>
                                           {
                                               new AssociationUserGroupMember
                                                   {
                                                       Email = SelectedSupervisorEmail,
                                                       IsManager = true
                                                   }
                                           }
                          };
     OpenAddEditUserGroupsWindow(groupToAdd);
 }
 public ValidateUserGroup(AssociationUserGroup userGroup)
 {
     _userGroup = userGroup;
 }