Beispiel #1
0
def synchronize_external_groups(userid, groups, login_method):
    """Synchronize external groups adding new groups that aren't already
    added, adding subscription for userid to groups, for groups that the user
    isn't already subsribed to, removing subscription to groups the user is no
    more subsribed to.
    @param userid, the intger representing the user inside the db
    @param groups, a dictionary of group_name : group_description
    @param login_method, a string unique to the type of authentication
           the groups are associated to, to be used inside the db
    """

    groups_already_known = db.get_login_method_groups(userid, login_method)
    group_dict = {}
    for name, groupid in groups_already_known:
        group_dict[name] = groupid
    groups_already_known_name = set([g[0] for g in groups_already_known])
    groups_name = set(groups.keys())

    nomore_groups = groups_already_known_name - groups_name
    for group in nomore_groups: # delete the user from no more affiliated group
        db.delete_member(group_dict[group], userid)

    potential_new_groups = groups_name - groups_already_known_name
    for group in potential_new_groups:
        groupid = db.get_group_id(group, login_method)
        if groupid: # Adding the user to an already existent group
            db.insert_new_member(userid, groupid[0][0],
                CFG_WEBSESSION_USERGROUP_STATUS['MEMBER'])
        else: # Adding a new group
            try:
                groupid = db.insert_new_group(userid, group, groups[group], \
                    CFG_WEBSESSION_GROUP_JOIN_POLICY['VISIBLEEXTERNAL'], \
                    login_method)
                db.add_pending_member(groupid, userid,
                    CFG_WEBSESSION_USERGROUP_STATUS['MEMBER'])
            except IntegrityError:
                ## The group already exists? Maybe because of concurrency?
                groupid = db.get_group_id(group, login_method)
                if groupid: # Adding the user to an already existent group
                    db.insert_new_member(userid, groupid[0][0],
                        CFG_WEBSESSION_USERGROUP_STATUS['MEMBER'])
Beispiel #2
0
def perform_request_remove_member(uid, grpID, member_id, ln=CFG_SITE_LANG):
    """Remove member from a group.
    @param uid: user ID
    @param grpID: ID of the group
    @param member_id: selected member ID
    @param ln: language
    @return: body with warnings
    """
    body = ''
    warnings = []
    infos = []
    _ = gettext_set_language(ln)
    user_status = db.get_user_status(uid, grpID)
    if not len(user_status):
        try:
            raise InvenioWebSessionError(_('Sorry, there was an error with the database.'))
        except InvenioWebSessionError as exc:
            register_exception()
            body = websession_templates.tmpl_error(exc.message, ln)
            return body
    if member_id == -1:
        try:
            raise InvenioWebSessionWarning(_('Please choose a member if you want to remove him from the group.'))
        except InvenioWebSessionWarning as exc:
            register_exception(stream='warning')
            warnings.append(exc.message)
        body = perform_request_manage_member(uid,
                                             grpID,
                                             warnings=warnings,
                                             ln=ln)
    else:
        db.delete_member(grpID, member_id)
        infos.append(CFG_WEBSESSION_INFO_MESSAGES["MEMBER_DELETED"])
        body = perform_request_manage_member(uid,
                                             grpID,
                                             infos=infos,
                                             warnings=warnings,
                                             ln=ln)
    return body
Beispiel #3
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'])
Beispiel #4
0
def perform_request_reject_member(uid,
        grpID,
        user_id,
        ln=CFG_SITE_LANG):
    """Reject waiting member and delete it from the list.
    @param uid: user ID
    @param grpID: ID of the group
    @param member_id: selected member ID
    @param ln: language
    @return: body with warnings
    """
    body = ''
    warnings = []
    infos = []
    _ = gettext_set_language(ln)
    user_status = db.get_user_status(uid, grpID)
    if not len(user_status):
        try:
            raise InvenioWebSessionError(_('Sorry, there was an error with the database.'))
        except InvenioWebSessionError as exc:
            register_exception()
            body = websession_templates.tmpl_error(exc.message, ln)
            return body
    if user_id == -1:
        try:
            raise InvenioWebSessionWarning(_('Please choose a user from the list if you want him to be removed from waiting list.'))
        except InvenioWebSessionWarning as exc:
            register_exception(stream='warning')
            warnings.append(exc.message)
        body = perform_request_manage_member(uid,
                                             grpID,
                                             warnings=warnings,
                                             ln=ln)
    else :
        # test if user is already member or pending
        status = db.get_user_status(user_id, grpID)
        if not status:
            try:
                raise InvenioWebSessionWarning(_('The user request for joining group has already been rejected.'))
            except InvenioWebSessionWarning as exc:
                register_exception(stream='warning')
                warnings.append(exc.message)
            body = perform_request_manage_member(uid,
                                                 grpID,
                                                 infos=infos,
                                                 warnings=warnings,
                                                 ln=ln)
        else:
            db.delete_member(grpID, user_id)
            group_infos = db.get_group_infos(grpID)
            group_name = group_infos[0][1]
            user = get_user_info(user_id, ln)[2]
            msg_subjet, msg_body = websession_templates.tmpl_member_msg(
                group_name=group_name,
                accepted=0,
                ln=ln)
            (body, dummy, dummy) = perform_request_send(
                uid,
                msg_to_user=user,
                msg_to_group="",
                msg_subject=msg_subjet,
                msg_body=msg_body,
                ln=ln)
            infos.append(CFG_WEBSESSION_INFO_MESSAGES["MEMBER_REJECTED"])
            body = perform_request_manage_member(uid,
                                                 grpID,
                                                 infos=infos,
                                                 warnings=warnings,
                                                 ln=ln)
    return body