예제 #1
0
 public void AcceptTeamMemberInvite(CompanyTeamMemberInvite invite)
 {
     if (invite.ResultantUserId != null)
     {
         _companyRepository.AcceptTeamMemberInvite(invite.ResultantUserId.Value, invite.UniqueId.ToString());
     }
 }
예제 #2
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var lastMonth    = DateTime.Now.AddMonths(-1);
                var existingUser = await UserManager.FindByEmailAsync(model.Email);

                CompanyTeamMemberInvite invite = null;
                bool hasInvite = false;

                if (existingUser != null)
                {
                    this.SetNotificationMessage(NotificationType.Error, string.Format("Email {0} is already taken.", model.Email));
                    return(View(model));
                }

                if (!model.TermsAndConditions)
                {
                    this.SetNotificationMessage(NotificationType.Error, "You must accept the Terms and Conditions and Privacy Policy.");
                    return(View(model));
                }

                // Check for invite
                if (model.Invite != null)
                {
                    invite = _companyManager.GetTeamMemberInvite(model.Invite.UniqueId);

                    if (invite == null)
                    {
                        return(View("InviteUnavailable"));
                    }

                    if (invite.ResultantUserId.HasValue)
                    {
                        this.SetNotificationMessage(NotificationType.Error, "There is already an account creating using this invitation. Please login to start using the application.");
                        return(View(model));
                    }

                    hasInvite = true;
                }

                IdentityResult result;
                AppUser        registeredUser;

                using (var scope = new TransactionScope())
                {
                    // Create new user.
                    registeredUser = new AppUser
                    {
                        UserName  = model.Email,
                        Email     = model.Email,
                        FirstName = model.FirstName,
                        LastName  = model.LastName
                    };
                    result = await UserManager.CreateAsync(registeredUser, model.Password);

                    if (result.Succeeded)
                    {
                        // Mark the latest terms and conditions as accepted
                        _termsAndConditionsManager.AcceptTermsAndConditions(registeredUser.Id);

                        // Check if the user has been invited.
                        if (hasInvite)
                        {
                            invite.ResultantUserId = registeredUser.Id;
                            _companyManager.AcceptTeamMemberInvite(invite);
                        }
                        else
                        {
                            // Create a new company.
                            var newCompany = _companyManager.Create(new Company
                            {
                                Name          = model.CompanyName,
                                StartMonth    = lastMonth,
                                ReportTitle   = DefaultReportTitle,
                                TwitterHandle = model.TwitterHandle
                            });

                            // Associate the user to the company.
                            _companyManager.AddUser(newCompany.Id, registeredUser.Id, true);

                            // Create a trial subscription
                            _subscriptionManager.CreateTrialSubscription(newCompany.Id);
                        }
                    }

                    // Complete the scope.
                    scope.Complete();
                }

                // Send the email confirmation email.
                if (result.Succeeded)
                {
                    // store UTM info
                    var utmInfo = GetUtmInfo();
                    if (utmInfo != null)
                    {
                        utmInfo.UserId = registeredUser.Id;
                        _adminManager.StoreUtmInfo(utmInfo);
                    }

                    var callbackUrl = await SendEmailConfirmationTokenAsync(registeredUser.Id, registeredUser.UniqueId, "Confirm your account");

                    ViewBag.Link = callbackUrl;

                    return(Redirect("http://supdate.com/confirm-email"));
                }

                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form

            return(View(model));
        }