コード例 #1
0
        public async Task <CreateUserCommandResponse> Post(CreateUserServiceModel createUserServiceModel)
        {
            //Use AutoMapper instance to transform ServiceModel into MediatR Request (Configured in Startup)
            var createUserCommand = _mapper.Map <CreateUserCommand>(createUserServiceModel);

            var result = await _mediator.Send(createUserCommand);

            return(result);
        }
コード例 #2
0
        public async Task <IActionResult> Save(UserRequestModel request)
        {
            if (request.Id == null)
            {
                var serviceModel = new CreateUserServiceModel
                {
                    Name  = request.Name,
                    Email = request.Email,
                    CommunicationsLanguage = request.CommunicationLanguage,
                    ReturnUrl = request.ReturnUrl,
                    Scheme    = this.HttpContext.Request.Scheme,
                    Host      = this.HttpContext.Request.Host
                };

                var validator        = new CreateUserModelValidator();
                var validationResult = await validator.ValidateAsync(serviceModel);

                if (validationResult != null)
                {
                    var response = await this.userService.CreateAsync(serviceModel);

                    return(this.StatusCode((int)HttpStatusCode.Created, new { response.Id }));
                }
                throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity);
            }
            else
            {
                var serviceModel = new UpdateUserServiceModel
                {
                    Id                = request.Id,
                    PhoneNumber       = request.PhoneNumber,
                    FirstName         = request.FirstName,
                    LastName          = request.LastName,
                    TwoFactorEnabled  = request.TwoFactorEnabled,
                    AccessFailedCount = request.AccessFailedCount,
                    LockoutEnd        = request.LockoutEnd,
                    ReturnUrl         = request.ReturnUrl,
                    Scheme            = this.HttpContext.Request.Scheme,
                    Host              = this.HttpContext.Request.Host
                };

                var validator        = new UpdateUserModelValidator();
                var validationResult = await validator.ValidateAsync(serviceModel);

                if (validationResult != null)
                {
                    var response = await this.userService.UpdateAsync(serviceModel);

                    return(this.StatusCode((int)HttpStatusCode.OK, new { response.Id }));
                }

                throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity);
            }
        }
コード例 #3
0
        public async Task <UserServiceModel> CreateAsync(CreateUserServiceModel serviceModel)
        {
            var timeExpiration = DateTime.UtcNow.AddHours(IdentityConstants.VerifyTimeExpiration);

            var existingOrganisation = await this.identityContext.Organisations.FirstOrDefaultAsync(x => x.ContactEmail == serviceModel.Email && x.IsActive);

            if (existingOrganisation == null)
            {
                throw new CustomException(this.accountLocalizer.GetString("OrganisationNotFound"), (int)HttpStatusCode.NotFound);
            }

            Thread.CurrentThread.CurrentCulture   = new CultureInfo(existingOrganisation.Language);
            Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

            var user = await this.identityContext.Accounts.FirstOrDefaultAsync(x => x.Email == serviceModel.Email);

            if (user != null)
            {
                user.EmailConfirmed       = false;
                user.VerifyExpirationDate = timeExpiration;
                user.ExpirationId         = Guid.NewGuid();

                await this.identityContext.SaveChangesAsync();

                await this.mailingService.SendTemplateAsync(new TemplateEmail
                {
                    RecipientEmailAddress = user.Email,
                    RecipientName         = user.UserName,
                    SenderEmailAddress    = this.mailingOptions.CurrentValue.SenderEmail,
                    SenderName            = this.mailingOptions.CurrentValue.SenderName,
                    TemplateId            = this.mailingOptions.CurrentValue.ActionSendGridResetTemplateId,
                    DynamicTemplateData   = new
                    {
                        lang             = existingOrganisation.Language,
                        ap_subject       = this.accountLocalizer.GetString("ap_subject").Value,
                        ap_preHeader     = this.accountLocalizer.GetString("ap_preHeader").Value,
                        ap_buttonLabel   = this.accountLocalizer.GetString("ap_buttonLabel").Value,
                        ap_headOne       = this.accountLocalizer.GetString("ap_headOne").Value,
                        ap_headTwo       = this.accountLocalizer.GetString("ap_headTwo").Value,
                        ap_lineOne       = this.accountLocalizer.GetString("ap_lineOne").Value,
                        resetAccountLink = this.linkGenerator.GetUriByAction("Index", "SetPassword", new { Area = "Accounts", culture = existingOrganisation.Language, Id = user.ExpirationId, ReturnUrl = string.IsNullOrWhiteSpace(serviceModel.ReturnUrl) ? null : HttpUtility.UrlEncode(serviceModel.ReturnUrl) }, serviceModel.Scheme, serviceModel.Host)
                    }
                });

                return(await this.GetById(new GetUserServiceModel { Id = Guid.Parse(user.Id), Language = serviceModel.Language, Username = serviceModel.Username, OrganisationId = serviceModel.OrganisationId }));
            }

            var userAccount = new ApplicationUser
            {
                UserName           = serviceModel.Email,
                NormalizedUserName = serviceModel.Email,
                Email                = serviceModel.Email,
                NormalizedEmail      = serviceModel.Email,
                OrganisationId       = existingOrganisation.Id,
                SecurityStamp        = Guid.NewGuid().ToString(),
                VerifyExpirationDate = timeExpiration,
                ExpirationId         = Guid.NewGuid(),
                AccessFailedCount    = 0,
                EmailConfirmed       = false,
                PhoneNumberConfirmed = false,
                TwoFactorEnabled     = false,
                LockoutEnabled       = false,
            };

            this.identityContext.Accounts.Add(userAccount);
            await this.identityContext.SaveChangesAsync();

            await this.mailingService.SendTemplateAsync(new TemplateEmail
            {
                RecipientEmailAddress = userAccount.Email,
                RecipientName         = userAccount.UserName,
                SenderEmailAddress    = this.mailingOptions.CurrentValue.SenderEmail,
                SenderName            = this.mailingOptions.CurrentValue.SenderName,
                TemplateId            = this.mailingOptions.CurrentValue.ActionSendGridCreateTemplateId,
                DynamicTemplateData   = new
                {
                    lang            = existingOrganisation.Language,
                    nc_subject      = this.accountLocalizer.GetString("nc_subject").Value,
                    nc_preHeader    = this.accountLocalizer.GetString("nc_preHeader").Value,
                    nc_buttonLabel  = this.accountLocalizer.GetString("nc_buttonLabel").Value,
                    nc_headOne      = this.accountLocalizer.GetString("nc_headOne").Value,
                    nc_headTwo      = this.accountLocalizer.GetString("nc_headTwo").Value,
                    nc_lineOne      = this.accountLocalizer.GetString("nc_lineOne").Value,
                    nc_lineTwo      = this.accountLocalizer.GetString("nc_lineTwo").Value,
                    signAccountLink = this.linkGenerator.GetUriByAction("Index", "SetPassword", new { Area = "Accounts", culture = existingOrganisation.Language, Id = userAccount.ExpirationId, ReturnUrl = string.IsNullOrWhiteSpace(serviceModel.ReturnUrl) ? null : HttpUtility.UrlEncode(serviceModel.ReturnUrl) }, serviceModel.Scheme, serviceModel.Host)
                }
            });

            return(await this.GetById(new GetUserServiceModel { Id = Guid.Parse(userAccount.Id), Language = serviceModel.Language, Username = serviceModel.Username, OrganisationId = serviceModel.OrganisationId }));
        }