/// <summary> /// Handles the specified request. /// </summary> /// <param name="request">The request.</param> /// <param name="response">The response.</param> protected override void Handle(CreateSystemAdminRequest request, DtoResponse <SystemAccountDto> response) { var systemAccount = _systemAccountRepository.GetByIdentifier(request.Email); var roleKey = _roleRepository.GetInternalRoleKeyByName("System Admin"); if (roleKey.HasValue) { if (systemAccount == null) { var result = _systemAccountIdentityServiceManager.Create(request.Email); if (result.Sucess) { var systemAccountFactory = new SystemAccountFactory(); systemAccount = systemAccountFactory.Create(Guid.Empty, request.Email, new Email(request.Email)); systemAccount.AddRole(roleKey.Value); var systemAccountDto = Mapper.Map <SystemAccount, SystemAccountDto> (systemAccount); response.DataTransferObject = systemAccountDto; } else { var dataErrorInfo = new DataErrorInfo(result.ErrorMessage, ErrorLevel.Error); response.DataTransferObject = new SystemAccountDto(); response.DataTransferObject.AddDataErrorInfo(dataErrorInfo); } } else { var dataErrorInfo = new DataErrorInfo("System Admin already exists.", ErrorLevel.Error); response.DataTransferObject = new SystemAccountDto(); response.DataTransferObject.AddDataErrorInfo(dataErrorInfo); } } }
protected override void Handle(CreateSystemAdminRequest request, DtoResponse <SystemAccountDto> response) { var systemAccount = _systemAccountRepository.GetByIdentifier(request.Email); var role = _roleRepository.GetInternalRoleKeyByName("System Admin"); if (role != null) { if (systemAccount == null) { var identityServiceResponse = _systemAccountIdentityServiceManager.Create(request.Username, request.Email); if (identityServiceResponse.Sucess) { systemAccount = _systemAccountFactory.CreateSystemAdmin(request.Email, new Email(request.Email)); var systemAccountDto = Mapper.Map <SystemAccount, SystemAccountDto>(systemAccount); response.DataTransferObject = systemAccountDto; systemAccount.AddRole(role); } else { var result = identityServiceResponse.ErrorMessage; var dataErrorInfo = new DataErrorInfo(result, ErrorLevel.Error); response.DataTransferObject = new SystemAccountDto(); response.DataTransferObject.AddDataErrorInfo(dataErrorInfo); } } else { var result = "System account already in use."; var dataErrorInfo = new DataErrorInfo(result, ErrorLevel.Error); response.DataTransferObject = new SystemAccountDto(); response.DataTransferObject.AddDataErrorInfo(dataErrorInfo); } } }
//[Authorize(Roles = Roles.SystemAdmin)] public async Task <IActionResult> CreateSystemAdmin(CreateSystemAdminRequest request) { if (await _userService.ExistsAsync(request.Email)) { return(BadRequest("User with this name already exists.")); } var userDto = _mapper.Map <UserDto>(request); userDto.Role = Role.SystemAdmin; userDto.OrganisationId = new Guid(UnchangeableEntities.AdminOrganisationGuidString); var credentials = await _userService.CreateAsync(userDto); return(Ok(credentials)); }