
connexion la base LDAP chaque authentification. darcs-hash:20061213181047-68412-f41c971d417a0374945e56b459fc702cb99ec560.gz
270 lines
10 KiB
Python
Executable file
270 lines
10 KiB
Python
Executable file
#!/usr/bin/env python
|
||
# -*- coding: iso-8859-15 -*-
|
||
# #############################################################
|
||
# ..
|
||
# .... ............ ........
|
||
# . ....... . .... ..
|
||
# . ... .. .. .. .. ..... . ..
|
||
# .. .. ....@@@. .. . ........ .
|
||
# .. . .. ..@.@@..@@. .@@@@@@@ @@@@@@. ....
|
||
# .@@@@. .@@@@. .@@@@..@@.@@..@@@..@@@..@@@@.... ....
|
||
# @@@@... .@@@.. @@ @@ .@..@@..@@...@@@. .@@@@@. ..
|
||
# .@@@.. . @@@. @@.@@..@@.@@..@@@ @@ .@@@@@@.. .....
|
||
# ...@@@.... @@@ .@@.......... ........ ..... ..
|
||
# . ..@@@@.. . .@@@@. .. ....... . .............
|
||
# . .. .... .. .. . ... ....
|
||
# . . .... ............. .. ...
|
||
# .. .. ... ........ ... ...
|
||
# ................................
|
||
#
|
||
# #############################################################
|
||
# __init__.py
|
||
#
|
||
# Classe impression
|
||
#
|
||
# Copyright (c) 2006 by www.crans.org
|
||
# #############################################################
|
||
|
||
|
||
import cherrypy, sys, os, datetime
|
||
import crans.utils.exceptions
|
||
sys.path.append('/usr/scripts/gestion/')
|
||
|
||
|
||
# ######################################################## #
|
||
# COMMAND LINE OPTION #
|
||
# ######################################################## #
|
||
#
|
||
#
|
||
|
||
from optparse import OptionParser
|
||
|
||
parser = OptionParser()
|
||
parser.add_option("-d", "--dev",
|
||
action="store_true", dest="dev", default=False,
|
||
help="launch in dev mode")
|
||
parser.add_option("-p", "--port",
|
||
action="store", type="int", dest="port",
|
||
help="change server port")
|
||
|
||
(options, args) = parser.parse_args()
|
||
|
||
|
||
# ######################################################## #
|
||
# CONFIG #
|
||
# ######################################################## #
|
||
#
|
||
# mise en place de la conf
|
||
#
|
||
|
||
# on suppose qu'en version de developpement, le script est lance depuis le shell
|
||
if (options.dev):
|
||
cherrypy.config.update(file=os.getcwd() + "/conf/intranet.cfg")
|
||
cherrypy.config.update(file=os.getcwd() + "/conf/dev.cfg")
|
||
settings={'global': { 'rootDir': os.getcwd() } }
|
||
cherrypy.config.update(settings)
|
||
|
||
else:
|
||
cherrypy.config.update(file="/usr/scripts/intranet/conf/intranet.cfg")
|
||
cherrypy.config.update(file="/usr/scripts/intranet/conf/prod.cfg")
|
||
|
||
# changer le port ??
|
||
if (options.port):
|
||
settings={'global':{'server.socketPort':options.port}}
|
||
cherrypy.config.update(settings)
|
||
|
||
|
||
# import du CransLdap qu'il va bien (on utilise CransLdap et non crans_ldap car on veut
|
||
# forcer l'ouverture d'une nouvelle connexion <20> chaque login)
|
||
if (cherrypy.config.configMap["global"]["server.environment"] == "development"):
|
||
from ldap_crans_test import CransLdap
|
||
cherrypy.log("settings : unsing test ldap : env=" + cherrypy.config.configMap["global"]["server.environment"], "LDAP")
|
||
else:
|
||
from ldap_crans import CransLdap
|
||
cherrypy.log("settings : unsing prod ldap : env=" + cherrypy.config.configMap["global"]["server.environment"], "LDAP")
|
||
|
||
# ######################################################## #
|
||
# FILTRES MAISON #
|
||
# ######################################################## #
|
||
|
||
from plugins.domfilter import DOMFilter
|
||
from plugins.templatesfilter import TemplatesFilter
|
||
from plugins.verifdroitsfilter import VerifDroitsFilter
|
||
from crans.mail import quickSend
|
||
# ######################################################## #
|
||
# SERVER #
|
||
# ######################################################## #
|
||
class Intranet:
|
||
def __init__(self):
|
||
from pages import monCompte, impression, factures, digicode, mesmachines
|
||
from pages import gestionFactures
|
||
|
||
# liste des modules disponibles
|
||
self.monCompte = monCompte.monCompte()
|
||
self.sous = factures.root()
|
||
self.impression = impression.root()
|
||
self.digicode = digicode.root()
|
||
self.mesMachines = mesmachines.root()
|
||
self.gestionFactures = gestionFactures.root()
|
||
|
||
# liste des modules en developpement
|
||
#if (cherrypy.config.configMap["global"]["server.environment"] == "development"):
|
||
|
||
|
||
_cpFilterList = [TemplatesFilter(), DOMFilter(), VerifDroitsFilter()]
|
||
|
||
def index(self):
|
||
return {
|
||
'template':'accueil',
|
||
'values':{},
|
||
'stylesheets':['accueil.css'],
|
||
}
|
||
index.exposed= True
|
||
|
||
def info(self):
|
||
return {
|
||
'template':'info-diverses',
|
||
'values':{},
|
||
'stylesheets':['accueil.css'],
|
||
}
|
||
info.exposed = True
|
||
|
||
def send_error_repport(self, **kw):
|
||
|
||
# on récupère tout de suite le traceback
|
||
tb = crans.utils.exceptions.formatExc()
|
||
# entêtes du mail
|
||
exp = "intranet"
|
||
dest = cherrypy.config.get("mail.bugreport", "nounous@crans.org")
|
||
subject = "Rapport de Bug"
|
||
text = """
|
||
Bonsoir,
|
||
|
||
Ceci est un rapport de bug envoye par l'intranet.
|
||
|
||
%s
|
||
""" % "\n".join( [ "%s: %s" % (str(a), str(kw[a])) for a in kw] )
|
||
|
||
#On ajoute des variables de cherrypy
|
||
text += "\n= Cherrypy vars =\n"
|
||
try:
|
||
text += "url: %s\n" % cherrypy.request.browser_url
|
||
except:
|
||
pass
|
||
try:
|
||
text += "headers: \n %s\n" % "\n".join( [" %s: %s" % (str(a), str(cherrypy.request.headers[a])) for a in cherrypy.request.headers] )
|
||
except:
|
||
pass
|
||
try:
|
||
text += "query_string: %s\n" % cherrypy.request.query_string
|
||
except:
|
||
pass
|
||
try:
|
||
text += "path: %s\n" % cherrypy.request.path
|
||
except:
|
||
pass
|
||
|
||
|
||
# on ajoute le traceback
|
||
text += "\n= Traceback =\n"
|
||
text += tb
|
||
|
||
#on signe, quand même !
|
||
text += "\n-- \nRoot.py pour l'intranet\n"
|
||
|
||
quickSend(exp, dest, subject, text)
|
||
return self.index()
|
||
|
||
send_error_repport.exposed = True
|
||
|
||
def testErreur(self):
|
||
raise Exception, "sdlfkjqmsdklj"
|
||
|
||
testErreur.exposed = True
|
||
|
||
def test(self):
|
||
return cherrypy.request.remote_addr
|
||
test.exposed = True
|
||
|
||
def _cp_on_http_error(self, status, message):
|
||
if (cherrypy.config.configMap["global"]["server.environment"] == "development"):
|
||
cherrypy._cputil._cp_on_http_error(status, message)
|
||
return
|
||
if status==403:
|
||
cherrypy.response.body = {
|
||
'template':'error403',
|
||
'values':{'status':status, 'message':message },
|
||
'standalone':False,
|
||
}
|
||
elif status==404:
|
||
cherrypy.response.body = {
|
||
'template':'error',
|
||
'values':{'status':status, 'message':message },
|
||
'standalone':False,
|
||
}
|
||
elif status==500:
|
||
self.send_error_repport(status = status, message = message )
|
||
# les filtres ne sont pas appliqués, on le fait à la main...
|
||
from plugins.templatesfilter import TemplatesFilter
|
||
TemplatesFilter().goWithThisDict({'template':'error', 'values':{'status':status, 'message':message }})
|
||
else:
|
||
self.send_error_repport(status = status, message = message)
|
||
cherrypy._cputil._cp_on_http_error(status, message)
|
||
|
||
|
||
# ######################################################## #
|
||
# LOGIN MAISON #
|
||
# ######################################################## #
|
||
#
|
||
# Methode pour afficher la template de login
|
||
#
|
||
def login(from_page = '', login = None, password = '', error_msg=''):
|
||
return {
|
||
'template':'login',
|
||
'values':{'login':login, 'password':password, 'from_page':from_page, 'message':error_msg},
|
||
'standalone':True
|
||
}
|
||
#
|
||
# methode qui verifie le login
|
||
#
|
||
def verifLogin(login = '', password = ''):
|
||
message = None
|
||
try:
|
||
if login != '' and password != '':
|
||
cherrypy.session['LDAP'] = CransLdap()
|
||
LDAP = cherrypy.session['LDAP']
|
||
adh = LDAP.search('uid=' + login)['adherent'][0]
|
||
if adh.checkPassword(password):
|
||
cherrypy.session['uid'] = login
|
||
cherrypy.session['session_key'] = True
|
||
cherrypy.session['droits'] = adh.droits()
|
||
cherrypy.log("User logged in : %s" % cherrypy.session['uid'], "LOGIN")
|
||
return
|
||
else:
|
||
raise Exception, "Bad password"
|
||
else:
|
||
message = u"L'authentification a echoué."
|
||
raise Exception, "Empty string"
|
||
except Exception, e:
|
||
cherrypy.log("%s (login:%s)" % (str(e), login), "LOGIN", 1)
|
||
message = u"L'authentification a echoué."
|
||
return message
|
||
|
||
|
||
|
||
|
||
|
||
# on indique tout ca a cherrypy
|
||
settings={'/': {
|
||
'sessionAuthenticateFilter.checkLoginAndPassword': verifLogin,
|
||
'sessionAuthenticateFilter.loginScreen': login
|
||
}}
|
||
cherrypy.config.update(settings)
|
||
|
||
|
||
|
||
# ######################################################## #
|
||
# LANCEMENT DE CHERRYPY #
|
||
# ######################################################## #
|
||
cherrypy.root = Intranet()
|
||
cherrypy.server.start()
|