예제 #1
0
def perform_request_update_group(uid, grpID, group_name, group_description,
        join_policy, ln=CFG_SITE_LANG):
    """Update group datas in database.
    @param uid: user ID
    @param grpID: ID of the group
    @param group_name: name of the group
    @param group_description: description of the group
    @param join_policy: join policy of the group
    @param ln: language
    @return: a (body, errors[], warnings[]) formed tuple
    """
    body = ''
    errors = []
    warnings = []
    infos = []
    _ = gettext_set_language(ln)
    group_name_available = db.group_name_exist(group_name)
    if group_name == "":
        warnings.append('WRN_WEBSESSION_NO_GROUP_NAME')
        (body, errors, warnings) = perform_request_edit_group(uid,
            grpID,
            warnings=warnings,
            ln=ln)
    elif not group_name_valid_p(group_name):
        warnings.append('WRN_WEBSESSION_NOT_VALID_GROUP_NAME')
        (body, errors, warnings) = perform_request_edit_group(uid,
            grpID,
            warnings=warnings,
            ln=ln)
    elif join_policy == "-1":
        warnings.append('WRN_WEBSESSION_NO_JOIN_POLICY')
        (body, errors, warnings) = perform_request_edit_group(uid,
            grpID,
            warnings=warnings,
            ln=ln)
    elif (group_name_available and group_name_available[0][0]!= grpID):
        warnings.append('WRN_WEBSESSION_GROUP_NAME_EXISTS')
        (body, errors, warnings) = perform_request_edit_group(uid,
            grpID,
            warnings=warnings,
            ln=ln)

    else:
        grpID = db.update_group_infos(grpID,
            group_name,
            group_description,
            join_policy)
        infos.append(CFG_WEBSESSION_INFO_MESSAGES["GROUP_UPDATED"])
        (body, errors, warnings) = perform_request_groups_display(uid,
            infos=infos,
            errors=errors,
            warnings=warnings,
            ln=CFG_SITE_LANG)

    return (body, errors, warnings)
예제 #2
0
def synchronize_all_external_groups(usersgroups, login_method):
    """Merges all the groups vs users memberships.
    @param usersgroups: is {'mygroup': ('description',
        ['email1', 'email2', ...]), ...}
    @return: True in case everythings is ok, False otherwise
    """
    db_users = db.get_all_users()  # All users of the database {email:uid, ...}
    db_users_set = set(db_users.keys())  # Set of all users set('email1',
    # 'email2', ...)
    for key, value in usersgroups.items():
        # cleaning users not in db
        cleaned_user_list = set()
        for username in value[1]:
            username = username.upper()
            if username in db_users_set:
                cleaned_user_list.add(db_users[username])
        if cleaned_user_list:
            usersgroups[key] = (value[0], cleaned_user_list)
        else:  # List of user now is empty
            del usersgroups[key]  # cleaning not interesting groups

    # now for each group we got a description and a set of uid
    groups_already_known = db.get_all_login_method_groups(login_method)
    # groups in the db {groupname: id}
    groups_already_known_set = set(groups_already_known.keys())
    # set of the groupnames in db
    usersgroups_set = set(usersgroups.keys())  # set of groupnames to be merged

    # deleted groups!
    nomore_groups = groups_already_known_set - usersgroups_set
    for group_name in nomore_groups:
        db.delete_group_and_members(groups_already_known[group_name])

    # new groups!
    new_groups = usersgroups_set - groups_already_known_set
    for group_name in new_groups:
        groupid = db.insert_only_new_group(
            group_name,
            usersgroups[group_name][0],  # description
            CFG_WEBSESSION_GROUP_JOIN_POLICY['VISIBLEEXTERNAL'],
            login_method)
        for uid in usersgroups[group_name][1]:
            db.insert_new_member(uid, groupid,
                                 CFG_WEBSESSION_USERGROUP_STATUS['MEMBER'])

    # changed groups?
    changed_groups = usersgroups_set & groups_already_known_set
    groups_description = db.get_all_groups_description(login_method)
    for group_name in changed_groups:
        users_already_in_group = db.get_users_in_group(
            groups_already_known[group_name])
        users_already_in_group_set = set(users_already_in_group)
        users_in_group_set = usersgroups[group_name][1]

        # no more affiliation
        nomore_users = users_already_in_group_set - users_in_group_set
        for uid in nomore_users:
            db.delete_member(groups_already_known[group_name], uid)

        # new affiliation
        new_users = users_in_group_set - users_already_in_group_set
        for uid in new_users:
            db.insert_new_member(uid, groups_already_known[group_name],
                                 CFG_WEBSESSION_USERGROUP_STATUS['MEMBER'])

        # check description
        if groups_description[group_name] != usersgroups[group_name][0]:
            db.update_group_infos(
                groups_already_known[group_name], group_name,
                usersgroups[group_name][0],
                CFG_WEBSESSION_GROUP_JOIN_POLICY['VISIBLEEXTERNAL'])
