<reponame>SUNET/ni
# -*- coding: utf-8 -*-
# Generated by Django 1.11.24 on 2019-09-27 19:37
from __future__ import unicode_literals

from django.db import migrations
from vakt import Policy, Inquiry, ALLOW_ACCESS, DENY_ACCESS

import apps.noclook.vakt.rules as srirules
import apps.noclook.vakt.utils as sriutils
import uuid
import vakt.rules as vakt_rules


def forwards_func(apps, schema_editor):
    # add AuthzAction
    AuthzAction = apps.get_model('noclook', 'AuthzAction')
    list_aa, created = AuthzAction.objects.get_or_create(name="list")

    # get storage and guard
    storage, guard = sriutils.get_vakt_storage_and_guard()
    Context = apps.get_model('noclook', 'Context')
    DjPolicy = apps.get_model('djangovakt', 'Policy')

    # iterate over all the existent contexts to add
    # a list policy for each of them
    all_contexts = Context.objects.all()

    for context in all_contexts:
        # check if the policy exist first
        qs = DjPolicy.objects.filter(doc__actions__0__val=list_aa.name)
        qs = qs.filter(doc__context__module__elem__in=(context.name,))

        if not qs.exists():
            policy = Policy(
                uuid.uuid4(),
                actions=[vakt_rules.Eq(list_aa.name)],
                resources=[vakt_rules.Any()],
                subjects=[srirules.HasAuthAction(list_aa, context)],
                context={ 'module': srirules.ContainsElement(context.name) },
                effect=ALLOW_ACCESS,
                description='Automatically created policy'
            )
            storage.add(policy)


def backwards_func(apps, schema_editor):
    # get storage and guard
    storage, guard = sriutils.get_vakt_storage_and_guard()

    # get authaction
    AuthzAction = apps.get_model('noclook', 'AuthzAction')
    list_aa = sriutils.get_list_authaction()

    # get the list policy for every context
    Context = apps.get_model('noclook', 'Context')
    DjPolicy = apps.get_model('djangovakt', 'Policy')
    all_contexts = Context.objects.all()

    for context in all_contexts:
        qs = DjPolicy.objects.filter(doc__actions__0__val=list_aa.name)
        qs = qs.filter(doc__context__module__elem__in=(context.name,))
        qs.delete()


class Migration(migrations.Migration):

    dependencies = [
        ('noclook', '0015_nodehandle_contexts'),
    ]

    operations = [
        migrations.RunPython(forwards_func, backwards_func),
    ]

# -*- coding: utf-8 -*-
# Generated by Django 1.11.24 on 2019-09-27 19:37
from __future__ import unicode_literals

from django.db import migrations
from vakt import Policy, Inquiry, ALLOW_ACCESS, DENY_ACCESS

import apps.noclook.vakt.rules as srirules
import apps.noclook.vakt.utils as sriutils
import uuid
import vakt.rules as vakt_rules


def forwards_func(apps, schema_editor):
    # add AuthzAction
    AuthzAction = apps.get_model('noclook', 'AuthzAction')
    list_aa, created = AuthzAction.objects.get_or_create(name="list")

    # get storage and guard
    storage, guard = sriutils.get_vakt_storage_and_guard()
    Context = apps.get_model('noclook', 'Context')
    DjPolicy = apps.get_model('djangovakt', 'Policy')

    # iterate over all the existent contexts to add
    # a list policy for each of them
    all_contexts = Context.objects.all()

    for context in all_contexts:
        # check if the policy exist first
        qs = DjPolicy.objects.filter(doc__actions__0__val=list_aa.name)
        qs = qs.filter(doc__context__module__elem__in=(context.name,))

        if not qs.exists():
            policy = Policy(
                uuid.uuid4(),
                actions=[vakt_rules.Eq(list_aa.name)],
                resources=[vakt_rules.Any()],
                subjects=[srirules.HasAuthAction(list_aa, context)],
                context={ 'module': srirules.ContainsElement(context.name) },
                effect=ALLOW_ACCESS,
                description='Automatically created policy'
            )
            storage.add(policy)


def backwards_func(apps, schema_editor):
    # get storage and guard
    storage, guard = sriutils.get_vakt_storage_and_guard()

    # get authaction
    AuthzAction = apps.get_model('noclook', 'AuthzAction')
    list_aa = sriutils.get_list_authaction()

    # get the list policy for every context
    Context = apps.get_model('noclook', 'Context')
    DjPolicy = apps.get_model('djangovakt', 'Policy')
    all_contexts = Context.objects.all()

    for context in all_contexts:
        qs = DjPolicy.objects.filter(doc__actions__0__val=list_aa.name)
        qs = qs.filter(doc__context__module__elem__in=(context.name,))
        qs.delete()


class Migration(migrations.Migration):

    dependencies = [
        ('noclook', '0015_nodehandle_contexts'),
    ]

    operations = [
        migrations.RunPython(forwards_func, backwards_func),
    ]
# -*- coding: utf-8 -*- # Generated by Django 1.11.24 on 2019-09-27 19:37 # add AuthzAction # get storage and guard # iterate over all the existent contexts to add # a list policy for each of them # check if the policy exist first # get storage and guard # get authaction # get the list policy for every context