示例#1
0
def member_list(context, data_dict):
    check_access('member_list', context, data_dict)

    # Copy from CKAN member_list:
    model = context['model']

    group = model.Group.get(_get_or_bust(data_dict, 'id'))
    if not group:
        raise NotFound

    obj_type = data_dict.get('object_type', None)
    capacity = data_dict.get('capacity', None)

    # User must be able to update the group to remove a member from it
    check_access('group_show', context, data_dict)

    q = model.Session.query(model.Member).\
        filter(model.Member.group_id == group.id).\
        filter(model.Member.state == "active")

    if obj_type:
        q = q.filter(model.Member.table_name == obj_type)
    if capacity:
        q = q.filter(model.Member.capacity == capacity)

    trans = new_authz.roles_trans()

    def translated_capacity(capacity):
        try:
            return _Capacity(trans[capacity], capacity) # Etsin modification
        except KeyError:
            return capacity

    return [(m.table_id, m.table_name, translated_capacity(m.capacity))
            for m in q.all()]
示例#2
0
def member_list(context, data_dict=None):
    '''Return the members of a group.

    Modified copy of the original ckan member_list action to also return 
    the non-translated capacity (role)

    :rtype: list of (id, type, translated capacity, capacity ) tuples

    '''
    model = context['model']

    group = model.Group.get(_get_or_bust(data_dict, 'id'))
    if not group:
        raise NotFound

    obj_type = data_dict.get('object_type', None)
    capacity = data_dict.get('capacity', None)
    show_user_info = data_dict.get('user_info', False)
    q_term = data_dict.get('q', None)

    # User must be able to update the group to remove a member from it
    _check_access('group_show', context, data_dict)
    
    q = model.Session.query(model.Member, model.User).\
        filter(model.Member.table_id==model.User.id).\
        filter(model.Member.group_id == group.id).\
        filter(model.Member.state == "active")
    
    if q_term and q_term != '':
        q = q.filter(sqlalchemy.or_(
                     model.User.fullname.ilike('%' + q_term + '%'),
                     model.User.name.ilike('%' + q_term + '%')
                     )
        )

    if obj_type:
        q = q.filter(model.Member.table_name == obj_type)
    if capacity:
        q = q.filter(model.Member.capacity == capacity)

    trans = new_authz.roles_trans()

    def translated_capacity(capacity):
        try:
            return trans[capacity]
        except KeyError:
            return capacity
    if show_user_info:
        return [(m.table_id, m.table_name, translated_capacity(m.capacity), m.capacity, u.fullname if u.fullname else u.name)
            for m,u in q.all()]
    else:
        return [(m.table_id, m.table_name, translated_capacity(m.capacity), m.capacity)
            for m,u in q.all()]
示例#3
0
def member_list(context, data_dict=None):
    '''Return the members of a group.

    The user must have permission to 'get' the group.

    :param id: the id or name of the group
    :type id: string
    :param object_type: restrict the members returned to those of a given type,
      e.g. ``'user'`` or ``'package'`` (optional, default: ``None``)
    :type object_type: string
    :param capacity: restrict the members returned to those with a given
      capacity, e.g. ``'member'``, ``'editor'``, ``'admin'``, ``'public'``,
      ``'private'`` (optional, default: ``None``)
    :type capacity: string

    :rtype: list of (id, type, capacity) tuples

    :raises: :class:`ckan.logic.NotFound`: if the group doesn't exist

    '''
    model = context['model']

    group = model.Group.get(ckan.logic.get_or_bust(data_dict, 'id'))
    if not group:
        raise ckan.logic.NotFound

    obj_type = data_dict.get('object_type', None)
    capacity = data_dict.get('capacity', None)

    ckan.logic.check_access('member_list', context, data_dict)

    q = model.Session.query(model.Member).\
        filter(model.Member.group_id == group.id).\
        filter(model.Member.state == "active")

    if obj_type:
        q = q.filter(model.Member.table_name == obj_type)
    if capacity:
        q = q.filter(model.Member.capacity == capacity)

    trans = new_authz.roles_trans()

    def translated_capacity(capacity):
        try:
            return trans[capacity]
        except KeyError:
            return capacity

    return [(m.table_id, m.table_name, translated_capacity(m.capacity))
            for m in q.all()]
示例#4
0
def member_list(context, data_dict=None):
    '''Return the members of a group.

    The user must have permission to 'get' the group.

    :param id: the id or name of the group
    :type id: string
    :param object_type: restrict the members returned to those of a given type,
      e.g. ``'user'`` or ``'package'`` (optional, default: ``None``)
    :type object_type: string
    :param capacity: restrict the members returned to those with a given
      capacity, e.g. ``'member'``, ``'editor'``, ``'admin'``, ``'public'``,
      ``'private'`` (optional, default: ``None``)
    :type capacity: string

    :rtype: list of (id, type, capacity) tuples

    :raises: :class:`ckan.logic.NotFound`: if the group doesn't exist

    '''
    model = context['model']

    group = model.Group.get(ckan.logic.get_or_bust(data_dict, 'id'))
    if not group:
        raise ckan.logic.NotFound

    obj_type = data_dict.get('object_type', None)
    capacity = data_dict.get('capacity', None)

    ckan.logic.check_access('member_list', context, data_dict)

    q = model.Session.query(model.Member). \
        filter(model.Member.group_id == group.id). \
        filter(model.Member.state == "active")

    if obj_type:
        q = q.filter(model.Member.table_name == obj_type)
    if capacity:
        q = q.filter(model.Member.capacity == capacity)

    trans = new_authz.roles_trans()

    def translated_capacity(capacity):
        try:
            return trans[capacity]
        except KeyError:
            return capacity

    return [(m.table_id, m.table_name, translated_capacity(m.capacity))
            for m in q.all()]
示例#5
0
def user_invite(context, data_dict):
    '''Invite a new user.

    You must be authorized to create group members.

    :param email: the email of the user to be invited to the group
    :type email: string
    :param group_id: the id or name of the group
    :type group_id: string
    :param role: role of the user in the group. One of ``member``, ``editor``,
        or ``admin``
    :type role: string

    :returns: the newly created yser
    :rtype: dictionary
    '''
    toolkit.check_access('user_invite', context, data_dict)

    schema = context.get('schema',
                         logic.schema.default_user_invite_schema())
    data, errors = toolkit.navl_validate(data_dict, schema, context)
    if errors:
        raise toolkit.ValidationError(errors)

    model = context['model']
    group = model.Group.get(data['group_id'])
    if not group:
        raise toolkit.ObjectNotFound()

    name = logic.action.create._get_random_username_from_email(data['email'])
    password = str(random.SystemRandom().random())
    data['name'] = name
    data['password'] = password
    data['state'] = model.State.PENDING
    user_dict = toolkit.get_action('user_create')(context, data)
    user = model.User.get(user_dict['id'])
    member_dict = {
        'username': user.id,
        'id': data['group_id'],
        'role': data['role']
    }
    toolkit.get_action('group_member_create')(context, member_dict)

    if group.is_organization:
        group_dict = toolkit.get_action('organization_show')(context,
            {'id': data['group_id']})
    else:
        group_dict = toolkit.get_action('group_show')(context,
            {'id': data['group_id']})

    mailer.create_reset_key(user)

    # Email body
    group_type = (toolkit._('organization') if group_dict['is_organization']
                  else toolkit._('group'))
    role = data['role']
    extra_vars = {
        'reset_link': mailer.get_reset_link(user),
        'site_title': config.get('ckan.site_title'),
        'site_url': config.get('ckan.site_url'),
        'user_name': user.name,
        'role_name': authz.roles_trans().get(role, toolkit._(role)),
        'group_type': group_type,
        'group_title': group_dict.get('title'),
    }

    # NOTE: This template is translated
    body = render_jinja2('emails/invite_user.txt', extra_vars)
    subject = toolkit._('Invite for {site_title}').format(
        site_title=config.get('ckan.site_title'))

    mailer.mail_user(user, subject, body)

    return model_dictize.user_dictize(user, context)