Ejemplo n.º 1
0
        public Task <CommandResult> Handle(LoginCommand input)
        {
            var user = userRepo.Get(input.Email);

            if (user == null || user.Credentials == null)
            {
                return(CommandResult.Fail(UserManagementError.InvalidEmailOrPassword()).AsTask());
            }

            if (user.Credentials.EmailVerified == null)
            {
                return(CommandResult.Fail(UserManagementError.EmailNotVerified()).AsTask());
            }

            if (!hasher.VerifyPassword(user.Credentials.Password, input.Password))
            {
                return(CommandResult.Fail(UserManagementError.InvalidEmailOrPassword()).AsTask());
            }

            var tokenDto = new TokenDto()
            {
                Token = tokenBuilder.Build(user, appSettings.TokenDurationMinutes),
                TokenDurationMinutes = appSettings.TokenDurationMinutes,
                Domain = appSettings.TokenDomain
            };

            return(CommandResult <TokenDto> .Success(tokenDto).AsTask());
        }
Ejemplo n.º 2
0
        public Task <CommandResult> Handle(VerifyEmailCommand input)
        {
            var user = userRepo.Get(input.UserId);

            if (user == null)
            {
                return(CommandResult.Fail(UserManagementError.UserNotFound("User not found")).AsTask());
            }

            var code = user.ValidationCodes.FirstOrDefault(c => c.Type == ValidationCodeType.Email);

            if (code == null || !code.Code.Equals(input.Code) || (DateTime.Today - code.CreatedDate).TotalMinutes > 15)
            {
                return(CommandResult.Fail(UserManagementError.InvalidEmailVerificationCode()).AsTask());
            }

            var creds = user.Credentials;

            creds.EmailVerified = DateTime.Now;

            var res = userRepo.Update(user);

            if (res == null)
            {
                return(CommandResult.Fail(UserManagementError.EmailCouldNotUpdateDatabase()).AsTask());
            }
            else
            {
                return(CommandResult.Success().AsTask());
            }
        }
        public async Task <CommandResult> Handle(ResetPasswordCommand input)
        {
            if (!input.Validate(out ValidationError error))
            {
                return(CommandResult.Fail(error));
            }

            if (input.ValidateOnly)
            {
                return(CommandResult.Success());
            }

            var userGuid = Guid.Parse(input.UserId);
            var user     = userRepo.Get(userGuid);

            if (user == null)
            {
                return(CommandResult.Fail(UserManagementError.UserNotFound("User not found")));
            }

            var code  = user.ValidationCodes.FirstOrDefault(c => c.Type == Entities.ValidationCodeType.PasswordReset);
            var creds = user.Credentials;

            bool codesMatch = code != null && code.Code.Equals(input.ResetCode);
            bool passMatch  = creds != null && hasher.VerifyPassword(creds.Password, input.NewPassword);

            if (!codesMatch && !passMatch)
            {
                return(CommandResult.Fail(UserManagementError.InvalidCodePasswordOrUserId()));
            }

            // Creds shouldn't be null
            if (creds == null)
            {
                return(CommandResult.Fail(UserManagementError.InvalidCodePasswordOrUserId()));
            }

            creds.Password = hasher.HashPassword(input.NewPassword);

            userRepo.Update(user);

            await emailSender.Send(new PasswordWasResetEmail()
            {
                To = creds.Email
            });

            return(CommandResult.Success());
        }
Ejemplo n.º 4
0
        public Task <CommandResult <TenantDto> > Handle(GetTenantQuery query)
        {
            var tenant = tenantRepo.Get(query.Id);

            if (tenant == null)
            {
                return(CommandResult <TenantDto> .Fail(UserManagementError.TenanNotFound()).AsTaskTyped());
            }
            var dto = new TenantDto()
            {
                Id   = tenant.TenantId.ToString(),
                Name = tenant.TenantName
            };

            return(CommandResult <TenantDto> .Success(dto).AsTaskTyped());
        }
Ejemplo n.º 5
0
        public Task <CommandResult> Handle(AddTenantCommand command)
        {
            var existinTenant = tenantRepo.Get(command.Id);

            if (existinTenant != null)
            {
                return(CommandResult.Fail(UserManagementError.TenantCodeAlreadyExists()).AsTask());
            }

            var tenant = tenantRepo.Add(new Entities.Tenant()
            {
                TenantId   = command.Id,
                TenantName = command.Name
            });

            return(CommandResult <string> .Success(tenant.TenantId.ToString()).AsTask());
        }
