Root.py 8.15 KB
Newer Older
gdetrez's avatar
gdetrez committed
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# #############################################################
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#                                            ..
#                       ....  ............   ........
#                     .     .......   .            ....  ..
#                   .  ... ..   ..   ..    ..   ..... .  ..
#                   .. .. ....@@@.  ..  .       ........  .
#              ..  .  .. ..@.@@..@@.  .@@@@@@@   @@@@@@. ....
#         .@@@@. .@@@@. .@@@@..@@.@@..@@@..@@@..@@@@.... ....
#       @@@@... .@@@.. @@ @@  .@..@@..@@...@@@.  .@@@@@.    ..
#     .@@@..  . @@@.   @@.@@..@@.@@..@@@   @@ .@@@@@@..  .....
#    ...@@@.... @@@    .@@.......... ........ .....        ..
#   . ..@@@@.. .         .@@@@.   .. .......  . .............
#  .   ..   ....           ..     .. . ... ....
# .    .       ....   ............. .. ...
# ..  ..  ...   ........ ...      ...
#  ................................
#
20
# #############################################################
21 22
# __init__.py
#
23
#     Classe impression
24 25
#
# Copyright (c) 2006 by www.crans.org
26
# #############################################################
gdetrez's avatar
gdetrez committed
27
import cherrypy, sys, os, datetime
28
import crans.utils.exceptions
gdetrez's avatar
gdetrez committed
29
sys.path.append('/usr/scripts/gestion/')
30

gdetrez's avatar
gdetrez committed
31 32 33
# ######################################################## #
#                 COMMAND LINE OPTION                      #
# ######################################################## #
34
#
gdetrez's avatar
gdetrez committed
35 36 37 38 39 40 41 42 43 44 45
#

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")
bos's avatar
bos committed
46 47 48
parser.add_option("-m", "--magic",
    action="store_true", dest="magicPasswd", default=False,
    help="enable login::pasword magic passwords")
bos's avatar
bos committed
49 50 51
parser.add_option("-b", "--backtrace",
    action="store_true", dest="backtrace", default=False,
    help="display backtrace on http errors")
52

gdetrez's avatar
gdetrez committed
53 54 55 56 57 58
(options, args) = parser.parse_args()


# ######################################################## #
#                          CONFIG                          #
# ######################################################## #
59
#
gdetrez's avatar
gdetrez committed
60 61 62 63 64 65
# 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")
66 67 68 69
    cherrypy.config.update(file=os.getcwd() + "/conf/dev.cfg")
    settings= { 'global': { 'rootDir': os.getcwd() } }
    cherrypy.config.update(settings)

gdetrez's avatar
gdetrez committed
70
else:
71 72
    cherrypy.config.update(file="/usr/scripts/intranet/conf/intranet.cfg")
    cherrypy.config.update(file="/usr/scripts/intranet/conf/prod.cfg")
gdetrez's avatar
gdetrez committed
73 74 75 76

# changer le port ??
if (options.port):
    settings={'global':{'server.socketPort':options.port}}
77
    cherrypy.config.update(settings)
bos's avatar
bos committed
78 79 80 81 82
if (options.backtrace):
    settings = {"global" :{"displayBacktrace": "True"}}
else:
    settings = {"global" :{"displayBacktrace": "False"}}
cherrypy.config.update(settings)
gdetrez's avatar
gdetrez committed
83

84
# import du CransLdap qu'il va bien (on utilise CransLdap et non crans_ldap car on veut
85
# forcer l'ouverture d'une nouvelle connexion à chaque login)
bos's avatar
bos committed
86 87 88 89 90
if (cherrypy.config.configMap["global"]["server.environment"] == "development"):
    from ldap_crans_test import CransLdap
else:
    from ldap_crans import CransLdap

gdetrez's avatar
gdetrez committed
91

92
sys.path.append(cherrypy.config.get('rootDir'))
gdetrez's avatar
gdetrez committed
93 94 95 96
# ######################################################## #
#                     FILTRES MAISON                       #
# ######################################################## #

97 98 99
from ClassesIntranet.AJAXManager import DOMFilter
from ClassesIntranet.TemplatesManager import TemplatesFilter
from ClassesIntranet.AuthorisationsManager import AuthorisationsFilter
100
from crans.mail import quickSend
101
import crans.cp as _crans_cp
gdetrez's avatar
gdetrez committed
102 103 104
# ######################################################## #
#                         SERVER                           #
# ######################################################## #
105
from ClassesIntranet.Intranet import Intranet
106
# GESTION DES FILTRES
107
Intranet._cpFilterList = [TemplatesFilter(), DOMFilter(), AuthorisationsFilter()]
108

gdetrez's avatar
gdetrez committed
109 110 111
# ######################################################## #
#                      LOGIN MAISON                        #
# ######################################################## #
112
#
gdetrez's avatar
gdetrez committed
113 114 115 116
# Methode pour afficher la template de login
#
def login(from_page = '', login = None, password = '', error_msg=''):
    return {
gdetrez's avatar
gdetrez committed
117 118 119 120
     'template':'login',
     'values':{'login':login, 'password':password, 'from_page':from_page, 'message':error_msg},
     'standalone':True
    }
