import rules


def has_organization_role(user, org, roles):
    memberships = org.memberships.all().filter(user=user)
    if roles:
        memberships = memberships.filter(role__in=roles)
    return memberships.exists()


@rules.predicate
def is_organization_owner(user, org):
    return has_organization_role(user, org, ["owner"])


@rules.predicate
def is_organization_admin(user, org):
    return has_organization_role(user, org, ["owner", "admin"])


@rules.predicate
def is_organization_member(user, org):
    return has_organization_role(user, org, None)
