blob: 1c01a42bf88e32f03f4a6128f1d1ceffae3f0c5a [file] [log] [blame]
Siobhan Tully53437282013-04-26 19:30:27 -04001import os
2import datetime
3from django.db import models
Tony Mack0e723b92013-04-27 11:08:19 -04004from plstackapi.core.models import PlCoreBase
5from plstackapi.core.models import Site
Tony Mack53106f32013-04-27 16:43:01 -04006from plstackapi.openstack.driver import OpenStackDriver
Siobhan Tully53437282013-04-26 19:30:27 -04007from django.contrib.auth.models import User, AbstractBaseUser, UserManager, BaseUserManager
8
9# Create your models here.
10
11class PLUserManager(BaseUserManager):
12 def create_user(self, email, firstname, lastname, password=None):
13 """
14 Creates and saves a User with the given email, date of
15 birth and password.
16 """
17 if not email:
18 raise ValueError('Users must have an email address')
19
20 user = self.model(
21 email=PLUserManager.normalize_email(email),
22 firstname=firstname,
23 lastname=lastname
24 )
25
26 user.set_password(password)
27 user.is_admin = True
28 user.save(using=self._db)
29 return user
30
31 def create_superuser(self, email, firstname, lastname, password):
32 """
33 Creates and saves a superuser with the given email, date of
34 birth and password.
35 """
36 user = self.create_user(email,
37 password=password,
38 firstname=firstname,
39 lastname=lastname
40 )
41 user.is_admin = True
42 user.save(using=self._db)
43 return user
44
45
46class PLUser(AbstractBaseUser):
47
48 class Meta:
49 app_label = "core"
50
51 email = models.EmailField(
52 verbose_name='email address',
53 max_length=255,
54 unique=True,
55 db_index=True,
56 )
57
Tony Mack53106f32013-04-27 16:43:01 -040058 user_id = models.CharField(help_text="keystone user id", max_length=200)
Siobhan Tully53437282013-04-26 19:30:27 -040059 firstname = models.CharField(help_text="person's given name", max_length=200)
60 lastname = models.CharField(help_text="person's surname", max_length=200)
61
62 phone = models.CharField(null=True, blank=True, help_text="phone number contact", max_length=100)
63 user_url = models.URLField(null=True, blank=True)
64 site = models.ForeignKey(Site, related_name='users', verbose_name="Site this user will be homed too", null=True)
65
66 is_active = models.BooleanField(default=True)
67 is_admin = models.BooleanField(default=True)
68 is_staff = models.BooleanField(default=True)
69
70 objects = PLUserManager()
71
72 USERNAME_FIELD = 'email'
73 REQUIRED_FIELDS = ['firstname', 'lastname']
74
75 def get_full_name(self):
76 # The user is identified by their email address
77 return self.email
78
79 def get_short_name(self):
80 # The user is identified by their email address
81 return self.email
82
83 def __unicode__(self):
84 return self.email
85
86 def has_perm(self, perm, obj=None):
87 "Does the user have a specific permission?"
88 # Simplest possible answer: Yes, always
89 return True
90
91 def has_module_perms(self, app_label):
92 "Does the user have permissions to view the app `app_label`?"
93 # Simplest possible answer: Yes, always
94 return True
95
96 @property
97 def is_staff(self):
98 "Is the user a member of staff?"
99 # Simplest possible answer: All admins are staff
100 return self.is_admin
101
102
Tony Mack53106f32013-04-27 16:43:01 -0400103 def save(self, *args, **kwds):
104 if not self.user_id:
105 if not hasattr(self, 'driver'):
106 setattr(self, 'driver', OpenStackDriver())
107 name = self.email[:self.email.find('@')]
108 user_fields = {'name': name,
109 'email': self.email,
110 'password': self.password,
111 'enabled': self.is_active}
112 user = self.driver.create_user(**user_fields)
113 self.user_id = user.id
114 super(PLUser, self).save(*args, **kwds)
115
116 def delete(self, *args, **kwds):
117 if self.user_id:
118 if not getattr(self, 'driver'):
119 setattr(self, 'driver', OpenStackDriver())
120 self.driver.delete_user(self.user_id)
121 super(PLUser, self).delete(*args, **kwds)