Exemple #1
0
def member_request_show(context, data_dict):
    ''' Create new member request. User is taken from context.

    :param member: id of the member object
    :type member: string

    :param fetch_user: fetch related user data
    :type fetch_user: boolean
    '''
    check_access('member_request_show', context, data_dict)

    model = context['model']
    member_id = data_dict.get("member")
    fetch_user = data_dict.get("fetch_user", False)
    member = model.Session.query(model.Member).get(member_id)

    if not member.group.is_organization:
        raise NotFound

    data = model_dictize.member_dictize(member, context)

    if fetch_user:
        member_user = model.Session.query(model.User).get(member.table_id)
        data['user'] = model_dictize.user_dictize(member_user, context)

    return data
Exemple #2
0
def _process_request(context, organization_id, member, status):
    ''' Cancel a member request or existing membership.
        Delete from database the member request (if existing) and set delete state in member table
    :param member: id of the member
    :type member: string
    '''
    user = context.get("user")

    # Logical delete on table member
    member.state = 'deleted'
    # Fetch the newest member_request associated to this membership (sort by
    # last modified field)
    member_request = model.Session.query(MemberRequest).filter(
        MemberRequest.membership_id == member.id).order_by('request_date desc').limit(1).first()

    # BFW: Create a new instance every time membership status is changed
    message = u'MemberRequest cancelled by own user'
    locale = get_safe_locale()
    mrequest_date = func.now()
    if member_request is not None and member_request.status == status:
        locale = member_request.language
        mrequest_date = member_request.request_date

    member_request = MemberRequest(membership_id=member.id, role=member.capacity, status="cancel", request_date=mrequest_date,
                                   language=locale, handling_date=func.now(), handled_by=c.userobj.name, message=message)
    model.Session.add(member_request)

    revision = model.repo.new_revision()
    revision.author = user
    revision.message = u'Member request deleted by user'

    member.save()
    model.repo.commit()

    return model_dictize.member_dictize(member, context)
Exemple #3
0
def _member_list_dictize(obj_list, context, sort_key=lambda x: x['group_id'], reverse=False):
    """ Helper to convert member list to dictionary """
    result_list = []
    for obj in obj_list:
        member_dict = model_dictize.member_dictize(obj, context)
        user = model.Session.query(model.User).get(obj.table_id)

        if obj.group is not None:
            member_dict['group_name'] = obj.group.name
        member_dict['role'] = obj.capacity
        # Member request must always exist since state is pending. Fetch just
        # the latest
        member_request = model.Session.query(MemberRequest).filter(MemberRequest.membership_id == obj.id)\
            .filter(MemberRequest.status == 'pending').order_by('request_date desc').limit(1).first()
        # This should never happen but..
        my_date = ""
        if member_request is not None:
            my_date = member_request.request_date.strftime("%d - %b - %Y")

        member_dict['request_date'] = my_date
        member_dict['mid'] = obj.id

        if user.email is not None:
            member_dict['user_name'] = user.name
        member_dict['user_email'] = user.email
        result_list.append(member_dict)
    return sorted(result_list, key=sort_key, reverse=reverse)
Exemple #4
0
def member_request_show(context, data_dict):
    ''' Create new member request. User is taken from context.

    :param member: id of the member object
    :type member: string

    :param fetch_user: fetch related user data
    :type fetch_user: boolean
    '''
    check_access('member_request_show', context, data_dict)

    model = context['model']
    member_id = data_dict.get("member")
    fetch_user = data_dict.get("fetch_user", False)
    member = model.Session.query(model.Member).get(member_id)

    if not member.group.is_organization:
        raise NotFound

    data = model_dictize.member_dictize(member, context)

    if fetch_user:
        member_user = model.Session.query(model.User).get(member.table_id)
        data['user'] = model_dictize.user_dictize(member_user, context)

    return data
Exemple #5
0
def _process_request(context, member, action):
    user = context["user"]

    approve = action == 'approve'  # else 'reject' or 'cancel'

    state = "active" if approve else "deleted"

    if not member or not member.group.is_organization:
        raise NotFound

    member.state = state
    revision = model.repo.new_revision()
    revision.author = user

    if 'message' in context:
        revision.message = context['message']
    else:
        revision.message = 'Processed member request'

    member.save()
    model.repo.commit()

    member_user = model.Session.query(model.User).get(member.table_id)
    admin_user = model.User.get(user)

    locale = member.extras.get('locale', None) or _get_default_locale()
    _log_process(member_user, member.group.display_name, approve, admin_user)
    _mail_process_status(locale, member_user, approve, member.group.display_name, member.capacity)

    return model_dictize.member_dictize(member, context)
