blob: 7fdde19c379478344fcfba5229f14a7023c6ea67 [file] [log] [blame]
Siobhan Tully53437282013-04-26 19:30:27 -04001import os
2import datetime
Scott Baker83f5b622014-12-19 13:02:31 -08003import sys
Tony Mack2040f5d2015-01-03 14:26:15 -05004import hashlib
Tony Mackc14de8f2013-05-09 21:44:17 -04005from collections import defaultdict
Scott Bakerc1057712014-12-19 12:22:51 -08006from django.forms.models import model_to_dict
Siobhan Tully53437282013-04-26 19:30:27 -04007from django.db import models
Tony Mack5b061472014-02-04 07:57:10 -05008from django.db.models import F, Q
Scott Baker1a6a3902014-10-03 00:32:37 -07009from core.models import PlCoreBase,Site, DashboardView, DiffModelMixIn
Siobhan Tully30fd4292013-05-10 08:59:56 -040010from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
Scott Baker9266e6b2013-05-19 15:54:48 -070011from timezones.fields import TimeZoneField
Scott Baker2c3cb642014-05-19 17:55:56 -070012from operator import itemgetter, attrgetter
Scott Bakera36d77e2014-08-29 11:43:23 -070013from django.core.mail import EmailMultiAlternatives
14from core.middleware import get_request
Sapan Bhatia39097192014-09-04 00:39:19 -040015import model_policy
Scott Baker1a6a3902014-10-03 00:32:37 -070016from django.core.exceptions import PermissionDenied
Siobhan Tully53437282013-04-26 19:30:27 -040017
Scott Bakerd29431b2014-10-09 16:16:02 -070018# ------ from plcorebase.py ------
19try:
20 # This is a no-op if observer_disabled is set to 1 in the config file
21 from observer import *
22except:
23 print >> sys.stderr, "import of observer failed! printing traceback and disabling observer:"
24 import traceback
25 traceback.print_exc()
26
27 # guard against something failing
28 def notify_observer(*args, **kwargs):
29 pass
30# ------ ------
31
Siobhan Tully53437282013-04-26 19:30:27 -040032# Create your models here.
Siobhan Tully30fd4292013-05-10 08:59:56 -040033class UserManager(BaseUserManager):
Siobhan Tully53437282013-04-26 19:30:27 -040034 def create_user(self, email, firstname, lastname, password=None):
35 """
36 Creates and saves a User with the given email, date of
37 birth and password.
38 """
39 if not email:
40 raise ValueError('Users must have an email address')
41
42 user = self.model(
Siobhan Tully30fd4292013-05-10 08:59:56 -040043 email=UserManager.normalize_email(email),
Siobhan Tully53437282013-04-26 19:30:27 -040044 firstname=firstname,
Siobhan Tully30fd4292013-05-10 08:59:56 -040045 lastname=lastname,
46 password=password
Siobhan Tully53437282013-04-26 19:30:27 -040047 )
Siobhan Tully30fd4292013-05-10 08:59:56 -040048 #user.set_password(password)
Siobhan Tully53437282013-04-26 19:30:27 -040049 user.is_admin = True
50 user.save(using=self._db)
51 return user
52
53 def create_superuser(self, email, firstname, lastname, password):
54 """
55 Creates and saves a superuser with the given email, date of
56 birth and password.
57 """
58 user = self.create_user(email,
59 password=password,
60 firstname=firstname,
61 lastname=lastname
62 )
63 user.is_admin = True
64 user.save(using=self._db)
65 return user
66
Scott Bakerf22e73a2014-10-17 18:45:00 -070067 def get_queryset(self):
68 parent=super(UserManager, self)
69 if hasattr(parent, "get_queryset"):
70 return parent.get_queryset().filter(deleted=False)
71 else:
72 return parent.get_query_set().filter(deleted=False)
73
74 # deprecated in django 1.7 in favor of get_queryset().
75 def get_query_set(self):
76 return self.get_queryset()
77
Sapan Bhatia5d605ff2014-07-21 20:08:04 -040078class DeletedUserManager(UserManager):
Scott Baker92f14222014-09-12 12:57:27 -070079 def get_queryset(self):
Sapan Bhatia5d605ff2014-07-21 20:08:04 -040080 return super(UserManager, self).get_query_set().filter(deleted=True)
Siobhan Tully53437282013-04-26 19:30:27 -040081
Scott Baker92f14222014-09-12 12:57:27 -070082 # deprecated in django 1.7 in favor of get_queryset()
83 def get_query_set(self):
84 return self.get_queryset()
85
Scott Bakerc1057712014-12-19 12:22:51 -080086class User(AbstractBaseUser): #, DiffModelMixIn):
87
88 # ---- copy stuff from DiffModelMixin ----
89
90 @property
91 def _dict(self):
92 return model_to_dict(self, fields=[field.name for field in
93 self._meta.fields])
94
95 @property
96 def diff(self):
97 d1 = self._initial
98 d2 = self._dict
99 diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]
100 return dict(diffs)
101
102 @property
103 def has_changed(self):
104 return bool(self.diff)
105
106 @property
107 def changed_fields(self):
108 return self.diff.keys()
109
110 def has_field_changed(self, field_name):
111 return field_name in self.diff.keys()
112
113 def get_field_diff(self, field_name):
114 return self.diff.get(field_name, None)
Scott Bakerebc03eb2015-01-05 17:26:36 -0800115
116 #classmethod
117 def getValidators(cls):
118 """ primarily for REST API, return a dictionary of field names mapped
119 to lists of the type of validations that need to be applied to
120 those fields.
121 """
122 validators = {}
123 for field in cls._meta.fields:
124 l = []
125 if field.blank==False:
126 l.append("notBlank")
127 if field.__class__.__name__=="URLField":
128 l.append("url")
129 validators[field.name] = l
130 return validators
Scott Bakerc1057712014-12-19 12:22:51 -0800131 # ---- end copy stuff from DiffModelMixin ----
Siobhan Tully53437282013-04-26 19:30:27 -0400132
Tony Mack2040f5d2015-01-03 14:26:15 -0500133 @property
134 def remote_password(self):
135 return hashlib.md5(self.password).hexdigest()[:12]
136
Siobhan Tully53437282013-04-26 19:30:27 -0400137 class Meta:
138 app_label = "core"
139
140 email = models.EmailField(
141 verbose_name='email address',
142 max_length=255,
143 unique=True,
144 db_index=True,
145 )
Siobhan Tullyfece0d52013-09-06 12:57:05 -0400146
147 username = models.CharField(max_length=255, default="Something" )
148
Siobhan Tully53437282013-04-26 19:30:27 -0400149 firstname = models.CharField(help_text="person's given name", max_length=200)
150 lastname = models.CharField(help_text="person's surname", max_length=200)
151
152 phone = models.CharField(null=True, blank=True, help_text="phone number contact", max_length=100)
153 user_url = models.URLField(null=True, blank=True)
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400154 site = models.ForeignKey(Site, related_name='users', help_text="Site this user will be homed too", null=True)
Tony Mack5cbadf82013-06-10 13:56:07 -0400155 public_key = models.TextField(null=True, blank=True, max_length=1024, help_text="Public key string")
Siobhan Tully53437282013-04-26 19:30:27 -0400156
157 is_active = models.BooleanField(default=True)
158 is_admin = models.BooleanField(default=True)
159 is_staff = models.BooleanField(default=True)
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500160 is_readonly = models.BooleanField(default=False)
Siobhan Tully53437282013-04-26 19:30:27 -0400161
Tony Mack0553f282013-06-10 22:54:50 -0400162 created = models.DateTimeField(auto_now_add=True)
163 updated = models.DateTimeField(auto_now=True)
164 enacted = models.DateTimeField(null=True, default=None)
Sapan Bhatia47b9bf22014-04-28 21:09:53 -0400165 backend_status = models.CharField(max_length=140,
Sapan Bhatiad507f432014-04-29 00:41:39 -0400166 default="Provisioning in progress")
Sapan Bhatiabcc18992014-04-29 10:32:14 -0400167 deleted = models.BooleanField(default=False)
Tony Mack0553f282013-06-10 22:54:50 -0400168
Scott Baker9266e6b2013-05-19 15:54:48 -0700169 timezone = TimeZoneField()
170
Scott Baker2c3cb642014-05-19 17:55:56 -0700171 dashboards = models.ManyToManyField('DashboardView', through='UserDashboardView', blank=True)
172
Siobhan Tully30fd4292013-05-10 08:59:56 -0400173 objects = UserManager()
Sapan Bhatia5d605ff2014-07-21 20:08:04 -0400174 deleted_objects = DeletedUserManager()
Siobhan Tully53437282013-04-26 19:30:27 -0400175
176 USERNAME_FIELD = 'email'
177 REQUIRED_FIELDS = ['firstname', 'lastname']
178
Scott Baker1bffe942014-10-06 22:58:48 -0700179 PI_FORBIDDEN_FIELDS = ["is_admin", "site", "is_staff"]
180 USER_FORBIDDEN_FIELDS = ["is_admin", "is_active", "site", "is_staff", "is_readonly"]
181
Scott Baker1a6a3902014-10-03 00:32:37 -0700182 def __init__(self, *args, **kwargs):
183 super(User, self).__init__(*args, **kwargs)
184 self._initial = self._dict # for DiffModelMixIn
185
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500186 def isReadOnlyUser(self):
187 return self.is_readonly
188
Siobhan Tully53437282013-04-26 19:30:27 -0400189 def get_full_name(self):
190 # The user is identified by their email address
191 return self.email
192
193 def get_short_name(self):
194 # The user is identified by their email address
195 return self.email
196
Sapan Bhatia5d605ff2014-07-21 20:08:04 -0400197 def delete(self, *args, **kwds):
198 # so we have something to give the observer
199 purge = kwds.get('purge',False)
Scott Baker3d2d3e42014-10-09 16:22:00 -0700200 if purge:
201 del kwds['purge']
Sapan Bhatia5d605ff2014-07-21 20:08:04 -0400202 try:
203 purge = purge or observer_disabled
204 except NameError:
205 pass
206
207 if (purge):
208 super(User, self).delete(*args, **kwds)
209 else:
210 self.deleted = True
211 self.enacted=None
212 self.save(update_fields=['enacted','deleted'])
213
Tony Mackb0d97422013-06-10 09:57:45 -0400214 @property
215 def keyname(self):
216 return self.email[:self.email.find('@')]
217
Siobhan Tully53437282013-04-26 19:30:27 -0400218 def __unicode__(self):
219 return self.email
220
221 def has_perm(self, perm, obj=None):
222 "Does the user have a specific permission?"
223 # Simplest possible answer: Yes, always
224 return True
225
226 def has_module_perms(self, app_label):
227 "Does the user have permissions to view the app `app_label`?"
228 # Simplest possible answer: Yes, always
229 return True
230
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400231 def is_superuser(self):
232 return False
Siobhan Tully53437282013-04-26 19:30:27 -0400233
Scott Baker2c3cb642014-05-19 17:55:56 -0700234 def get_dashboards(self):
235 DEFAULT_DASHBOARDS=["Tenant"]
236
Sapan Bhatia6bfa2ca2014-11-11 21:47:45 -0500237 dashboards = sorted(list(self.userdashboardviews.all()), key=attrgetter('order'))
Scott Baker2c3cb642014-05-19 17:55:56 -0700238 dashboards = [x.dashboardView for x in dashboards]
239
240 if not dashboards:
241 for dashboardName in DEFAULT_DASHBOARDS:
242 dbv = DashboardView.objects.filter(name=dashboardName)
243 if dbv:
244 dashboards.append(dbv[0])
245
246 return dashboards
247
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400248# def get_roles(self):
249# from core.models.site import SitePrivilege
250# from core.models.slice import SliceMembership
251#
252# site_privileges = SitePrivilege.objects.filter(user=self)
253# slice_memberships = SliceMembership.objects.filter(user=self)
254# roles = defaultdict(list)
255# for site_privilege in site_privileges:
256# roles[site_privilege.role.role_type].append(site_privilege.site.login_base)
257# for slice_membership in slice_memberships:
258# roles[slice_membership.role.role_type].append(slice_membership.slice.name)
259# return roles
Siobhan Tully53437282013-04-26 19:30:27 -0400260
Tony Mack53106f32013-04-27 16:43:01 -0400261 def save(self, *args, **kwds):
Siobhan Tully30fd4292013-05-10 08:59:56 -0400262 if not self.id:
Scott Bakera36d77e2014-08-29 11:43:23 -0700263 self.set_password(self.password)
264 if self.is_active:
265 if self.password=="!":
266 self.send_temporary_password()
267
Siobhan Tullyfece0d52013-09-06 12:57:05 -0400268 self.username = self.email
Scott Bakera36d77e2014-08-29 11:43:23 -0700269 super(User, self).save(*args, **kwds)
270
Scott Baker1a6a3902014-10-03 00:32:37 -0700271 self._initial = self._dict
272
Scott Bakera36d77e2014-08-29 11:43:23 -0700273 def send_temporary_password(self):
274 password = User.objects.make_random_password()
275 self.set_password(password)
276 subject, from_email, to = 'OpenCloud Account Credentials', 'support@opencloud.us', str(self.email)
277 text_content = 'This is an important message.'
Scott Baker51e7d402014-08-29 12:32:46 -0700278 userUrl="http://%s/" % get_request().get_host()
Scott Bakera36d77e2014-08-29 11:43:23 -0700279 html_content = """<p>Your account has been created on OpenCloud. Please log in <a href="""+userUrl+""">here</a> to activate your account<br><br>Username: """+self.email+"""<br>Temporary Password: """+password+"""<br>Please change your password once you successully login into the site.</p>"""
280 msg = EmailMultiAlternatives(subject,text_content, from_email, [to])
281 msg.attach_alternative(html_content, "text/html")
282 msg.send()
Tony Mack5b061472014-02-04 07:57:10 -0500283
Scott Baker1a6a3902014-10-03 00:32:37 -0700284 def can_update(self, user):
285 from core.models import SitePrivilege
Scott Baker1bffe942014-10-06 22:58:48 -0700286 _cant_update_fieldName = None
Scott Baker1a6a3902014-10-03 00:32:37 -0700287 if user.is_readonly:
288 return False
289 if user.is_admin:
290 return True
Scott Baker1a6a3902014-10-03 00:32:37 -0700291 # site pis can update
292 site_privs = SitePrivilege.objects.filter(user=user, site=self.site)
293 for site_priv in site_privs:
294 if site_priv.role.role == 'pi':
Scott Baker1bffe942014-10-06 22:58:48 -0700295 for fieldName in self.diff.keys():
296 if fieldName in self.PI_FORBIDDEN_FIELDS:
297 _cant_update_fieldName = fieldName
298 return False
Scott Baker1a6a3902014-10-03 00:32:37 -0700299 return True
Scott Baker1bffe942014-10-06 22:58:48 -0700300 if (user.id == self.id):
301 for fieldName in self.diff.keys():
302 if fieldName in self.USER_FORBIDDEN_FIELDS:
303 _cant_update_fieldName = fieldName
304 return False
305 return True
Scott Baker1a6a3902014-10-03 00:32:37 -0700306
307 return False
308
Tony Mack5b061472014-02-04 07:57:10 -0500309 @staticmethod
310 def select_by_user(user):
311 if user.is_admin:
312 qs = User.objects.all()
313 else:
314 # can see all users at any site where this user has pi role
315 from core.models.site import SitePrivilege
316 site_privs = SitePrivilege.objects.filter(user=user)
317 sites = [sp.site for sp in site_privs if sp.role.role == 'pi']
318 # get site privs of users at these sites
319 site_privs = SitePrivilege.objects.filter(site__in=sites)
Scott Bakera36d77e2014-08-29 11:43:23 -0700320 user_ids = [sp.user.id for sp in site_privs] + [user.id]
Tony Mack5b061472014-02-04 07:57:10 -0500321 qs = User.objects.filter(Q(site__in=sites) | Q(id__in=user_ids))
Scott Bakera36d77e2014-08-29 11:43:23 -0700322 return qs
Tony Mack5b061472014-02-04 07:57:10 -0500323
Scott Baker1a6a3902014-10-03 00:32:37 -0700324 def save_by_user(self, user, *args, **kwds):
325 if not self.can_update(user):
Scott Baker1bffe942014-10-06 22:58:48 -0700326 if getattr(self, "_cant_update_fieldName", None) is not None:
327 raise PermissionDenied("You do not have permission to update field %s on object %s" % (self._cant_update_fieldName, self.__class__.__name__))
328 else:
329 raise PermissionDenied("You do not have permission to update %s objects" % self.__class__.__name__)
Scott Baker1a6a3902014-10-03 00:32:37 -0700330
331 self.save(*args, **kwds)
332
333 def delete_by_user(self, user, *args, **kwds):
334 if not self.can_update(user):
335 raise PermissionDenied("You do not have permission to delete %s objects" % self.__class__.__name__)
336 self.delete(*args, **kwds)
337
Scott Baker2c3cb642014-05-19 17:55:56 -0700338class UserDashboardView(PlCoreBase):
Sapan Bhatia6bfa2ca2014-11-11 21:47:45 -0500339 user = models.ForeignKey(User, related_name='userdashboardviews')
340 dashboardView = models.ForeignKey(DashboardView, related_name='userdashboardviews')
Scott Baker2c3cb642014-05-19 17:55:56 -0700341 order = models.IntegerField(default=0)