Root.py 6.35 KB
Newer Older
gdetrez's avatar
gdetrez committed
1
#!/usr/bin/env python
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
# -*- coding: iso-8859-15 -*-
# #############################################################
#                                            ..                
#                       ....  ............   ........          
#                     .     .......   .            ....  ..    
#                   .  ... ..   ..   ..    ..   ..... .  ..    
#                   .. .. ....@@@.  ..  .       ........  .    
#              ..  .  .. ..@.@@..@@.  .@@@@@@@   @@@@@@. ....  
#         .@@@@. .@@@@. .@@@@..@@.@@..@@@..@@@..@@@@.... ....  
#       @@@@... .@@@.. @@ @@  .@..@@..@@...@@@.  .@@@@@.    .. 
#     .@@@..  . @@@.   @@.@@..@@.@@..@@@   @@ .@@@@@@..  ..... 
#    ...@@@.... @@@    .@@.......... ........ .....        ..  
#   . ..@@@@.. .         .@@@@.   .. .......  . .............  
#  .   ..   ....           ..     .. . ... ....                
# .    .       ....   ............. .. ...                     
# ..  ..  ...   ........ ...      ...                          
#  ................................                            
#                                                              
# #############################################################
# __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 45 46 47 48 49 50 51 52 53 54 55 56 57 58
# ######################################################## #
#                 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")
59
    cherrypy.config.update(file=os.getcwd() + "/conf/dev.cfg") 
60
    settings= { 'global': { 'rootDir': os.getcwd() } }                                                                                                                                         
gdetrez's avatar
gdetrez committed
61 62 63 64
    cherrypy.config.update(settings)                                                                                                               
                
else:
    cherrypy.config.update(file="/usr/scripts/intranet/conf/intranet.cfg") 
65
    cherrypy.config.update(file="/usr/scripts/intranet/conf/prod.cfg") 
gdetrez's avatar
gdetrez committed
66 67 68 69 70 71 72

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

73
# import du CransLdap qu'il va bien (on utilise CransLdap et non crans_ldap car on veut
74
# forcer l'ouverture d'une nouvelle connexion à chaque login)
gdetrez's avatar
gdetrez committed
75
if (cherrypy.config.configMap["global"]["server.environment"] == "development"):
76
    from ldap_crans_test import CransLdap
gdetrez's avatar
gdetrez committed
77
else:
78
    from ldap_crans import CransLdap
gdetrez's avatar
gdetrez committed
79

80
sys.path.append(cherrypy.config.get('rootDir'))
gdetrez's avatar
gdetrez committed
81 82 83 84
# ######################################################## #
#                     FILTRES MAISON                       #
# ######################################################## #

85 86 87
from ClassesIntranet.AJAXManager import DOMFilter
from ClassesIntranet.TemplatesManager import TemplatesFilter
from ClassesIntranet.AuthorisationsManager import AuthorisationsFilter
gdetrez's avatar
gdetrez committed
88
from crans.mail import quickSend                    
89
import crans.cp as _crans_cp
gdetrez's avatar
gdetrez committed
90 91 92
# ######################################################## #
#                         SERVER                           #
# ######################################################## #
93 94 95
from ClassesIntranet.Intranet import Intranet
# GESTION DES FILTRES 
Intranet._cpFilterList = [TemplatesFilter(), DOMFilter(), AuthorisationsFilter()]
gdetrez's avatar
gdetrez committed
96 97 98 99 100 101 102 103 104
    
# ######################################################## #
#                      LOGIN MAISON                        #
# ######################################################## #
# 
# Methode pour afficher la template de login
#
def login(from_page = '', login = None, password = '', error_msg=''):
    return {
gdetrez's avatar
gdetrez committed
105 106 107 108
     'template':'login',
     'values':{'login':login, 'password':password, 'from_page':from_page, 'message':error_msg},
     'standalone':True
    }
gdetrez's avatar
gdetrez committed
109 110 111 112 113
#
# methode qui verifie le login
#
def verifLogin(login = '', password = ''):
    message = None
gdetrez's avatar
gdetrez committed
114 115
    try:
        if login != '' and password != '':
116
            cherrypy.session['LDAP'] = CransLdap()
gdetrez's avatar
gdetrez committed
117 118
            LDAP = cherrypy.session['LDAP']
            adh = LDAP.search('uid=' + login)['adherent'][0]
gdetrez's avatar
gdetrez committed
119 120 121 122
            if adh.checkPassword(password):
                cherrypy.session['uid'] = login
                cherrypy.session['session_key'] = True
                cherrypy.session['droits'] = adh.droits()
123
                cherrypy.log("User logged in : %s" % cherrypy.session['uid'], "LOGIN")
gdetrez's avatar
gdetrez committed
124 125
                return
            else:
gdetrez's avatar
gdetrez committed
126 127
                raise Exception, "Bad password"
        else:
gdetrez's avatar
gdetrez committed
128
            message = u"L'authentification a echoué."
gdetrez's avatar
gdetrez committed
129 130 131
            raise Exception, "Empty string"
    except Exception, e:
        cherrypy.log("%s (login:%s)" % (str(e), login), "LOGIN", 1)
gdetrez's avatar
gdetrez committed
132
        message = u"L'authentification a echoué."
133
    return message
gdetrez's avatar
gdetrez committed
134 135 136 137 138





gdetrez's avatar
gdetrez committed
139
# on indique tout ca a cherrypy
gdetrez's avatar
gdetrez committed
140 141 142 143 144 145 146 147
settings={'/': {
    'sessionAuthenticateFilter.checkLoginAndPassword': verifLogin,
    'sessionAuthenticateFilter.loginScreen': login
    }}
cherrypy.config.update(settings)


    
gdetrez's avatar
gdetrez committed
148 149 150
# ######################################################## #
#                 LANCEMENT DE CHERRYPY                    #
# ######################################################## #
151
cherrypy.tree.mount(Intranet(),'/')
gdetrez's avatar
gdetrez committed
152
cherrypy.server.start()