Exemplo n.º 1
0
def create_user_db_and_data_folder_if_needed(sender, instance, **kwargs):
    """
    Creates a Postgres role and db and data folder to go with new Django users.

    Raises an exception if the role, database, or user data folder exists
    before this user.
    """
    username = instance.username
    hashed_password = instance.password

    # The Django user doesn't exist yet, so we can't just try to create a
    # DataHubManager connection as the user. We need to act as the db
    # superuser and check for any existing db role or database.
    db_exists = DataHubManager.database_exists(username)
    user_exists = DataHubManager.user_exists(username)
    user_data_folder_exists = DataHubManager.user_data_folder_exists(username)
    if db_exists and user_exists and user_data_folder_exists:
        # Make sure new users don't inherit orphaned roles or databases that
        # are missing a matching Django user.
        try:
            User.objects.get(username=username)
        except User.DoesNotExist:
            raise IntegrityError("Failed to create user. That name is already"
                                 " in use by an orphaned user.")
    elif not db_exists and not user_exists and not user_data_folder_exists:
        try:
            DataHubManager.create_user(
                username=username,
                password=hashed_password)
        except OperationalError:
            raise
    else:
        raise Exception("Failed to create user. That name is already"
                        " in use by either a role, database, or data folder.")
Exemplo n.º 2
0
def validate_unique_username(value):
    """
    Validates that a proposed username is not already in use.

    Checks User and App models, databases, database roles, and user data
    folders.
    """
    username = value.lower()

    try:
        existing_user = User.objects.get(username=username)
    except User.DoesNotExist:
        existing_user = None

    try:
        existing_app = App.objects.get(app_id=username)
    except App.DoesNotExist:
        existing_app = None

    db_exists = DataHubManager.database_exists(username)
    user_exists = DataHubManager.user_exists(username)
    user_data_folder_exists = DataHubManager.user_data_folder_exists(username)

    if (existing_user or existing_app or
            db_exists or user_exists or
            user_data_folder_exists):
        raise forms.ValidationError(
            ('The username %(value)s is not available.'),
            params={'value': value},
        )
    return True
Exemplo n.º 3
0
def validate_unique_username(value):
    """
    Validates that a proposed username is not already in use.

    Checks User and App models, databases, database roles, and user data
    folders.
    """
    username = value.lower()

    try:
        existing_user = User.objects.get(username=username)
    except User.DoesNotExist:
        existing_user = None

    try:
        existing_app = App.objects.get(app_id=username)
    except App.DoesNotExist:
        existing_app = None

    db_exists = DataHubManager.database_exists(username)
    user_exists = DataHubManager.user_exists(username)
    user_data_folder_exists = DataHubManager.user_data_folder_exists(username)

    if (existing_user or existing_app or db_exists or user_exists
            or user_data_folder_exists):
        raise forms.ValidationError(
            ('The username %(value)s is not available.'),
            params={'value': value},
        )
    return True
Exemplo n.º 4
0
def create_user_db_and_data_folder_if_needed(sender, instance, **kwargs):
    """
    Creates a Postgres role and db and data folder to go with new Django users.

    Raises an exception if the role, database, or user data folder exists
    before this user.
    """
    username = instance.username
    hashed_password = instance.password

    # The Django user doesn't exist yet, so we can't just try to create a
    # DataHubManager connection as the user. We need to act as the db
    # superuser and check for any existing db role or database.
    db_exists = DataHubManager.database_exists(username)
    user_exists = DataHubManager.user_exists(username)
    user_data_folder_exists = DataHubManager.user_data_folder_exists(username)
    if db_exists and user_exists and user_data_folder_exists:
        # Make sure new users don't inherit orphaned roles or databases that
        # are missing a matching Django user.
        try:
            User.objects.get(username=username)
        except User.DoesNotExist:
            raise IntegrityError("Failed to create user. That name is already"
                                 " in use by an orphaned user.")
    elif not db_exists and not user_exists and not user_data_folder_exists:
        try:
            DataHubManager.create_user(username=username,
                                       password=hashed_password)
        except OperationalError:
            raise
    else:
        raise Exception("Failed to create user. That name is already"
                        " in use by either a role, database, or data folder.")
