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