Merge branch 'fibre' of ssh://git.onelab.eu/git/myslice into fibre
[unfold.git] / portal / homeview.py
index 7efd420..718ba60 100644 (file)
@@ -5,6 +5,7 @@ from django.contrib.auth import authenticate, login, logout, get_user_model
 from django.template import RequestContext
 from django.shortcuts import render_to_response
 from django.shortcuts import render
+
 import json
 
 from unfold.loginrequired import FreeAccessView
@@ -16,7 +17,7 @@ from manifoldapi.manifoldapi            import execute_query, execute_admin_quer
 # Edelberto - LDAP XXX
 from portal.models             import PendingUser
 from django.contrib.auth.models import User   #Pedro
-from portal.actions             import create_pending_user, create_user
+from portal.actions             import create_pending_user, create_user, create_user_in_ldap, clear_user_creds
 from registrationview          import RegistrationView
 from random     import randint
 from hashlib    import md5
@@ -34,6 +35,8 @@ from myslice.theme import ThemeView
 # Edelberto LDAP authentication XXX
 import ldap
 
+#import activity.user
+
 class HomeView (FreeAccessView, ThemeView):
     template_name = 'home-view.html'
         
@@ -61,8 +64,8 @@ class HomeView (FreeAccessView, ThemeView):
        #################################################
        ## first you must open a connection to the server
        try:
-               # Connect to NOC
-               l = ldap.initialize("ldap://200.130.15.186:389")
+               # Connect to UFRJ
+                l = ldap.initialize("ldap://200.130.15.186:389")
                # Bind/authenticate with a root user to search all objects
                l.simple_bind_s("cn=Manager,dc=br,dc=fibre","fibre2013")
                
@@ -79,7 +82,10 @@ class HomeView (FreeAccessView, ThemeView):
        searchFilter = "uid=" + username
        print searchFilter
 
+        in_ldap = 0
+
        try:
+            if username != "admin":
                ldap_result_id = l.search(baseDN, searchScope, searchFilter, retrieveAttributes)
                result_set = []
                result_type, result_data = l.result(ldap_result_id, 0)
@@ -122,10 +128,20 @@ class HomeView (FreeAccessView, ThemeView):
                                                print "DEBUG: user NOT exists on MySlice DBs"
                                                
                                                cn              = result_set[0][0][1]['cn'][0] 
-                                               print cn
                                                sn              =  result_set[0][0][1]['sn'][0]
-                                               print sn
-                                               authority_hrn   =  'fibre' + '.' + username.split('@')[1] 
+
+                                                fname=None
+                                                lname=None
+
+                                                try:
+                                                    fname =  sn.split(' ')[0]
+                                                    lname =  sn.split(' ')[1]
+                                                except:
+                                                    fname = sn
+                                                    lname = ""
+
+                                               #authority_hrn  =  'fibre' + '.' + username.split('@')[1] 
+                                               authority_hrn   =  'fibre'
                                                print authority_hrn
                                                email           = ldap_mail
                                                print ldap_mail
@@ -133,7 +149,8 @@ class HomeView (FreeAccessView, ThemeView):
                                                print username
                                                password        = password
                                                print password
-                                               user_hrn        = 'fibre' + '.' + username.split('@')[1] + '.' + username
+                                               # user_hrn      = 'fibre' + '.' + username.split('@')[1] + '.' + username
+                                               user_hrn        = 'fibre' + '.' + username
                                                print user_hrn
 
                                                # Based on registrationview
@@ -150,10 +167,8 @@ class HomeView (FreeAccessView, ThemeView):
                                                print email_hash
 
                                                user_request = {
-                                               #'first_name'    : cn,
-                                               'first_name'    : sn,
-                                               'last_name'     : '',
-                                               #'organization'  : username.split('@')[1],
+                                               'first_name'    : fname,
+                                               'last_name'     : lname,
                                                'organization'  : authority_hrn,
                                                'authority_hrn' : authority_hrn,
                                                'email'         : ldap_mail,
@@ -163,6 +178,7 @@ class HomeView (FreeAccessView, ThemeView):
                                                'email_hash'    : email_hash,
                                                'pi'            : '',
                                                'user_hrn'      : user_hrn,
+                                                'reasons'       : 'already exists in the LDAP',
                                                'type'          : 'user',
                                                'validation_link': 'https://' + current_site + '/portal/email_activation/'+ email_hash
                                                }
