Esempio n. 1
0
        public void CustomerRegister(TenantRegisterModel customerRegistration)
        {
            string errMessage;
            if (!ValidModel(customerRegistration, out errMessage))
            {
                throw new BusinessException(errMessage);
            }

            Logger.Info("Creating Membership");
            MembershipCreateStatus createStatus;
            var membershipUser = Membership.CreateUser(customerRegistration.UserName,
                customerRegistration.Password,
                customerRegistration.UserName, "¿?", "!!", true, Guid.NewGuid(), out createStatus);
            Logger.Info("Membership created");
            var created = false;

            if (membershipUser != null && membershipUser.ProviderUserKey != null)
            {
                try
                {
                    using (var scope = new TransactionScope())
                    {
                        using (var db = new AdministrationDb())
                        {

                            var userId = (Guid)membershipUser.ProviderUserKey;
                            var appUser = new ApplicationUser
                            {
                                UserId = userId,
                                FirstName = customerRegistration.FirstName,
                                LastName = customerRegistration.LastName,
                                EmailAddress = customerRegistration.UserName,
                                IsEmailVerified = true,
                                EmailVerificationToken = string.Empty,
                                IsActive = true,
                                CreatedOn = DateTime.Now,
                                CreatedBy = userId,
                                ModifiedOn = DateTime.Now,
                                ModifiedBy = userId
                            };

                            var customer = new Customer
                            {
                                CompanyName = customerRegistration.DomainName,
                                Domain = customerRegistration.Domain,
                                DomainName = customerRegistration.DomainName,
                                PhoneNumber = customerRegistration.Phone,
                                IsActive = true,
                                CreatedOn = DateTime.Now,
                                CreatedBy = userId,
                                ModifiedOn = DateTime.Now,
                                ModifiedBy = userId
                            };

                            var customerUser = new CustomerUser
                            {
                                ApplicationUser = appUser,
                                Customer = customer,
                                IsCreator = true,
                                IsAdmin = true,
                                IsRelationActive = true
                            };

                            db.ApplicationUsers.Add(appUser);
                            Logger.InfoFormat("User {0} Created", appUser.EmailAddress);

                            db.Customers.Add(customer);
                            Logger.InfoFormat("Customer {0} Created", customer.CompanyName);

                            db.SaveChanges();
                            Logger.Info("Register Checkpoint");
                            db.CustomerUsers.Add(customerUser);
                            Logger.Info("Relationship created");

                            db.SaveChanges();
                            scope.Complete();
                            Logger.Info("Transacion Completed");
                            created = true;
                        }
                    }

                    MailService.SendMail(customerRegistration.UserName, MailMessageType.Welcome);
                }
                catch (Exception ex)
                {
                    if (!created)
                        Membership.DeleteUser(customerRegistration.UserName, true);
                    Logger.Error(ex);
                }
            }
            else
            {
                Logger.Error("Membership user couldn't be created");
                throw new BusinessException("The user could not be created at this time.  Try again soon.");
            }
        }
Esempio n. 2
0
        public ActionResult Register(TenantRegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    SecurityService.CustomerRegister(model);
                    return Login(new LoginModel
                    {
                        Password = model.Password,
                        UserName = model.UserName,
                        RememberMe = true
                    }, "/Scheduler");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                    Logger.Error(e);
                }
                catch (BusinessException be)
                {
                    ModelState.AddModelError("be", be.Message);
                    Logger.Info(be);
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("ge", "An unexpected error has occurred");
                    Logger.Error(ex);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Esempio n. 3
0
        private bool ValidModel(TenantRegisterModel customerRegistration, out string errMessage)
        {
            errMessage = string.Empty;
            using (var db = new AdministrationDb())
            {
                if (customerRegistration.Domain.EndsWith(".onmicrosoft.com"))
                {
                    customerRegistration.DomainName = customerRegistration.Domain.Split('.')[0];
                }
                else
                {
                    errMessage = "Domain must be an Office 365 valid domain.";
                    return false;
                }

                var cust = db.Customers.FirstOrDefault(x => x.DomainName == customerRegistration.DomainName);
                if (cust != null)
                {
                    errMessage = "Customer Domain Name already in use";
                    return false;
                }

                var user = db.ApplicationUsers.FirstOrDefault(x => x.EmailAddress == customerRegistration.UserName);
                if (user != null)
                {
                    errMessage = "Username already in use";
                    return false;
                }
            }
            return true;
        }