Exemple #6
0
def _member_list_dictize(obj_list,
                         context,
                         sort_key=lambda x: x['group_id'],
                         reverse=False):
    """ Helper to convert member list to dictionary """
    result_list = []
    for obj in obj_list:
        member_dict = model_dictize.member_dictize(obj, context)
        user = model.Session.query(model.User).get(obj.table_id)

        member_dict['group_name'] = obj.group.name
        member_dict['role'] = obj.capacity
        # Member request must always exist since state is pending. Fetch just
        # the latest
        member_request = model.Session.query(MemberRequest) \
            .filter(MemberRequest.membership_id == obj.id) \
            .filter(MemberRequest.status == 'pending') \
            .order_by(desc(MemberRequest.request_date)).limit(1).first()
        # This should never happen but..
        my_date = ""
        if member_request is not None:
            my_date = member_request.request_date.strftime("%d - %b - %Y")

        member_dict['request_date'] = my_date
        member_dict['mid'] = obj.id

        member_dict['user_name'] = user.name
        result_list.append(member_dict)
    return sorted(result_list, key=sort_key, reverse=reverse)
Exemple #7
0
def _process_request(context, member, action, new_role=None):
    user = context["user"]

    approve = action == 'approve'  # else 'reject' or 'cancel'

    state = "active" if approve else "deleted"

    if not member or not member.group.is_organization:
        raise NotFound

    member.state = state
    if new_role:
        member.capacity = new_role
    revision = model.repo.new_revision()
    revision.author = user

    if 'message' in context:
        revision.message = context['message']
    else:
        revision.message = 'Processed member request'

    member.save()
    model.repo.commit()

    member_user = model.Session.query(model.User).get(member.table_id)
    admin_user = model.User.get(user)

    locale = member.extras.get('locale', None) or _get_default_locale()
    _log_process(member_user, member.group.display_name, approve, admin_user)
    _mail_process_status(locale, member_user, approve,
                         member.group.display_name, member.capacity)

    return model_dictize.member_dictize(member, context)
Exemple #8
0
def member_create(context, data_dict=None):
    '''Make an object (e.g. a user, dataset or group) a member of a group.

    If the object is already a member of the group then the capacity of the
    membership will be updated.

    You must be authorized to edit the group.

    :param id: the id or name of the group to add the object to
    :type id: string
    :param object: the id or name of the object to add
    :type object: string
    :param object_type: the type of the object being added, e.g. ``'package'``
        or ``'user'``
    :type object_type: string
    :param capacity: the capacity of the membership
    :type capacity: string

    :returns: the newly created (or updated) membership
    :rtype: dictionary

    '''
    model = context['model']
    user = context['user']

    rev = model.repo.new_revision()
    rev.author = user
    if 'message' in context:
        rev.message = context['message']
    else:
        rev.message = _(u'REST API: Create member object %s') % data_dict.get(
            "name", "")

    group = model.Group.get(data_dict.get('id', ''))
    obj_id, obj_type, capacity = _get_or_bust(
        data_dict, ['object', 'object_type', 'capacity'])

    # User must be able to update the group to add a member to it
    _check_access('group_update', context, data_dict)

    # Look up existing, in case it exists
    member = model.Session.query(model.Member).\
            filter(model.Member.table_name == obj_type).\
            filter(model.Member.table_id == obj_id).\
            filter(model.Member.group_id == group.id).\
            filter(model.Member.state == "active").first()
    if member:
        member.capacity = capacity
    else:
        member = model.Member(table_name=obj_type,
                              table_id=obj_id,
                              group_id=group.id,
                              state='active',
                              capacity=capacity)

    model.Session.add(member)
    model.repo.commit()

    return model_dictize.member_dictize(member, context)
Exemple #9
0
def member_request_create(context, data_dict):
    ''' Create new member request. User is taken from context.

    :param group: name of the group or organization
    :type group: string
    '''
    check_access('member_request_create', context, data_dict)
    member, _changed = _create_member_request(context, data_dict)
    return model_dictize.member_dictize(member, context)
