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