Exemple #1
0
def is_accounting_admin(user):
    accounting_privilege = Role.get_privilege(privileges.ACCOUNTING_ADMIN)
    if accounting_privilege is None:
        return False
    try:
        return user.prbac_role.has_privilege(accounting_privilege)
    except (AttributeError, UserRole.DoesNotExist):
        return False
Exemple #2
0
def is_accounting_admin(user):
    accounting_privilege = Role.get_privilege(privileges.ACCOUNTING_ADMIN)
    if accounting_privilege is None:
        return False
    try:
        return user.prbac_role.has_privilege(accounting_privilege)
    except (AttributeError, UserRole.DoesNotExist):
        return False
Exemple #3
0
def domain_has_privilege(domain, privilege_slug, **assignment):
    from corehq.apps.accounting.models import Subscription
    try:
        plan_version = Subscription.get_subscribed_plan_by_domain(domain)[0]
        privilege = Role.get_privilege(privilege_slug, assignment)
        if privilege is None:
            return False
        if plan_version.role.has_privilege(privilege):
            return True
    except ProductPlanNotFoundError:
        return False
    except AccountingError:
        pass
    return False
Exemple #4
0
def domain_has_privilege(domain, privilege_slug, **assignment):
    from corehq.apps.accounting.models import Subscription
    try:
        plan_version = Subscription.get_subscribed_plan_by_domain(domain)[0]
        privilege = Role.get_privilege(privilege_slug, assignment)
        if privilege is None:
            return False
        if plan_version.role.has_privilege(privilege):
            return True
    except ProductPlanNotFoundError:
        return False
    except AccountingError:
        pass
    return False
Exemple #5
0
 def apply_prbac(cls, request):
     if hasattr(request, 'domain'):
         try:
             plan_version, subscription = Subscription.get_subscribed_plan_by_domain(request.domain)
             request.role = plan_version.role
             request.plan = plan_version
             request.subscription = subscription
             return None
         except AccountingError:
             pass
     privilege = Role.get_privilege('community_plan_v0')
     if privilege is not None:
         request.role = privilege.role
     else:
         request.role = Role()  # A fresh Role() has no privileges
Exemple #6
0
 def apply_prbac(cls, request):
     if hasattr(request, 'domain'):
         try:
             plan_version, subscription = Subscription.get_subscribed_plan_by_domain(request.domain)
             request.role = plan_version.role
             request.plan = plan_version
             request.subscription = subscription
             return None
         except AccountingError:
             pass
     privilege = Role.get_privilege('community_plan_v1')
     if privilege is not None:
         request.role = privilege.role
     else:
         request.role = Role()  # A fresh Role() has no privileges
Exemple #7
0
def has_privilege(request, slug, **assignment):
    """
    Returns true if the request has the privilege specified by slug,
    otherwise false
    """
    privilege = Role.get_privilege(slug, assignment)
    if privilege is None:
        return False

    if hasattr(request, 'role'):
        if request.role.has_privilege(privilege):
            return True

    if hasattr(request, 'user') and hasattr(request.user, 'prbac_role'):
        try:
            request.user.prbac_role
        except UserRole.DoesNotExist:
            return False
        return request.user.prbac_role.has_privilege(privilege)

    return False
Exemple #8
0
def has_privilege(request, slug, **assignment):
    """
    Returns true if the request has the privilege specified by slug,
    otherwise false
    """
    privilege = Role.get_privilege(slug, assignment)
    if privilege is None:
        return False

    if hasattr(request, 'role'):
        if request.role.has_privilege(privilege):
            return True

    if hasattr(request, 'user') and hasattr(request.user, 'prbac_role'):
        try:
            request.user.prbac_role
        except UserRole.DoesNotExist:
            return False
        return request.user.prbac_role.has_privilege(privilege)

    return False