def login(self, user_dto, accept_terms=False, timeout=None): # type: (UserDTO, Optional[bool], Optional[float]) -> Tuple[bool, str] """ Login a user given a UserDTO """ if timeout is not None: try: timeout = int(timeout) timeout = min(60 * 60 * 24 * 30, max(60 * 60, timeout)) except ValueError: timeout = None if timeout is None: timeout = self._token_timeout user_orm = User.select().where( User.username == user_dto.username.lower(), User.password == user_dto.hashed_password).first() if user_orm is None: return False, UserEnums.AuthenticationErrors.INVALID_CREDENTIALS if user_orm.accepted_terms == UserController.TERMS_VERSION: return True, self._gen_token(user_orm.username, time.time() + timeout) if accept_terms is True: user_orm.accepted_terms = UserController.TERMS_VERSION user_orm.save() return True, self._gen_token(user_orm.username, time.time() + timeout) return False, UserEnums.AuthenticationErrors.TERMS_NOT_ACCEPTED
def load_users(self): # type: () -> List[UserDTO] """ Returns a list of UserDTOs with all the usernames """ _ = self users = [] for user_orm in User.select(): user_dto = UserMapper.orm_to_dto(user_orm) user_dto.clear_password() users.append(user_dto) return users
def get_number_of_users(): # type: () -> int """ Return the number of registred users """ return User.select().count()