def user_create( context: object, username: str, password: str, email: str, first_name: str, last_name: str, role: str, ) -> None: try: config_setup(context) user = User( username=username, password=password, first_name=first_name, last_name=last_name, email=email, role=role if role else "", ) user.add() if user.is_admin(): click.echo(f"Admin user {username} registered") else: click.echo(f"User {username} registered") rv = 0 except Exception as exc: click.echo(exc, err=True) rv = 2 if isinstance(exc, BadConfig) else 1 click.get_current_context().exit(rv)
def create_user() -> User: user = User( email="*****@*****.**", password="******", username="******", first_name="Test", last_name="Account", ) user.add() return user
def execute(self): config = PbenchServerConfig(self.context.config) logger = get_pbench_logger(_NAME_, config) # We're going to need the Postgres DB to track dataset state, so setup # DB access. Database.init_db(config, logger) user = User( username=self.context.username, password=self.context.password, first_name=self.context.first_name, last_name=self.context.last_name, email=self.context.email, role=self.context.role if self.context.role else "", ) user.add() if user.is_admin(): click.echo(f"Admin user {self.context.username} registered") else: click.echo(f"User {self.context.username} registered")
def post(self): """ Post request for registering a new user. This requires a JSON data with required user fields { "username": "******", "password": "******", "first_name": first_name, "last_name": "last_name", "email": "*****@*****.**" } Required headers include Content-Type: application/json Accept: application/json :return: Success: 201 with empty payload Failure: <status_Code>, response_object = { "message": "failure message" } To get the auth token user has to perform the login action """ # get the post data user_data = request.get_json() if not user_data: self.logger.warning("Invalid json object: {}", request.url) abort(HTTPStatus.BAD_REQUEST, message="Invalid json object in request") username = user_data.get("username") if not username: self.logger.warning("Missing username field") abort(HTTPStatus.BAD_REQUEST, message="Missing username field") username = username.lower() if User.is_admin_username(username): self.logger.warning("User tried to register with admin username") abort( HTTPStatus.BAD_REQUEST, message="Please choose another username", ) # check if provided username already exists try: user = User.query(username=user_data.get("username")) except Exception: self.logger.exception("Exception while querying username") abort(HTTPStatus.INTERNAL_SERVER_ERROR, message="INTERNAL ERROR") if user: self.logger.warning("A user tried to re-register. Username: {}", user.username) abort(HTTPStatus.FORBIDDEN, message="Provided username is already in use.") password = user_data.get("password") if not password: self.logger.warning("Missing password field") abort(HTTPStatus.BAD_REQUEST, message="Missing password field") email_id = user_data.get("email") if not email_id: self.logger.warning("Missing email field") abort(HTTPStatus.BAD_REQUEST, message="Missing email field") # check if provided email already exists try: user = User.query(email=email_id) except Exception: self.logger.exception("Exception while querying user email") abort(HTTPStatus.INTERNAL_SERVER_ERROR, message="INTERNAL ERROR") if user: self.logger.warning("A user tried to re-register. Email: {}", user.email) abort(HTTPStatus.FORBIDDEN, message="Provided email is already in use") first_name = user_data.get("first_name") if not first_name: self.logger.warning("Missing first_name field") abort(HTTPStatus.BAD_REQUEST, message="Missing first_name field") last_name = user_data.get("last_name") if not last_name: self.logger.warning("Missing last_name field") abort(HTTPStatus.BAD_REQUEST, message="Missing last_name field") try: user = User( username=username, password=password, first_name=first_name, last_name=last_name, email=email_id, ) # insert the user user.add() self.logger.info("New user registered, username: {}, email: {}", username, email_id) return "", HTTPStatus.CREATED except EmailNotValidError: self.logger.warning("Invalid email {}", email_id) abort(HTTPStatus.BAD_REQUEST, message=f"Invalid email: {email_id}") except Exception: self.logger.exception("Exception while registering a user") abort(HTTPStatus.INTERNAL_SERVER_ERROR, message="INTERNAL ERROR")