2 # Copyright 2018 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 from __future__
import unicode_literals
19 from django
.contrib
.auth
.models
import (
20 AbstractBaseUser
, BaseUserManager
, PermissionsMixin
)
21 from django
.utils
import timezone
22 from django
.utils
.translation
import ugettext_lazy
as _
23 from django
.contrib
.sessions
.base_session
import AbstractBaseSession
24 from django
.db
import models
26 class CustomUserManager(BaseUserManager
):
27 """Custom manager for CustomUser."""
29 def _create_user(self
, username
, password
, is_staff
, is_superuser
, **extra_fields
):
30 """Create and save a CustomUser with the given username and password. """
34 raise ValueError('The given username must be set')
36 is_active
= extra_fields
.pop("is_active", True)
37 user
= self
.model(username
=username
, is_staff
=is_staff
, is_active
=is_active
,
38 is_superuser
=is_superuser
, last_login
=now
,
39 date_joined
=now
, **extra_fields
)
40 user
.set_password(password
)
41 user
.save(using
=self
._db
)
44 """Create and save an CustomUser with the given username and password."""
45 def create_superuser(self
, username
, password
, **extra_fields
):
46 return self
._create
_user
(username
, password
, True, True, is_admin
=True,
49 """Create and save an FullOperator with the given email and password. """
50 def create_full_operator(self
, username
, password
=None, **extra_fields
):
51 return self
._create
_user
(username
, password
, False, False, is_full_operator
=True,
54 """Create and save an BasicUser with the given email and password. """
55 def create_basic_user(self
, username
, password
=None, **extra_fields
):
56 return self
._create
_user
(username
, password
, False, False, is_basic_user
=True,
59 """Create and save an GuestUser with the given email and password. """
60 def create_guest_user(self
, username
, password
="guest", **extra_fields
):
61 return self
._create
_user
(username
, password
, False, False, is_guest_user
=True,
65 class AbstractCustomUser(AbstractBaseUser
, PermissionsMixin
):
66 """Abstract User with the same behaviour as Django's default User.
68 AbstractCustomUser does not have username field. Uses email as the
69 USERNAME_FIELD for authentication.
71 Use this if you need to extend EmailUser.
73 Inherits from both the AbstractBaseUser and PermissionMixin.
75 The following attributes are inherited from the superclasses:
81 username
= models
.CharField(_('username'), max_length
=255, unique
=True, db_index
=True)
83 email
= models
.EmailField(_('email address'), max_length
=255, unique
=True)
85 first_name
= models
.CharField(_('first name'), max_length
=255, blank
=True)
86 last_name
= models
.CharField(_('last name'), max_length
=255, blank
=True)
87 phone
= models
.CharField(_('phone'), max_length
=100, blank
=True)
88 # user_groups = models.ManyToManyField(UserGroup, verbose_name=_('user groups'), blank=True)
90 is_admin
= models
.BooleanField(_('admin status'), default
=False)
91 is_full_operator
= models
.BooleanField(_('full_operator status'), default
=False)
92 is_basic_user
= models
.BooleanField(_('basic_user status'), default
=False)
93 is_guest_user
= models
.BooleanField(_('guest_user status'), default
=False)
95 is_staff
= models
.BooleanField(
96 _('staff status'), default
=False, help_text
=_(
97 'Designates whether the user can log into this admin site.'))
98 is_active
= models
.BooleanField(_('active'), default
=True, help_text
=_(
99 'Designates whether this user should be treated as '
100 'active. Unselect this instead of deleting accounts.'))
102 date_joined
= models
.DateTimeField(_('date joined'), default
=timezone
.now
)
104 objects
= CustomUserManager()
106 USERNAME_FIELD
= 'username'
107 REQUIRED_FIELDS
= ['email', 'first_name', 'last_name', 'phone', ]
110 verbose_name
= _('custom user')
111 verbose_name_plural
= _('custom users')
114 def get_full_name(self
):
115 """Return the fullname."""
116 return "%s %s" % (self
.last_name
, self
.first_name
)
118 def get_short_name(self
):
119 """Return the firstname."""
120 return self
.first_name
123 class CustomUser(AbstractCustomUser
):
125 Concrete class of AbstractCustomUser.
127 Use this if you don't need to extend CustomUser.
131 class Meta(AbstractCustomUser
.Meta
):
132 swappable
= 'AUTH_USER_MODEL'
134 def count_admins(self
):
135 return CustomUser
.objects
.filter(is_admin
=True).count()
137 def count_employee(self
):
138 return CustomUser
.objects
.filter(is_full_operator
=True).count()
140 def count_basic_users(self
):
141 return CustomUser
.objects
.filter(is_basic_user
=True).count()
143 def count_inactives(self
):
144 return CustomUser
.objects
.filter(is_active
=False).count()
147 return self
.is_guest_user
149 def get_avatar(self
):
151 return "assets/img/employer.png"
152 elif self
.is_full_operator
:
153 return "assets/img/employer.png"
154 elif self
.is_basic_user
:
155 return "assets/img/employer.png"
156 elif self
.is_guest_user
:
157 return "assets/img/account_circle.png"
159 def get_user_role(self
):
162 elif self
.is_full_operator
:
163 return 1, "Full operator"
164 elif self
.is_basic_user
:
165 return 2, "Basic user"
166 elif self
.is_guest_user
:
167 return 3, "Guest user"
169 def get_user_role_name(self
):
172 elif self
.is_full_operator
:
173 return "Full operator"
174 elif self
.is_basic_user
:
176 elif self
.is_guest_user
:
179 def has_perm(self
, perm
, obj
=None):
180 if perm
== 'deploymenthandler':
181 if self
.is_guest_user
:
186 super.has_perm(perm
, obj
)
189 class CustomSession(AbstractBaseSession
):
190 account_id
= models
.IntegerField(null
=True, db_index
=True)
193 def get_session_store_class(cls
):