@@ -208,13 +224,14 @@ class HomeView (FreeAccessView, ThemeView):
 
                                                # XXX Verify if errors exist - After!
                                                #if not errors:
-                                               create_pending_user(request, user_request, user_detail)
-                                                       
-                                               create_user(request, user_request)
-
-                                               env['state'] = "User LDAP associated. Authenticate again."
-                                               return render_to_response(self.template, env, context_instance=RequestContext(request))
+                                               create_user_in_ldap(request, user_request, user_detail)
+                                               #create_pending_user(request, user_request, user_detail)
 
+                                                #create_user(request, user_request)
+                                                            
+                                                env['state'] = "LDAP associated. Please, login again."
+                                                return render_to_response(self.template, env, context_instance=RequestContext(request))
+                                                        
 
                                else:
                                        env['state'] = "Access denied. Verify LDAP userEnable and password."
@@ -232,7 +249,7 @@ class HomeView (FreeAccessView, ThemeView):
                print e 
 
         #else:
-       if in_ldap and enabled and pwd:
+       if in_ldap and enabled and pwd or username=="admin":
 
 ################################################################################
 ### XXX Edelberto LDAP auth end XXX
@@ -297,35 +314,53 @@ class HomeView (FreeAccessView, ThemeView):
                        return render_to_response(self.template,env, context_instance=RequestContext(request))
                # otherwise
         else:
-            env['state'] = "Your username and/or password were incorrect."
-            
+            # log user activity
+            #activity.user.login(self.request, "error")
+
+           env['state'] = "Your username and/or password were incorrect."
+
             return render_to_response(self.template, env, context_instance=RequestContext(request))
 
     def get (self, request, state=None):
         env = self.default_env()
         acc_auth_cred={}
         if request.user.is_authenticated():
+           
             ## check user is pi or not
+            platform_details = {}
+            account_details = {}
+            acc_auth_cred = {}
+            acc_user_cred = {}
             platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
             account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
             # XXX Something like an invalid session seems to make the execute fail sometimes, and thus gives an error on the main page
             platform_details = execute_query(self.request, platform_query)
             account_details = execute_query(self.request, account_query)
-            for platform_detail in platform_details:
-                for account_detail in account_details:
-                    if 'platform_id' in platform_detail:
-                        if platform_detail['platform_id'] == account_detail['platform_id']:
-                            if 'config' in account_detail and account_detail['config'] is not '':
-                                account_config = json.loads(account_detail['config'])
-                                if 'myslice' in platform_detail['platform']:
-                                    acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
+            if platform_details is not None and platform_details != {}:
+                for platform_detail in platform_details:
+                    for account_detail in account_details:
+                        if 'platform_id' in platform_detail:
+                            if platform_detail['platform_id'] == account_detail['platform_id']:
+                                if 'config' in account_detail and account_detail['config'] is not '':
+                                    account_config = json.loads(account_detail['config'])
+                                    if 'myslice' in platform_detail['platform']:
+                                        acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
+                                        acc_user_cred = account_config.get('delegated_user_credential','N/A')
             # assigning values
             if acc_auth_cred=={} or acc_auth_cred=='N/A':
                 pi = "is_not_pi"
             else:
                 pi = "is_pi"
 
-            env['pi'] = pi     
+            # check if the user has creds or not
+            if acc_user_cred == {} or acc_user_cred == 'N/A':
+                user_cred = 'no_creds'
+            else:
+                user_cred = 'has_creds'
+           
+
+            env['pi'] = pi
+            env['user_cred'] = user_cred                
             env['person'] = self.request.user
         else: 
             env['person'] = None