Tony Mack | 06c8e47 | 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 | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 9 | class ControllerUser(PlCoreBase): |
Tony Mack | 06c8e47 | 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 | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 22 | qs = ControllerUser.objects.all() |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 23 | else: |
| 24 | users = Users.select_by_user(user) |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 25 | qs = ControllerUser.objects.filter(user__in=users) |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 26 | return qs |
Tony Mack | ce4e2c2 | 2015-01-06 23:48:02 -0500 | [diff] [blame] | 27 | |
Tony Mack | 3428e6e | 2015-02-08 21:38:41 -0500 | [diff] [blame] | 28 | def can_update(self, user): |
| 29 | return user.can_update_root() |
| 30 | |
Tony Mack | ce4e2c2 | 2015-01-06 23:48:02 -0500 | [diff] [blame] | 31 | |
| 32 | class 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 | |
| 63 | class 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 | |