Example #1
0
def replace_permission_fields(*, data, action):
    if data.permission_data:
        for index, permission in enumerate(data.permission_data):
            change_object = get_state_change_object(
                permission["permission_type"])
            context = {"context": change_object.all_context_instances(action)}
            for field_name, field_value in permission.items():
                result = replacer(field_value, context)
                result = result if result != ... else field_value
                data.permission_data[index][field_name] = result
Example #2
0
def validate_permission_data(model_instance, permission_data):

    if not permission_data: return True, None

    for permission in permission_data:

        state_change_object = get_state_change_object(
            permission["permission_type"])
        if model_instance.__class__ not in state_change_object.get_allowable_targets(
        ):
            return False, f"Permission type {permission['permission_type']} cannot be set on {model_instance}"

        if "permission_roles" not in permission and "permission_actors" not in permission:
            return False, f"Must supply either roles or actors to permission {permission['permission_type']}"

    return True, None
Example #3
0
def condition_to_text(condition_change_object):
    """Written to be understood by people setting conditions on permissions, so typically we're adding the second
    part of a sentence that begins 'X has permission to Y...'"""

    # For now, we ignore the configuration in condition_action, and build our text from the permissions_actions

    phrases = []
    permissions = condition_change_object.condition_data.get(
        "permission_data", [])
    for permission in permissions:

        roles_and_actors_string = roles_and_actors({
            "roles":
            permission.get("permission_roles", []),
            "actors":
            permission.get("permission_actors", [])
        })

        change_type = get_state_change_object(permission["permission_type"])

        if hasattr(change_type,
                   "rejects_condition") and change_type.rejects_condition:
            phrases.append(roles_and_actors_string + " does not " +
                           change_type._description().verb)
        else:
            phrases.append(roles_and_actors_string + " " +
                           change_type._description().verb)

    text = "on the condition that "

    if len(phrases) == 0:
        # This means that the default permissions will be used, but this function has no way of knowing what
        # the default permission is, so we must be vague.  Condition action should always be
        # SetConditionOnActionStateChange so get_condition_verb should always be valid.
        return text + "the governors and/or owners " + condition_change_object.get_condition_verb(
        )

    if len(phrases) == 1:
        return text + phrases[0]

    if len(phrases) == 2:
        return text + phrases[0] + " and " + phrases[1]

    if len(phrases) > 2:
        return text + ", ".join(phrases[:-1]) + " and " + phrases[-1]
Example #4
0
    def validate(self, actor, target):

        permission = get_state_change_object(self.change_type)

        # check that target is a valid class for the permission to be set on
        if target.__class__ not in permission.get_settable_classes():
            settable_classes_str = ", ".join(
                [str(option) for option in permission.get_settable_classes()])
            raise ValidationError(
                f"This kind of permission cannot be set on target {target} of class "
                + f"{target.__class__}, must be {settable_classes_str}")

        # validate condition data
        if self.condition_data:
            for condition in self.condition_data:
                is_valid, message = validate_condition(
                    condition["condition_type"], condition["condition_data"],
                    condition["permission_data"], target)
                if not is_valid:
                    raise ValidationError(message)
Example #5
0
 def is_conditionally_foundational(self, action):
     """Some state changes are only foundational in certain conditions. Those state changes override this
     method to apply logic and determine whether a specific instance is foundational or not."""
     from concord.utils.lookups import get_state_change_object
     change_object = get_state_change_object(self.change_type)
     return action.change.is_foundational
Example #6
0
 def get_state_change_object(self):
     """Get the state change object associated with the change_type of the permission."""
     return get_state_change_object(self.change_type)
Example #7
0
def get_verb_given_permission_type(permission_type):
    """Given a permission type, get the verb specified by the corresponding change object."""
    state_change_object = get_state_change_object(permission_type)
    return state_change_object.change_description(capitalize=False)