# Path: tutelary/exceptions.py
# class RoleVariableException(TutelaryException):
#     """Exception raised for missing or illegal variable substitutions for
#     permissions roles.
# 
#     """
#     def __init__(self, msg):
#         super().__init__("illegal role variables: " + msg)


import json
import re
import tutelary.engine as engine
from django.db import models
from django.conf import settings
from django.db.models.signals import pre_delete
from django.dispatch import receiver
from django.core.exceptions import ObjectDoesNotExist
from django.core.cache import cache
from audit_log.models.managers import AuditLog
from tutelary.exceptions import RoleVariableException
    """

    policy = models.ForeignKey('Policy', on_delete=models.PROTECT)

    role = models.ForeignKey('Role', on_delete=models.CASCADE)

    index = models.IntegerField()
    """Integer index used to order the sequence of policies composing a
    role.

    """

    class Meta:
        ordering = ['index']

    def __str__(self):
        return "role={} policy={}".format(self.role.name, self.policy.name)


class RoleManager(models.Manager):
    """Custom manager for roles: ensures that role variable assignments
    cover all the variables used in the role's policies.

    """
    def create(self, name, policies=None, variables=None):
        policies = policies or []
        variables = variables or {}
        role = super().create(name=name, variables=variables)
        variable_names = set().union(*[p.variable_names() for p in policies])
        if not variable_names.issubset(variables.keys()):

# Path: tutelary/exceptions.py
# class RoleVariableException(TutelaryException):
#     """Exception raised for missing or illegal variable substitutions for
#     permissions roles.
# 
#     """
#     def __init__(self, msg):
#         super().__init__("illegal role variables: " + msg)


# Path: tutelary/models.py
import json
import re
import tutelary.engine as engine
from django.db import models
from django.conf import settings
from django.db.models.signals import pre_delete
from django.dispatch import receiver
from django.core.exceptions import ObjectDoesNotExist
from django.core.cache import cache
from audit_log.models.managers import AuditLog
from tutelary.exceptions import RoleVariableException

    """

    policy = models.ForeignKey('Policy', on_delete=models.PROTECT)

    role = models.ForeignKey('Role', on_delete=models.CASCADE)

    index = models.IntegerField()
    """Integer index used to order the sequence of policies composing a
    role.

    """

    class Meta:
        ordering = ['index']

    def __str__(self):
        return "role={} policy={}".format(self.role.name, self.policy.name)


class RoleManager(models.Manager):
    """Custom manager for roles: ensures that role variable assignments
    cover all the variables used in the role's policies.

    """
    def create(self, name, policies=None, variables=None):
        policies = policies or []
        variables = variables or {}
        role = super().create(name=name, variables=variables)
        variable_names = set().union(*[p.variable_names() for p in policies])
        if not variable_names.issubset(variables.keys()):

            raise RoleVariableException("missing variable in role definition")