blob: b3ba720b6cb8b4fab257de7156780bb336363e94 [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
Tony Mack3428e6e2015-02-08 21:38:41 -050028 def can_update(self, user):
29 return user.can_update_root()
30
Tony Mackce4e2c22015-01-06 23:48:02 -050031
32class ControllerSitePrivilege(PlCoreBase):
33 objects = ControllerLinkManager()
34 deleted_objects = ControllerLinkDeletionManager()
35
36 controller = models.ForeignKey('Controller', related_name='controllersiteprivileges')
37 site_privilege = models.ForeignKey('SitePrivilege', related_name='controllersiteprivileges')
38 role_id = models.CharField(null=True, blank=True, max_length=200, db_index=True, help_text="Keystone id")
39
40 def __unicode__(self): return u'%s %s' % (self.controller, self.site_privilege)
41
42 def can_update(self, user):
43 if user.is_readonly:
44 return False
45 if user.is_admin:
46 return True
47 cprivs = ControllerSitePrivilege.objects.filter(site_privilege__user=user)
48 for cpriv in dprivs:
49 if cpriv.site_privilege.role.role == ['admin', 'Admin']:
50 return True
51 return False
52
53 @staticmethod
54 def select_by_user(user):
55 if user.is_admin:
56 qs = ControllerSitePrivilege.objects.all()
57 else:
58 cpriv_ids = [cp.id for cp in ControllerSitePrivilege.objects.filter(site_privilege__user=user)]
59 qs = ControllerSitePrivilege.objects.filter(id__in=cpriv_ids)
60 return qs
61
62
63class ControllerSlicePrivilege(PlCoreBase):
64 objects = ControllerLinkManager()
65 deleted_objects = ControllerLinkDeletionManager()
66
67 controller = models.ForeignKey('Controller', related_name='controllersliceprivileges')
68 slice_privilege = models.ForeignKey('SlicePrivilege', related_name='controllersliceprivileges')
69 role_id = models.CharField(null=True, blank=True, max_length=200, db_index=True, help_text="Keystone id")
70
71 def __unicode__(self): return u'%s %s' % (self.controller, self.slice_privilege)
72
73 def can_update(self, user):
74 if user.is_readonly:
75 return False
76 if user.is_admin:
77 return True
78 cprivs = ControllerSlicePrivilege.objects.filter(slice_privilege__user=user)
79 for cpriv in dprivs:
80 if cpriv.role.role == ['admin', 'Admin']:
81 return True
82 return False
83
84 @staticmethod
85 def select_by_user(user):
86 if user.is_admin:
87 qs = ControllerSlicePrivilege.objects.all()
88 else:
89 cpriv_ids = [cp.id for cp in ControllerSlicePrivilege.objects.filter(slice_privilege__user=user)]
90 qs = ControllerSlicePrivilege.objects.filter(id__in=cpriv_ids)
91 return qs
92