aboutsummaryrefslogtreecommitdiffstats
path: root/keystone-moon/keystone/contrib/moon/controllers.py
blob: 8541b477d19930e117a2d7e66a98dbbc22415180 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
# This software is distributed under the terms and conditions of the 'Apache-2.0'
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.

from keystone.common import controller
from keystone.common import dependency
from keystone import config
from keystone.models import token_model
from keystone.contrib.moon.exception import *
from oslo_log import log
from uuid import uuid4

CONF = config.CONF
LOG = log.getLogger(__name__)


@dependency.requires('configuration_api')
class Configuration(controller.V3Controller):
    collection_name = 'configurations'
    member_name = 'configuration'

    def __init__(self):
        super(Configuration, self).__init__()

    def _get_user_id_from_token(self, token_id):
        response = self.token_provider_api.validate_token(token_id)
        token_ref = token_model.KeystoneToken(token_id=token_id, token_data=response)
        return token_ref.get('user')

    @controller.protected()
    def get_policy_templates(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        return self.configuration_api.get_policy_templates_dict(user_id)

    @controller.protected()
    def get_aggregation_algorithms(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        return self.configuration_api.get_aggregation_algorithms_dict(user_id)

    @controller.protected()
    def get_sub_meta_rule_algorithms(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        return self.configuration_api.get_sub_meta_rule_algorithms_dict(user_id)


@dependency.requires('tenant_api', 'resource_api')
class Tenants(controller.V3Controller):

    def __init__(self):
        super(Tenants, self).__init__()

    def _get_user_id_from_token(self, token_id):
        response = self.token_provider_api.validate_token(token_id)
        token_ref = token_model.KeystoneToken(token_id=token_id, token_data=response)
        return token_ref.get('user')

    @controller.protected()
    def get_tenants(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        return self.tenant_api.get_tenants_dict(user_id)

    def __get_keystone_tenant_dict(self, tenant_id="", tenant_name="", tenant_description="", domain="default"):
        tenants = self.resource_api.list_projects()
        for tenant in tenants:
            if tenant_id and tenant_id == tenant['id']:
                return tenant
            if tenant_name and tenant_name == tenant['name']:
                return tenant
        if not tenant_id:
            tenant_id = uuid4().hex
        if not tenant_name:
            tenant_name = tenant_id
        tenant = {
            "id": tenant_id,
            "name": tenant_name,
            "description": tenant_description,
            "enabled": True,
            "domain_id": domain
        }
        keystone_tenant = self.resource_api.create_project(tenant["id"], tenant)
        return keystone_tenant

    @controller.protected()
    def add_tenant(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        k_tenant_dict = self.__get_keystone_tenant_dict(
            tenant_name=kw.get('tenant_name'),
            tenant_description=kw.get('tenant_description', kw.get('tenant_name')),
            domain=kw.get('tenant_domain', "default"),

        )
        tenant_dict = dict()
        tenant_dict['id'] = k_tenant_dict['id']
        tenant_dict['name'] = kw.get('tenant_name', None)
        tenant_dict['description'] = kw.get('tenant_description', None)
        tenant_dict['intra_authz_extension_id'] = kw.get('tenant_intra_authz_extension_id', None)
        tenant_dict['intra_admin_extension_id'] = kw.get('tenant_intra_admin_extension_id', None)
        return self.tenant_api.add_tenant_dict(user_id, tenant_dict['id'], tenant_dict)

    @controller.protected()
    def get_tenant(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        tenant_id = kw.get('tenant_id', None)
        return self.tenant_api.get_tenant_dict(user_id, tenant_id)

    @controller.protected()
    def del_tenant(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        tenant_id = kw.get('tenant_id', None)
        return self.tenant_api.del_tenant(user_id, tenant_id)

    @controller.protected()
    def set_tenant(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        # Next line will raise an error if tenant doesn't exist
        k_tenant_dict = self.resource_api.get_project(kw.get('tenant_id', None))
        tenant_id = kw.get('tenant_id', None)
        tenant_dict = dict()
        tenant_dict['name'] = k_tenant_dict.get('name', None)
        if 'tenant_description' in kw:
            tenant_dict['description'] = kw.get('tenant_description', None)
        if 'tenant_intra_authz_extension_id' in kw:
            tenant_dict['intra_authz_extension_id'] = kw.get('tenant_intra_authz_extension_id', None)
        if 'tenant_intra_admin_extension_id' in kw:
            tenant_dict['intra_admin_extension_id'] = kw.get('tenant_intra_admin_extension_id', None)
        self.tenant_api.set_tenant_dict(user_id, tenant_id, tenant_dict)


@dependency.requires('authz_api')
class Authz_v3(controller.V3Controller):

    def __init__(self):
        super(Authz_v3, self).__init__()

    @controller.protected()
    def get_authz(self, context, tenant_id, subject_k_id, object_name, action_name):
        try:
            return self.authz_api.authz(tenant_id, subject_k_id, object_name, action_name)
        except Exception as e:
            return {'authz': False, 'comment': unicode(e)}


@dependency.requires('admin_api', 'root_api')
class IntraExtensions(controller.V3Controller):
    collection_name = 'intra_extensions'
    member_name = 'intra_extension'

    def __init__(self):
        super(IntraExtensions, self).__init__()

    def _get_user_id_from_token(self, token_id):
        response = self.token_provider_api.validate_token(token_id)
        token_ref = token_model.KeystoneToken(token_id=token_id, token_data=response)
        return token_ref.get('user')['id']

    # IntraExtension functions
    @controller.protected()
    def get_intra_extensions(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        return self.admin_api.get_intra_extensions_dict(user_id)

    @controller.protected()
    def add_intra_extension(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_dict = dict()
        intra_extension_dict['name'] = kw.get('intra_extension_name', None)
        intra_extension_dict['model'] = kw.get('intra_extension_model', None)
        intra_extension_dict['genre'] = kw.get('intra_extension_genre', None)
        intra_extension_dict['description'] = kw.get('intra_extension_description', None)
        intra_extension_dict['subject_categories'] = kw.get('intra_extension_subject_categories', dict())
        intra_extension_dict['object_categories'] = kw.get('intra_extension_object_categories', dict())
        intra_extension_dict['action_categories'] = kw.get('intra_extension_action_categories', dict())
        intra_extension_dict['subjects'] = kw.get('intra_extension_subjects', dict())
        intra_extension_dict['objects'] = kw.get('intra_extension_objects', dict())
        intra_extension_dict['actions'] = kw.get('intra_extension_actions', dict())
        intra_extension_dict['subject_scopes'] = kw.get('intra_extension_subject_scopes', dict())
        intra_extension_dict['object_scopes'] = kw.get('intra_extension_object_scopes', dict())
        intra_extension_dict['action_scopes'] = kw.get('intra_extension_action_scopes', dict())
        intra_extension_dict['subject_assignments'] = kw.get('intra_extension_subject_assignments', dict())
        intra_extension_dict['object_assignments'] = kw.get('intra_extension_object_assignments', dict())
        intra_extension_dict['action_assignments'] = kw.get('intra_extension_action_assignments', dict())
        intra_extension_dict['aggregation_algorithm'] = kw.get('intra_extension_aggregation_algorithm', dict())
        intra_extension_dict['sub_meta_rules'] = kw.get('intra_extension_sub_meta_rules', dict())
        intra_extension_dict['rules'] = kw.get('intra_extension_rules', dict())
        return self.admin_api.load_intra_extension_dict(user_id, intra_extension_dict=intra_extension_dict)

    @controller.protected()
    def get_intra_extension(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        return self.admin_api.get_intra_extension_dict(user_id, intra_extension_id)

    @controller.protected()
    def del_intra_extension(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        self.admin_api.del_intra_extension(user_id, intra_extension_id)

    @controller.protected()
    def set_intra_extension(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        intra_extension_dict = dict()
        intra_extension_dict['name'] = kw.get('intra_extension_name', None)
        intra_extension_dict['model'] = kw.get('intra_extension_model', None)
        intra_extension_dict['genre'] = kw.get('intra_extension_genre', None)
        intra_extension_dict['description'] = kw.get('intra_extension_description', None)
        return self.admin_api.set_intra_extension_dict(user_id, intra_extension_id, intra_extension_dict)

    @controller.protected()
    def load_root_intra_extension(self, context, **kw):
        self.root_api.load_root_intra_extension_dict()

    # Metadata functions
    @controller.protected()
    def get_subject_categories(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        return self.admin_api.get_subject_categories_dict(user_id, intra_extension_id)

    @controller.protected()
    def add_subject_category(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        subject_category_dict = dict()
        subject_category_dict['name'] = kw.get('subject_category_name', None)
        subject_category_dict['description'] = kw.get('subject_category_description', None)
        return self.admin_api.add_subject_category_dict(user_id, intra_extension_id, subject_category_dict)

    @controller.protected()
    def get_subject_category(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        subject_category_id = kw.get('subject_category_id', None)
        return self.admin_api.get_subject_category_dict(user_id, intra_extension_id, subject_category_id)

    @controller.protected()
    def del_subject_category(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        subject_category_id = kw.get('subject_category_id', None)
        self.admin_api.del_subject_category(user_id, intra_extension_id, subject_category_id)

    @controller.protected()
    def set_subject_category(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        subject_category_id = kw.get('subject_category_id', None)
        subject_category_dict = dict()
        subject_category_dict['name'] = kw.get('subject_category_name', None)
        subject_category_dict['description'] = kw.get('subject_category_description', None)
        return self.admin_api.set_subject_category_dict(user_id, intra_extension_id, subject_category_id, subject_category_dict)

    @controller.protected()
    def get_object_categories(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        return self.admin_api.get_object_categories_dict(user_id, intra_extension_id)

    @controller.protected()
    def add_object_category(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        object_category_dict = dict()
        object_category_dict['name'] = kw.get('object_category_name', None)
        object_category_dict['description'] = kw.get('object_category_description', None)
        return self.admin_api.add_object_category_dict(user_id, intra_extension_id, object_category_dict)

    @controller.protected()
    def get_object_category(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        object_category_id = kw.get('object_category_id', None)
        return self.admin_api.get_object_categories_dict(user_id, intra_extension_id, object_category_id)

    @controller.protected()
    def del_object_category(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        object_category_id = kw.get('object_category_id', None)
        self.admin_api.del_object_category(user_id, intra_extension_id, object_category_id)

    @controller.protected()
    def set_object_category(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        object_category_id = kw.get('object_category_id', None)
        object_category_dict = dict()
        object_category_dict['name'] = kw.get('object_category_name', None)
        object_category_dict['description'] = kw.get('object_category_description', None)
        return self.admin_api.set_object_category_dict(user_id, intra_extension_id, object_category_id, object_category_dict)

    @controller.protected()
    def get_action_categories(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        return self.admin_api.get_action_categories_dict(user_id, intra_extension_id)

    @controller.protected()
    def add_action_category(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        action_category_dict = dict()
        action_category_dict['name'] = kw.get('action_category_name', None)
        action_category_dict['description'] = kw.get('action_category_description', None)
        return self.admin_api.add_action_category_dict(user_id, intra_extension_id, action_category_dict)

    @controller.protected()
    def get_action_category(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        action_category_id = kw.get('action_category_id', None)
        return self.admin_api.get_action_categories_dict(user_id, intra_extension_id, action_category_id)

    @controller.protected()
    def del_action_category(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        action_category_id = kw.get('action_category_id', None)
        self.admin_api.del_action_category(user_id, intra_extension_id, action_category_id)

    @controller.protected()
    def set_action_category(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        action_category_id = kw.get('action_category_id', None)
        action_category_dict = dict()
        action_category_dict['name'] = kw.get('action_category_name', None)
        action_category_dict['description'] = kw.get('action_category_description', None)
        return self.admin_api.set_action_category_dict(user_id, intra_extension_id, action_category_id, action_category_dict)

    # Perimeter functions
    @controller.protected()
    def get_subjects(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        return self.admin_api.get_subjects_dict(user_id, intra_extension_id)

    @controller.protected()
    def add_subject(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        subject_dict = dict()
        subject_dict['name'] = kw.get('subject_name', None)
        subject_dict['description'] = kw.get('subject_description', None)
        subject_dict['password'] = kw.get('subject_password', None)
        subject_dict['email'] = kw.get('subject_email', None)
        return self.admin_api.add_subject_dict(user_id, intra_extension_id, subject_dict)

    @controller.protected()
    def get_subject(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        subject_id = kw.get('subject_id', None)
        return self.admin_api.get_subject_dict(user_id, intra_extension_id, subject_id)

    @controller.protected()
    def del_subject(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        subject_id = kw.get('subject_id', None)
        self.admin_api.del_subject(user_id, intra_extension_id, subject_id)

    @controller.protected()
    def set_subject(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        subject_id = kw.get('subject_id', None)
        subject_dict = dict()
        subject_dict['name'] = kw.get('subject_name', None)
        subject_dict['description'] = kw.get('subject_description', None)
        return self.admin_api.set_subject_dict(user_id, intra_extension_id, subject_id, subject_dict)

    @controller.protected()
    def get_objects(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        return self.admin_api.get_objects_dict(user_id, intra_extension_id)

    @controller.protected()
    def add_object(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        object_dict = dict()
        object_dict['name'] = kw.get('object_name', None)
        object_dict['description'] = kw.get('object_description', None)
        return self.admin_api.add_object_dict(user_id, intra_extension_id, object_dict)

    @controller.protected()
    def get_object(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        object_id = kw.get('object_id', None)
        return self.admin_api.get_object_dict(user_id, intra_extension_id, object_id)

    @controller.protected()
    def del_object(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        object_id = kw.get('object_id', None)
        self.admin_api.del_object(user_id, intra_extension_id, object_id)

    @controller.protected()
    def set_object(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        object_id = kw.get('object_id', None)
        object_dict = dict()
        object_dict['name'] = kw.get('object_name', None)
        object_dict['description'] = kw.get('object_description', None)
        return self.admin_api.set_object_dict(user_id, intra_extension_id, object_id, object_dict)

    @controller.protected()
    def get_actions(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        return self.admin_api.get_actions_dict(user_id, intra_extension_id)

    @controller.protected()
    def add_action(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        action_dict = dict()
        action_dict['name'] = kw.get('action_name', None)
        action_dict['description'] = kw.get('action_description', None)
        return self.admin_api.add_action_dict(user_id, intra_extension_id, action_dict)

    @controller.protected()
    def get_action(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        action_id = kw.get('action_id', None)
        return self.admin_api.get_action_dict(user_id, intra_extension_id, action_id)

    @controller.protected()
    def del_action(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        action_id = kw.get('action_id', None)
        self.admin_api.del_action(user_id, intra_extension_id, action_id)

    @controller.protected()
    def set_action(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        action_id = kw.get('action_id', None)
        action_dict = dict()
        action_dict['name'] = kw.get('action_name', None)
        action_dict['description'] = kw.get('action_description', None)
        return self.admin_api.set_action_dict(user_id, intra_extension_id, action_id, action_dict)

    # Scope functions
    @controller.protected()
    def get_subject_scopes(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        subject_category_id = kw.get('subject_category_id', None)
        return self.admin_api.get_subject_scopes_dict(user_id, intra_extension_id, subject_category_id)

    @controller.protected()
    def add_subject_scope(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        subject_category_id = kw.get('subject_category_id', None)
        subject_scope_dict = dict()
        subject_scope_dict['name'] = kw.get('subject_scope_name', None)
        subject_scope_dict['description'] = kw.get('subject_scope_description', None)
        return self.admin_api.add_subject_scope_dict(user_id, intra_extension_id, subject_category_id, subject_scope_dict)

    @controller.protected()
    def get_subject_scope(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        subject_category_id = kw.get('subject_category_id', None)
        subject_scope_id = kw.get('subject_scope_id', None)
        return self.admin_api.get_subject_scope_dict(user_id, intra_extension_id, subject_category_id, subject_scope_id)

    @controller.protected()
    def del_subject_scope(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        subject_category_id = kw.get('subject_category_id', None)
        subject_scope_id = kw.get('subject_scope_id', None)
        self.admin_api.del_subject_scope(user_id, intra_extension_id, subject_category_id, subject_scope_id)

    @controller.protected()
    def set_subject_scope(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        subject_category_id = kw.get('subject_category_id', None)
        subject_scope_id = kw.get('subject_scope_id', None)
        subject_scope_dict = dict()
        subject_scope_dict['name'] = kw.get('subject_scope_name', None)
        subject_scope_dict['description'] = kw.get('subject_scope_description', None)
        return self.admin_api.set_subject_scope_dict(user_id, intra_extension_id, subject_category_id, subject_scope_id, subject_scope_dict)

    @controller.protected()
    def get_object_scopes(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        object_category_id = kw.get('object_category_id', None)
        return self.admin_api.get_object_scopes_dict(user_id, intra_extension_id, object_category_id)

    @controller.protected()
    def add_object_scope(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        object_category_id = kw.get('object_category_id', None)
        object_scope_dict = dict()
        object_scope_dict['name'] = kw.get('object_scope_name', None)
        object_scope_dict['description'] = kw.get('object_scope_description', None)
        return self.admin_api.add_object_scope_dict(user_id, intra_extension_id, object_category_id, object_scope_dict)

    @controller.protected()
    def get_object_scope(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        object_category_id = kw.get('object_category_id', None)
        object_scope_id = kw.get('object_scope_id', None)
        return self.admin_api.get_object_scope_dict(user_id, intra_extension_id, object_category_id, object_scope_id)

    @controller.protected()
    def del_object_scope(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        object_category_id = kw.get('object_category_id', None)
        object_scope_id = kw.get('object_scope_id', None)
        self.admin_api.del_object_scope(user_id, intra_extension_id, object_category_id, object_scope_id)

    @controller.protected()
    def set_object_scope(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        object_category_id = kw.get('object_category_id', None)
        object_scope_id = kw.get('object_scope_id', None)
        object_scope_dict = dict()
        object_scope_dict['name'] = kw.get('object_scope_name', None)
        object_scope_dict['description'] = kw.get('object_scope_description', None)
        return self.admin_api.set_object_scope_dict(user_id, intra_extension_id, object_category_id, object_scope_id, object_scope_dict)

    @controller.protected()
    def get_action_scopes(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        action_category_id = kw.get('action_category_id', None)
        return self.admin_api.get_action_scopes_dict(user_id, intra_extension_id, action_category_id)

    @controller.protected()
    def add_action_scope(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        action_category_id = kw.get('action_category_id', None)
        action_scope_dict = dict()
        action_scope_dict['name'] = kw.get('action_scope_name', None)
        action_scope_dict['description'] = kw.get('action_scope_description', None)
        return self.admin_api.add_action_scope_dict(user_id, intra_extension_id, action_category_id, action_scope_dict)

    @controller.protected()
    def get_action_scope(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        action_category_id = kw.get('action_category_id', None)
        action_scope_id = kw.get('action_scope_id', None)
        return self.admin_api.get_action_scope_dict(user_id, intra_extension_id, action_category_id, action_scope_id)

    @controller.protected()
    def del_action_scope(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        action_category_id = kw.get('action_category_id', None)
        action_scope_id = kw.get('action_scope_id', None)
        self.admin_api.del_action_scope(user_id, intra_extension_id, action_category_id, action_scope_id)

    @controller.protected()
    def set_action_scope(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        action_category_id = kw.get('action_category_id', None)
        action_scope_id = kw.get('action_scope_id', None)
        action_scope_dict = dict()
        action_scope_dict['name'] = kw.get('action_scope_name', None)
        action_scope_dict['description'] = kw.get('action_scope_description', None)
        return self.admin_api.set_action_scope_dict(user_id, intra_extension_id, action_category_id, action_scope_id, action_scope_dict)

    # Assignment functions

    @controller.protected()
    def add_subject_assignment(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        subject_id = kw.get('subject_id', None)
        subject_category_id = kw.get('subject_category_id', None)
        subject_scope_id = kw.get('subject_scope_id', None)
        return self.admin_api.add_subject_assignment_list(user_id, intra_extension_id, subject_id, subject_category_id, subject_scope_id)

    @controller.protected()
    def get_subject_assignment(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        subject_id = kw.get('subject_id', None)
        subject_category_id = kw.get('subject_category_id', None)
        return self.admin_api.get_subject_assignment_list(user_id, intra_extension_id, subject_id, subject_category_id)

    @controller.protected()
    def del_subject_assignment(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        subject_id = kw.get('subject_id', None)
        subject_category_id = kw.get('subject_category_id', None)
        subject_scope_id = kw.get('subject_scope_id', None)
        self.admin_api.del_subject_assignment(user_id, intra_extension_id, subject_id, subject_category_id, subject_scope_id)

    @controller.protected()
    def add_object_assignment(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        object_id = kw.get('object_id', None)
        object_category_id = kw.get('object_category_id', None)
        object_scope_id = kw.get('object_scope_id', None)
        return self.admin_api.add_object_assignment_list(user_id, intra_extension_id, object_id, object_category_id, object_scope_id)

    @controller.protected()
    def get_object_assignment(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        object_id = kw.get('object_id', None)
        object_category_id = kw.get('object_category_id', None)
        return self.admin_api.get_object_assignment_list(user_id, intra_extension_id, object_id, object_category_id)

    @controller.protected()
    def del_object_assignment(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        object_id = kw.get('object_id', None)
        object_category_id = kw.get('object_category_id', None)
        object_scope_id = kw.get('object_scope_id', None)
        self.admin_api.del_object_assignment(user_id, intra_extension_id, object_id, object_category_id, object_scope_id)

    @controller.protected()
    def add_action_assignment(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        action_id = kw.get('action_id', None)
        action_category_id = kw.get('action_category_id', None)
        action_scope_id = kw.get('action_scope_id', None)
        return self.admin_api.add_action_assignment_list(user_id, intra_extension_id, action_id, action_category_id, action_scope_id)

    @controller.protected()
    def get_action_assignment(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        action_id = kw.get('action_id', None)
        action_category_id = kw.get('action_category_id', None)
        return self.admin_api.get_action_assignment_list(user_id, intra_extension_id, action_id, action_category_id)

    @controller.protected()
    def del_action_assignment(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        action_id = kw.get('action_id', None)
        action_category_id = kw.get('action_category_id', None)
        action_scope_id = kw.get('action_scope_id', None)
        self.admin_api.del_action_assignment(user_id, intra_extension_id, action_id, action_category_id, action_scope_id)

    # Metarule functions

    @controller.protected()
    def get_aggregation_algorithm(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        return self.admin_api.get_aggregation_algorithm_id(user_id, intra_extension_id)

    @controller.protected()
    def set_aggregation_algorithm(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        aggregation_algorithm_id = kw.get('aggregation_algorithm_id', None)
        return self.admin_api.set_aggregation_algorithm_id(user_id, intra_extension_id, aggregation_algorithm_id)

    @controller.protected()
    def get_sub_meta_rules(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        return self.admin_api.get_sub_meta_rules_dict(user_id, intra_extension_id)

    @controller.protected()
    def add_sub_meta_rule(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        sub_meta_rule_dict = dict()
        sub_meta_rule_dict['name'] = kw.get('sub_meta_rule_name', None)
        sub_meta_rule_dict['algorithm'] = kw.get('sub_meta_rule_algorithm', None)
        sub_meta_rule_dict['subject_categories'] = kw.get('sub_meta_rule_subject_categories', None)
        sub_meta_rule_dict['object_categories'] = kw.get('sub_meta_rule_object_categories', None)
        sub_meta_rule_dict['action_categories'] = kw.get('sub_meta_rule_action_categories', None)
        return self.admin_api.add_sub_meta_rule_dict(user_id, intra_extension_id, sub_meta_rule_dict)

    @controller.protected()
    def get_sub_meta_rule(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        sub_meta_rule_id = kw.get('sub_meta_rule_id', None)
        return self.admin_api.get_sub_meta_rule_dict(user_id, intra_extension_id, sub_meta_rule_id)

    @controller.protected()
    def del_sub_meta_rule(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        sub_meta_rule_id = kw.get('sub_meta_rule_id', None)
        self.admin_api.del_sub_meta_rule(user_id, intra_extension_id, sub_meta_rule_id)

    @controller.protected()
    def set_sub_meta_rule(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        sub_meta_rule_id = kw.get('sub_meta_rule_id', None)
        sub_meta_rule_dict = dict()
        sub_meta_rule_dict['name'] = kw.get('sub_meta_rule_name', None)
        sub_meta_rule_dict['algorithm'] = kw.get('sub_meta_rule_algorithm', None)
        sub_meta_rule_dict['subject_categories'] = kw.get('sub_meta_rule_subject_categories', None)
        sub_meta_rule_dict['object_categories'] = kw.get('sub_meta_rule_object_categories', None)
        sub_meta_rule_dict['action_categories'] = kw.get('sub_meta_rule_action_categories', None)
        return self.admin_api.set_sub_meta_rule_dict(user_id, intra_extension_id, sub_meta_rule_id, sub_meta_rule_dict)

    # Rules functions
    @controller.protected()
    def get_rules(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        sub_meta_rule_id = kw.get('sub_meta_rule_id', None)
        return self.admin_api.get_rules_dict(user_id, intra_extension_id, sub_meta_rule_id)

    @controller.protected()
    def add_rule(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        sub_meta_rule_id = kw.get('sub_meta_rule_id', None)
        subject_category_list = kw.get('subject_categories', [])
        object_category_list = kw.get('object_categories', [])
        action_category_list = kw.get('action_categories', [])
        enabled_bool = kw.get('enabled', True)
        rule_list = subject_category_list + action_category_list + object_category_list + [enabled_bool, ]
        return self.admin_api.add_rule_dict(user_id, intra_extension_id, sub_meta_rule_id, rule_list)

    @controller.protected()
    def get_rule(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        sub_meta_rule_id = kw.get('sub_meta_rule_id', None)
        rule_id = kw.get('rule_id', None)
        return self.admin_api.get_rule_dict(user_id, intra_extension_id, sub_meta_rule_id, rule_id)

    @controller.protected()
    def del_rule(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        sub_meta_rule_id = kw.get('sub_meta_rule_id', None)
        rule_id = kw.get('rule_id', None)
        self.admin_api.del_rule(user_id, intra_extension_id, sub_meta_rule_id, rule_id)

    @controller.protected()
    def set_rule(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        intra_extension_id = kw.get('intra_extension_id', None)
        sub_meta_rule_id = kw.get('sub_meta_rule_id', None)
        rule_id = kw.get('rule_id', None)
        rule_list = list()
        subject_category_list = kw.get('subject_categories', [])
        object_category_list = kw.get('object_categories', [])
        action_category_list = kw.get('action_categories', [])
        rule_list = subject_category_list + action_category_list + object_category_list
        return self.admin_api.set_rule_dict(user_id, intra_extension_id, sub_meta_rule_id, rule_id, rule_list)


@dependency.requires('authz_api')
class InterExtensions(controller.V3Controller):

    def __init__(self):
        super(InterExtensions, self).__init__()

    def _get_user_from_token(self, token_id):
        response = self.token_provider_api.validate_token(token_id)
        token_ref = token_model.KeystoneToken(token_id=token_id, token_data=response)
        return token_ref['user']

    # @controller.protected()
    # def get_inter_extensions(self, context, **kw):
    #     user = self._get_user_from_token(context.get('token_id'))
    #     return {
    #         'inter_extensions':
    #             self.interextension_api.get_inter_extensions()
    #     }

    # @controller.protected()
    # def get_inter_extension(self, context, **kw):
    #     user = self._get_user_from_token(context.get('token_id'))
    #     return {
    #         'inter_extensions':
    #             self.interextension_api.get_inter_extension(uuid=kw['inter_extension_id'])
    #     }

    # @controller.protected()
    # def create_inter_extension(self, context, **kw):
    #     user = self._get_user_from_token(context.get('token_id'))
    #     return self.interextension_api.create_inter_extension(kw)

    # @controller.protected()
    # def delete_inter_extension(self, context, **kw):
    #     user = self._get_user_from_token(context.get('token_id'))
    #     if 'inter_extension_id' not in kw:
    #         raise exception.Error
    #     return self.interextension_api.delete_inter_extension(kw['inter_extension_id'])


@dependency.requires('moonlog_api', 'authz_api')
class Logs(controller.V3Controller):

    def __init__(self):
        super(Logs, self).__init__()

    def _get_user_id_from_token(self, token_id):
        response = self.token_provider_api.validate_token(token_id)
        token_ref = token_model.KeystoneToken(token_id=token_id, token_data=response)
        return token_ref['user']

    @controller.protected()
    def get_logs(self, context, **kw):
        user_id = self._get_user_id_from_token(context.get('token_id'))
        options = kw.get('options', '')
        return self.moonlog_api.get_logs(user_id, options)