Beispiel #1
0
def remove_user_from_groups(current_session, username, groups=None):
    if not groups:
        groups = []
    usr = us.get_user(current_session, username)
    user_groups = us.get_user_groups(current_session, username)["groups"]
    groups_to_keep = [x for x in user_groups if x not in groups]

    projects_to_keep = {
        item
        for sublist in
        [gp.get_group_projects(current_session, x) for x in groups_to_keep]
        for item in sublist
    }

    projects_to_remove = {
        item
        for sublist in
        [gp.get_group_projects(current_session, x) for x in groups]
        for item in sublist if item not in projects_to_keep
    }

    responses = []
    for groupname in groups:
        try:
            response = disconnect_user_from_group(current_session, usr,
                                                  groupname)
            responses.append(response)
        except Exception as e:
            current_session.rollback()
            raise e
    for project in projects_to_remove:
        remove_user_from_project(current_session, usr, project)
    return {"result": responses}
Beispiel #2
0
def create_user(current_session, username, role, email):
    """
    Create a user for all the projects or groups in the list.
    If the user already exists, to avoid unadvertedly changing it, we suggest update
    Returns a dictionary.
    """
    if not username:
        raise UserError(("Error: Please provide a username"))
    try:
        usr = us.get_user(current_session, username)
        raise UserError(("Error: user already exist. If this is not a"
                         " mistake, please, retry using update"))
    except NotFound:
        user_list = [
            user["name"].upper()
            for user in get_all_users(current_session)["users"]
        ]
        if username.upper() in user_list:
            raise UserError(
                ("Error: user with a name with the same combination/order "
                 "of characters already exists. Please remove this other user"
                 " or modify the new one. Contact us in case of doubt"))
        is_admin = role == "admin"
        email_add = email
        usr = User(username=username,
                   active=True,
                   is_admin=is_admin,
                   email=email_add)
        current_session.add(usr)
        return us.get_user_info(current_session, username)
Beispiel #3
0
def update_user_projects_within_group(
    current_session, username, groupname, projectname
):
    user_groups = us.get_user_groups(current_session, username)
    """
    Simplified version for awg:
      Users only have read permission, so just checking the
      presence of the project in any of their other groups
      suffices to keep the projec in the list.
    In real life we should check permissions coming from all groups
    and remove the specific ones comiing from groupname
    """
    group_projects = [
        gp.get_group_projects(current_session, group)
        for group in user_groups["groups"]
        if group != groupname
    ]

    projects_to_keep = [item for sublist in group_projects for item in sublist]

    if projectname not in projects_to_keep:
        try:
            us.remove_user_from_project(
                current_session,
                us.get_user(current_session, username),
                pj.get_project(current_session, projectname),
            )
        except NotFound:
            # somehow the user was not linked to that project
            pass
Beispiel #4
0
def add_user_to_groups(current_session, username, groups=None):
    if not groups:
        groups = []
    usr = us.get_user(current_session, username)
    responses = []
    for groupname in groups:
        try:
            response = connect_user_to_group(current_session, usr, groupname)
            responses.append(response)
        except Exception as e:
            current_session.rollback()
            raise e
    return {"result": responses}
Beispiel #5
0
def add_user_to_projects(current_session, username, projects=None):
    if not projects:
        projects = []
    usr = us.get_user(current_session, username)
    responses = []
    for proj in projects:
        try:
            response = connect_user_to_project(current_session, usr, proj)
            responses.append(response)
        except Exception as e:
            current_session.rollback()
            raise e
    return {"result": responses}
Beispiel #6
0
def update_user(current_session, username, role, email, new_name):
    usr = us.get_user(current_session, username)
    user_list = [
        user["name"].upper()
        for user in get_all_users(current_session)["users"]
    ]
    if (new_name and new_name.upper() in user_list
            and not username.upper() == new_name.upper()):
        raise UserError(
            ("Error: user with a name with the same combination/order "
             "of characters already exists. Please remove this other user"
             " or modify the new one. Contact us in case of doubt"))
    usr.email = email or usr.email
    if role:
        usr.is_admin = role == "admin"
    usr.username = new_name or usr.username
    return us.get_user_info(current_session, usr.username)
Beispiel #7
0
def update_group_users_projects(current_session, group, project, users):
    proj = pj.get_project(current_session, project)
    for user in users:
        try:
            user_projects = user.project_access.keys()
            if project not in user_projects:
                project_info = {"auth_id": proj.auth_id, "privilege": ["read"]}
                au.connect_user_to_project(
                    current_session,
                    us.get_user(current_session, user.username),
                    project_info,
                )
        except NotFound:
            pass
    return {
        "success": "users {0} connected to project {1}".format(
            [user.username for user in users], project
        )
    }