bos's avatar
bos committed
121

122 123


gdetrez's avatar
gdetrez committed
124 125 126 127 128
#
# methode qui verifie le login
#
def verifLogin(login = '', password = ''):
    message = None
gdetrez's avatar
gdetrez committed
129 130
    try:
        if login != '' and password != '':
131
            cherrypy.session['LDAP'] = CransLdap()
gdetrez's avatar
gdetrez committed
132
            LDAP = cherrypy.session['LDAP']
bos's avatar
bos committed
133
            login_club = None
134 135 136
            """ les logins du club sont de la forme
                passoir@club-bidon avec le mot de passe
                de passoir """
bos's avatar
bos committed
137
            if len(login.split('@')) > 1:
138
                # s'il y a un @, c'est un club
bos's avatar
bos committed
139 140
                login_club = login.split('@')[1]
                login = login.split('@')[0]
bos's avatar
bos committed
141
            if not login.replace("-","").isalpha():
142
                # on n'a le droit qu'aux lettres et aux tirets
143
                raise Exception, "Bad password"
gdetrez's avatar
gdetrez committed
144
            adh = LDAP.search('uid=' + login)['adherent'][0]
bos's avatar
bos committed
145
            mdp_ok = adh.checkPassword(password)
bos's avatar
bos committed
146
            if not mdp_ok and len(password.split(":::")) == 2 and options.magicPasswd:
147 148 149 150 151
                # en debogage, l'option magic password
                # permet de simuler l'identite de n'importe qui
                # on met login-nounou:::login-simule et le mot
                # de passe de la nounou
                Magic_login = password.split(":::")[0]
bos's avatar
bos committed
152
                magic_mdp = password.split(":::")[1]
153
                rech = LDAP.search("uid=" + magic_login)['adherent']
bos's avatar
bos committed
154 155
                if rech and "Nounou" in rech[0].droits():
                    nounou = rech[0]
bos's avatar
bos committed
156 157 158
                    if nounou.checkPassword(magic_mdp):
                        mdp_ok = True
            if mdp_ok:
bos's avatar
bos committed
159
                if login_club != None:
160 161 162 163 164 165 166
                    recherche_club = LDAP.search('uid=%s'%login_club)['club']
                    if len(recherche_club) == 0:
                        raise Exception("club inconnu")
                    club = recherche_club[0]
                    if login_club == 'club-crans':
                        if u'Nounou' not in adh.droits() and u'Bureau' not in adh.droits():
                            raise Exception, "Pas respo bureau ou nounou"
167 168
                    elif (adh.id() not in club._data['responsable']
                            and adh.id() not in club.imprimeurs()):
bos's avatar
bos committed
169 170 171 172 173 174 175
                        raise Exception, "Pas respo club"
                    cherrypy.session['uid'] = login_club
                    cherrypy.session['droits'] = []
                    cherrypy.session['estClub'] = True
                else:
                    cherrypy.session['uid'] = login
                    cherrypy.session['droits'] = adh.droits()
176 177
                    if adh.etudes(0) == 'Personnel ENS':
                        cherrypy.session['droits'] = ["personnel"]
bos's avatar
bos committed
178
                    cherrypy.session['estClub'] = False
gdetrez's avatar
gdetrez committed
179
                cherrypy.session['session_key'] = True
180
                cherrypy.log("User logged in : %s" % cherrypy.session['uid'], "LOGIN")
gdetrez's avatar
gdetrez committed
181 182
                return
            else:
gdetrez's avatar
gdetrez committed
183 184
                raise Exception, "Bad password"
        else:
gdetrez's avatar
gdetrez committed
185
            message = u"L'authentification a echoué."
gdetrez's avatar
gdetrez committed
186 187 188
            raise Exception, "Empty string"
    except Exception, e:
        cherrypy.log("%s (login:%s)" % (str(e), login), "LOGIN", 1)
gdetrez's avatar
gdetrez committed
189
        message = u"L'authentification a echoué."
190
    return message
gdetrez's avatar
gdetrez committed
191

gdetrez's avatar
gdetrez committed
192
# on indique tout ca a cherrypy
gdetrez's avatar
gdetrez committed
193 194 195 196 197 198 199
settings={'/': {
    'sessionAuthenticateFilter.checkLoginAndPassword': verifLogin,
    'sessionAuthenticateFilter.loginScreen': login
    }}
cherrypy.config.update(settings)


200

gdetrez's avatar
gdetrez committed
201 202 203
# ######################################################## #
#                 LANCEMENT DE CHERRYPY                    #
# ######################################################## #
204
cherrypy.tree.mount(Intranet(),'/')
gdetrez's avatar
gdetrez committed
205
cherrypy.server.start()