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