Exemple #10
0
def member_request_create(context, data_dict):
    ''' Create new member request. User is taken from context.
    Sysadmins should not be able to create "member" requests since they have full access to all organizations
    :param group: name of the group or organization
    :type group: string
    '''
    logic.check_access('member_request_create', context, data_dict)
    member = _create_member_request(context, data_dict)
    return model_dictize.member_dictize(member, context)
Exemple #11
0
def member_request_create(context, data_dict):
    ''' Create new member request. User is taken from context.

    :param group: name of the group or organization
    :type group: string
    '''
    check_access('member_request_create', context, data_dict)
    member, _changed = _create_member_request(context, data_dict)
    return model_dictize.member_dictize(member, context)
def member_request_create(context, data_dict):
    ''' Create new member request. User is taken from context.
    Sysadmins should not be able to create "member" requests since they have full access to all organizations
    :param group: name of the group or organization
    :type group: string
    '''
    logic.check_access('member_request_create', context, data_dict)
    member = _create_member_request(context, data_dict)
    return model_dictize.member_dictize(member, context)
Exemple #13
0
def member_create(context, data_dict=None):
    '''Make an object (e.g. a user, dataset or group) a member of a group.

    If the object is already a member of the group then the capacity of the
    membership will be updated.

    You must be authorized to edit the group.

    :param id: the id or name of the group to add the object to
    :type id: string
    :param object: the id or name of the object to add
    :type object: string
    :param object_type: the type of the object being added, e.g. ``'package'``
        or ``'user'``
    :type object_type: string
    :param capacity: the capacity of the membership
    :type capacity: string

    :returns: the newly created (or updated) membership
    :rtype: dictionary

    '''
    model = context['model']
    user = context['user']

    rev = model.repo.new_revision()
    rev.author = user
    if 'message' in context:
        rev.message = context['message']
    else:
        rev.message = _(u'REST API: Create member object %s') % data_dict.get("name", "")

    group = model.Group.get(data_dict.get('id', ''))
    obj_id, obj_type, capacity = _get_or_bust(data_dict, ['object', 'object_type', 'capacity'])

    # User must be able to update the group to add a member to it
    _check_access('group_update', context, data_dict)

    # Look up existing, in case it exists
    member = model.Session.query(model.Member).\
            filter(model.Member.table_name == obj_type).\
            filter(model.Member.table_id == obj_id).\
            filter(model.Member.group_id == group.id).\
            filter(model.Member.state == "active").first()
    if member:
        member.capacity = capacity
    else:
        member = model.Member(table_name = obj_type,
                              table_id = obj_id,
                              group_id = group.id,
                              state = 'active',
                              capacity=capacity)

    model.Session.add(member)
    model.repo.commit()

    return model_dictize.member_dictize(member, context)
Exemple #14
0
def _member_list_dictize(obj_list, context, sort_key=lambda x: x['group_id'], reverse=False):
    """ Helper to convert member list to dictionary """
    result_list = []
    for obj in obj_list:
        member_dict = model_dictize.member_dictize(obj, context)
        member_dict['group_name'] = obj.group.name
        user = model.Session.query(model.User).get(obj.table_id)
        member_dict['user_name'] = user.name
        result_list.append(member_dict)
    return sorted(result_list, key=sort_key, reverse=reverse)
Exemple #15
0
def _member_list_dictize(obj_list, context, sort_key=lambda x: x['group_id'], reverse=False):
    """ Helper to convert member list to dictionary """
    result_list = []
    for obj in obj_list:
        member_dict = model_dictize.member_dictize(obj, context)
        member_dict['group_name'] = obj.group.name
        user = model.Session.query(model.User).get(obj.table_id)
        member_dict['user_name'] = user.name
        result_list.append(member_dict)
    return sorted(result_list, key=sort_key, reverse=reverse)