Exemplo n.º 5
0
def create_anonymous_user(apps, schema_editor):
    # Create anonymous user
    username = settings.ANONYMOUS_ROLE
    email = settings.ANONYMOUS_ROLE_EMAIL
    users = User.objects.filter(username=username)

    dh_user_exists = False
    if len(users) > 0:
        dh_user_exists = True
    db_exists = DataHubManager.database_exists(username)
    db_role_exists = DataHubManager.user_exists(username)
    user_data_folder_exists = DataHubManager.user_data_folder_exists(username)

    # try to create the django user. Get the password from them
    password = None
    if not dh_user_exists:
        password = User.objects.create_user(username=username,
                                            email=email,
                                            password=None).password

    # try to create the db role
    if not db_role_exists:
        DataHubManager.create_user(username=username,
                                   password=password,
                                   create_db=False)

    # delete any db that exists
    if db_exists:
        DataHubManager.remove_database(repo_base=username,
                                       revoke_collaborators=False)

    # delete any user data folder that exists
    if user_data_folder_exists:
        DataHubManager.delete_user_data_folder(username)
Exemplo n.º 6
0
def create_public_user(apps, schema_editor):
    # Create public user
    username = settings.PUBLIC_ROLE
    email = settings.PUBLIC_ROLE_EMAIL
    users = User.objects.filter(username=username)

    dh_user_exists = False
    if len(users) > 0:
        dh_user_exists = True
    db_exists = DataHubManager.database_exists(username)
    db_role_exists = DataHubManager.user_exists(username)
    user_data_folder_exists = DataHubManager.user_data_folder_exists(username)

    # try to create the django user. Get the password from them
    password = None
    if not dh_user_exists:
        password = User.objects.create_user(username=username,
                                            email=email,
                                            password=None).password

    # try to create the db role
    if not db_role_exists:
        DataHubManager.create_user(username=username,
                                   password=password,
                                   create_db=False)

    # try to create the db
    if not db_exists:
        DataHubManager.create_user_database(username=username)

    # delete any user data folder that exists
    if user_data_folder_exists:
        DataHubManager.delete_user_data_folder(username)
Exemplo n.º 7
0
def create_oauth2_user(apps, schema_editor):
    """
    Create a user to own default oauth2 applications.
    This user has a django account, but not a database role/db, or data folder.
    """
    username = OAUTH2_APP_OWNER
    users = User.objects.filter(username=username)

    dh_user_exists = bool(len(users) > 0)
    db_exists = DataHubManager.database_exists(username)
    db_role_exists = DataHubManager.user_exists(username)
    user_data_folder_exists = DataHubManager.user_data_folder_exists(username)

    # try to create the django user. This skips validation, so the password
    # and email may as well be none.
    if not dh_user_exists:
        User.objects.create_user(username=username, email=None, password=None)
        print_if_not_testing('...django user created')

    # delete any db that exists
    if db_exists:
        DataHubManager.remove_database(repo_base=username,
                                       revoke_collaborators=True)
        print_if_not_testing('...db removed')

    # delete any db role that exists
    if db_role_exists:
        DataHubManager.remove_user(username,
                                   remove_db=False,
                                   ignore_missing_user=False)
        print_if_not_testing('...db role removed')

    # delete any user data folder that exists
    if user_data_folder_exists:
        DataHubManager.delete_user_data_folder(username)
        print_if_not_testing('...data folder removed')

    print_if_not_testing('...success.')
Exemplo n.º 8
0
def create_oauth2_user(apps, schema_editor):
    """
    Create a user to own default oauth2 applications.
    This user has a django account, but not a database role/db, or data folder.
    """
    username = OAUTH2_APP_OWNER
    users = User.objects.filter(username=username)

    dh_user_exists = bool(len(users) > 0)
    db_exists = DataHubManager.database_exists(username)
    db_role_exists = DataHubManager.user_exists(username)
    user_data_folder_exists = DataHubManager.user_data_folder_exists(username)

    # try to create the django user. This skips validation, so the password
    # and email may as well be none.
    if not dh_user_exists:
        User.objects.create_user(
            username=username, email=None, password=None)
        print_if_not_testing('...django user created')

    # delete any db that exists
    if db_exists:
        DataHubManager.remove_database(
            repo_base=username, revoke_collaborators=True)
        print_if_not_testing('...db removed')

    # delete any db role that exists
    if db_role_exists:
        DataHubManager.remove_user(username, remove_db=False,
                                   ignore_missing_user=False)
        print_if_not_testing('...db role removed')

    # delete any user data folder that exists
    if user_data_folder_exists:
        DataHubManager.delete_user_data_folder(username)
        print_if_not_testing('...data folder removed')

    print_if_not_testing('...success.')