Example #1
0
        public async Task <IActionResult> CompleteInvite(CompleteInviteModel model, CancellationToken cancellationToken)
        {
            model.Invite = await _invitesService.GetInviteByCodeAsync(Guid.Parse(model.Code));

            model.Email = model.Invite.EmailAddress;

            if (!StringHelpers.ValidateEmail(model.Email))
            {
                ModelState.AddModelError("EmailAddresses", string.Format("{0} does not appear to be valid. Check the address and try again.", model.Email));
            }

            var existingUser = _usersService.GetUserByEmail(model.Email);

            if (existingUser != null)
            {
                ModelState.AddModelError("EmailAddresses", string.Format("The email address {0} is already in use in this department on another. Email address can only be used once per account in the system. Use the account recovery form to recover your username and password.", model.Email));
            }

            if (ModelState.IsValid)
            {
                var user = new IdentityUser {
                    UserName = model.UserName, Email = model.Email, SecurityStamp = Guid.NewGuid().ToString()
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    UserProfile up = new UserProfile();
                    up.UserId    = user.Id;
                    up.FirstName = model.FirstName;
                    up.LastName  = model.LastName;
                    await _userProfileService.SaveProfileAsync(model.Invite.DepartmentId, up, cancellationToken);

                    _usersService.AddUserToUserRole(user.Id);
                    _usersService.InitUserExtInfo(user.Id);
                    await _departmentsService.AddUserToDepartmentAsync(model.Invite.DepartmentId, user.Id, false, cancellationToken);

                    _eventAggregator.SendMessage <UserCreatedEvent>(new UserCreatedEvent()
                    {
                        DepartmentId = model.Invite.Department.DepartmentId,
                        Name         = $"{model.FirstName} {model.LastName}",
                        User         = user
                    });

                    _departmentsService.InvalidateDepartmentUsersInCache(model.Invite.DepartmentId);
                    _departmentsService.InvalidatePersonnelNamesInCache(model.Invite.DepartmentId);
                    _usersService.ClearCacheForDepartment(model.Invite.DepartmentId);
                    _departmentsService.InvalidateDepartmentMembers();

                    await _invitesService.CompleteInviteAsync(model.Invite.Code, user.UserId, cancellationToken);

                    _emailMarketingProvider.SubscribeUserToUsersList(model.FirstName, model.LastName, user.Email);

                    _emailService.SendWelcomeEmail(model.Invite.Department.Name, $"{model.FirstName} {model.LastName}", model.Email, model.UserName, model.Password, model.Invite.DepartmentId);

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(RedirectToAction("Dashboard", "Home", new { area = "User" }));
                }
                AddErrors(result);
            }

            return(View(model));
        }
Example #2
0
        public async Task <IActionResult> Register(RegisterViewModel model, CancellationToken cancellationToken, string returnUrl = null)
        {
            if (Config.SystemBehaviorConfig.RedirectHomeToLogin)
            {
                return(RedirectToAction("LogOn", "Account"));
            }


            ViewBag.DepartmentTypes = new SelectList(model.DepartmentTypes);
            model.SiteKey           = WebConfig.RecaptchaPublicKey;
            ViewData["ReturnUrl"]   = returnUrl;

            if (ModelState.IsValid)
            {
                var user = new IdentityUser {
                    UserName = model.Username, Email = model.Email, SecurityStamp = Guid.NewGuid().ToString()
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    UserProfile up = new UserProfile();
                    up.UserId    = user.Id;
                    up.FirstName = model.FirstName;
                    up.LastName  = model.LastName;
                    await _userProfileService.SaveProfileAsync(0, up, cancellationToken);

                    _usersService.AddUserToUserRole(user.Id);
                    _usersService.InitUserExtInfo(user.Id);

                    Department department = await _departmentsService.CreateDepartmentAsync(model.DepartmentName, user.Id, model.DepartmentType, null, cancellationToken);

                    await _departmentsService.AddUserToDepartmentAsync(department.DepartmentId, user.Id, true, cancellationToken);

                    await _subscriptionsService.CreateFreePlanPaymentAsync(department.DepartmentId, user.Id, cancellationToken);

                    // Guard, in case testing has caching turned on for the shared redis cache there can be artifacts
                    _departmentsService.InvalidateAllDepartmentsCache(department.DepartmentId);
                    _departmentsService.InvalidateDepartmentMembers();

                    _emailMarketingProvider.SubscribeUserToAdminList(model.FirstName, model.LastName, model.Email);
                    _emailService.SendWelcomeEmail(department.Name, $"{model.FirstName} {model.LastName}", model.Email, model.Username, model.Password, department.DepartmentId);

                    var loginResult = await _signInManager.PasswordSignInAsync(model.Username, model.Password, true, lockoutOnFailure : false);

                    if (loginResult.Succeeded)
                    {
                        await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, HttpContext.User, new AuthenticationProperties
                        {
                            ExpiresUtc   = DateTime.UtcNow.AddHours(24),
                            IsPersistent = false,
                            AllowRefresh = false
                        });

                        if (!String.IsNullOrWhiteSpace(returnUrl))
                        {
                            return(RedirectToLocal(returnUrl));
                        }
                        else
                        {
                            return(RedirectToAction("Dashboard", "Home", new { Area = "User" }));
                        }
                    }
                    else
                    {
                        return(View(model));
                    }
                }
                AddErrors(result);
            }

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