Exemple #16
0
def member_create(context, data_dict=None):
    """
    Add an object as a member to a group. If the membership already exists
    and is active then the capacity will be overwritten in case it has
    changed.

    context:
        model - The CKAN model module
        user  - The name of the current user

    data_dict:
        group - The ID of the group to which we want to add a new object
        object - The ID of the object being added as a member
        object_type - The name of the type being added, all lowercase,
                      e.g. package, or user
        capacity - The capacity with which to add this object
    """
    model = context['model']
    user = context['user']
    group = context['group']

    rev = model.repo.new_revision()
    rev.author = user
    if 'message' in context:
        rev.message = context['message']
    else:
        rev.message = _(u'REST API: Create member object %s') % data_dict.get(
            "name", "")

    obj_id = data_dict['object']
    obj_type = data_dict['object_type']
    capacity = data_dict['capacity']

    # User must be able to update the group to add a member to it
    check_access('group_update', context, data_dict)

    # Look up existing, in case it exists
    member = model.Session.query(model.Member).\
            filter(model.Member.table_name == obj_type).\
            filter(model.Member.table_id == obj_id).\
            filter(model.Member.group_id == group.id).\
            filter(model.Member.state    == "active").first()
    if member:
        member.capacity = capacity
    else:
        member = model.Member(table_name=obj_type,
                              table_id=obj_id,
                              group_id=group.id,
                              capacity=capacity)

    model.Session.add(member)
    model.repo.commit()

    return model_dictize.member_dictize(member, context)
def member_request_create(context, data_dict):
    """
    Create new member request. User is taken from context.
    Sysadmins should not be able to create "member" requests since they have full access to all organizations
    :param context: context object
    :param data_dict: data dictionary
    :type context: dict
    :type data_dict: dict
    """
    logic.check_access('member_request_create', context, data_dict)
    member = _create_member_request(context, data_dict)
    return model_dictize.member_dictize(member, context)
Exemple #18
0
def member_create(context, data_dict=None):
    """
    Add an object as a member to a group. If the membership already exists
    and is active then the capacity will be overwritten in case it has
    changed.

    context:
        model - The CKAN model module
        user  - The name of the current user

    data_dict:
        id - The ID of the group to which we want to add a new object
        object - The ID of the object being added as a member
        object_type - The name of the type being added, all lowercase,
                      e.g. package, or user
        capacity - The capacity with which to add this object
    """
    model = context["model"]
    user = context["user"]

    rev = model.repo.new_revision()
    rev.author = user
    if "message" in context:
        rev.message = context["message"]
    else:
        rev.message = _(u"REST API: Create member object %s") % data_dict.get("name", "")

    group = model.Group.get(data_dict.get("id", ""))
    obj_id = data_dict["object"]
    obj_type = data_dict["object_type"]
    capacity = data_dict["capacity"]

    # User must be able to update the group to add a member to it
    check_access("group_update", context, data_dict)

    # Look up existing, in case it exists
    member = (
        model.Session.query(model.Member)
        .filter(model.Member.table_name == obj_type)
        .filter(model.Member.table_id == obj_id)
        .filter(model.Member.group_id == group.id)
        .filter(model.Member.state == "active")
        .first()
    )
    if member:
        member.capacity = capacity
    else:
        member = model.Member(table_name=obj_type, table_id=obj_id, group_id=group.id, capacity=capacity)

    model.Session.add(member)
    model.repo.commit()

    return model_dictize.member_dictize(member, context)
Exemple #19
0
def user_role_show(context, data_dict):
    model = context['model']
    session = context['session']
    user_id = data_dict['user_id']
    organization_id = data_dict['organization_id']

    user = model.User.get(user_id)
    organization = model.Group.get(organization_id)

    member = session.query(model.Member)\
        .filter(model.Member.table_name == 'user')\
        .filter(model.Member.table_id == user.id)\
        .filter(model.Member.group_id == organization.id)\
        .filter(model.Member.state == 'active').first()
    if member:
        member_dict = member_dictize(member, context)
        member_dict['role'] = member_dict.pop('capacity')
        member_dict['organization_id'] = member_dict.pop('group_id')
        member_dict['user_id'] = member_dict.pop('table_id')
        member_dict.pop('table_name')
        return member_dict
