Browse files

first commit

  • Loading branch information...
0 parents commit ed2087818ef7b6ffbc00430315a3d3901213833d @revolunet committed Feb 1, 2010
Showing with 495 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +28 −0 README.md
  3. 0 __init__.py
  4. +12 −0 includes.py
  5. BIN static/img/loading.gif
  6. +101 −0 static/js/Ext.ux.EasyChangePass.js
  7. +185 −0 static/js/Ext.ux.EasyLogin.js
  8. +59 −0 templates/login.html
  9. +107 −0 views.py
3 .gitignore
@@ -0,0 +1,3 @@
+*.svn
+*.pyc
+settings.py
28 README.md
@@ -0,0 +1,28 @@
+django-extjs-login
+=====================
+
+created by [gary][5] and [julien][6] @revolunet and hosted at [GitHub][4]
+
+this an ExtJs login Widget with a django backend
+
+**Features** :
+
+ - ExtJs login widget
+ - password reset via email
+ - password change view
+
+
+**Requirements :**
+
+ - [django-skeleton][1]
+ - [django-extjs][2]
+ - [python-imaging][3]
+
+
+
+ [1]: http://github.com/revolunet/django-skeleton
+ [2]: http://github.com/revolunet/django-extjs
+ [3]: http://www.pythonware.com/products/pil/
+ [4]: https://github.com/revolunet/django-extjs-explorer
+ [5]: mailto:gary@chewam.com
+ [6]: mailto:julien@bouquillon.com
0 __init__.py
No changes.
12 includes.py
@@ -0,0 +1,12 @@
+# here needed JS and CSS
+
+JS = [
+ 'Ext.ux.EasyChangePass.js',
+ 'Ext.ux.EasyLogin.js',
+
+]
+
+CSS = [
+
+]
+
BIN static/img/loading.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
101 static/js/Ext.ux.EasyChangePass.js
@@ -0,0 +1,101 @@
+
+
+Ext.ux.EasyChangePass = Ext.extend(Ext.Panel, {
+
+ // title: 'Change password'
+ // ,iconCls: 'icon-key'
+ border:false
+ ,initComponent:function() {
+
+ this.form_change_pass = new Ext.FormPanel({
+ waitMsgTarget: true
+ ,labelAlign: 'right'
+ ,labelWidth: 150
+ ,disabled: false
+ ,border:false
+
+ ,items: [
+ {
+ html:'Changement de votre mot de passe'
+ ,style:'margin:20px'
+ ,border:false
+ },
+
+ {
+ xtype:'textfield',
+ fieldLabel: 'mot de passe actuel',
+ inputType:'password',
+ name: 'current',
+ width:120
+ }
+ ,{
+ xtype:'textfield',
+ fieldLabel: 'nouveau mot de passe',
+ inputType:'password',
+ name: 'new1',
+ width:120
+ }
+ ,{
+ xtype:'textfield',
+ fieldLabel: 'confirmation',
+ inputType:'password',
+ name: 'new2',
+ width:120
+ }
+ , {
+ xtype:'button'
+ ,text:'changer le mot de passe'
+ ,iconCls:'icon-disk'
+ ,style:'margin-top:20px;margin-left:auto;margin-right:auto'
+ ,listeners: {
+ scope:this
+ ,'click': {
+ fn: function(button, e) {
+ var formpanel = button.findParentByType('form');
+ var form = formpanel.getForm();
+ form.el.mask('Loading...');
+ form.submit({
+ url: '/apps/login/changepassword'
+ ,method: 'POST'
+ ,scope: this
+ ,success: function(form, action) {
+ Ext.Msg.alert("Success", "Le mot de passe a été changé avec succès");
+ console.log(this);
+ this.fireEvent('submitSuccess');
+ }
+ ,failure: function(form, action) { form.el.unmask(); Ext.Msg.alert("Failure", action.result.msg);}
+ });
+ }
+ }
+ }
+ }
+ ]
+
+
+ })
+ this.form_change_pass
+ this.items = this.form_change_pass;
+
+ // Ext.apply(this, {
+ // layout:'fit'
+ // ,items:this.form_change_pass
+ // })
+
+ Ext.ux.EasyChangePass.superclass.initComponent.apply(this, arguments);
+
+ this.addEvents(['submitSuccess']);
+ }
+
+
+});
+
+// register xtype
+
+
+
+Ext.reg('EasyChangePass', Ext.ux.EasyChangePass);
+
+
+
+
+
185 static/js/Ext.ux.EasyLogin.js
@@ -0,0 +1,185 @@
+/*
+ * url = "/apps/form"
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+Ext.ux.EasyLogin = Ext.extend(Ext.form.FormPanel, {
+
+ width:250
+ ,height:135
+ ,username:''
+ ,frame:true
+ ,title:'Identification'
+ ,bodyStyle:"padding:5px 5px 5px 5px"
+ ,labelWidth:100
+ ,labelAlign:'right'
+ ,cls:"easy-login"
+ ,redirectUrl:false
+ ,defaults:{
+ xtype:"textfield"
+ ,anchor:"100%"
+
+
+
+ }
+
+ ,initComponent:function() {
+
+ this.submitButton = new Ext.Button({
+ text:"vérifier"
+ ,iconCls:"icon-key"
+ ,scope:this
+ ,handler:this.submitLogin
+ });
+
+ this.resetButton = new Ext.Button({
+ text:"mot de passe perdu"
+ ,iconCls:"icon-help"
+ ,scope:this
+ ,handler:this.resetPassword
+ });
+
+
+ this.buttons = [this.submitButton, this.resetButton];
+
+ Ext.apply(this, {
+ items:[{
+ fieldLabel:"utilisateur"
+ ,name:"login"
+ ,id:"login"
+ ,value:this.username
+ ,listeners:{
+ scope:this,
+ specialkey:function(f,o){
+ if(o.getKey()==13){
+
+ //console.log(this, f, o);
+ this.submitLogin();
+ //f.submit();
+ }
+ }
+ }
+ }, {
+ fieldLabel:"mot de passe"
+ ,inputType:"password"
+ ,name:"password"
+ ,id:"password"
+ ,value:''
+ ,listeners:{
+ scope:this,
+ specialkey:function(f,o){
+ if(o.getKey()==13){
+
+ //console.log(this, f, o);
+ this.submitLogin();
+ //f.submit();
+ }
+ }
+ }
+ }]
+ });
+
+ Ext.ux.EasyLogin.superclass.initComponent.apply(this, arguments);
+
+
+
+ this.on('afterlayout',function() {
+ if (this.username == '') {
+
+ this.getForm().findField('login').focus();
+ }
+ else {
+ this.getForm().findField('password').focus();
+ }
+ })
+
+ }
+ ,resetPassword:function(btn) {
+ Ext.Msg.prompt("votre email", "Saisissez votre email", function(btn, email) {
+ if(btn == 'ok') {
+ Ext.Ajax.request({
+ url:'/apps/login/lostpassword',
+ params: {email: email},
+ scope:this,
+ success: function(response) {
+ json = Ext.decode(response.responseText)
+ if (json.success) {
+
+ Ext.Msg.show({
+ title:'Succès',
+ msg: 'Un email contenant les instructions vous a été envoyé',
+ buttons: Ext.Msg.OK,
+ icon: Ext.MessageBox.INFO
+ });
+ }
+ else {
+
+ Ext.Msg.show({
+ title:'Erreur',
+ msg: json.msg,
+ buttons: Ext.Msg.OK,
+ icon: Ext.MessageBox.WARNING
+ });
+ }
+
+ }
+ ,failure:function() {
+ Ext.Msg.show({
+ title:'Erreur',
+ msg: 'Impossible',
+ buttons: Ext.Msg.OK,
+ icon: Ext.MessageBox.WARNING
+ });
+ }
+ })
+
+ }
+
+ }, this)
+
+ }
+ ,submitLogin:function(btn) {
+ // this.submitButton = btn;
+ this.submitButton.disable();
+ this.submitButton.setIconClass("icon-loading");
+ this.getForm().submit({
+ url:"/apps/login"
+ ,scope:this
+ ,success:this.submitLoginCallback
+ ,failure:this.submitLoginCallback
+ });
+ }
+
+ ,submitLoginCallback:function(form, action) {
+
+ var json = Ext.decode(action.response.responseText);
+ //alert(json);
+ //console.log(json);
+ if (json.success === true) window.location = json.redirect;
+ else this.error(form, json);
+ }
+
+ ,error:function(form, json) {
+ //console.log(json);
+ Ext.Msg.show({
+ buttons: Ext.Msg.OK,
+ // fn: processResult,
+ animEl: 'elId',
+ title: 'erreur',
+ msg: 'Mauvais utilisateur ou mot de passe',
+ icon: Ext.MessageBox.ERROR
+
+ });
+ this.submitButton.enable();
+ this.submitButton.setIconClass("icon-key");
+
+ }
+});
59 templates/login.html
@@ -0,0 +1,59 @@
+{% extends "base.html" %}
+
+ {% block title %}Login LFAweb{% endblock %}
+
+ {% block extrahead %}
+
+ <style>
+ * {
+ font-family:Verdana, Arial, Trebuchet ms;
+ font-size:12px;
+ }
+ .easy-login .icon-loading {
+ background: transparent url(/apps/login/static/img/loading.gif) no-repeat 1px 2px;
+ }
+
+
+ </style>
+
+ {% endblock %}
+
+
+ {% block content %}
+ <center>
+ {% block branding %}
+ <br><br>
+ <span class="menulogo">Bienvenue sur LFAweb</span>
+ <br><br>
+ {% endblock %}
+
+ <div id="test"></div>
+
+ <div id="login"></div>
+
+ <div style="color:darkgrey;font-size:10px">
+ powered by <a style="color:darkgrey;font-size:10px" href='http://www.revolunet.com' target='_blank'>revolunet.com</a> ©2009
+ </div>
+ <script type="text/javascript" src="/apps/login/static/js/Ext.ux.EasyLogin.js"></script>
+ <script type="text/javascript">
+ Ext.onReady(function() {
+
+ Ext.QuickTips.init();
+
+ var form = new Ext.ux.EasyLogin({
+ renderTo:'login'
+ ,username:'{{ username }}'
+ });
+
+ form.render("login");
+
+
+
+
+
+ });
+
+ </script>
+ </center>
+
+ {% endblock %}
107 views.py
@@ -0,0 +1,107 @@
+# -*- coding: UTF8 -*-
+
+from django.contrib.auth.models import User
+from django.shortcuts import render_to_response, get_list_or_404
+from django.http import Http404, HttpResponse, HttpResponseRedirect
+from django.template import RequestContext
+from django.contrib.auth import login, authenticate, get_backends
+
+from django.conf import settings
+
+from core.decorators import publish
+from django.contrib.auth.decorators import login_required
+from apps.django_extjs import utils
+
+@publish
+def default(request):
+ if request.user.is_authenticated() and request.GET.get('next'):
+ return HttpResponseRedirect(request.GET['next'])
+ from django.template import RequestContext
+ if request.method == 'POST':
+ user = request.POST['login']
+ apass = request.POST['password']
+ user = authenticate(username=user, password=apass)
+ if user is not None:
+ if user.is_active:
+ login(request, user)
+ resp = utils.JsonSuccess({'redirect':'/' })
+ resp = utils.set_cookie(resp, 'username', user.username)
+ return resp
+ else:
+ # Return a 'disabled account' error message
+ return utils.JsonError(u"Cet utilisateur est desactiv&eacute;")
+ else:
+ # Return an 'invalid login' error message.
+ return utils.JsonError("Mauvais utilisateur ou mot de passe")
+ params = {}
+ params['username'] = utils.get_cookie(request, 'username') or ''
+ response = render_to_response('login.html', params, context_instance=RequestContext(request))
+ return response
+
+
+@publish
+def logout(request):
+ from django.contrib.auth import logout as dlogout
+ dlogout(request)
+ return HttpResponseRedirect('/')
+
+
+def user_token(user):
+ import hashlib
+ salt = settings.SECRET_KEY
+ hash = hashlib.md5(user.email + salt).hexdigest()
+ return hash
+
+@publish
+@login_required
+def changepassword(request):
+ newpass1 = request.POST['new1']
+ newpass2 = request.POST['new2']
+ current = request.POST['current']
+ if request.user.check_password(current):
+ if newpass1==newpass2:
+ request.user.set_password(newpass1)
+ request.user.save()
+ if request.user.email:
+ try:
+ message = u'Votre mot de passe a été réinitialisé : %s \n\n%s' % (newpass1, settings.HOST)
+ request.user.email_user('Nouveau mot de passe', message)
+ except:
+ pass
+ return utils.JsonSuccess()
+ return utils.JsonError('Les mots de passe ne correspondent pas')
+
+
+
+@publish
+def resetpassword(request):
+ if request.GET.get('a') and request.GET.get('t'):
+ u = User.objects.get(pk = request.GET['a'])
+ token = user_token(u)
+ if request.GET['t'] == token:
+ newpass = User.objects.make_random_password(length=8)
+ u.set_password(newpass)
+ u.save()
+ message = u'Votre mot de passe LFAweb a été réinitialisé : %s \n\n%s' % (newpass, settings.HOST)
+ u.email_user('Nouveau mot de passe LFAweb', message)
+ # auto log user
+ backend = get_backends()[0]
+ u.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__)
+ authenticate(user = u)
+ login(request, u)
+ return HttpResponseRedirect('/')
+
+@publish
+def lostpassword(request):
+ if request.POST.get('email'):
+ try:
+ u = User.objects.get(email = request.POST['email'])
+ token = user_token(u)
+ link = '%s/apps/login/resetpassword?a=%s&t=%s' % (settings.HOST, u.pk, token)
+ message = u'Vous avez demandé à réinitialiser votre mot de passe LFAweb.\n\nCliquez ici pour le réinitialiser : %s\n\n%s\n\nOrigine de la demande : %s' % (link, settings.HOST, request.META.get('REMOTE_ADDR', '?'))
+ u.email_user('Mot de passe LFAweb', message)
+ return utils.JsonSuccess()
+ except:
+ return utils.JsonError("Email inconnu")
+
+ return HttpResponseRedirect('/')

0 comments on commit ed20878

Please sign in to comment.