PHP8
[Pman.BAdmin] / Pman.Login.js
1 //<script type="text/javascript">
2
3 /**
4  * login code -
5  * fires Pman 'authrefreshed'  event on every poll to server..
6  *
7  */
8
9 /***
10 re-arrange language code...
11 * flipping language should be like this:
12
13 * Ext.apply(_T, _T[lang]);
14
15 **/
16  
17   
18
19 Pman.Login =  new Roo.util.Observable({
20     
21     events : {
22         
23         'render' : true
24        
25     },
26     disabled : false,
27     
28     dialog : false,
29     form: false,
30     haslogo : false,
31     
32     authUserId: 0,
33     authUser: { id : false },
34
35     checkFails : 0,
36     versionWarn: false,
37     sending : false,
38     
39     checkConnection : false, // the Roo.data.Connection for checking if still authenticated.
40     
41     onLoad : function() // called on page load...
42     {
43         // load 
44        
45          
46         if (Roo.get('loading')) {
47             Roo.get('loading').remove();
48         }
49         this.switchLang('en');
50        
51         // check if we are logged in..
52         new Pman.Request({  
53             url: baseURL + '/Login',
54             method: 'POST',  
55             params: {
56                 getAuthUser: true
57             },  
58             
59             success:  function(res)  {  // check successfull...
60                 
61                 Roo.log('success : ');
62                 Roo.log(res);
63                 
64                 this.checkFails =0;
65                 if (!res.success) { // error!
66                     this.checkFails = 5;
67                     //console.log('call failure');
68                     return Pman.Login.failure(response,opts);
69                 }
70                 
71                 if (res.data.id*1 < 0) {
72                     this.fillAuth(res.data);
73                     return this.openSystem();
74                 }
75                     
76                 var _this = this;
77                 if (!res.data.id) { // id=0 == login failure.
78                     
79                     if(window.location.pathname.substr(baseURL.length).match(/\/PasswordReset\//)){
80                         Pman.Dialog.BAdminPasswordReset.show({}, function(){
81                             window.location.href = baseURL;
82                         });
83                         
84                         return;
85                     }
86                      
87                     
88                     
89                     return Pman.Dialog.Login.show(
90                         {},
91                         function(data) {
92                            Pman.Login.success(data);
93                         }
94                     );
95                 }
96                 this.success(res.data);
97             },
98             failure : function(res)
99             {
100                 return Pman.Dialog.Login.show({},
101                     function(data) {
102                        Pman.Login.success(data);
103                     }
104                 );
105             
106             },
107             scope : Pman.Login
108               
109         });  
110     }, 
111     
112     
113     check: function(again) // called every so often to refresh cookie etc..
114     {
115         if (again) { // could be undefined..
116             Pman.Login.checkFails++;
117         } else {
118             Pman.Login.checkFails = 0;
119         }
120         var _this = this;
121         if (this.sending) {
122             
123             if ( Pman.Login.checkFails > 4) {
124                 //Pman.Preview.disable();
125                 Roo.bootstrap.MessageBox.alert("Error",  
126                     "Error getting authentication status. - try reloading, or wait a while", function() {
127                         _this.sending = false;
128                     }); 
129                 return;
130             }
131             
132             _this.check.defer(10000, _this, [ true ]); // check in 10 secs.
133             return;
134         }
135         this.sending = true;
136         
137         new Pman.Request({
138             url: baseURL + '/Login',  
139             params: {
140                 getAuthUser: true
141             },  
142             method: 'POST',  
143             success:  Pman.Login.success,
144             failure : Pman.Login.failure,
145             scope : Pman.Login
146               
147         });  
148     }, 
149     
150    
151     
152     
153     failure : function (res) // called if login 'check' fails.. (causes re-check)
154     {
155         this.authUser = -1;
156         this.sending = false;
157         
158         //console.log(res);
159         if ( Pman.Login.checkFails > 2) {
160             //Pman.Preview.disable();
161             Roo.bootstrap.MessageBox.alert("Error", res.errorMsg ? res.errorMsg : 
162                 "Error getting authentication status. - try reloading"); 
163             return;
164         }
165             
166         Pman.Login.check.defer(1000, Pman.Login, [ true ]);
167         return;  
168     },
169     
170     
171     success : function(res)  // check successfull...
172     {  
173         this.sending = false;
174         
175         if (!res.success) {
176             return this.failure(res);
177         }
178         if (!res.data || !res.data.id) {
179             return this.failure(res);
180         }
181         //console.log(res);
182         this.fillAuth(res.data);
183         
184         this.checkFails =0;
185         
186         
187         
188         Pman.loadUserInterface();
189         
190         if (Pman.Login.callback) {
191             Pman.Login.callback();
192             
193         }
194         return false;
195     },
196     
197     fillAuth: function(au) {
198         
199         //pause auth check in open system
200         if(au.id * 1 > 0) {
201             this.startAuthCheck();
202         }
203         
204         this.authUserId = au.id;
205         this.authUser = au;
206         this.lastChecked = new Date();
207         // if login is used on other applicaitons..
208         if (Pman.fireEvent) { Pman.fireEvent('authrefreshed', au); }
209         
210         
211         //Pman.Tab.FaxQueue.newMaxId(au.faxMax);
212         //Pman.Tab.FaxTab.setTitle(au.faxNumPending);
213         
214         //this.switchLang(Roo.state.Manager.get('Pman.Login.lang', 'en'));
215         Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, au.lang);
216         this.switchLang(au.lang);
217      
218         // open system... - -on setyp..
219         if (this.authUserId  < 0) {
220             Roo.bootstrap.MessageBox.alert(
221                 "Warning",
222                 "This is an open system - please set up a admin user with a password."
223             );  
224         }
225          
226         //Pman.onload(); // which should do nothing if it's a re-auth result...
227     },
228     
229     
230     intervalID : false,   /// the login refresher...
231     
232     lastChecked : false,
233     
234     startAuthCheck : function() // starter for timeout checking..
235     {
236         if (Pman.Login.intervalID) { // timer already in place...
237             return false;
238         }
239         
240         Pman.Login.intervalID =  window.setInterval(function() {
241                   Pman.Login.check(false);
242                 }, 120000); // every 120 secs = 2mins..
243         return true;
244         
245     },
246     
247        
248      
249     logout: function()
250     {
251         window.onbeforeunload = function() { }; // false does not work for IE..
252         Pman.Login.authUserId = -1;
253         Roo.Ajax.request({  
254             url: baseURL + '/Login.html',  
255             params: {
256                 logout: 1
257             },  
258             method: 'GET',
259             failure : function() {
260                 Roo.bootstrap.MessageBox.alert("Error", "Error logging out. - continuing anyway.", function() {
261                     document.location = baseURL + '?ts=' + Math.random();
262                 });
263                 
264             },
265             success : function() {
266                 Pman.Login.authUserId = -1;
267                 Pman.Login.checkFails =0;
268                 // remove the 
269                 document.location = baseURL + '?ts=' + Math.random();
270             }
271               
272               
273         }); 
274     },
275     switchLang : function (lang) {
276         if (!lang || !lang.length) {
277             return;
278         }
279         if (typeof(_T.en) == 'undefined') {
280             _T.en = {};
281             Roo.apply(_T.en, _T);
282         }
283         
284         if (typeof(_T[lang]) == 'undefined') {
285             Roo.bootstrap.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
286             return;
287         }
288         
289         
290         Roo.apply(_T, _T[lang]);
291         // just need to set the text values for everything...
292         if (this.form) {
293             
294                
295             function formLabel(name, val) {
296                 
297                 var lbl = Pman.Login.form.findField( name ).el.dom.parentNode.parentNode;
298                 if (lbl.getElementsByTagName('label').length) {
299                     lbl = lbl.getElementsByTagName('label')[0];
300                 } else  {
301                     lbl = lbl.parentNode.getElementsByTagName('label')[0];
302                 }
303                    
304                 lbl.innerHTML = val;
305             }
306             
307             formLabel('password', "Password"+':');
308             formLabel('username', "Email Address"+':');
309             formLabel('lang', "Language"+':');
310             this.dialog.setTitle("Login");
311             this.dialog.buttons[0].setText("Forgot Password");
312             this.dialog.buttons[1].setText("Login");
313         }
314         
315         
316     },
317     
318     inGroup : function(g)
319     {
320         return this.authUser && this.authUser.groups && 
321             this.authUser.groups.indexOf(g) > -1;
322     },
323     isOwner : function()
324     {
325         return this.authUser && this.authUser.company_id_comptype && 
326             this.authUser.company_id_comptype == 'OWNER';
327     },
328     
329     /**
330      * Depreciated = use Pman.I18n
331      */
332     
333     i18nList: function (type, codes)
334     {
335         
336         return Pman.I18n.listToNames(type, codes);
337     },
338     i18n: function(type, code) 
339     {
340         return Pman.I18n.toName(type, code);
341         
342     },
343     
344     openSystemCreateUser : function(data)
345     {
346         if (!data || !data.id) {
347             //Roo.log("Force Admin");
348             Pman.Dialog.BAdminStaff.show( 
349                 { 
350                     id : 0, 
351                     company_id : Pman.Login.authUser.company_id* 1,
352                     company_id_name : Pman.Login.authUser.company_id_name,
353                     role : 'Administrators'
354                 }, function(data) {
355                     //forceAdmin(data);
356                     Pman.Login.openSystemCreateUser(data);
357                 }
358             );
359             return;
360         }
361         Roo.state.Manager.set('Pman.Login.username', data.email),
362         window.onbeforeunload = false;
363         document.location = baseURL + '?ts=' + Math.random();
364     },
365     
366     openSystemCreateCompany: function(data)
367     {
368         if (Pman.Login.authUser.company_id * 1 > 0) {
369             //forceAdmin();
370             Pman.Login.openSystemCreateUser(data);
371             return;
372         }
373         if (!data || !data.id) {
374             Pman.Dialog.BAdminCompany.show( { id : 0, comptype: 'OWNER' }, function(data) {
375                 Roo.log("company dialog returned");
376                 Roo.log(data);
377                 //forceCompany(data);
378                 Pman.Login.openSystemCreateCompany(data);
379             });
380             return;
381         }
382         Pman.Login.authUser.company_id_id  = data.id;
383         Pman.Login.authUser.company_id  = data.id;
384         Pman.Login.authUser.company_id_name  = data.name;
385         Roo.log("forcing admin");
386         this.openSystemCreateUser();
387     },
388     
389     openSystem : function()
390     {
391         Roo.bootstrap.MessageBox.alert("Error", "Admin accounts have not been created - use the old admin at present");
392         
393         new Pman.Request({  
394             url: baseURL + '/Login',
395             method: 'POST',  
396             params: {
397                 check_owner_company: true
398             },  
399             success:  function(res)  {  // check successfull...
400                 
401                 if(res.data == 1) {
402                     this.openSystemCreateUser();
403                     return;
404                 }
405                 
406                 if(res.data == 0) {
407                     this.openSystemCreateCompany();
408                     return;
409                 }
410                 
411                 if(res.data > 0) {
412                     Roo.bootstrap.MessageBox.alert(
413                         "Error",  
414                         "There are more than 1 company in the system. please fix the data"
415                     );
416                     return;
417                 }
418             },
419             failure : function(res)
420             {
421                 Roo.bootstrap.MessageBox.alert(
422                     "Error",  
423                     "Invalid params for check owner company"
424                 );
425             },
426             scope : Pman.Login
427         });
428     },
429      /**
430      * hasPerm:
431      * Does the authenticated user have permission to see this.
432      * 
433      * @param {String} name the [Module].[permission] to check for
434      * @param {Char} lvl  - which type of permission to use (eg. S=show...)
435      * @returns {Boolean} tue indicates permission allowed
436      */
437     hasPerm: function(name, lvl) {
438         if (
439             (typeof(this.authUser) != 'object')
440             ||
441             (typeof(this.authUser.perms) != 'object')
442             ||
443             (typeof(this.authUser.perms[name]) != 'string')
444             ) {
445                 return false;
446         }
447         
448         return this.authUser.perms[name].indexOf(lvl) > -1;
449         
450     },
451     /**
452      * hasPermExists:
453      * Is there a permission defined for this (used by module registration.)
454      * 
455      * @param {String} name the [Module].[permission] to check for
456      * @returns {Boolean} tue indicates permission exists.
457      */
458     hasPermExists: function(name) {
459         if (
460             (typeof(this.authUser) != 'object')
461             ||
462             (typeof(this.authUser.perms) != 'object')
463             ||
464             (typeof(this.authUser.perms[name]) != 'string')
465             ) {
466                 return false;
467         }
468         return true;
469     }
470     
471     
472 });
473
474
475
476
477