forms.py 2.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017 Antoine BERNARD
# Authors: Antoine BERNARD <abernard@crans.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
    Formulaire de l'application password_reset
"""
#: Import des formulaires
from django import forms

#: i18n de l'intranet
from django.utils.translation import ugettext_lazy as _

#: Fonctions de communication avec la base LDAP
from lc_ldap import shortcuts

class EmailForm(forms.Form):
    """
	Formulaire de demande d'une adresse e-mail.
    """
    email = forms.EmailField(
        label=_(u'Adresse e-mail'),
        max_length=254,
        required=True
    )

    def get_user(self):
        """
            Renvoie l'objet LDAP à partir de l'e-mail
        """
        try:
            email = self.cleaned_data['email']
            conn = shortcuts.lc_ldap_readonly()
            # On cherches les objets LDAP tels que :
            # * le mail est dans le champ `mail` ou `mailExt`
            # * et il a droit de se connecter
            # * et c'est un adhérent
            # * et il a un compte crans
            res = conn.search(
                u"(&(|(mail=%s)(mailExt=%s))\
                (!(shadowExpire=0))(aid=*)(uid=*))"
                % (email, email)
            )
            return res[0]
        except IndexError:
            return []


class UsernameForm(forms.Form):
    """
        Formulaire de demande d'un login Cr@ns.
    """
    username = forms.CharField(
        label=_(u"Nom d'utilisateur Cr@ns"),
        max_length=254,
        required=True
    )

    def get_user(self):
        """
            Renvoie l'objet LDAP à partir du login
        """
        try:
            login = self.cleaned_data['username']
            conn = shortcuts.lc_ldap_readonly()
            # On cherches les objets LDAP tels que :
            # * le login est dans le champ `uid`
            # * et il a droit de se connecter
            # * et c'est un adhérent
            res = conn.search(u"(&(uid=%s)(!(shadowExpire=0))(aid=*))" % login)
            return res[0]
        except IndexError:
            return []