Ejemplo n.º 1
0
        /// <summary>
        /// Request reset password token
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public async Task <ApiOkResult> ResetPasswordRequest(string email)
        {
            var user = await _appUserManager.FindByEmailAsync(email);

            if (user == null)
            {
                throw new EntityNotFoundException(email, typeof(AppUser));
            }

            var token = UserInviteCodeGenerator.Generate();

            var entity = new UserResetPassword
            {
                Code   = token,
                UserId = user.Id
            };

            entity = await _userResetPasswordRepository.Insert(entity);

            var model = new ResetPasswordModel
            {
                Email     = user.Email,
                FirstName = user.FirstName,
                LastName  = user.LastName,
                Token     = token,
                Url       = _urlRedirectSettings.ResetPasswordWithToken(user.Email, token)
            };

            var mailBody = await _templateParser.Render(EmailTemplateCollection.USER_RESET_PASSWORD, model);

            var to = user.Email;

            await _emailSendTaskRepository.InsertEmail(to, "Reset password", mailBody, Infrastructure.Entities.Tasks.EmailTaskBot.MadRatBot);

            return(new ApiOkResult(true));
        }
Ejemplo n.º 2
0
        public async Task <ProviderWorkerDisplayModel> CreateBySlug(string slug, ProviderWorkerCreateModel model)
        {
            if (!await _providerRepository.IsWorkerInRoleBySlug(slug, _userId, ProviderWorkerRole.USER_MANAGER))
            {
                throw new AccessDeniedException(slug, typeof(ProviderWorker));
            }

            model.Email = model.Email.ToLower();

            ProviderWorker worker = null;

            var existsUser = await _appUserRepository.FindByEmailAsync(model.Email, new System.Threading.CancellationToken());

            if (existsUser == null)
            {
                existsUser = new AppUser
                {
                    CreateTime = DateTime.UtcNow,
                    State      = MREntityState.Active,
                    Status     = UserStatus.Invited,
                    Tels       = new List <MRUserTel>(),
                    UserName   = model.Email,
                    Email      = model.Email,
                    FirstName  = model.FirstName,
                    LastName   = model.LastName,
                    UpdateTime = DateTime.UtcNow,
                };

                existsUser = await _appUserRepository.Insert(existsUser);

                await _appUserManager.AddToRolesAsync(existsUser, new List <string>()
                {
                    AppUserRoleList.MANAGER,
                    AppUserRoleList.USER
                });

                var providerShort = await _providerRepository.GetShortBySlug(slug);

                var invite = await _userInviteRepository.Insert(new UserInvite
                {
                    Code         = UserInviteCodeGenerator.Generate(),
                    IsByIdentity = false,
                    ProviderId   = providerShort.Id,
                    ProviderName = providerShort.Name,
                    State        = MREntityState.Active,
                    UserId       = existsUser.Id
                });

                _logger.LogInformation("Created new user {0} from provider {1}", existsUser.Email, providerShort.Slug);

                // TODO add invite user email
            }
            else
            {
                if (await _providerRepository.IsWorkerExistsBySlug(slug, existsUser.Id))
                {
                    throw new EntityExistsException("Id", existsUser.Id, typeof(AppUser));
                }

                if (!await _appUserManager.IsInRoleAsync(existsUser, AppUserRoleList.MANAGER))
                {
                    await _appUserManager.AddToRoleAsync(existsUser, AppUserRoleList.MANAGER);
                }


                // TODO add welcome to provider user email
            }

            worker = new ProviderWorker
            {
                Roles     = model.Roles,
                UserEmail = model.Email,
                UserId    = existsUser.Id
            };

            await _providerRepository.InsertWorkersBySlug(slug, worker);

            _logger.LogInformation("Added new worker {0} to provider {1} by user {2}", worker.UserEmail, slug, _userEmail);

            return(_mapper.Map <ProviderWorkerDisplayModel>(worker).ApplyUser(existsUser));
        }