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