Ejemplo n.º 6
0
        public Task <CommandResult <UserForUserDto> > Handle(GetCurrentUserQuery query)
        {
            var user = access.CurrentUser();

            if (user == null)
            {
                return(CommandResult <UserForUserDto> .Fail(UserManagementError.UserNotLoggedIn()).AsTaskTyped());
            }

            var userDto = UserMap.ToUserForUserDto(user, tenantRepo.GetAll().ToDictionary(t => t.TenantId.ToString(), t => t.TenantName));

            if (userDto == null)
            {
                return(CommandResult <UserForUserDto> .Fail(UserManagementError.UserNotFound()).AsTaskTyped());
            }

            return(CommandResult <UserForUserDto> .Success(userDto).AsTaskTyped());
        }
Ejemplo n.º 7
0
        public async Task <CommandResult> Handle(AddUserCommand command)
        {
            if (!command.Validate(out ValidationError error))
            {
                return(CommandResult.Fail(error));
            }

            if (!string.IsNullOrWhiteSpace(command.Email) && userRepo.Get(command.Email) != null)
            {
                return(CommandResult.Fail(ValidationError.New().AddError(nameof(command.Email), "Email Already Exists")));
            }

            if (tenantRepository.Get(command.TenantId) == null)
            {
                return(CommandResult.Fail(UserManagementError.TenanNotFound()));
            }

            if (command.ValidateOnly)
            {
                return(CommandResult.Success());
            }

            var user = GetUserFromInput(command);

            user.Credentials     = GetCredentialsIfEmail(command.Email);
            user.ValidationCodes = SetupValidationCodes(command);
            userRepo.Add(user);

            if (!string.IsNullOrWhiteSpace(command.Email))
            {
                var email = new VerifyEmailEmail()
                {
                    To  = command.Email,
                    Url = string.Format(appSettings.EmailVerificationUrl, user.Id.ToString(), user.ValidationCodes.FirstOrDefault().Code)
                };
                await emailSender.Send(email);
            }

            return(CommandResult <string> .Success(user.Id.ToString()));
        }
Ejemplo n.º 8
0
        public Task <CommandResult <UserForTenantDto> > Handle(GetUserQuery query)
        {
            if (string.IsNullOrEmpty(query.UserId))
            {
                return(CommandResult <UserForTenantDto> .Fail(ValidationError.New().AddError(nameof(query.UserId), "User Id is required")).AsTaskTyped());
            }

            if (!Guid.TryParse(query.UserId, out Guid userId))
            {
                return(CommandResult <UserForTenantDto> .Fail(ValidationError.New().AddError(nameof(query.UserId), "User Id is not in a correct format")).AsTaskTyped());
            }

            var user = userRepo.Get(userId);

            var userDto = UserMap.ToUserForTenantDto(user, query.TenantId);

            if (userDto == null)
            {
                return(CommandResult <UserForTenantDto> .Fail(UserManagementError.UserNotFound()).AsTaskTyped());
            }

            return(CommandResult <UserForTenantDto> .Success(userDto).AsTaskTyped());
        }
Ejemplo n.º 9
0
        public Task <CommandResult> Handle(AddTenantToUserCommand command)
        {
            var user = access.CurrentUser();

            if (user == null)
            {
                return(CommandResult.Fail(UserManagementError.UserNotLoggedIn()).AsTask());
            }

            var tenant = tenantRepo.Get(command.TenantId);

            if (tenant == null)
            {
                return(CommandResult.Fail(ValidationError.New().AddError(nameof(command.TenantId), "Invalid Organization Id")).AsTask());
            }

            if (command.ValidateOnly)
            {
                return(CommandResult.Success().AsTask());
            }

            if (user.Tenants.Any(t => t.TenantId == command.TenantId))
            {
                return(CommandResult.Success().AsTask());
            }

            user.Tenants.Add(new Entities.UserTenant()
            {
                TenantId    = command.TenantId,
                Permissions = new Dictionary <string, HashSet <string> >()
            });

            userRepo.Update(user);

            return(CommandResult.Success().AsTask());
        }