Root.py 7.61 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 23 24 25 26
# #############################################################
#                                            ..                
#                       ....  ............   ........          
#                     .     .......   .            ....  ..    
#                   .  ... ..   ..   ..    ..   ..... .  ..    
#                   .. .. ....@@@.  ..  .       ........  .    
#              ..  .  .. ..@.@@..@@.  .@@@@@@@   @@@@@@. ....  
#         .@@@@. .@@@@. .@@@@..@@.@@..@@@..@@@..@@@@.... ....  
#       @@@@... .@@@.. @@ @@  .@..@@..@@...@@@.  .@@@@@.    .. 
#     .@@@..  . @@@.   @@.@@..@@.@@..@@@   @@ .@@@@@@..  ..... 
#    ...@@@.... @@@    .@@.......... ........ .....        ..  
#   . ..@@@@.. .         .@@@@.   .. .......  . .............  
#  .   ..   ....           ..     .. . ... ....                
# .    .       ....   ............. .. ...                     
# ..  ..  ...   ........ ...      ...                          
#  ................................                            
#                                                              
# #############################################################
# __init__.py                                                                                                                    
#                                                              
#     Classe impression
#                                                              
# Copyright (c) 2006 by www.crans.org                          
# #############################################################
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/')
gdetrez's avatar
gdetrez committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
# ######################################################## #
#                 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")
bos's avatar
bos committed
45 46 47
parser.add_option("-m", "--magic",
    action="store_true", dest="magicPasswd", default=False,
    help="enable login::pasword magic passwords")
gdetrez's avatar
gdetrez committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61
     
(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")
62
    cherrypy.config.update(file=os.getcwd() + "/conf/dev.cfg") 
63
    settings= { 'global': { 'rootDir': os.getcwd() } }                                                                                                                                         
gdetrez's avatar
gdetrez committed
64 65 66 67
    cherrypy.config.update(settings)                                                                                                               
                
else:
    cherrypy.config.update(file="/usr/scripts/intranet/conf/intranet.cfg") 
68
    cherrypy.config.update(file="/usr/scripts/intranet/conf/prod.cfg") 
gdetrez's avatar
gdetrez committed
69 70 71 72 73 74 75

# changer le port ??
if (options.port):
    settings={'global':{'server.socketPort':options.port}}
    cherrypy.config.update(settings)                                                                                                               
    

76
# import du CransLdap qu'il va bien (on utilise CransLdap et non crans_ldap car on veut
77
# forcer l'ouverture d'une nouvelle connexion à chaque login)
bos's avatar
bos committed
78 79 80 81 82
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
83

84
sys.path.append(cherrypy.config.get('rootDir'))
gdetrez's avatar
gdetrez committed
85 86 87 88
# ######################################################## #
#                     FILTRES MAISON                       #
# ######################################################## #

89 90 91
from ClassesIntranet.AJAXManager import DOMFilter
from ClassesIntranet.TemplatesManager import TemplatesFilter
from ClassesIntranet.AuthorisationsManager import AuthorisationsFilter
gdetrez's avatar
gdetrez committed
92
from crans.mail import quickSend                    
93
import crans.cp as _crans_cp
gdetrez's avatar
gdetrez committed
94 95 96
# ######################################################## #
#                         SERVER                           #
# ######################################################## #
97 98 99
from ClassesIntranet.Intranet import Intranet
# GESTION DES FILTRES 
Intranet._cpFilterList = [TemplatesFilter(), DOMFilter(), AuthorisationsFilter()]
gdetrez's avatar
gdetrez committed
100 101 102 103 104 105 106 107 108
    
# ######################################################## #
#                      LOGIN MAISON                        #
# ######################################################## #
# 
# Methode pour afficher la template de login
#
def login(from_page = '', login = None, password = '', error_msg=''):
    return {
gdetrez's avatar
gdetrez committed
109 110 111 112
     'template':'login',
     'values':{'login':login, 'password':password, 'from_page':from_page, 'message':error_msg},
     'standalone':True
    }
bos's avatar
bos committed
113

gdetrez's avatar
gdetrez committed
114 115 116 117 118
#
# methode qui verifie le login
#
def verifLogin(login = '', password = ''):
    message = None
gdetrez's avatar
gdetrez committed
119 120
    try:
        if login != '' and password != '':
121
            cherrypy.session['LDAP'] = CransLdap()
gdetrez's avatar
gdetrez committed
122
            LDAP = cherrypy.session['LDAP']
bos's avatar
bos committed
123 124 125 126
            login_club = None
            if len(login.split('@')) > 1:
                login_club = login.split('@')[1]
                login = login.split('@')[0]
gdetrez's avatar
gdetrez committed
127
            adh = LDAP.search('uid=' + login)['adherent'][0]
bos's avatar
bos committed
128
            mdp_ok = adh.checkPassword(password)
bos's avatar
bos committed
129
            if not mdp_ok and len(password.split(":::")) == 2 and options.magicPasswd:
bos's avatar
bos committed
130 131
                magic_login = password.split(":::")[0]
                magic_mdp = password.split(":::")[1]
bos's avatar
bos committed
132 133 134
                rech = LDAP.search("uid=" + magic_login)['adherent']                
                if rech and "Nounou" in rech[0].droits():
                    nounou = rech[0]
bos's avatar
bos committed
135 136 137
                    if nounou.checkPassword(magic_mdp):
                        mdp_ok = True
            if mdp_ok:
bos's avatar
bos committed
138 139 140 141 142 143 144 145 146 147 148
                if login_club != None:
                    club = LDAP.search('uid=%s'%login_club)['club'][0]
                    if  adh.id() not in club._data['responsable']:
                        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()
                    cherrypy.session['estClub'] = False
gdetrez's avatar
gdetrez committed
149
                cherrypy.session['session_key'] = True
150
                cherrypy.log("User logged in : %s" % cherrypy.session['uid'], "LOGIN")
gdetrez's avatar
gdetrez committed
151 152
                return
            else:
gdetrez's avatar
gdetrez committed
153 154
                raise Exception, "Bad password"
        else:
gdetrez's avatar
gdetrez committed
155
            message = u"L'authentification a echoué."
gdetrez's avatar
gdetrez committed
156 157 158
            raise Exception, "Empty string"
    except Exception, e:
        cherrypy.log("%s (login:%s)" % (str(e), login), "LOGIN", 1)
gdetrez's avatar
gdetrez committed
159
        message = u"L'authentification a echoué."
160
    return message
gdetrez's avatar
gdetrez committed
161

gdetrez's avatar
gdetrez committed
162
# on indique tout ca a cherrypy
gdetrez's avatar
gdetrez committed
163 164 165 166 167 168 169 170
settings={'/': {
    'sessionAuthenticateFilter.checkLoginAndPassword': verifLogin,
    'sessionAuthenticateFilter.loginScreen': login
    }}
cherrypy.config.update(settings)


    
gdetrez's avatar
gdetrez committed
171 172 173
# ######################################################## #
#                 LANCEMENT DE CHERRYPY                    #
# ######################################################## #
174
cherrypy.tree.mount(Intranet(),'/')
gdetrez's avatar
gdetrez committed
175
cherrypy.server.start()