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 },
41 checkConnection : false, // the Roo.data.Connection for checking if still authenticated.
43 onLoad : function() // called on page load...
48 if (Roo.get('loading')) {
49 Roo.get('loading').remove();
51 this.switchLang('en');
53 // check if we are logged in..
55 url: baseURL + '/Login',
61 success: function(res) { // check successfull...
64 if (!res.success) { // error!
66 //console.log('call failure');
67 return Pman.Login.failure(response,opts);
69 if (res.data.id*1 < 0) {
70 this.companyId = res.data.company_id;
71 return this.openSystem();
75 if (!res.data.id) { // id=0 == login failure.
76 return Pman.Dialog.Login.show({},
78 Pman.Login.success(data);
82 this.success(res.data);
86 return Pman.Dialog.Login.show({},
88 Pman.Login.success(data);
99 check: function(again) // called every so often to refresh cookie etc..
101 if (again) { // could be undefined..
102 Pman.Login.checkFails++;
104 Pman.Login.checkFails = 0;
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;
118 _this.check.defer(10000, _this, [ true ]); // check in 10 secs.
124 url: baseURL + '/Login',
129 success: Pman.Login.success,
130 failure : Pman.Login.failure,
139 failure : function (res) // called if login 'check' fails.. (causes re-check)
142 this.sending = false;
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");
152 Pman.Login.check.defer(1000, Pman.Login, [ true ]);
157 success : function(res) // check successfull...
161 this.sending = false;
164 return this.failure(res);
166 if (!res.data || !res.data.id) {
167 return this.failure(res);
170 this.fillAuth(res.data);
176 Pman.loadUserInterface();
178 if (Pman.Login.callback) {
179 Pman.Login.callback();
185 fillAuth: function(au) {
186 this.startAuthCheck();
187 this.authUserId = au.id;
189 this.lastChecked = new Date();
190 // if login is used on other applicaitons..
191 if (Pman.fireEvent) { Pman.fireEvent('authrefreshed', au); }
194 //Pman.Tab.FaxQueue.newMaxId(au.faxMax);
195 //Pman.Tab.FaxTab.setTitle(au.faxNumPending);
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);
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.");
208 //Pman.onload(); // which should do nothing if it's a re-auth result...
214 intervalID : false, /// the login refresher...
218 startAuthCheck : function() // starter for timeout checking..
220 if (Pman.Login.intervalID) { // timer already in place...
224 Pman.Login.intervalID = window.setInterval(function() {
225 Pman.Login.check(false);
226 }, 120000); // every 120 secs = 2mins..
235 window.onbeforeunload = function() { }; // false does not work for IE..
236 Pman.Login.authUserId = -1;
238 url: baseURL + '/Login.html',
243 failure : function() {
244 Roo.bootstrap.MessageBox.alert("Error", "Error logging out. - continuing anyway.", function() {
245 document.location = baseURL + '?ts=' + Math.random();
249 success : function() {
250 Pman.Login.authUserId = -1;
251 Pman.Login.checkFails =0;
253 document.location = baseURL + '?ts=' + Math.random();
259 switchLang : function (lang) {
260 if (!lang || !lang.length) {
263 if (typeof(_T.en) == 'undefined') {
265 Roo.apply(_T.en, _T);
268 if (typeof(_T[lang]) == 'undefined') {
269 Roo.bootstrap.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
274 Roo.apply(_T, _T[lang]);
275 // just need to set the text values for everything...
279 function formLabel(name, val) {
281 var lbl = Pman.Login.form.findField( name ).el.dom.parentNode.parentNode;
282 if (lbl.getElementsByTagName('label').length) {
283 lbl = lbl.getElementsByTagName('label')[0];
285 lbl = lbl.parentNode.getElementsByTagName('label')[0];
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");
302 inGroup : function(g)
304 return this.authUser && this.authUser.groups &&
305 this.authUser.groups.indexOf(g) > -1;
309 return this.authUser && this.authUser.company_id_comptype &&
310 this.authUser.company_id_comptype == 'OWNER';
314 * Depreciated = use Pman.I18n
317 i18nList: function (type, codes)
320 return Pman.I18n.listToNames(type, codes);
322 i18n: function(type, code)
324 return Pman.I18n.toName(type, code);
328 openSystemCreateUser : function(data)
330 if (!data || !data.id) {
331 //Roo.log("Force Admin");
332 Pman.Dialog.BAdminStaff.show(
335 company_id : Pman.Login.companyId * 1,
336 company_id_name : Pman.Login.authUser.company_id_name
339 Pman.Login.openSystemCreateUser(data);
344 Roo.state.Manager.set('Pman.Login.username', data.email),
345 window.onbeforeunload = false;
346 document.location = baseURL + '?ts=' + Math.random();
348 openSystemCreateCompany: function(data)
351 if (Pman.Login.authUser.company_id * 1 > 0) {
353 Pman.Login.openSystemCreateUser(data);
356 if (!data || !data.id) {
357 Pman.Dialog.BAdminCompany.show( { id : 0, comptype: 'OWNER' }, function(data) {
358 Roo.log("company dialog returned");
360 //forceCompany(data);
361 Pman.Login.openSystemCreateCompany(data);
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();
373 openSystem : function()
376 Roo.bootstrap.MessageBox.alert("Error", "Admin accounts have not been created - use the old admin at present");
378 // create company if company_id is -1
379 if (Pman.Login.companyId* 1 < 0) {
380 this.openSystemCreateCompany();
384 // create user if id is -1
385 if (Pman.Login.authUser.id* 1 < 0) {
386 this.openSystemCreateUser();