예제 #1
0
        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;
        }
예제 #2
0
        public AssociationUser SaveAssociationUser(AssociationUser user)
        {
            try
            {
                string email = user.Email != null ? user.Email.ToLower().Trim() : null;

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

                    using (var transaction = db.Connection.BeginTransaction())
                    {
                        if (!string.IsNullOrWhiteSpace(email))
                        {
                            //check id user with such email existed already
                            var existedUser = (from u in db.AssociationUsers
                                               where u.AssociationId == CurrentAssociationId && u.Email == email && (user.Id == 0 || u.Id != user.Id)
                                               select u).SingleOrDefault();

                            if (existedUser != null)
                            {
                                user.AddError("Email", "User with such email is already registered!");
                                return user;
                            }

                            user.Email = email;
                        }
                        else
                            user.Email = null;


                        user.AssociationId = CurrentAssociationId;
                        user.CreatedTime = user.LastUpdatedTime = DateTime.UtcNow;

                        db.AssociationUsers.ApplyChanges(user);
                        db.SaveChanges();

                        if (!string.IsNullOrWhiteSpace(user.Email))
                        {
                            AssociationUserActivation activation = new AssociationUserActivation();
                            activation.UserId = user.Id;
                            activation.Guid = Guid.NewGuid();
                            activation.ExpiryTime = DateTime.UtcNow.AddDays(7.0);  //expiry
                            db.AssociationUserActivations.ApplyChanges(activation);
                            db.SaveChanges();

                            var emailProvider = db.AssociationEmails.FirstOrDefault(e => e.AssociationId == CurrentAssociationId);
                            if (emailProvider != null)
                            {
                                var association = db.Associations.FirstOrDefault(a => a.Id == CurrentAssociationId);

                                var uri = HttpContext.Current.Request.Url;
                                string baseUrl = String.Format("{0}://{1}:{2}", uri.Scheme, uri.Host ?? "80", uri.Port);
                                string activtionLink = Path.Combine(baseUrl + string.Format("/#Activation/{0}", activation.Guid.ToString("D")));
                                string contactUsLink = Path.Combine(baseUrl + "/#Contact");
                                string termAndConditionsLink = Path.Combine(baseUrl + "/terms");

                                var emailTemplate = new EmailTemplate("StaffAdminAddsNewManagedProfileWithEmail");

                                emailTemplate["UserName"] = user.FullName.ToUpper();
                                emailTemplate["AssociationName"] = association.Name.ToUpper();

                                emailTemplate["ActivationLink"] = activtionLink;
                                emailTemplate["ContactUsLink"] = contactUsLink;
                                emailTemplate["TermAndConditionLink"] = termAndConditionsLink;

                                var avBody = AlternateView.CreateAlternateViewFromString(emailTemplate.Html, null, MediaTypeNames.Text.Html);
                                emailProvider.SendMail(user.Email, association.Name + " Account activation", emailTemplate.Txt, null, avBody, true);
                            }
                        }

                        transaction.Commit();
                    }
                }
                using (var db = new LomsContext())
                {
                    var entity = db.AssociationUsers.IncludeAll("Country").Single(u => u.Id == user.Id);
                    return entity;
                }
            }
            catch (Exception ex)
            {
                StringBuilder builder = new StringBuilder();
                builder.AppendLine(ex.Message);
                if (ex.InnerException != null)
                {
                    builder.AppendLine(ex.InnerException.Message);
                    if (ex.InnerException.InnerException != null)
                        builder.AppendLine(ex.InnerException.InnerException.Message);
                }
                user.AddError("Error", builder.ToString());
                return user;
            }

        }