def create_user(cls, user_data): if UserDatabaseClient.get_user_by_username( user_data.username) is not None: cls.logger().info( f"Failing to create user #{user_data.username}. Username already in use." ) return BadRequestUserMessageResponse( "Username already in use for other user.", UserResponseStatus.ALREADY_REGISTERED.value) try: new_client = UserDatabaseClient.add_client() new_user = User(user_id=new_client.id, username=user_data.username, email=user_data.email, password=hashing.hash(user_data.password), first_name=user_data.first_name, last_name=user_data.last_name, profile_pic=user_data.profile_pic, role=user_data.role or UserRoles.USER.value, token=Authenticator.generate( new_client.id, user_data.password)) UserDatabaseClient.add_user(new_user) DatabaseClient.commit() cls.logger().info(f"User #{new_client.id} created.") headers = {"auth_token": new_user.token} return SuccessfulUserResponse(new_user, headers) except IntegrityError as exc: DatabaseClient.rollback() if UserDatabaseClient.get_user_by_email( user_data.email) is not None: cls.logger().info( f"Failing to create user {user_data.username}. Email already in use.", exc) return BadRequestUserMessageResponse( "Email already in use for other user.", UserResponseStatus.ALREADY_REGISTERED.value) else: cls.logger().info( f"Failing to create user #{user_data.username}.") return UnsuccessfulClientResponse("Couldn't create user.") except: DatabaseClient.rollback() cls.logger().info(f"Failing to create user #{user_data.username}.") return UnsuccessfulClientResponse("Couldn't create user.")
def _login_facebook_user(cls, user_data): try: facebook_user = FacebookService.get_user_from_facebook(user_data) user = UserDatabaseClient.get_user_by_facebook_id( facebook_user.facebook_id) if user is not None: cls.logger().info( f"Logging in Facebook user with Facebook ID #{facebook_user.facebook_id}." ) cls.logger().debug(f"Generating token for user {user.id}") user.token = Authenticator.generate(user.id) user.online = True UserDatabaseClient.update_user(user) DatabaseClient.commit() cls.logger().info(f"User #{user.id} logged in.") headers = {"auth_token": user.token} return SuccessfulUserResponse(user, headers) else: cls.logger().info( f"Creating new Facebook user with Facebook ID #{facebook_user.facebook_id}." ) new_client = UserDatabaseClient.add_client() new_user = User(user_id=new_client.id, facebook_id=facebook_user.facebook_id, username=facebook_user.username, email=facebook_user.email, first_name=facebook_user.first_name, last_name=facebook_user.last_name, profile_pic=facebook_user.profile_pic, role=UserRoles.USER.value, token=Authenticator.generate(new_client.id)) UserDatabaseClient.add_user(new_user) DatabaseClient.commit() cls.logger().info(f"User #{new_client.id} logged in.") headers = {"auth_token": new_user.token} return SuccessfulUserResponse(new_user, headers) except FacebookWrongTokenError: cls.logger().info( f"Failing to logging in user with Facebook token #{user_data.facebook_token}." ) return UnsuccessfulClientResponse("Couldn't perform login.")