internal void RemoveUser(AssociationUserTravelAgencyRole role)
        {
            Kernel.Get<PopupApprovalViewModel>().Ask(
                 "",
                 "Please enter your password to confirm the removal of the Travel Agent from this Travel Agency.\n\nYou are confirming the removal of " + role.User.FirstName + " " + role.User.LastName + "from " + SelectedTravelAgency.Name + ".",
                 () => Service.BeginRemoveTravelAgencyRole(role.UserId, CreateAsyncCallback(
                         ar => Service.EndRemoveTravelAgencyRole(ar), () =>
                         {
                             SearchTravelAgencyRoles();
                         }), null));

        }
Esempio n. 2
0
        public AssociationUserTravelAgencyRole SaveTravelAgencySupervisor(int managerId, AssociationUserTravelAgencyRole supervisorRole)
        {
            using (var db = new LomsContext())
            {

                AssociationEmail emailProvider;

                if (supervisorRole.UserId == 0)
                {
                    var user = (from u in db.AssociationUsers
                                where u.AssociationId == CurrentAssociationId && u.Email == supervisorRole.UserEmail
                                select u)
                                .SingleOrDefault();

                    if (user == null || !user.HasOnlineAccess)
                    {
                        supervisorRole.Errors.AddError("UserEmail", "User is not registered!");
                        return supervisorRole;
                    }
                    else if (!user.IsTravelAgency)
                    {
                        supervisorRole.Errors.AddError("UserEmail", "User is not a travel agent!");
                        return supervisorRole;
                    }
                    else
                    {
                        var currenntRole = (from r in db.AssociationUserTravelAgencyRoles
                                            where r.UserId == user.Id
                                            select r).SingleOrDefault();

                        if (currenntRole != null)
                        {
                            if (currenntRole.AgencyId != supervisorRole.AgencyId)
                            {
                                supervisorRole.Errors.AddError("UserEmail", "User is a travel agent in another travel agency!");
                                return supervisorRole;
                            }
                            else if (currenntRole.AgencyId == supervisorRole.AgencyId &&
                                    (currenntRole.Role == TravelAgencyRole.Manager || currenntRole.Role == TravelAgencyRole.SupervisorAsManager || currenntRole.Role == TravelAgencyRole.Supervisor))
                            {
                                supervisorRole.Errors.AddError("UserEmail", "User is a existing travel team supervisor!");
                                return supervisorRole;
                            }
                            else
                            {
                                //upgrade
                                currenntRole.Role = supervisorRole.Role;
                                db.AssociationUserTravelAgencyRoles.ApplyChanges(supervisorRole);
                                db.SaveChanges();

                                //send upgrade email
                                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 contactUsLink = Path.Combine(baseUrl + "/#Contact");

                                    var txtContent = MailTemplateHelper.GetTravelAgentIsUpgradedToTravelSupervisorTxtContent(association.Name.ToUpper(), user.FullName.ToUpper(), contactUsLink, "TRAVEL AGENCY");
                                    var htmlContent = MailTemplateHelper.GetTravelAgentIsUpgradedToTravelSupervisorHtmlContent(association.Name.ToUpper(), user.FullName.ToUpper(), contactUsLink, "TRAVEL AGENCY");
                                    var avBody = AlternateView.CreateAlternateViewFromString(htmlContent, null, MediaTypeNames.Text.Html);

                                    emailProvider.SendMail(user.Email, association.Name.ToUpper() + " Supervisor Upgrade", txtContent, null, avBody, true);
                                }

                                return currenntRole;
                            }
                        }
                    }

                    supervisorRole.UserId = user.Id;
                    supervisorRole.Status = TravelAgencyStatus.Pending;
                    db.AssociationUserTravelAgencyRoles.ApplyChanges(supervisorRole);

                    //start activation process
                    var activation = new AssociationUserTravelAgencyRoleActivation();
                    activation.UserId = user.Id;
                    activation.AgencyId = supervisorRole.AgencyId;
                    activation.Guid = Guid.NewGuid();
                    activation.ExpiryTime = DateTime.UtcNow.AddDays(7.0);
                    db.AssociationUserTravelAgencyRoleActivations.ApplyChanges(activation);
                    db.SaveChanges();

                    //send activation email
                    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 manager = (from u in db.AssociationUsers
                                       where u.Id == managerId
                                       select u)
                                        .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("/#SupervisorActivation/{0}", activation.Guid.ToString("D")));
                        string contactUsLink = Path.Combine(baseUrl + "/#Contact");
                        string termAndConditionsLink = Path.Combine(baseUrl + "/terms");

                        var txtContent = MailTemplateHelper.GetTravelAgentIsAddedAsTravelSupervisorTxtContent(association.Name.ToUpper(), user.FullName.ToUpper(), activtionLink, contactUsLink, "TRAVEL AGENCY", "Group", manager.FullName, manager.Email, termAndConditionsLink);
                        var htmlContent = MailTemplateHelper.GetTravelAgentIsAddedAsTravelSupervisorHtmlContent(association.Name.ToUpper(), user.FullName.ToUpper(), activtionLink, contactUsLink, "TRAVEL AGENCY", "Group", manager.FullName, manager.Email, termAndConditionsLink);
                        var avBody = AlternateView.CreateAlternateViewFromString(htmlContent, null, MediaTypeNames.Text.Html);

                        emailProvider.SendMail(user.Email, association.Name.ToUpper() + " Supervisor Add", txtContent, null, avBody, true);
                    }


                    supervisorRole.UserFirstName = user.FirstName;
                    supervisorRole.UserLastName = user.LastName;
                    return supervisorRole;
                }
                else
                {
                    //only role can be changed
                    db.AssociationUserTravelAgencyRoles.ApplyChanges(supervisorRole);
                    db.SaveChanges();
                    return supervisorRole;
                }
            }
        }
     public bool Equals(AssociationUserTravelAgencyRole other)
     {
         if (ReferenceEquals(null, other)) return false;
         if (ReferenceEquals(this, other)) return true;
 		if (other.UserId == 0 && UserId == 0)
 			return false;
 		else
 			return other.UserId == UserId;
     }
