Created
January 18, 2019 13:25
-
-
Save diegodfsd/013647d9678e95c59772daf4e9da0874 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CreateTenantCommand : ICommand | |
{ | |
public string Name { get; } | |
public string Description { get; } | |
public Status Status { get; } | |
public string Hostname { get; } | |
public CreateTenantCommand(string hostname, string name, string description, Status status) | |
{ | |
Hostname = hostname; | |
Name = name; | |
Description = description; | |
Status = status; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Mehdime.Entity; | |
using Microsoft.AspNet.Identity; | |
using Xgen.CommandProcessor.Command; | |
using Xgen.Commands.Commands.Accounts; | |
using Xgen.Common; | |
using Xgen.Common.Extensions; | |
using Xgen.Common.Results; | |
using Xgen.Common.Utils; | |
using Xgen.Domain.Common; | |
using Xgen.Domain.Model.Users; | |
using Xgen.Domain.Services.Impl; | |
namespace Xgen.Commands.Handlers.Accounts | |
{ | |
public class CreateUserHandler : ICommandHandlerAsync<CreateUserCommand> | |
{ | |
private readonly UserManager<User, int> _userManager; | |
private readonly IDbContextScopeFactory _dbContextScopeFactory; | |
private readonly ICompanyRepository _companyRepository; | |
public CreateUserHandler(LocalUserManager userManager, IDbContextScopeFactory dbContextScopeFactory, | |
ICompanyRepository companyRepository) | |
{ | |
_userManager = userManager; | |
_dbContextScopeFactory = dbContextScopeFactory; | |
_companyRepository = companyRepository; | |
} | |
public async Task<ICommandResult> Handle(CreateUserCommand command) | |
{ | |
using (var dbContextScope = _dbContextScopeFactory.Create()) | |
{ | |
var passwordHash = _userManager.PasswordHasher.HashPassword(command.Password); | |
var company = _companyRepository.GetAllAsync().FirstOrDefault(); | |
var avatar = command.Filename ?? CreateDefaultAvatar(command.Name); | |
var stepBuilder = User.Create( | |
command.TenantId, | |
command.Name, | |
command.UserName, | |
passwordHash, | |
command.Email, | |
avatar, | |
command.BornAt, | |
command.Status, | |
company) | |
.WithNickname(command.Nickname) | |
.WithCmsPreferences(CmsPreferences.Create(command.MainManualId)) | |
.WithExternalId(command.ExternalId) | |
.WithAdditionalData(command.AdditionalData) | |
.ItsNoOp(); | |
var preferences = AttendancePreferences.Create( | |
maxSessions: command.MaxSessions.GetValueOrDefault(), | |
maxEmailSessions: command.MaxEmailSessions.GetValueOrDefault(), | |
emailSignature: command.EmailSignature, | |
allowFormat: command.AllowFormat.GetValueOrDefault(), | |
groupCallTypeMain: command.GroupCallTypeMain, | |
groupAttendanceMain: command.GroupAttendanceMain, | |
style: command.Style, | |
userInfo: command.UserInfo); | |
if ((command.Type & UserType.User) == UserType.User) stepBuilder.ItsAUser(); | |
if ((command.Type & UserType.Student) == UserType.Student) stepBuilder.ItsAStudent(); | |
if ((command.Type & UserType.Admin) == UserType.Admin) stepBuilder.ItsAnAdmin(); | |
if ((command.Type & UserType.Master) == UserType.Master) stepBuilder.ItsAMaster(); | |
if ((command.Type & UserType.Operator) == UserType.Operator) stepBuilder.ItsAnOperator(preferences); | |
if ((command.Type & UserType.Supervisor) == UserType.Supervisor) stepBuilder.ItsASupervisor(preferences); | |
var user = stepBuilder.Build(); | |
var result = await _userManager.CreateAsync(user); | |
if (!result.Succeeded) return GetErrorCode(result); | |
await dbContextScope.SaveChangesAsync(); | |
return CommandResult.Created((new { Id = user.Id }).ToDynamic()); | |
} | |
} | |
private ICommandResult GetErrorCode(IdentityResult identityResult) | |
{ | |
var errorMessage = identityResult.Errors.Join(separator: " "); | |
if (errorMessage.MatchRegex(@"^user\sname\s(.*?)\sis\sinvalid")) | |
return CommandResult.BadRequest(ErrorCode.USERNAME_CAN_ONLY_CONTAIN_LETTERS_AND_NUMBERS); | |
if (errorMessage.MatchRegex("email")) | |
return CommandResult.Conflict(ErrorCode.EMAIL_IS_ALREADY_TAKEN); | |
if (errorMessage.MatchRegex("name")) | |
return CommandResult.Conflict(ErrorCode.USERNAME_IS_ALREADY_TAKEN); | |
return CommandResult.BadRequest(ErrorCode.BAD_REQUEST); | |
} | |
private string CreateDefaultAvatar(string name) | |
{ | |
var filename = $"avatar_{Guid.NewGuid():N}.png"; | |
var directory = AppPathManager.Physical.GeneratePathUserImage(); | |
var path = Gravatar.Create(name, $"{directory}\\{filename}"); | |
return Path.GetFileName(path); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Xgen.CommandProcessor.Command; | |
using Xgen.Commands.Commands.Forms; | |
using Xgen.Common; | |
using Xgen.Common.Extensions; | |
using Xgen.Common.Results; | |
using Xgen.Domain.Model.Forms; | |
namespace Xgen.Commands.Validators.Forms | |
{ | |
[ValidationOrder(1)] | |
public class FieldResponseTimeoutValidator : IValidationHandler<CreateFieldResponseCommand>, IValidationHandler<CreateAnonymousFieldResponseCommand> | |
{ | |
private readonly IFormResponseRepository _formResponseRepository; | |
public FieldResponseTimeoutValidator(IFormResponseRepository formResponseRepository) | |
{ | |
_formResponseRepository = formResponseRepository; | |
} | |
public IValidationResult Validate(CreateAnonymousFieldResponseCommand command) | |
{ | |
return Validate(command.ResponseId); | |
} | |
public IValidationResult Validate(CreateFieldResponseCommand command) | |
{ | |
return Validate(command.ResponseId); | |
} | |
private IValidationResult Validate(int responseId) | |
{ | |
var answer = _formResponseRepository | |
.GetAll() | |
.NotTracking() | |
.Eager(_ => _.Form) | |
.GetById(responseId); | |
if (answer.IsNotNull()) | |
{ | |
return answer.Match() | |
.When(HasNoTimeLimit, _ => ValidationResult.Success) | |
.OrElse(TimeLimitHasBeenExceeded) | |
.Do(); | |
} | |
return ValidationResult.Success; | |
} | |
private static bool HasNoTimeLimit(FormResponse response) | |
{ | |
return !response.Form.HasTimeLimit; | |
} | |
private static ValidationResult TimeLimitHasBeenExceeded(FormResponse response) | |
{ | |
var timeLimit = response.Period.Begin.Value | |
.AddSeconds(response.Form.TimeLimit.GetValueOrDefault(0)) | |
.AddSeconds(5); | |
return DateTime.Now <= timeLimit ? ValidationResult.Success : ValidationResult.BadRequest(ErrorCode.REPLY_TIME_EXCEEDED); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment