Ejemplo n.º 1
0
def _permission_name(entity, which_perm):
    """Find a permission name.

    Attempt to locate a permission in :data:`robottelo.constants.PERMISSIONS`.
    For example, return 'view_architectures' if ``entity`` is ``Architecture``
    and ``which_perm`` is 'read'.

    :param entity: A ``nailgun.entity_mixins.Entity`` subclass.
    :param str which_perm: Either the word "create", "read", "update" or
        "delete".
    :raise: ``LookupError`` if a relevant permission cannot be found, or if
        multiple results are found.
    """
    pattern = {
        'create': '^create_',
        'delete': '^destroy_',
        'read': '^view_',
        'update': '^edit_',
    }[which_perm]
    perm_names = []
    permissions = (PERMISSIONS.get(entity.__name__)
                   or PERMISSIONS.get('Katello::{0}'.format(entity.__name__)))
    for permission in permissions:
        match = re.match(pattern, permission)
        if match is not None:
            perm_names.append(permission)
    if len(perm_names) != 1:
        raise LookupError(
            'Could not find the requested permission. Found: {0}'.format(
                perm_names))
    return perm_names[0]
Ejemplo n.º 2
0
def _permission_name(entity, which_perm):
    """Find a permission name.

    Attempt to locate a permission in :data:`robottelo.constants.PERMISSIONS`.
    For example, return 'view_architectures' if ``entity`` is ``Architecture``
    and ``which_perm`` is 'read'.

    :param entity: A ``nailgun.entity_mixins.Entity`` subclass.
    :param str which_perm: Either the word "create", "read", "update" or
        "delete".
    :raise: ``LookupError`` if a relevant permission cannot be found, or if
        multiple results are found.
    """
    pattern = {
        'create': '^create_',
        'delete': '^destroy_',
        'read': '^view_',
        'update': '^edit_',
    }[which_perm]
    perm_names = []
    permissions = (PERMISSIONS.get(entity.__name__) or
                   PERMISSIONS.get('Katello::{0}'.format(entity.__name__)))
    for permission in permissions:
        match = re.match(pattern, permission)
        if match is not None:
            perm_names.append(permission)
    if len(perm_names) != 1:
        raise LookupError(
            'Could not find the requested permission. Found: {0}'
            .format(perm_names)
        )
    return perm_names[0]