def create(userJson): userSerialized = JSONSerializator().serialize(userJson, ignoreProperties=True) print(userSerialized.dumpModel()) user = DBUtil.findByUsername(User, userSerialized.username) if user is not None: return GenericResponseDto.createResponse( ResponseMessage.FORBIDDEN, ResponseCode.FORBIDDEN, "Username already exists!") app = ApplicationService.findAppById(userSerialized.appId) if app is None: return GenericResponseDto.createResponse( ResponseMessage.NOT_FOUND, ResponseCode.NOT_FOUND, "Application with ID=[{}] not found!".format( userSerialized.appId)) user = UserService.mapUser(userSerialized) status, data = DBUtil.insert(user) if not status: return GenericResponseDto.createResponse( ResponseMessage.INTERNAL_SERVER_ERROR, ResponseCode.INTERNAL_SERVER_ERROR, data) else: userDto = UserDto.createFromEntity(data) return GenericResponseDto.createResponse(ResponseMessage.OK, ResponseCode.OK, userDto.getJson())
def checkLoginInformation(loginDto: LoginDto): user = DBUtil.findByUsername(User, loginDto.username) if user is None: return False, GenericResponseDto.createResponse( ResponseMessage.NOT_FOUND, ResponseCode.NOT_FOUND, "User not found!") else: if loginDto.password == user.pwd: app = ApplicationService.findAppByToken(loginDto.token) if app is None: return False, GenericResponseDto.createResponse( ResponseMessage.NOT_FOUND, ResponseCode.NOT_FOUND, "Invalid application token!") if app.id == user.app_id: return True, GenericResponseDto.createResponse( ResponseMessage.OK, ResponseCode.OK, UserDto.createFromEntity(user).getJson()) else: return False, GenericResponseDto.createResponse( ResponseMessage.NOT_ACCEPTABLE, ResponseCode.NOT_ACCEPTABLE, "Invalid application token!") else: return False, GenericResponseDto.createResponse( ResponseMessage.UNAUTHORIZED, ResponseCode.UNAUTHORIZED, "Password is not correct!")
def changeUserPassword(username, password): try: user = DBUtil.findByUsername(User, username) user.pwd = password db.session.commit() return True except Exception as e: print(e) db.session.rollback() return False
def getUserByUsername(username): user = DBUtil.findByUsername(User, username) return user