Exemple #20
0
def user_role_show(context, data_dict):
    model = context["model"]
    session = context["session"]
    user_id = data_dict["user_id"]
    organization_id = data_dict["organization_id"]

    user = model.User.get(user_id)
    organization = model.Group.get(organization_id)

    member = (
        session.query(model.Member)
        .filter(model.Member.table_name == "user")
        .filter(model.Member.table_id == user.id)
        .filter(model.Member.group_id == organization.id)
        .filter(model.Member.state == "active")
        .first()
    )
    if member:
        member_dict = member_dictize(member, context)
        member_dict["role"] = member_dict.pop("capacity")
        member_dict["organization_id"] = member_dict.pop("group_id")
        member_dict["user_id"] = member_dict.pop("table_id")
        member_dict.pop("table_name")
        return member_dict
Exemple #21
0
def member_create(context, data_dict=None):
    '''Make an object (e.g. a user, dataset or group) a member of a group.

    If the object is already a member of the group then the capacity of the
    membership will be updated.

    You must be authorized to edit the group.

    :param id: the id or name of the group to add the object to
    :type id: string
    :param object: the id or name of the object to add
    :type object: string
    :param object_type: the type of the object being added, e.g. ``'package'``
        or ``'user'``
    :type object_type: string
    :param capacity: the capacity of the membership
    :type capacity: string

    :returns: the newly created (or updated) membership
    :rtype: dictionary

    '''
    model = context['model']
    user = context['user']

    rev = model.repo.new_revision()
    rev.author = user
    if 'message' in context:
        rev.message = context['message']
    else:
        rev.message = _(u'REST API: Create member object %s') % data_dict.get(
            'name', '')

    group_id, obj_id, obj_type, capacity = _get_or_bust(
        data_dict, ['id', 'object', 'object_type', 'capacity'])

    group = model.Group.get(group_id)
    if not group:
        raise NotFound('Group was not found.')

    obj_class = ckan.logic.model_name_to_class(model, obj_type)
    obj = obj_class.get(obj_id)
    if not obj:
        raise NotFound('%s was not found.' % obj_type.title())

    _check_access('member_create', context, data_dict)

    # Look up existing, in case it exists
    member = model.Session.query(model.Member).\
            filter(model.Member.table_name == obj_type).\
            filter(model.Member.table_id == obj.id).\
            filter(model.Member.group_id == group.id).\
            filter(model.Member.state == 'active').first()
    if not member:
        member = model.Member(table_name=obj_type,
                              table_id=obj.id,
                              group_id=group.id,
                              state='active')

    member.capacity = capacity

    model.Session.add(member)
    model.repo.commit()

    return model_dictize.member_dictize(member, context)
Exemple #22
0
def member_create(context, data_dict=None):
    """Make an object (e.g. a user, dataset or group) a member of a group.

    If the object is already a member of the group then the capacity of the
    membership will be updated.

    You must be authorized to edit the group.

    :param id: the id or name of the group to add the object to
    :type id: string
    :param object: the id or name of the object to add
    :type object: string
    :param object_type: the type of the object being added, e.g. ``'package'``
        or ``'user'``
    :type object_type: string
    :param capacity: the capacity of the membership
    :type capacity: string

    :returns: the newly created (or updated) membership
    :rtype: dictionary

    """
    model = context["model"]
    user = context["user"]

    rev = model.repo.new_revision()
    rev.author = user
    if "message" in context:
        rev.message = context["message"]
    else:
        rev.message = _(u"REST API: Create member object %s") % data_dict.get("name", "")

    group_id, obj_id, obj_type, capacity = _get_or_bust(data_dict, ["id", "object", "object_type", "capacity"])

    group = model.Group.get(group_id)
    if not group:
        raise NotFound("Group was not found.")

    obj_class = ckan.logic.model_name_to_class(model, obj_type)
    obj = obj_class.get(obj_id)
    if not obj:
        raise NotFound("%s was not found." % obj_type.title())

    _check_access("member_create", context, data_dict)

    # Look up existing, in case it exists
    member = (
        model.Session.query(model.Member)
        .filter(model.Member.table_name == obj_type)
        .filter(model.Member.table_id == obj.id)
        .filter(model.Member.group_id == group.id)
        .filter(model.Member.state == "active")
        .first()
    )
    if not member:
        member = model.Member(table_name=obj_type, table_id=obj.id, group_id=group.id, state="active")

    member.capacity = capacity

    model.Session.add(member)
    model.repo.commit()

    return model_dictize.member_dictize(member, context)
