Commit 438e8ce7 authored by chirac's avatar chirac
Browse files

Support des alias mails

parent 928990e7
......@@ -3,7 +3,7 @@ from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from reversion.admin import VersionAdmin
from .models import User, Right, ListRight, ListShell, Request, LdapUser, LdapUserGroup
from .models import User, Right, ListRight, ListShell, Request, MailAlias, LdapUser, LdapUserGroup
from .forms import UserChangeForm, UserCreationForm
......@@ -32,6 +32,9 @@ class LdapUserGroupAdmin(admin.ModelAdmin):
class ListRightAdmin(VersionAdmin):
list_display = ('listright',)
class MailAliasAdmin(VersionAdmin):
list_display = ('user','alias','redirection')
class ListShellAdmin(VersionAdmin):
list_display = ('shell',)
......@@ -75,6 +78,7 @@ admin.site.register(LdapUserGroup, LdapUserGroupAdmin)
admin.site.register(Right, RightAdmin)
admin.site.register(ListRight, ListRightAdmin)
admin.site.register(ListShell, ListShellAdmin)
admin.site.register(MailAlias, MailAliasAdmin)
admin.site.register(Request, RequestAdmin)
# Now register the new UserAdmin...
admin.site.unregister(User)
......
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
from django.conf import settings
import django.db.models.deletion
import ldapdb.models.fields
class Migration(migrations.Migration):
dependencies = [
('users', '0005_ldapuser_shadow_expire'),
]
operations = [
migrations.CreateModel(
name='MailAlias',
fields=[
('id', models.AutoField(auto_created=True, serialize=False, primary_key=True, verbose_name='ID')),
('alias', models.EmailField(max_length=254, unique=True)),
('redirection', models.EmailField(max_length=254)),
('user', models.ForeignKey(to=settings.AUTH_USER_MODEL, on_delete=django.db.models.deletion.PROTECT)),
],
),
migrations.AlterField(
model_name='ldapuser',
name='shadow_expire',
field=ldapdb.models.fields.IntegerField(null=True, db_column='shadowExpire', blank=True),
),
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
import users.models
class Migration(migrations.Migration):
dependencies = [
('users', '0006_auto_20160825_2213'),
]
operations = [
migrations.AlterField(
model_name='mailalias',
name='alias',
field=models.CharField(max_length=32, unique=True, help_text="N'indiquer que le préfixe de l'alias (@ares.fr)", validators=[users.models.mailalias_check]),
),
]
......@@ -19,8 +19,16 @@ from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
def linux_user_check(login):
""" Validation du pseudo pour respecter les contraintes unix"""
UNIX_LOGIN_PATTERN = re.compile("^[a-z_][a-z0-9_-]*[$]?$")
return UNIX_LOGIN_PATTERN.match(login)
if not UNIX_LOGIN_PATTERN.match(login):
raise forms.ValidationError("Ce login n'est pas valide")
return
def mailalias_check(alias):
""" Validation de l'alias mail pour respecter les contraintes"""
ALIAS_PATTERN = re.compile("^[a-z_][a-z0-9_-]*[$]?$")
if not ALIAS_PATTERN.match(alias):
raise forms.ValidationError("Cet alias n'est pas valide")
return
def linux_user_validator(login):
if not linux_user_check(login):
......@@ -272,6 +280,14 @@ class ListShell(models.Model):
def __str__(self):
return self.shell
class MailAlias(models.Model):
user = models.ForeignKey('User', on_delete=models.PROTECT)
alias = models.CharField(max_length=32, unique=True, help_text="N'indiquer que le préfixe de l'alias (devant @ares.fr)", validators=[mailalias_check])
redirection = models.EmailField()
def __str__(self):
return self.alias
class Request(models.Model):
PASSWD = 'PW'
EMAIL = 'EM'
......@@ -384,6 +400,10 @@ class StateForm(ModelForm):
model = User
fields = ['state']
class MailAliasForm(ModelForm):
class Meta:
model = MailAlias
fields = ['alias','redirection']
class ListRightForm(ModelForm):
class Meta:
......
......@@ -50,7 +50,25 @@
<td>Aucun</td>
{% endif %}
</table>
<h2>Alias Mail</h2>
<a class="btn btn-primary btn-sm" role="button" href="{% url 'users:add-mailalias' user.id %}"><i class="glyphicon glyphicon-edit"></i> Ajouter un alias</a>
<table class="table table-striped">
<thead>
<tr>
<th>Alias</th>
<th>Redirigé vers</th>
<th></th>
</tr>
</thead>
{% for alias in list_alias %}
<tr>
<td>{{ alias.alias }}</td>
<td>{{ alias.redirection }}</td>
<td><a class="btn btn-primary btn-sm" role="button" href="{% url 'users:edit-mailalias' alias.id %}"><i class="glyphicon glyphicon-hdd"></i> Editer</a>
<a class="btn btn-danger btn-sm" role="button" href="{% url 'users:del-mailalias' alias.id %}"><i class="glyphicon glyphicon-trash"></i> Supprimer</a></td>
</tr>
{% endfor %}
</table>
<br />
<br />
<br />
......
......@@ -12,6 +12,9 @@ urlpatterns = [
url(r'^add_listright/$', views.add_listright, name='add-listright'),
url(r'^edit_listright/(?P<listrightid>[0-9]+)$', views.edit_listright, name='edit-listright'),
url(r'^del_listright/$', views.del_listright, name='del-listright'),
url(r'^add_mailalias/(?P<userid>[0-9]+)$', views.add_mailalias, name='add-mailalias'),
url(r'^edit_mailalias/(?P<mailaliasid>[0-9]+)$', views.edit_mailalias, name='edit-mailalias'),
url(r'^del_mailalias/(?P<mailaliasid>[0-9]+)$', views.del_mailalias, name='del-mailalias'),
url(r'^profil/(?P<userid>[0-9]+)$', views.profil, name='profil'),
url(r'^index_listright/$', views.index_listright, name='index-listright'),
url(r'^mon_profil/$', views.mon_profil, name='mon-profil'),
......
......@@ -15,8 +15,8 @@ from django.core.urlresolvers import reverse
from django.db import transaction
from reversion import revisions as reversion
from users.models import User, Right, ListRight, Request
from users.models import DelRightForm, DelListRightForm, NewListRightForm
from users.models import User, Right, ListRight, Request, MailAlias
from users.models import DelRightForm, DelListRightForm, NewListRightForm, MailAliasForm
from users.models import EditInfoForm, BaseInfoForm, StateForm, RightForm, ListRightForm
from users.forms import PassForm, ResetPasswordForm
......@@ -219,11 +219,68 @@ def del_listright(request):
except ProtectedError:
messages.error(
request,
"L'établissement %s est affecté à au moins un user, \
"Le droit %s est affecté à au moins un user, \
vous ne pouvez pas le supprimer" % listright_del)
return redirect("/users/index_listright/")
return form({'userform': listright}, 'users/user.html', request)
@login_required
def add_mailalias(request, userid):
try:
user = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
if not request.user.has_perms(('bureau',)) and user != request.user:
messages.error(request, "Vous ne pouvez pas modifier un autre user que vous sans droit bureau")
return redirect("/users/profil/" + str(request.user.id))
alias = MailAliasForm(request.POST or None)
if alias.is_valid():
alias = alias.save(commit=False)
alias.user = user
with transaction.atomic(), reversion.create_revision():
alias.save()
reversion.set_user(request.user)
reversion.set_comment("Création")
messages.success(request, "L'alias a été ajouté")
return redirect("/users/profil/" + str(user.id))
return form({'userform': alias}, 'users/user.html', request)
@login_required
def edit_mailalias(request, mailaliasid):
try:
mailalias_instance = MailAlias.objects.get(pk=mailaliasid)
except MailAlias.DoesNotExist:
messages.error(request, u"Entrée inexistante" )
return redirect("/users/")
if not request.user.has_perms(('bureau',)) and mailalias_instance.user != request.user:
messages.error(request, "Vous ne pouvez pas modifier les alias d'autres user que vous sans droit bureau")
return redirect("/users/profil/" + str(request.user.id))
mailalias = MailAliasForm(request.POST or None, instance=mailalias_instance)
if mailalias.is_valid():
with transaction.atomic(), reversion.create_revision():
mailalias.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in mailalias.changed_data))
messages.success(request, "Alias modifié")
return redirect("/users/profil/" + str(mailalias_instance.user.id))
return form({'userform': mailalias}, 'users/user.html', request)
@login_required
def del_mailalias(request, mailaliasid):
try:
mailalias_instance = MailAlias.objects.get(pk=mailaliasid)
except MailAlias.DoesNotExist:
messages.error(request, u"Entrée inexistante" )
return redirect("/users/")
if not request.user.has_perms(('bureau',)) and mailalias_instance.user != request.user:
messages.error(request, "Vous ne pouvez pas modifier les alias d'autres user que vous sans droit bureau")
with transaction.atomic(), reversion.create_revision():
mailalias_instance.delete()
reversion.set_comment("Destruction")
messages.success(request, "L'alias a été supprimé")
return redirect("/users/profil/" + str(mailalias_instance.user.id))
@login_required
@permission_required('bureau')
def index(request):
......@@ -295,12 +352,14 @@ def profil(request, userid):
messages.error(request, "Vous ne pouvez pas afficher un autre user que vous sans droit bureau")
return redirect("/users/profil/" + str(request.user.id))
list_droits = Right.objects.filter(user=users)
list_alias = MailAlias.objects.filter(user=users)
return render(
request,
'users/profil.html',
{
'user': users,
'list_droits': list_droits,
'list_alias': list_alias,
}
)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment