def test_dataset_survives_user(self, db_session, create_user): """The Dataset isn't automatically removed when the referenced user is removed. """ user = create_user ds = Dataset(owner=user.username, controller="frodo", name="fio") ds.add() User.delete(username=user.username) ds1 = Dataset.attach(controller="frodo", name="fio") assert ds1 == ds
def user_delete(context: object, username: str) -> None: try: # Delete the the user with specified username config_setup(context) User.delete(username=username) 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 delete(self, username): """ Delete request for deleting a user from database. This requires a Pbench auth token in the header field Required headers include Content-Type: application/json Accept: application/json Authorization: Bearer Pbench_auth_token (user received upon login) :return: Success: 200 with empty payload Failure: <status_Code>, response_object = { "message": "failure message" } """ try: user, verified = self.auth.verify_user(username) except Exception: self.logger.exception("Exception occurred during the getUser {}", username) abort(500, message="INTERNAL ERROR") # TODO: Check if the user has the right privileges if not verified and not user.is_admin(): self.logger.warning( "User {} is not authorized to delete user {}.", user.username, username, ) abort(403, message="Not authorized to delete user") try: user = User.query(username=username) # Do not delete if the user is admin if not user.is_admin(): User.delete(username) except Exception: self.logger.exception( "Exception occurred during deleting the user entry for user '{}'", username, ) abort(500, message="INTERNAL ERROR") else: if user.is_admin(): self.logger.warning("Admin attempted to delete admin user") abort(403, message="Admin user can not be deleted") self.logger.info("User entry deleted for user with username {}", username) return "", 200
def delete(self, target_username): """ Delete request for deleting a user from database. This requires a Pbench auth token in the header field Required headers include Content-Type: application/json Accept: application/json Authorization: Bearer Pbench_auth_token (user received upon login) :return: Success: 200 with empty payload Failure: <status_Code>, response_object = { "message": "failure message" } """ result = self.get_valid_target_user(target_username, "DELETE") if not result.target_user: abort(result.http_status, message=result.http_message) # Do not allow admin user to get self deleted via API if (result.target_user.is_admin() and self.auth.token_auth.current_user() == result.target_user): self.logger.warning( "Admin user is not allowed to self delete via API call. Username: {}", target_username, ) abort(HTTPStatus.FORBIDDEN, message="Not authorized to delete user") # If target user is a valid and not an admin proceed to delete try: User.delete(target_username) self.logger.info( "User entry deleted for user with username: {}, by user: {}", target_username, self.auth.token_auth.current_user().username, ) except Exception: self.logger.exception( "Exception occurred while deleting a user {}", target_username, ) abort(HTTPStatus.INTERNAL_SERVER_ERROR, message="INTERNAL ERROR") return "", HTTPStatus.OK