Ejemplo n.º 1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            using (var db = ApplicationDbContext.Create())
            {
                var org = OrganizationHelper.GetOrganizationByHost(Request, db);
                if (org == null ||
                    !org.IsAnonymousRegistrationAllowed)
                {
                    throw new HttpException(403, "Forbidden");
                }

                if (!model.IsTermsOfServiceAgreed)
                {
                    model.SkippedTermsOfService = true;
                    InitializeRegisterModel(model, org);
                    return(View(model));
                }

                if (ModelState.IsValid)
                {
                    var user = new ApplicationUser();
                    user.UserName  = model.Email;
                    user.Email     = model.Email;
                    user.FirstName = model.FirstName;
                    user.LastName  = model.LastName;

                    IdentityResult result = await UserManager.CreateAsync(user, model.Password);

                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent : false);

                        db.Users.Attach(user);
                        org.ApplicationUsers.Add(user);

                        db.SaveChanges();


                        // Notify permission assigners of the new user.
                        var usersToNotify = OrganizationHelper.GetUsersWithRight(db, org.Id, Right.CanAssignRights)
                                            .ToList();
                        foreach (var notifyUser in usersToNotify)
                        {
                            try
                            {
                                NotificationService.SendActionEmail(
                                    notifyUser.Email, notifyUser.FullName,
                                    org.ReplyToAddress, org.Name,
                                    "[New User] " + model.Email,
                                    string.Format("{0} {1} has created a new account with user name {2}.", model.FirstName, model.LastName, model.Email),
                                    "Please assign any appropriate rights to the new user.",
                                    "Assign Rights ",
                                    Request.Url.Scheme + "://" + Request.Url.Host + "/User/Permissions/" + model.Email,
                                    org.NotificationEmailClosing,
                                    Request.Url.Scheme + "://" + Request.Url.Host + "/User/EmailPreferences/" + notifyUser.UserName,
                                    db);
                            }
                            catch (Exception ex)
                            {
                                Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                            }
                        }

                        // Send an email with this link
                        string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                        var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);

                        NotificationService.SendActionEmail(
                            user.Email, string.Format("{0} {1}", model.FirstName, model.LastName),
                            org.ReplyToAddress, org.Name,
                            "Confirm your account",
                            "Welcome to " + org.Name + ".",
                            "We are ready to activate your account. All we need to do is make sure this is your email address.",
                            "Verify Address", callbackUrl,
                            "If you didn't create an account, just delete this email.",
                            Request.Url.Scheme + "://" + Request.Url.Host + "/User/EmailPreferences/" + user.UserName,
                            db);

                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }

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