예제 #3
0
                _('Please choose a group join policy.'))
        except InvenioWebSessionWarning, exc:
            register_exception(stream='warning')
            warnings.append(exc.message)
        body = perform_request_edit_group(uid, grpID, warnings=warnings, ln=ln)
    elif (group_name_available and group_name_available[0][0] != grpID):
        try:
            raise InvenioWebSessionWarning(
                _('Group name already exists. Please choose another group name.'
                  ))
        except InvenioWebSessionWarning, exc:
            register_exception(stream='warning')
            warnings.append(exc.message)
        body = perform_request_edit_group(uid, grpID, warnings=warnings, ln=ln)
    else:
        grpID = db.update_group_infos(grpID, group_name, group_description,
                                      join_policy)
        infos.append(CFG_WEBSESSION_INFO_MESSAGES["GROUP_UPDATED"])
        body = perform_request_groups_display(uid,
                                              infos=infos,
                                              warnings=warnings,
                                              ln=CFG_SITE_LANG)
    return body


def perform_request_delete_group(uid, grpID, confirmed=0, ln=CFG_SITE_LANG):
    """First display confirm message(confirmed=0).
    then(confirmed=1) delete group and all its members
    @param uid: user ID
    @param grpID: ID of the group
    @param confirmed: =1 if confirmed message has been previously displayed
    @param ln: language
예제 #4
0
def synchronize_all_external_groups(usersgroups, login_method):
    """Merges all the groups vs users memberships.
    @param usersgroups: is {'mygroup': ('description',
        ['email1', 'email2', ...]), ...}
    @return: True in case everythings is ok, False otherwise
    """
    db_users = db.get_all_users()  # All users of the database {email:uid, ...}
    db_users_set = set(db_users.keys())  # Set of all users set('email1',
    # 'email2', ...)
    for key, value in usersgroups.items():
        # cleaning users not in db
        cleaned_user_list = set()
        for username in value[1]:
            username = username.upper()
            if username in db_users_set:
                cleaned_user_list.add(db_users[username])
        if cleaned_user_list:
            usersgroups[key] = (value[0], cleaned_user_list)
        else:  # List of user now is empty
            del usersgroups[key]  # cleaning not interesting groups

    # now for each group we got a description and a set of uid
    groups_already_known = db.get_all_login_method_groups(login_method)
    # groups in the db {groupname: id}
    groups_already_known_set = set(groups_already_known.keys())
    # set of the groupnames in db
    usersgroups_set = set(usersgroups.keys())  # set of groupnames to be merged

    # deleted groups!
    nomore_groups = groups_already_known_set - usersgroups_set
    for group_name in nomore_groups:
        db.delete_group_and_members(groups_already_known[group_name])

    # new groups!
    new_groups = usersgroups_set - groups_already_known_set
    for group_name in new_groups:
        groupid = db.insert_only_new_group(
            group_name,
            usersgroups[group_name][0],  # description
            CFG_WEBSESSION_GROUP_JOIN_POLICY["VISIBLEEXTERNAL"],
            login_method,
        )
        for uid in usersgroups[group_name][1]:
            db.insert_new_member(uid, groupid, CFG_WEBSESSION_USERGROUP_STATUS["MEMBER"])

    # changed groups?
    changed_groups = usersgroups_set & groups_already_known_set
    groups_description = db.get_all_groups_description(login_method)
    for group_name in changed_groups:
        users_already_in_group = db.get_users_in_group(groups_already_known[group_name])
        users_already_in_group_set = set(users_already_in_group)
        users_in_group_set = usersgroups[group_name][1]

        # no more affiliation
        nomore_users = users_already_in_group_set - users_in_group_set
        for uid in nomore_users:
            db.delete_member(groups_already_known[group_name], uid)

        # new affiliation
        new_users = users_in_group_set - users_already_in_group_set
        for uid in new_users:
            db.insert_new_member(uid, groups_already_known[group_name], CFG_WEBSESSION_USERGROUP_STATUS["MEMBER"])

        # check description
        if groups_description[group_name] != usersgroups[group_name][0]:
            db.update_group_infos(
                groups_already_known[group_name],
                group_name,
                usersgroups[group_name][0],
                CFG_WEBSESSION_GROUP_JOIN_POLICY["VISIBLEEXTERNAL"],
            )
예제 #5
0
    elif join_policy == "-1":
        try:
            raise InvenioWebSessionWarning(_("Please choose a group join policy."))
        except InvenioWebSessionWarning, exc:
            register_exception(stream="warning")
            warnings.append(exc.message)
        body = perform_request_edit_group(uid, grpID, warnings=warnings, ln=ln)
    elif group_name_available and group_name_available[0][0] != grpID:
        try:
            raise InvenioWebSessionWarning(_("Group name already exists. Please choose another group name."))
        except InvenioWebSessionWarning, exc:
            register_exception(stream="warning")
            warnings.append(exc.message)
        body = perform_request_edit_group(uid, grpID, warnings=warnings, ln=ln)
    else:
        grpID = db.update_group_infos(grpID, group_name, group_description, join_policy)
        infos.append(CFG_WEBSESSION_INFO_MESSAGES["GROUP_UPDATED"])
        body = perform_request_groups_display(uid, infos=infos, warnings=warnings, ln=CFG_SITE_LANG)
    return body


def perform_request_delete_group(uid, grpID, confirmed=0, ln=CFG_SITE_LANG):
    """First display confirm message(confirmed=0).
    then(confirmed=1) delete group and all its members
    @param uid: user ID
    @param grpID: ID of the group
    @param confirmed: =1 if confirmed message has been previously displayed
    @param ln: language
    @return: body with warnings
    """
    body = ""