blob: 678ab77ff88d72e9cf4639b24ad375ac48f880ef [file] [log] [blame]
Tony Mack06c8e472014-11-30 15:53:08 -05001import os
2import datetime
3from collections import defaultdict
4from django.db import models
5from django.db.models import F, Q
6from core.models import PlCoreBase,User,Controller
7from core.models import Controller,ControllerLinkManager,ControllerLinkDeletionManager
8
Tony Mack3066a952015-01-05 22:48:11 -05009class ControllerUser(PlCoreBase):
Tony Mack06c8e472014-11-30 15:53:08 -050010 objects = ControllerLinkManager()
11 deleted_objects = ControllerLinkDeletionManager()
12
13 user = models.ForeignKey(User,related_name='controllerusers')
14 controller = models.ForeignKey(Controller,related_name='controllersusers')
15 kuser_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone user id")
16
17 def __unicode__(self): return u'%s %s' % (self.controller, self.user)
18
19 @staticmethod
20 def select_by_user(user):
21 if user.is_admin:
Tony Mack3066a952015-01-05 22:48:11 -050022 qs = ControllerUser.objects.all()
Tony Mack06c8e472014-11-30 15:53:08 -050023 else:
24 users = Users.select_by_user(user)
Tony Mack3066a952015-01-05 22:48:11 -050025 qs = ControllerUser.objects.filter(user__in=users)
Tony Mack06c8e472014-11-30 15:53:08 -050026 return qs
Tony Mackce4e2c22015-01-06 23:48:02 -050027
28
29class ControllerSitePrivilege(PlCoreBase):
30 objects = ControllerLinkManager()
31 deleted_objects = ControllerLinkDeletionManager()
32
33 controller = models.ForeignKey('Controller', related_name='controllersiteprivileges')
34 site_privilege = models.ForeignKey('SitePrivilege', related_name='controllersiteprivileges')
35 role_id = models.CharField(null=True, blank=True, max_length=200, db_index=True, help_text="Keystone id")
36
37 def __unicode__(self): return u'%s %s' % (self.controller, self.site_privilege)
38
39 def can_update(self, user):
40 if user.is_readonly:
41 return False
42 if user.is_admin:
43 return True
44 cprivs = ControllerSitePrivilege.objects.filter(site_privilege__user=user)
45 for cpriv in dprivs:
46 if cpriv.site_privilege.role.role == ['admin', 'Admin']:
47 return True
48 return False
49
50 @staticmethod
51 def select_by_user(user):
52 if user.is_admin:
53 qs = ControllerSitePrivilege.objects.all()
54 else:
55 cpriv_ids = [cp.id for cp in ControllerSitePrivilege.objects.filter(site_privilege__user=user)]
56 qs = ControllerSitePrivilege.objects.filter(id__in=cpriv_ids)
57 return qs
58
59
60class ControllerSlicePrivilege(PlCoreBase):
61 objects = ControllerLinkManager()
62 deleted_objects = ControllerLinkDeletionManager()
63
64 controller = models.ForeignKey('Controller', related_name='controllersliceprivileges')
65 slice_privilege = models.ForeignKey('SlicePrivilege', related_name='controllersliceprivileges')
66 role_id = models.CharField(null=True, blank=True, max_length=200, db_index=True, help_text="Keystone id")
67
68 def __unicode__(self): return u'%s %s' % (self.controller, self.slice_privilege)
69
70 def can_update(self, user):
71 if user.is_readonly:
72 return False
73 if user.is_admin:
74 return True
75 cprivs = ControllerSlicePrivilege.objects.filter(slice_privilege__user=user)
76 for cpriv in dprivs:
77 if cpriv.role.role == ['admin', 'Admin']:
78 return True
79 return False
80
81 @staticmethod
82 def select_by_user(user):
83 if user.is_admin:
84 qs = ControllerSlicePrivilege.objects.all()
85 else:
86 cpriv_ids = [cp.id for cp in ControllerSlicePrivilege.objects.filter(slice_privilege__user=user)]
87 qs = ControllerSlicePrivilege.objects.filter(id__in=cpriv_ids)
88 return qs
89