1 //<script type="text/javascript">
5 * fires Pman 'authrefreshed' event on every poll to server..
10 re-arrange language code...
11 * flipping language should be like this:
13 * Ext.apply(_T, _T[lang]);
19 Pman.Login = new Roo.util.Observable({
33 authUser: { id : false },
39 checkConnection : false, // the Roo.data.Connection for checking if still authenticated.
41 onLoad : function() // called on page load...
46 if (Roo.get('loading')) {
47 Roo.get('loading').remove();
49 this.switchLang('en');
51 // check if we are logged in..
53 url: baseURL + '/Login',
59 success: function(res) { // check successfull...
62 if (!res.success) { // error!
64 //console.log('call failure');
65 return Pman.Login.failure(response,opts);
67 if (res.data.id*1 < 0) {
68 return this.openSystem();
72 if (!res.data.id) { // id=0 == login failure.
73 return Pman.Dialog.Login.show({},
75 Pman.Login.success(data);
79 this.success(res.data);
83 return Pman.Dialog.Login.show({},
85 Pman.Login.success(data);
96 check: function(again) // called every so often to refresh cookie etc..
98 if (again) { // could be undefined..
99 Pman.Login.checkFails++;
101 Pman.Login.checkFails = 0;
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;
115 _this.check.defer(10000, _this, [ true ]); // check in 10 secs.
121 url: baseURL + '/Login',
126 success: Pman.Login.success,
127 failure : Pman.Login.failure,
136 failure : function (res) // called if login 'check' fails.. (causes re-check)
139 this.sending = false;
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");
149 Pman.Login.check.defer(1000, Pman.Login, [ true ]);
154 success : function(res) // check successfull...
158 this.sending = false;
161 return this.failure(res);
163 if (!res.data || !res.data.id) {
164 return this.failure(res);
167 this.fillAuth(res.data);
173 Pman.loadUserInterface();
175 if (Pman.Login.callback) {
176 Pman.Login.callback();
182 fillAuth: function(au) {
183 this.startAuthCheck();
184 this.authUserId = au.id;
186 this.lastChecked = new Date();
187 // if login is used on other applicaitons..
188 if (Pman.fireEvent) { Pman.fireEvent('authrefreshed', au); }
191 //Pman.Tab.FaxQueue.newMaxId(au.faxMax);
192 //Pman.Tab.FaxTab.setTitle(au.faxNumPending);
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);
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.");
205 //Pman.onload(); // which should do nothing if it's a re-auth result...
211 intervalID : false, /// the login refresher...
215 startAuthCheck : function() // starter for timeout checking..
217 if (Pman.Login.intervalID) { // timer already in place...
221 Pman.Login.intervalID = window.setInterval(function() {
222 Pman.Login.check(false);
223 }, 120000); // every 120 secs = 2mins..
232 window.onbeforeunload = function() { }; // false does not work for IE..
233 Pman.Login.authUserId = -1;
235 url: baseURL + '/Login.html',
240 failure : function() {
241 Roo.bootstrap.MessageBox.alert("Error", "Error logging out. - continuing anyway.", function() {
242 document.location = baseURL + '?ts=' + Math.random();
246 success : function() {
247 Pman.Login.authUserId = -1;
248 Pman.Login.checkFails =0;
250 document.location = baseURL + '?ts=' + Math.random();
256 switchLang : function (lang) {
257 if (!lang || !lang.length) {
260 if (typeof(_T.en) == 'undefined') {
262 Roo.apply(_T.en, _T);
265 if (typeof(_T[lang]) == 'undefined') {
266 Roo.bootstrap.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
271 Roo.apply(_T, _T[lang]);
272 // just need to set the text values for everything...
276 function formLabel(name, val) {
278 var lbl = Pman.Login.form.findField( name ).el.dom.parentNode.parentNode;
279 if (lbl.getElementsByTagName('label').length) {
280 lbl = lbl.getElementsByTagName('label')[0];
282 lbl = lbl.parentNode.getElementsByTagName('label')[0];
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");
299 inGroup : function(g)
301 return this.authUser && this.authUser.groups &&
302 this.authUser.groups.indexOf(g) > -1;
306 return this.authUser && this.authUser.company_id_comptype &&
307 this.authUser.company_id_comptype == 'OWNER';
311 * Depreciated = use Pman.I18n
314 i18nList: function (type, codes)
317 return Pman.I18n.listToNames(type, codes);
319 i18n: function(type, code)
321 return Pman.I18n.toName(type, code);
325 openSystemCreateUser : function(data)
327 if (!data || !data.id) {
328 //Roo.log("Force Admin");
329 Pman.Dialog.BAdminStaff.show(
332 company_id : Pman.Login.authUser.company_id_id * 1,
333 company_id_name : Pman.Login.authUser.company_id_name
336 Pman.Login.openSystemCreateUser(data);
341 Roo.state.Manager.set('Pman.Login.username', data.email),
342 window.onbeforeunload = false;
343 document.location = baseURL + '?ts=' + Math.random();
345 openSystemCreateCompany: function(data)
348 if (Pman.Login.authUser.company_id * 1 > 0) {
350 Pman.Login.openSystemCreateUser(data);
353 if (!data || !data.id) {
354 Pman.Dialog.BAdminCompany.show( { id : 0, comptype: 'OWNER' }, function(data) {
355 Roo.log("company dialog returned");
357 //forceCompany(data);
358 Pman.Login.openSystemCreateCompany(data);
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();
370 openSystem : function()
373 Roo.bootstrap.MessageBox.alert("Error", "Admin accounts have not been created - use the old admin at present");
375 // admin company has been created - create the user..
376 if (Pman.Login.authUser.company_id_id* 1 > 0) {
377 this.openSystemCreateUser();
381 this.openSystemCreateCompany();