Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 1 | import os |
| 2 | import datetime |
| 3 | from collections import defaultdict |
| 4 | from django.db import models |
| 5 | from django.db.models import F, Q |
| 6 | from core.models import PlCoreBase,User,Controller |
| 7 | from core.models import Controller,ControllerLinkManager,ControllerLinkDeletionManager |
| 8 | |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 9 | class ControllerUser(PlCoreBase): |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 10 | 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 Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 22 | qs = ControllerUser.objects.all() |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 23 | else: |
| 24 | users = Users.select_by_user(user) |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 25 | qs = ControllerUser.objects.filter(user__in=users) |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 26 | return qs |
Tony Mack | ffe6d8b | 2015-01-06 23:48:02 -0500 | [diff] [blame] | 27 | |
| 28 | |
| 29 | class 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 | |
| 60 | class 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 | |