Esempio n. 1
0
        public ActionResult Signup(RegisterModel model, string returnUrl, string token)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try {
                    User eu = _membershipProvider.GetUserByEmailAddress(model.EmailAddress);
                    if (eu != null)
                    {
                        ModelState.AddModelError("EmailAddress", "A user already exists with this email address.");
                        return(View(model));
                    }

                    var user = new User {
                        EmailAddress = model.EmailAddress, Password = model.Password, FullName = model.FullName
                    };
                    user.Roles.Add(AuthorizationRoles.User);

                    // Add the GlobalAdmin role to the first user of the system.
                    if (!_isFirstUserChecked)
                    {
                        _isFirstUserChecked = true;

                        if (_userRepository.Count() == 0)
                        {
                            user.Roles.Add(AuthorizationRoles.GlobalAdmin);
                        }
                    }

                    _membershipProvider.CreateAccount(user);

                    if (!String.IsNullOrEmpty(token))
                    {
                        AddInvitedUserToOrganization(token, user);
                    }
                    else
                    {
                        // TODO: We should verify that the passed in token was valid. If it wasn't, we should resend the verify email token.
                        user.VerifyEmailAddressToken = _membershipProvider.GenerateVerifyEmailToken(user.EmailAddress);
                        _mailer.SendVerifyEmailAsync(user);
                    }

                    if (Settings.Current.WebsiteMode == WebsiteMode.Dev && user.Roles.Contains(AuthorizationRoles.GlobalAdmin))
                    {
                        _dataHelper.CreateSampleOrganizationAndProject(user.Id);
                        return(String.IsNullOrEmpty(returnUrl) ? RedirectToAction("Index", "Project") : RedirectToLocal(returnUrl));
                    }

                    return(String.IsNullOrEmpty(returnUrl) ? RedirectToAction("Add", "Project") : RedirectToLocal(returnUrl));
                } catch (MembershipException e) {
                    _isFirstUserChecked = false;
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }