public async Task <DepartmentCreationResult> RegisterDepartment([FromBody] DepartmentCreationInput model)
        {
            DepartmentCreationResult creationResult = new DepartmentCreationResult();
            var user = new Microsoft.AspNet.Identity.EntityFramework6.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;

                var names = model.FullName.Split(char.Parse(" "));

                if (names.Length > 1)
                {
                    up.FirstName = names[0];
                    up.LastName  = names[1];
                }
                else
                {
                    up.FirstName = model.FullName;
                    up.LastName  = "";
                }
                _userProfileService.SaveProfile(0, up);

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

                var savedUser = await _userManager.FindByIdAsync(user.Id);

                Department department = _departmentsService.CreateDepartment(model.DepartmentName, user.Id, model.DepartmentType);

                //_departmentsService.AddUserToDepartment(model.DepartmentName, user.Id);
                _departmentsService.AddUserToDepartment(department.DepartmentId, user.Id);
                _subscriptionsService.CreateFreePlanPayment(department.DepartmentId, user.Id);
                _emailMarketingProvider.SubscribeUserToAdminList(up.FirstName, up.LastName, model.Email);
                _departmentsService.InvalidateDepartmentMembers();

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


                creationResult.Successful = true;
            }
            else
            {
                creationResult.Successful = false;
            }

            return(creationResult);
        }
Beispiel #2
0
        public async Task <IActionResult> CompleteInvite(CompleteInviteModel model)
        {
            model.Invite = _invitesService.GetInviteByCode(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 Microsoft.AspNet.Identity.EntityFramework6.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;
                    _userProfileService.SaveProfile(model.Invite.DepartmentId, up);

                    _usersService.AddUserToUserRole(user.Id);
                    _usersService.InitUserExtInfo(user.Id);
                    _departmentsService.AddUserToDepartment(model.Invite.DepartmentId, user.Id);

                    _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();

                    _invitesService.CompleteInvite(model.Invite.Code, user.UserId);
                    _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));
        }
Beispiel #3
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            if (Config.SystemBehaviorConfig.RedirectHomeToLogin)
            {
                return(RedirectToAction("LogOn", "Account"));
            }

            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new Microsoft.AspNet.Identity.EntityFramework6.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;
                    _userProfileService.SaveProfile(0, up);

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

                    var savedUser = await _userManager.FindByIdAsync(user.Id);

                    Department department = _departmentsService.CreateDepartment(model.DepartmentName, user.Id, model.DepartmentType);
                    _departmentsService.AddUserToDepartment(model.DepartmentName, user.Id);
                    _subscriptionsService.CreateFreePlanPayment(department.DepartmentId, user.Id);
                    _emailMarketingProvider.SubscribeUserToAdminList(model.FirstName, model.LastName, model.Email);

                    _departmentsService.InvalidateDepartmentMembers();

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

                    //await _signInManager.SignInAsync(savedUser, isPersistent: false);
                    //return RedirectToLocal(returnUrl);

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

                    if (result.Succeeded)
                    {
                        await HttpContext.Authentication.SignInAsync("ResgridCookieMiddlewareInstance", 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));
        }