Esempio n. 4
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;
            }
        }
        public void RemoveTravelAgencyRole(int userId)
        {
            if (!Roles.IsUserInRole(RoleName.StaffUser))
                throw new SecurityException();

            using (var db = new LomsContext())
            {
                var agency = new AssociationUserTravelAgencyRole() { UserId = userId };
                db.AssociationUserTravelAgencyRoles.Attach(agency);
                db.AssociationUserTravelAgencyRoles.DeleteObject(agency);
                db.SaveChanges();
            }
        }
        public AssociationTravelAgency SaveTravelAgency(AssociationTravelAgency agency)
        {
            if (!Roles.IsUserInRole(RoleName.StaffUser))
                throw new SecurityException();

            using (var db = new LomsContext())
            {
                db.Connection.Open();

                using (var transaction = db.Connection.BeginTransaction())
                {
                    db.AssociationTravelAgencies.ApplyChanges(agency);
                    db.SaveChanges();

                    var managerRole = (from r in db.AssociationUserTravelAgencyRoles
                                       where r.AgencyId == agency.Id && r.RoleId == (int)TravelAgencyRole.Manager
                                       select r).SingleOrDefault();

                    if (agency.ManagerId == 0)
                    {
                        if (managerRole != null)
                        {
                            //make current manager supervisor
                            managerRole.Role = TravelAgencyRole.Supervisor;
                            db.AssociationUserTravelAgencyRoles.ApplyChanges(managerRole);
                            db.SaveChanges();
                        }
                    }
                    else
                    {
                        bool createNewRole = false;
                        if (managerRole != null && managerRole.UserId != agency.ManagerId)
                        {
                            //make current manager supervisor
                            managerRole.Role = TravelAgencyRole.Supervisor;
                            db.AssociationUserTravelAgencyRoles.ApplyChanges(managerRole);

                            var userRole = db.AssociationUserTravelAgencyRoles.SingleOrDefault(r => r.UserId == agency.ManagerId);
                            if (userRole != null)
                            {
                                db.AssociationUserTravelAgencyRoles.DeleteObject(userRole);
                                db.SaveChanges();
                            }

                            createNewRole = true;
                        }
                        else if (managerRole == null)
                            createNewRole = true;

                        if (createNewRole)
                        {
                            managerRole = new AssociationUserTravelAgencyRole()
                            {
                                UserId = agency.ManagerId.Value,
                                AgencyId = agency.Id,
                                Role = TravelAgencyRole.Manager,
                                Status = TravelAgencyStatus.Accepted
                            };

                            db.AssociationUserTravelAgencyRoles.ApplyChanges(managerRole);
                            db.SaveChanges();

                            var emailProvider = db.AssociationEmails.FirstOrDefault(e => e.AssociationId == CurrentAssociationId);
                            if (emailProvider != null)
                            {
                                var association = db.Associations.FirstOrDefault(a => a.Id == CurrentAssociationId);
                                var user = db.AssociationUsers.SingleOrDefault(u => u.Id == managerRole.UserId);

                                var uri = HttpContext.Current.Request.Url;

                                string baseUrl = String.Format("{0}://{1}:{2}", uri.Scheme, uri.Host ?? "80", uri.Port);
                                string contactUsLink = Path.Combine(baseUrl + "/#Contact");

                                var emailTemplate = new EmailTemplate("ExistingTravelAgentIsAddedAsTravelManager");
                                emailTemplate["UserName"] = user.FullName.ToUpper();
                                emailTemplate["TravelAgencyName"] = agency.Name.ToUpper();
                                emailTemplate["AssociationName"] = association.Name.ToUpper();
                                emailTemplate["ContactUsLink"] = contactUsLink;

                                var avBody = AlternateView.CreateAlternateViewFromString(emailTemplate.Html, null, MediaTypeNames.Text.Html);
                                emailProvider.SendMail(user.Email, association.Name.ToUpper() + " Travel Agency Manager", emailTemplate.Txt, null, avBody, true);
                            }
                        }
                    }
                    transaction.Commit();
                }
            }

            using (var db = new LomsContext())
            {
                agency = db.AssociationTravelAgencies.IncludeAll("Country", "State", "State.Country", "Suburb", "Suburb.Country", "Suburb.State", "Suburb.State.Country", "Manager")
                          .SingleOrDefault(a => a.Id == agency.Id);
            }

            return agency;
        }
        public AssociationTravelAgency SaveTravelAgency(AssociationTravelAgency agency)
        {
            if (!Roles.IsUserInRole(RoleName.StaffUser))
                throw new SecurityException();

            using (TransactionScope scope = new TransactionScope())
            using (var db = new LomsContext())
            {
                if (agency.Suburb != null)
                {
                    agency.Country = null;
                    agency.State = null;
                }
                else if (agency.State != null)
                    agency.Country = null;

                agency.AssociationId = CurrentAssociationId;

                db.AssociationTravelAgencies.ApplyChanges(agency);
                db.SaveChanges();

                var managerRole = (from r in db.AssociationUserTravelAgencyRoles
                                   where r.AgencyId == agency.Id && r.RoleId == (int)TravelAgencyRole.Manager
                                   select r).
                 SingleOrDefault();

                if (agency.ManagerId == 0)
                {
                    if (managerRole != null)
                    {
                        //make current manager supervisor
                        managerRole.Role = TravelAgencyRole.Supervisor;
                        db.AssociationUserTravelAgencyRoles.ApplyChanges(managerRole);
                        db.SaveChanges();
                    }
                }
                else
                {
                    bool createNewRole = false;
                    if (managerRole != null && managerRole.UserId != agency.ManagerId)
                    {
                        //make current manager supervisor
                        managerRole.Role = TravelAgencyRole.Supervisor;
                        db.AssociationUserTravelAgencyRoles.ApplyChanges(managerRole);

                        var userRole = (from r in db.AssociationUserTravelAgencyRoles
                                        where r.UserId == agency.ManagerId
                                        select r).
                                         SingleOrDefault();

                        if (userRole != null)
                            db.AssociationUserTravelAgencyRoles.DeleteObject(userRole);

                        createNewRole = true;
                    }
                    else if (managerRole == null)
                        createNewRole = true;

                    if (createNewRole)
                    {
                        managerRole = new AssociationUserTravelAgencyRole()
                        {
                            UserId = agency.ManagerId.Value,
                            AgencyId = agency.Id,
                            Role = TravelAgencyRole.Manager,
                            Status = TravelAgencyStatus.Accepted
                        };

                        db.AssociationUserTravelAgencyRoles.ApplyChanges(managerRole);
                        db.SaveChanges();

                        //send letter
                        var emailProvider = (from e in db.AssociationEmails
                                             where e.AssociationId == agency.AssociationId
                                             select e)
                                    .FirstOrDefault();

                        if (emailProvider != null)
                        {
                            var association = (from a in db.Associations
                                               where a.Id == agency.AssociationId
                                               select a)
                                             .SingleOrDefault();

                            var user = (from a in db.AssociationUsers
                                        where a.Id == managerRole.UserId
                                        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 contactUsLink = Path.Combine(baseUrl + "/#Contact");

                            var txtContent = MailTemplateHelper.GetExistingTravelAgentIsAddedAsTravelManagerTxtContent(association.Name.ToUpper(), user.FullName.ToUpper(), contactUsLink, agency.Name);
                            var htmlContent = MailTemplateHelper.GetExistingTravelAgentIsAddedAsTravelManagerHtmlContent(association.Name.ToUpper(), user.FullName.ToUpper(), contactUsLink, agency.Name);
                            var avBody = AlternateView.CreateAlternateViewFromString(htmlContent, null, MediaTypeNames.Text.Html);

                            emailProvider.SendMail(user.Email, association.Name.ToUpper() + " Travel Agency Manager", txtContent, null, avBody, true);
                        }
                    }
                }

                agency = db.AssociationTravelAgencies.IncludeAll("Country", "State", "State.Country", "Suburb", "Suburb.Country", "Suburb.State", "Suburb.State.Country")
                    .FirstOrDefault(a => a.Id == agency.Id);

                scope.Complete();

                return agency;
            }
        }
Esempio n. 8
0
 private void OpenAddEditSupervisorWindow(AssociationUserTravelAgencyRole supervisor)
 {
     CloseEditPanel();
     EditingSupervisor = supervisor;
     AddEditSupervisorInProgress = true;
 }
Esempio n. 9
0
 private void AddSupervisor()
 {
     var newSupervisor = new AssociationUserTravelAgencyRole { Role = TravelAgencyRole.Supervisor, AgencyId = Manager.TravelAgencyRole.AgencyId };
     OpenAddEditSupervisorWindow(newSupervisor);
 }
Esempio n. 10
0
     private void FixupTravelAgencyRole(AssociationUserTravelAgencyRole 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 (TravelAgencyRole != null)
         {
             ChangeTracker.ObjectStateChanging += TravelAgencyRole.HandleCascadeDelete;
         }
 
         if (IsDeserializing)
         {
             return;
         }
 
         if (previousValue != null && ReferenceEquals(previousValue.User, this))
         {
             previousValue.User = null;
         }
 
         if (TravelAgencyRole != null)
         {
             TravelAgencyRole.User = this;
         }
 
         if (ChangeTracker.ChangeTrackingEnabled)
         {
             if (ChangeTracker.OriginalValues.ContainsKey("TravelAgencyRole")
                 && (ChangeTracker.OriginalValues["TravelAgencyRole"] == TravelAgencyRole))
             {
                 ChangeTracker.OriginalValues.Remove("TravelAgencyRole");
             }
             else
             {
                 ChangeTracker.RecordOriginalValue("TravelAgencyRole", 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 (TravelAgencyRole != null && !TravelAgencyRole.ChangeTracker.ChangeTrackingEnabled)
             {
                 TravelAgencyRole.StartTracking();
             }
         }
     }
 public ValidateUserTravelAgencyRole(AssociationUserTravelAgencyRole associationUserTravelAgencyRole)
 {
     _associationUserTravelAgencyRole = associationUserTravelAgencyRole;
 }