Exemple #23
0
def member_create(context, data_dict=None):
    '''Make an object (e.g. a user, dataset or group) a member of a group.

    If the object is already a member of the group then the capacity of the
    membership will be updated.

    You must be authorized to edit the group.

    :param id: the id or name of the group to add the object to
    :type id: string
    :param object: the id or name of the object to add
    :type object: string
    :param object_type: the type of the object being added, e.g. ``'package'``
        or ``'user'``
    :type object_type: string
    :param capacity: the capacity of the membership
    :type capacity: string

    :returns: the newly created (or updated) membership
    :rtype: dictionary

    '''
    model = context['model']
    user = context['user']

    rev = model.repo.new_revision()
    rev.author = user
    if 'message' in context:
        rev.message = context['message']
    else:
        rev.message = _(u'REST API: Create member object %s') \
            % data_dict.get('name', '')

    group_id, obj_id, obj_type, capacity = \
        _get_or_bust(data_dict, ['id', 'object', 'object_type', 'capacity'])

    group = model.Group.get(group_id)
    if not group:
        raise NotFound('Group was not found.')

    obj_class = ckan.logic.model_name_to_class(model, obj_type)
    obj = obj_class.get(obj_id)
    if not obj:
        raise NotFound('%s was not found.' % obj_type.title())

    _check_access('member_create', context, data_dict)

    # Look up existing, in case it exists
    member = model.Session.query(model.Member).\
        filter(model.Member.table_name == obj_type).\
        filter(model.Member.table_id == obj.id).\
        filter(model.Member.group_id == group.id).\
        filter(model.Member.state == 'active').first()
    if not member:
        member = model.Member(table_name=obj_type,
                              table_id=obj.id,
                              group_id=group.id,
                              state='active')

    member.capacity = capacity

    model.Session.add(member)
    model.repo.commit()

    return model_dictize.member_dictize(member, context)
def member_create(context, data_dict=None):
    '''
    Function is based on the code from the member_create CKAN function.

    Make an object (e.g. a user, dataset or group) a member of a group.

    If the object is already a member of the group then the capacity of the
    membership will be updated.

    You must be authorized to edit the group.

    :param id: the id or name of the group to add the object to
    :type id: string
    :param object: the id or name of the object to add
    :type object: string
    :param object_type: the type of the object being added, e.g. ``'package'``
        or ``'user'``
    :type object_type: string
    :param capacity: the capacity of the membership
    :type capacity: string

    :returns: the newly created (or updated) membership
    :rtype: dictionary

    '''
    model = context['model']
    user = context['user']

    rev = model.repo.new_revision()
    rev.author = user
    if 'message' in context:
        rev.message = context['message']
    else:
        rev.message = _(u'REST API: Create member object %s') \
            % data_dict.get('name', '')

    group_id, obj_id, obj_type, capacity = \
        _get_or_bust(data_dict, ['id', 'object', 'object_type', 'capacity'])

    group = model.Group.get(group_id)
    if not group:
        raise NotFound('Group was not found.')

    obj_class = ckan.logic.model_name_to_class(model, obj_type)
    obj = obj_class.get(obj_id)
    if not obj:
        raise NotFound('%s was not found.' % obj_type.title())

    _check_access('member_create', context, data_dict)

    # Look up existing, in case it exists
    member = model.Session.query(model.Member).\
        filter(model.Member.table_name == obj_type).\
        filter(model.Member.table_id == obj.id).\
        filter(model.Member.group_id == group.id).\
        filter(model.Member.state == 'active').first()
    if not member:
        member = model.Member(table_name=obj_type,
                              table_id=obj.id,
                              group_id=group.id,
                              state='active')

    member.capacity = capacity

    model.Session.add(member)
    model.repo.commit()
    # New code to add AuthMember objects
    if obj_type == 'user':
        role = AuthRole.get(data_dict.get('capacity', ''))
        if role is None:
            roles = [x for x in AuthRole.all() if x.org_member]
            roles.sort(key=lambda x: x.rank)
            lowest_role = roles[0]
            role = lowest_role

        existing_member = AuthMember.by_group_and_user_id(group_id=group.id,
                                                          user_id=obj.id)
        if not existing_member:
            AuthMember.create(group_id=group.id, user_id=obj_id, role=role)
        else:
            existing_member.role = role
            existing_member.save()

    return model_dictize.member_dictize(member, context)