diff --git a/re2o/templatetags/acl.py b/re2o/templatetags/acl.py index f2eb1062..17a51baf 100644 --- a/re2o/templatetags/acl.py +++ b/re2o/templatetags/acl.py @@ -135,7 +135,6 @@ MODEL_NAME = { 'SwitchBay': topologie.models.SwitchBay, # users 'User': users.models.User, - 'Mail': users.models.Mail, 'MailAlias': users.models.MailAlias, 'Adherent': users.models.Adherent, 'Club': users.models.Club, diff --git a/users/admin.py b/users/admin.py index 92528455..8389c029 100644 --- a/users/admin.py +++ b/users/admin.py @@ -34,7 +34,6 @@ from reversion.admin import VersionAdmin from .models import ( User, - Mail, MailAlias, ServiceUser, School, @@ -127,6 +126,7 @@ class UserAdmin(VersionAdmin, BaseUserAdmin): list_display = ( 'pseudo', 'surname', + 'external_mail', 'school', 'is_admin', 'shell' @@ -140,7 +140,7 @@ class UserAdmin(VersionAdmin, BaseUserAdmin): 'Personal info', { 'fields': - ('surname', 'school', 'shell', 'uid_number') + ('surname', 'external_mail', 'school', 'shell', 'uid_number') } ), ('Permissions', {'fields': ('is_admin', )}), @@ -155,6 +155,7 @@ class UserAdmin(VersionAdmin, BaseUserAdmin): 'fields': ( 'pseudo', 'surname', + 'external_mail', 'school', 'is_admin', 'password1', diff --git a/users/forms.py b/users/forms.py index edcaf569..8631d28b 100644 --- a/users/forms.py +++ b/users/forms.py @@ -53,7 +53,6 @@ from .models import ( School, ListRight, Whitelist, - Mail, MailAlias, ListShell, Ban, @@ -307,7 +306,6 @@ class AdherentForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): self.fields['room'].label = 'Chambre' self.fields['room'].empty_label = "Pas de chambre" self.fields['school'].empty_label = "Séléctionner un établissement" - class Meta: model = Adherent fields = [ @@ -600,10 +598,10 @@ class MailAliasForm(FormRevMixin, ModelForm): class Meta: model = MailAlias - exclude = ['mail'] + exclude = ['user'] class MailForm(FormRevMixin, ModelForm): - """Creation, edition d'un objet mail""" + """Creation, edition des paramètres mail""" def __init__(self, *args, **kwargs): prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(MailForm, self).__init__(*args, prefix=prefix, **kwargs) @@ -612,5 +610,5 @@ class MailForm(FormRevMixin, ModelForm): self.fields['internal_address'].label = 'Adresse mail interne' class Meta: - model = Mail - exclude = ['user'] + model = User + fields = ['external_mail', 'redirection', 'internal_address'] diff --git a/users/migrations/0073_auto_20180629_1614.py b/users/migrations/0073_auto_20180629_1614.py new file mode 100644 index 00000000..681cb722 --- /dev/null +++ b/users/migrations/0073_auto_20180629_1614.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-06-29 14:14 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import re2o.mixins + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0072_auto_20180426_2021'), + ] + + operations = [ + migrations.CreateModel( + name='MailAlias', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('valeur', models.CharField(help_text="username de l'adresse mail", max_length=64, unique=True)), + ], + bases=(re2o.mixins.RevMixin, re2o.mixins.AclMixin, models.Model), + ), + migrations.RenameField( + model_name='user', + old_name='email', + new_name='external_mail', + ), + migrations.AddField( + model_name='user', + name='internal_address', + field=models.BooleanField(default=False, help_text="Activer ou non l'utilisation de l'adresse mail interne"), + ), + migrations.AddField( + model_name='user', + name='redirection', + field=models.BooleanField(default=False, help_text='Activer ou non la redirection du mail interne vers le mail externe'), + ), + migrations.AddField( + model_name='mailalias', + name='user', + field=models.ForeignKey(blank=True, help_text='Utilisateur associé', null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/users/migrations/0073_mail_mailalias.py b/users/migrations/0073_mail_mailalias.py deleted file mode 100644 index ce676464..00000000 --- a/users/migrations/0073_mail_mailalias.py +++ /dev/null @@ -1,38 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.10.7 on 2018-06-26 19:07 -from __future__ import unicode_literals - -from django.conf import settings -from django.db import migrations, models -import django.db.models.deletion -import re2o.mixins - - -class Migration(migrations.Migration): - - dependencies = [ - ('users', '0072_auto_20180426_2021'), - ] - - operations = [ - migrations.CreateModel( - name='Mail', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('external_mail', models.EmailField(help_text='Mail externe', max_length=254)), - ('redirection', models.BooleanField(default=False)), - ('internal_address', models.BooleanField(default=False)), - ('user', models.OneToOneField(help_text="Object mail d'un User", on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), - ], - bases=(re2o.mixins.RevMixin, re2o.mixins.AclMixin, models.Model), - ), - migrations.CreateModel( - name='MailAlias', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('valeur', models.CharField(help_text="username de l'adresse mail", max_length=64, unique=True)), - ('mail', models.ForeignKey(help_text='Objects Mail associé', on_delete=django.db.models.deletion.CASCADE, to='users.Mail')), - ], - bases=(re2o.mixins.RevMixin, re2o.mixins.AclMixin, models.Model), - ), - ] diff --git a/users/migrations/0074_transfermail.py b/users/migrations/0074_transfermail.py deleted file mode 100644 index dbf6adca..00000000 --- a/users/migrations/0074_transfermail.py +++ /dev/null @@ -1,36 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.10.7 on 2018-06-26 18:34 -from __future__ import unicode_literals - -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ('users', '0073_mail_mailalias'), - ] - - def create_mail(apps, schema_editor): - db_alias = schema_editor.connection.alias - user = apps.get_model('users','User') - mail = apps.get_model('users','Mail') - mailalias = apps.get_model('users','MailAlias') - users = user.objects.using(db_alias).all() - for us in users: - ma = mail() - ma.user=us - ma.external_mail = us.email - ma.save() - al = mailalias() - al.mail = ma - al.valeur = us.pseudo - al.save() - - def uncreatemail(apps,schema_editor): - pass - - operations = [ - migrations.RunPython(create_mail, uncreatemail) - ] diff --git a/users/models.py b/users/models.py index 233db6a4..7da3e195 100644 --- a/users/models.py +++ b/users/models.py @@ -134,6 +134,7 @@ class UserManager(BaseUserManager): self, pseudo, surname, + external_mail, password=None, su=False ): @@ -147,6 +148,7 @@ class UserManager(BaseUserManager): pseudo=pseudo, surname=surname, name=surname, + external_mail=external_mail, ) user.set_password(password) @@ -155,19 +157,19 @@ class UserManager(BaseUserManager): user.save(using=self._db) return user - def create_user(self, pseudo, surname, password=None): + def create_user(self, pseudo, surname, external_mail, password=None): """ Creates and saves a User with the given pseudo, name, surname, email, and password. """ - return self._create_user(pseudo, surname, password, False) + return self._create_user(pseudo, surname, external_mail, password, False) - def create_superuser(self, pseudo, surname, password): + def create_superuser(self, pseudo, surname, external_mail, password): """ Creates and saves a superuser with the given pseudo, name, surname, email, and password. """ - return self._create_user(pseudo, surname, password, True) + return self._create_user(pseudo, surname, external_mail, password, True) class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser, @@ -186,13 +188,21 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser, ) surname = models.CharField(max_length=255) - email = models.EmailField() pseudo = models.CharField( max_length=32, unique=True, help_text="Doit contenir uniquement des lettres, chiffres, ou tirets", validators=[linux_user_validator] ) + external_mail = models.EmailField() + redirection = models.BooleanField( + default=False, + help_text='Activer ou non la redirection du mail interne vers le mail externe' + ) + internal_address = models.BooleanField( + default=False, + help_text='Activer ou non l\'utilisation de l\'adresse mail interne' + ) school = models.ForeignKey( 'School', on_delete=models.PROTECT, @@ -225,7 +235,7 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser, ) USERNAME_FIELD = 'pseudo' - REQUIRED_FIELDS = ['surname'] + REQUIRED_FIELDS = ['surname', 'external_mail'] objects = UserManager() @@ -519,7 +529,7 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser, user_ldap.sn = self.pseudo user_ldap.dialupAccess = str(self.has_access()) user_ldap.home_directory = '/home/' + self.pseudo - user_ldap.mail = self.email + user_ldap.mail = self.get_mail() user_ldap.given_name = self.surname.lower() + '_'\ + self.name.lower()[:3] user_ldap.gid = LDAP['user_gid'] @@ -672,10 +682,10 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser, """ Return the mail address choosen by the user """ - if not self.mail.internal_address: - return self.mail.external_mail + if not self.internal_address: + return self.external_mail else: - return self.mail.mailalias_set.get(valeur=self.pseudo) + return self.mailalias_set.get(valeur=self.pseudo) def get_next_domain_name(self): """Look for an available name for a new interface for @@ -1586,49 +1596,18 @@ class LdapServiceUserGroup(ldapdb.models.Model): return self.name -class Mail(RevMixin, AclMixin, models.Model): - """ - Mail account of a user - - Compte mail d'un utilisateur - """ - external_mail = models.EmailField(help_text="Mail externe") - user = models.OneToOneField( - 'User', - on_delete=models.CASCADE, - help_text="Object mail d'un User" - ) - redirection = models.BooleanField( - default=False - ) - internal_address = models.BooleanField( - default=False - ) - - def __str__(self): - return self.user.get_mail() - - def can_edit(self, user_request, *_args, **_kwargs): - """ - Check if the user can edit the mail - """ - - if user_request.has_perm('users.change_mail') or user_request == self.user: - return True, None - else: - return False, "Vous n'avez pas les droits suffisants et n'êtes pas propriétairs de cet alias" - - class MailAlias(RevMixin, AclMixin, models.Model): """ Define a alias for a user Mail Définit un aliase pour un Mail d'utilisateur """ - mail = models.ForeignKey( - 'Mail', + user = models.ForeignKey( + User, on_delete=models.CASCADE, - help_text="Objects Mail associé" + help_text="Utilisateur associé", + null=True, + blank=True ) valeur = models.CharField( unique=True, @@ -1636,7 +1615,6 @@ class MailAlias(RevMixin, AclMixin, models.Model): help_text="username de l'adresse mail" ) - def __str__(self): return self.valeur + OptionalUser.get_cached_value('mail_extension') diff --git a/users/templates/users/profil.html b/users/templates/users/profil.html index d402e108..770ee8fa 100644 --- a/users/templates/users/profil.html +++ b/users/templates/users/profil.html @@ -436,8 +436,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,