[Mpuls-commits] r5842 - base/trunk/mpulsweb/lib

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Fri Mar 2 10:38:22 CET 2012


Author: ludwig
Date: 2012-03-02 10:38:21 +0100 (Fri, 02 Mar 2012)
New Revision: 5842

Modified:
   base/trunk/mpulsweb/lib/security.py
Log:
Issue 2926: refactoring of checkLogin
removed enter/leave parts.
removed some exception handlers which obscure the program flow.
changed log of the OperationalError to error.
This log happens, if connecting to db fails because of wrong password.


Modified: base/trunk/mpulsweb/lib/security.py
===================================================================
--- base/trunk/mpulsweb/lib/security.py	2012-03-01 14:40:07 UTC (rev 5841)
+++ base/trunk/mpulsweb/lib/security.py	2012-03-02 09:38:21 UTC (rev 5842)
@@ -107,76 +107,58 @@
                                           config.get('mpuls.db.database'),
                                           config.get('mpuls.db.user'))
         mydb = DB(**db_parameters)
+        login = db_parameters["user"]
+
+        conn = None
         try:
-            enter(mydb)
-            login = db_parameters["user"]
+            conn = mydb.getConnection()
 
-            # Only normal logins need data.
             if user != 'adm':
-                data = fetch_user_data(login)
+                data = fetch_user_data(conn, login)
+                userobject = SessionUser(login, password)
+                userobject.setData(data)
+                update_last_login_time(conn, data)
             else:
-                data = None
-                
-            try:
-                if data:
-                    userobject = SessionUser(login, password)
-                    userobject.setData(data)
-                    update_last_login_time(data)
-                elif user == 'adm':
-                    userobject = SessionSuperAdmin(login, password)
-                else:
-                    return None
+                userobject = SessionSuperAdmin(login, password)
 
-            except StandardError:
-                log.exception("Error creating user Object")
-
             # return userobject
             log.info('Login ok :)')
             return userobject
         finally:
-            leave(mydb)
             mydb.closeConnections()
     except dbapi.OperationalError, err:
         # This is likely a login failure
         # XXX: Is there a way in DB Api 2.0 to determine
         # this database independent?
-        log.exception("OperationalError from database"
-                      " while checking user credentials")
+        log.error("OperationalError from database"
+                  " while checking user credentials")
     except:
         log.exception("Exception while checking user credentials")
     log.info('Login failed :(')
     return None
 
-def fetch_user_data(login):    
+def fetch_user_data(conn, login):
     data = None
-    conn, cursor = None, None
-    try:
-        conn = db.getConnection()
-        cursor = conn.cursor()
-        fields = {'login': login}
-        cursor.execute(FETCH_USER_DATA_SQL, fields)
-        result = cursor.fetchone()
-        data = {}
-        data['id'] = result[0]
-        data['firstname'] = result[1]
-        data['lastname'] = result[2]
-        data['newpass'] = result[3]
-        data['activated'] = result[4]
-        data['role'] = result[5]
-        data['gid'] = result[6]
-        data['last_login'] = result[7]
-        data['standin'] = result[8]
-    finally:
-        db.recycleConnection(conn, cursor)
-    return data    
+    cursor = conn.cursor()
+    fields = {'login': login}
+    cursor.execute(FETCH_USER_DATA_SQL, fields)
+    result = cursor.fetchone()
+    data = {}
+    data['id'] = result[0]
+    data['firstname'] = result[1]
+    data['lastname'] = result[2]
+    data['newpass'] = result[3]
+    data['activated'] = result[4]
+    data['role'] = result[5]
+    data['gid'] = result[6]
+    data['last_login'] = result[7]
+    data['standin'] = result[8]
+    return data
 
-def update_last_login_time(data):
-    try:
-        db.execute(UPDATE_LASTLOGIN_SQL,
+def update_last_login_time(conn, data):
+    cursor = conn.cursor()
+    cursor.execute(UPDATE_LASTLOGIN_SQL,
                    {'user_id': data.get('id')})
-    except StandardError:
-        log.exception("Error setting last login time for"
-                      " user %r" % (data.get('id'),))
 
 def generateID(obj=None):
     array = [time.time(), os.times(), random.random()]



More information about the Mpuls-commits mailing list