Exemple #1
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    if (_volunteerSvc.EmailAlreadyInUse(model.Email))
                    {
                        throw new PersonAlreadyExistsException();
                    }

                    string errorMessage;
                    if (PasswordComplexity.IsValid(model.Password, model.UserName, out errorMessage))
                    {
                        int    userId;
                        string token     = _webSecurity.CreateUser(model.UserName, model.Password, new[] { Constants.RoleVolunteer }, out userId);
                        var    volunteer = _volunteerSvc.Register(model.FirstName, model.LastName, model.SelectedOrganizationId, model.Email, model.PhoneNumber, userId);
                        if (volunteer != null)
                        {
                            // Generate the absolute Url for the account activation action.
                            var routeValues = new RouteValueDictionary {
                                { "token", token }
                            };
                            var accountActivationLink = Url.Action("ConfirmAccount", "Account", routeValues, Request.Url.Scheme);

                            var body    = String.Format(@"<p>Click on the following link to activate your account: <a href='{0}'>{0}</a></p>", accountActivationLink);
                            var message = new Message("CrisisCheckin - Activate your account", body);

                            _messageService.SendMessage(message, volunteer);
                        }

                        return(RedirectToAction("RegistrationSuccessful", "Account"));
                    }
                    ModelState.AddModelError("Password", errorMessage ?? DefaultErrorMessages.InvalidPasswordFormat);
                }
                catch (PersonAlreadyExistsException)
                {
                    ModelState.AddModelError("Email", "Email is already in use!");
                }
                catch (UserCreationException e)
                {
                    ModelState.AddModelError("", e.Message);
                }
            }

            // If we got this far, something failed, redisplay form
            model.Organizations = _organizationService.GetActiveList();
            return(View(model));
        }
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    if (_volunteerSvc.EmailAlreadyInUse(model.Email))
                    {
                        throw new PersonAlreadyExistsException();
                    }

                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                    WebSecurity.Login(model.UserName, model.Password);

                    Roles.AddUserToRole(model.UserName, "Volunteer");

                    var userId = WebSecurity.GetUserId(model.UserName);

                    _volunteerSvc.Register(model.FirstName, model.LastName, model.Email, model.PhoneNumber, model.Cluster, userId);

                    return(RedirectToAction("Index", "Home"));
                }
                catch (PersonAlreadyExistsException)
                {
                    ModelState.AddModelError("", "Email is already in use!");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            model.Clusters = _clusterSvc.GetList();
            return(View(model));
        }