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

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Thu Mar 1 15:18:55 CET 2012


Author: ludwig
Date: 2012-03-01 15:18:55 +0100 (Thu, 01 Mar 2012)
New Revision: 5840

Modified:
   base/trunk/mpulsweb/lib/security.py
Log:
Issue 2926 Refactoring of checkLogin
Extracted two parts into own functions.
Only fetch user data at a not-adm login.


Modified: base/trunk/mpulsweb/lib/security.py
===================================================================
--- base/trunk/mpulsweb/lib/security.py	2012-03-01 13:01:34 UTC (rev 5839)
+++ base/trunk/mpulsweb/lib/security.py	2012-03-01 14:18:55 UTC (rev 5840)
@@ -109,53 +109,28 @@
         mydb = DB(**db_parameters)
         try:
             enter(mydb)
-            conn, cursor = None, None
-            try:
-                conn = db.getConnection()
-                cursor = conn.cursor()
-                login = db_parameters["user"]
-                fields = {'login': login}
-                cursor.execute(FETCH_USER_DATA_SQL, fields)
-                try:
-                    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]
-                except:
-                    log.exception("Error fetching user data")
-                    data = None
-            finally:
-                db.recycleConnection(conn, cursor)
+            login = db_parameters["user"]
 
+            # Only normal logins need data.
+            if user != 'adm':
+                data = fetch_user_data(login)
+            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
 
-                # UPDATE last login time
-                if data:
-                    try:
-                        db.execute(UPDATE_LASTLOGIN_SQL,
-                                   {'user_id': data.get('id')})
-                    except StandardError:
-                        log.exception("Error setting last login time for"
-                                      " user %r" % (data.get('id'),))
-
             except StandardError:
                 log.exception("Error creating user Object")
 
-            #RETURN userobject
+            # return userobject
             log.info('Login ok :)')
             return userobject
         finally:
@@ -172,6 +147,37 @@
     log.info('Login failed :(')
     return None
 
+def fetch_user_data(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    
+
+def update_last_login_time(data):
+    try:
+        db.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()]
     if not obj is None:



More information about the Mpuls-commits mailing list