Ejemplo n.º 1
0
 public UserIdentityModel Login(LoginCommand command)
 {
     EnsureIsValid(command);
     try
     {
         var user = _deps.Users.QueryOne(command.ToDbQuery());
         if (user == null)
         {
             throw AccessFailure.ExceptionBecause(AccessFailureReason.BadCredentials);
         }
         var reason = VerifyAccess(user, command.Password);
         if (reason == null)
         {
             var model = user.ToModel <User, UserIdentityModel>();
             Publish(new UserLoggedIn(Operation.Id, model));
             return(model);
         }
         Commit();
         Publish(new UserLoginFailed(Operation.Id, user.ToModel <User, UserIdentityModel>()));
         throw AccessFailure.ExceptionBecause(reason.Value);
     }
     catch (ServiceException)
     {
         throw;
     }
     catch (Exception ex)
     {
         throw new ServiceException("Can't login user.", ex);
     }
 }
Ejemplo n.º 2
0
 public UserMessage ChangePassword(ChangePasswordCommand command)
 {
     EnsureIsValid(command);
     try
     {
         var user = _deps.Users.Find(command.UserId);
         if (user == null || user.Deleted)
         {
             throw NotFound.ExceptionFor <User>(command.UserId);
         }
         if (!user.ValidatePassword(command.CurrentPassword))
         {
             throw AccessFailure.ExceptionBecause(AccessFailureReason.BadCredentials);
         }
         user.UpdatePassword(command.NewPassword);
         _deps.Tokens.Delete(DbQuery.For <ApplicationToken>().FilterBy(x => x.User.Id == command.UserId));
         Commit();
         return(UserMessage.Resource(() => Messages.PasswordChanged));
     }
     catch (ServiceException)
     {
         throw;
     }
     catch (Exception ex)
     {
         throw new ServiceException("Can't change password.", ex);
     }
 }
Ejemplo n.º 3
0
 public UserIdentityModel RefreshLogin(IdentityQuery <Guid> query)
 {
     EnsureIsValid(query);
     try
     {
         var user = _deps.Users.QueryIdentity(query);
         if (user == null)
         {
             throw AccessFailure.ExceptionBecause(AccessFailureReason.BadCredentials);
         }
         var reason = VerifyAccess(user);
         if (reason == null)
         {
             return(user.ToModel <User, UserIdentityModel>());
         }
         Commit();
         throw AccessFailure.ExceptionBecause(reason.Value);
     }
     catch (Exception ex)
     {
         throw new ServiceException("Can't get user.", ex);
     }
 }