Root.py 4.74 KB
Newer Older
gdetrez's avatar
gdetrez committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/env python
# -*- coding: utf-8 -*-


import cherrypy, sys, os, datetime
sys.path.append('/usr/scripts/gestion/')

# ######################################################## #
#                     FILTRES MAISON                       #
# ######################################################## #

from plugins.domfilter import DOMFilter
from plugins.templatesfilter import TemplatesFilter
from plugins.verifdroitsfilter import VerifDroitsFilter
                    
# ######################################################## #
#                         SERVER                           #
# ######################################################## #
class Intranet:
    __ldap = None
    def __init__(self):
gdetrez's avatar
gdetrez committed
22
        from pages import monCompte, impression, factures
gdetrez's avatar
gdetrez committed
23 24 25 26
        self.__ldap = cherrypy.config.configMap["global"]["crans_ldap"]
        
        # liste des modules disponibles
        self.monCompte = monCompte.monCompte()
27 28
        self.sous = factures.root()
        
gdetrez's avatar
gdetrez committed
29 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        # liste des modules en developpement
        if (cherrypy.config.configMap["global"]["server.environment"] == "development"):
            self.impression = impression.root()
            
            
    _cpFilterList = [TemplatesFilter(), DOMFilter(), VerifDroitsFilter()]
    
    def index(self):
        return {
            'template':'accueil',
            'values':{},
            }
    index.exposed= True
    
    def info(self):
        return {
    'template':'info-diverses',
    'values':{}
    }
    info.exposed = True
    
    '''
    def nounous(self):
        return "coucou"
    nounous.exposed= True
    
    def test(self):
        return {"quoi":cherrypy.request.path, "il_faut":cherrypy.config.configMap["/nounous"]["crans.droits"], "moi":cherrypy.session['droits']
    }
    test.exposed = True
    
    def environment(self):
        return cherrypy.config.configMap["global"]["server.environment"]
    environment.exposed = True
    '''
    
# ######################################################## #
#                      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
    if login != '' and password != '':
        try:
            globalLdap = cherrypy.config.configMap["global"]["crans_ldap"]
            adh =globalLdap.search('uid=' + login)['adherent'][0]
            if adh.checkPassword(password):
                cherrypy.session['uid'] = login
                cherrypy.session['session_key'] = True
                cherrypy.session['droits'] = adh.droits()
                return
            else:
                #print("bad password")
                message = u"L'authentification a echoué."
        except Exception, e:
            #print(e)
            message = u"L'authentification a echoué."
    else:
        #print("sth empty")
        message = u"L'authentification a echoué."
    return message    
            
# ######################################################## #
#                 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")
     
(options, args) = parser.parse_args()


# ######################################################## #
#                         CHERRYPY                         #
# ######################################################## #
# 
# mise en place de cherrypy + conf
#

gdetrez's avatar
gdetrez committed
125
cherrypy.config.update(file="/usr/scripts/intranet/conf/intranet.cfg") 
gdetrez's avatar
gdetrez committed
126
if (options.dev):
gdetrez's avatar
gdetrez committed
127
    cherrypy.config.update(file="/usr/scripts/intranet/conf/dev.cfg") 
gdetrez's avatar
gdetrez committed
128
else:
gdetrez's avatar
gdetrez committed
129
    cherrypy.config.update(file="/usr/scripts/intranet/conf/prod.cfg") 
gdetrez's avatar
gdetrez committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149


settings={'/': {
    'sessionAuthenticateFilter.checkLoginAndPassword': verifLogin,
    'sessionAuthenticateFilter.loginScreen': login
    }}
cherrypy.config.update(settings)

if (cherrypy.config.configMap["global"]["server.environment"] == "development"):
    from ldap_crans_test import crans_ldap
    print("settings : unsing test ldap : env=" + cherrypy.config.configMap["global"]["server.environment"])
else:
    from ldap_crans import crans_ldap
    print("settings : unsing prod ldap : env=" + cherrypy.config.configMap["global"]["server.environment"])
cherrypy.config.update({'global':{'crans_ldap':crans_ldap()}})
cherrypy.root = Intranet()

cherrypy.server.start()