PHP8
[Pman.BAdmin] / Pman.Login.js
index 1c6b0ab..77fb3b4 100644 (file)
@@ -31,8 +31,6 @@ Pman.Login =  new Roo.util.Observable({
     
     authUserId: 0,
     authUser: { id : false },
-       
-    companyId: 0,
 
     checkFails : 0,
     versionWarn: false,
@@ -53,27 +51,43 @@ Pman.Login =  new Roo.util.Observable({
         // check if we are logged in..
         new Pman.Request({  
             url: baseURL + '/Login',
-            method: 'GET',  
+            method: 'POST',  
             params: {
                 getAuthUser: true
             },  
             
             success:  function(res)  {  // check successfull...
-                 
+                
+                Roo.log('success : ');
+                Roo.log(res);
+                
                 this.checkFails =0;
                 if (!res.success) { // error!
                     this.checkFails = 5;
                     //console.log('call failure');
                     return Pman.Login.failure(response,opts);
                 }
-                if (res.data.id*1 < 0) { 
-                   this.companyId = res.data.company_id;
-                    return this.openSystem(res.data);
+                
+                if (res.data.id*1 < 0) {
+                    this.fillAuth(res.data);
+                    return this.openSystem();
                 }
                     
                 var _this = this;
                 if (!res.data.id) { // id=0 == login failure.
-                    return Pman.Dialog.Login.show({},
+                    
+                    if(window.location.pathname.substr(baseURL.length).match(/\/PasswordReset\//)){
+                        Pman.Dialog.BAdminPasswordReset.show({}, function(){
+                            window.location.href = baseURL;
+                        });
+                        
+                        return;
+                    }
+                     
+                    
+                    
+                    return Pman.Dialog.Login.show(
+                        {},
                         function(data) {
                            Pman.Login.success(data);
                         }
@@ -81,7 +95,7 @@ Pman.Login =  new Roo.util.Observable({
                 }
                 this.success(res.data);
             },
-            failure : function()
+            failure : function(res)
             {
                 return Pman.Dialog.Login.show({},
                     function(data) {
@@ -125,7 +139,7 @@ Pman.Login =  new Roo.util.Observable({
             params: {
                 getAuthUser: true
             },  
-            method: 'GET',  
+            method: 'POST',  
             success:  Pman.Login.success,
             failure : Pman.Login.failure,
             scope : Pman.Login
@@ -156,8 +170,6 @@ Pman.Login =  new Roo.util.Observable({
     
     success : function(res)  // check successfull...
     {  
-        
-        Roo.log(res);
         this.sending = false;
         
         if (!res.success) {
@@ -183,7 +195,12 @@ Pman.Login =  new Roo.util.Observable({
     },
     
     fillAuth: function(au) {
-        this.startAuthCheck();
+        
+        //pause auth check in open system
+        if(au.id * 1 > 0) {
+            this.startAuthCheck();
+        }
+        
         this.authUserId = au.id;
         this.authUser = au;
         this.lastChecked = new Date();
@@ -197,17 +214,16 @@ Pman.Login =  new Roo.util.Observable({
         //this.switchLang(Roo.state.Manager.get('Pman.Login.lang', 'en'));
         Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, au.lang);
         this.switchLang(au.lang);
-        
      
         // open system... - -on setyp..
         if (this.authUserId  < 0) {
-            Roo.bootstrap.MessageBox.alert("Warning", 
-                "This is an open system - please set up a admin user with a password.");  
+            Roo.bootstrap.MessageBox.alert(
+                "Warning",
+                "This is an open system - please set up a admin user with a password."
+            );  
         }
          
         //Pman.onload(); // which should do nothing if it's a re-auth result...
-        
-             
     },
     
     
@@ -332,11 +348,12 @@ Pman.Login =  new Roo.util.Observable({
             Pman.Dialog.BAdminStaff.show( 
                 { 
                     id : 0, 
-                    company_id : Pman.Login.companyId * 1, 
-                    company_id_name : Pman.Login.authUser.company_id_name
+                    company_id : Pman.Login.authUser.company_id* 1,
+                    company_id_name : Pman.Login.authUser.company_id_name,
+                    role : 'Administrators'
                 }, function(data) {
                     //forceAdmin(data);
-                   Pman.Login.openSystemCreateUser(data);
+                    Pman.Login.openSystemCreateUser(data);
                 }
             );
             return;
@@ -345,12 +362,12 @@ Pman.Login =  new Roo.util.Observable({
         window.onbeforeunload = false;
         document.location = baseURL + '?ts=' + Math.random();
     },
+    
     openSystemCreateCompany: function(data)
     {
-            
-         if (Pman.Login.authUser.company_id * 1 > 0) {
+        if (Pman.Login.authUser.company_id * 1 > 0) {
             //forceAdmin();
-           Pman.Login.openSystemCreateUser(data);
+            Pman.Login.openSystemCreateUser(data);
             return;
         }
         if (!data || !data.id) {
@@ -367,28 +384,88 @@ Pman.Login =  new Roo.util.Observable({
         Pman.Login.authUser.company_id_name  = data.name;
         Roo.log("forcing admin");
         this.openSystemCreateUser();
-       
-        
     },
+    
     openSystem : function()
     {
-          
         Roo.bootstrap.MessageBox.alert("Error", "Admin accounts have not been created - use the old admin at present");
-
-        // create company if company_id is -1 
-        if (Pman.Login.companyId* 1 < 0) {
-            this.openSystemCreateCompany();
-            return;
-        }
         
-        // create user if id is -1 
-        if (Pman.Login.authUser.id* 1 < 0) {
-            this.openSystemCreateUser();
-            return;
+        new Pman.Request({  
+            url: baseURL + '/Login',
+            method: 'POST',  
+            params: {
+                check_owner_company: true
+            },  
+            success:  function(res)  {  // check successfull...
+                
+                if(res.data == 1) {
+                    this.openSystemCreateUser();
+                    return;
+                }
+                
+                if(res.data == 0) {
+                    this.openSystemCreateCompany();
+                    return;
+                }
+                
+                if(res.data > 0) {
+                    Roo.bootstrap.MessageBox.alert(
+                        "Error",  
+                        "There are more than 1 company in the system. please fix the data"
+                    );
+                    return;
+                }
+            },
+            failure : function(res)
+            {
+                Roo.bootstrap.MessageBox.alert(
+                    "Error",  
+                    "Invalid params for check owner company"
+                );
+            },
+            scope : Pman.Login
+        });
+    },
+     /**
+     * hasPerm:
+     * Does the authenticated user have permission to see this.
+     * 
+     * @param {String} name the [Module].[permission] to check for
+     * @param {Char} lvl  - which type of permission to use (eg. S=show...)
+     * @returns {Boolean} tue indicates permission allowed
+     */
+    hasPerm: function(name, lvl) {
+        if (
+            (typeof(this.authUser) != 'object')
+            ||
+            (typeof(this.authUser.perms) != 'object')
+            ||
+            (typeof(this.authUser.perms[name]) != 'string')
+            ) {
+                return false;
         }
         
+        return this.authUser.perms[name].indexOf(lvl) > -1;
         
-        
+    },
+    /**
+     * hasPermExists:
+     * Is there a permission defined for this (used by module registration.)
+     * 
+     * @param {String} name the [Module].[permission] to check for
+     * @returns {Boolean} tue indicates permission exists.
+     */
+    hasPermExists: function(name) {
+        if (
+            (typeof(this.authUser) != 'object')
+            ||
+            (typeof(this.authUser.perms) != 'object')
+            ||
+            (typeof(this.authUser.perms[name]) != 'string')
+            ) {
+                return false;
+        }
+        return true;
     }