5e2adade1a770cb909a437ac6afa38e473b4631c
[plstackapi.git] / planetstack / model_policies / model_policy_User.py
1 from core.models import *
2
3 def handle(user):
4         from core.models import Controller, ControllerSite, ControllerUser
5         from collections import defaultdict
6         ctrl_site_deployments = ControllerSite.objects.all()
7         controller_lookup = defaultdict(list)
8         for ctrl_site_deployment in ctrl_site_deployments:
9                 controller_site_lookup[ctrl_site_deployment.site_deployment].append(ctrl_site_deployment)
10
11         controller_user_lookup = defaultdict(list)
12         for controller_user in ControllerUser.objects.all():
13                 controller_user_lookup[controller_user.user].append(controller_user.controller)
14    
15         if user.is_admin:
16                 # admins should have an account at all controllers
17                 expected_controllers = controllers
18         else:
19                 # normal users should have an account at their site's controllers
20                 #expected_controllers = controller_site_lookup[user.site]
21                 # users are added to all controllers for now
22                 expected_controllers = controllers        
23
24         for expected_controller in expected_controllers:
25                 if not user in controller_user_lookup or \
26                   expected_controller not in controller_user_lookup[user]: 
27                         # add new record
28                         ud = ControllerUser(user=user, controller=expected_controller)
29                         ud.save()    
30