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