blob: 6c776b15800baa22a7fae0679adb95d8ab501864 [file] [log] [blame]
Siobhan Tully53437282013-04-26 19:30:27 -04001import os
2import datetime
3from django.db import models
Siobhan Tully30fd4292013-05-10 08:59:56 -04004from core.models import PlCoreBase
5from core.models import Site
6from core.models import Key
7from openstack.manager import OpenStackManager
8from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
Siobhan Tully53437282013-04-26 19:30:27 -04009
10# Create your models here.
Siobhan Tully30fd4292013-05-10 08:59:56 -040011has_openstack = False
Siobhan Tully53437282013-04-26 19:30:27 -040012
Siobhan Tully30fd4292013-05-10 08:59:56 -040013class UserManager(BaseUserManager):
Siobhan Tully53437282013-04-26 19:30:27 -040014 def create_user(self, email, firstname, lastname, password=None):
15 """
16 Creates and saves a User with the given email, date of
17 birth and password.
18 """
19 if not email:
20 raise ValueError('Users must have an email address')
21
22 user = self.model(
Siobhan Tully30fd4292013-05-10 08:59:56 -040023 email=UserManager.normalize_email(email),
Siobhan Tully53437282013-04-26 19:30:27 -040024 firstname=firstname,
Siobhan Tully30fd4292013-05-10 08:59:56 -040025 lastname=lastname,
26 password=password
Siobhan Tully53437282013-04-26 19:30:27 -040027 )
Siobhan Tully30fd4292013-05-10 08:59:56 -040028 #user.set_password(password)
Siobhan Tully53437282013-04-26 19:30:27 -040029 user.is_admin = True
30 user.save(using=self._db)
31 return user
32
33 def create_superuser(self, email, firstname, lastname, password):
34 """
35 Creates and saves a superuser with the given email, date of
36 birth and password.
37 """
38 user = self.create_user(email,
39 password=password,
40 firstname=firstname,
41 lastname=lastname
42 )
43 user.is_admin = True
44 user.save(using=self._db)
45 return user
46
47
Siobhan Tully30fd4292013-05-10 08:59:56 -040048class User(AbstractBaseUser):
Siobhan Tully53437282013-04-26 19:30:27 -040049
50 class Meta:
51 app_label = "core"
52
53 email = models.EmailField(
54 verbose_name='email address',
55 max_length=255,
56 unique=True,
57 db_index=True,
58 )
59
Siobhan Tully30fd4292013-05-10 08:59:56 -040060 kuser_id = models.CharField(help_text="keystone user id", max_length=200)
Siobhan Tully53437282013-04-26 19:30:27 -040061 firstname = models.CharField(help_text="person's given name", max_length=200)
62 lastname = models.CharField(help_text="person's surname", max_length=200)
63
64 phone = models.CharField(null=True, blank=True, help_text="phone number contact", max_length=100)
65 user_url = models.URLField(null=True, blank=True)
66 site = models.ForeignKey(Site, related_name='users', verbose_name="Site this user will be homed too", null=True)
Siobhan Tully30fd4292013-05-10 08:59:56 -040067 key = models.ForeignKey(Key, related_name='user', null=True, blank=True)
Siobhan Tully53437282013-04-26 19:30:27 -040068
69 is_active = models.BooleanField(default=True)
70 is_admin = models.BooleanField(default=True)
71 is_staff = models.BooleanField(default=True)
72
Siobhan Tully30fd4292013-05-10 08:59:56 -040073 objects = UserManager()
Siobhan Tully53437282013-04-26 19:30:27 -040074
75 USERNAME_FIELD = 'email'
76 REQUIRED_FIELDS = ['firstname', 'lastname']
77
78 def get_full_name(self):
79 # The user is identified by their email address
80 return self.email
81
82 def get_short_name(self):
83 # The user is identified by their email address
84 return self.email
85
86 def __unicode__(self):
87 return self.email
88
89 def has_perm(self, perm, obj=None):
90 "Does the user have a specific permission?"
91 # Simplest possible answer: Yes, always
92 return True
93
94 def has_module_perms(self, app_label):
95 "Does the user have permissions to view the app `app_label`?"
96 # Simplest possible answer: Yes, always
97 return True
98
99 @property
100 def is_staff(self):
101 "Is the user a member of staff?"
102 # Simplest possible answer: All admins are staff
103 return self.is_admin
104
105
Tony Mack53106f32013-04-27 16:43:01 -0400106 def save(self, *args, **kwds):
Siobhan Tully30fd4292013-05-10 08:59:56 -0400107 if has_openstack:
108 if not hasattr(self, 'os_manager'):
109 setattr(self, 'os_manager', OpenStackManager())
Tony Mackd685bfa2013-05-02 10:09:51 -0400110
Siobhan Tully30fd4292013-05-10 08:59:56 -0400111 self.os_manager.save_user(self)
112 if not self.id:
113 self.set_password(self.password)
114 super(User, self).save(*args, **kwds)
Tony Mack53106f32013-04-27 16:43:01 -0400115
116 def delete(self, *args, **kwds):
Siobhan Tully30fd4292013-05-10 08:59:56 -0400117 if has_openstack:
118 if not hasattr(self, 'os_manager'):
119 setattr(self, 'os_manager', OpenStackManager())
Tony Mackd685bfa2013-05-02 10:09:51 -0400120
Siobhan Tully30fd4292013-05-10 08:59:56 -0400121 self.os_manager.delete_user(self)
122 super(User, self).delete(*args, **kwds)