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...
61 Roo.log('success : ');
65 if (!res.success) { // error!
67 //console.log('call failure');
68 return Pman.Login.failure(response,opts);
70 if (res.data.id*1 < 0) {
71 this.fillAuth(res.data);
72 return this.openSystem();
76 if (!res.data.id) { // id=0 == login failure.
77 return Pman.Dialog.Login.show({},
79 Pman.Login.success(data);
83 this.success(res.data);
85 failure : function(res)
87 switch (res.errorMsg) {
88 case 'BAD-IP-ADDRESS' :
89 Roo.bootstrap.MessageBox.alert('Error', 'Access Denied');
90 case 'NEW-IP-ADDRESS' :
92 case 'PENDING-IP-ADDRESS' :
94 case 'BLOCKED-IP-ADDRESS' :
100 return Pman.Dialog.Login.show({},
102 Pman.Login.success(data);
113 check: function(again) // called every so often to refresh cookie etc..
115 if (again) { // could be undefined..
116 Pman.Login.checkFails++;
118 Pman.Login.checkFails = 0;
123 if ( Pman.Login.checkFails > 4) {
124 //Pman.Preview.disable();
125 Roo.bootstrap.MessageBox.alert("Error",
126 "Error getting authentication status. - try reloading, or wait a while", function() {
127 _this.sending = false;
132 _this.check.defer(10000, _this, [ true ]); // check in 10 secs.
138 url: baseURL + '/Login',
143 success: Pman.Login.success,
144 failure : Pman.Login.failure,
153 failure : function (res) // called if login 'check' fails.. (causes re-check)
156 this.sending = false;
159 if ( Pman.Login.checkFails > 2) {
160 //Pman.Preview.disable();
161 Roo.bootstrap.MessageBox.alert("Error", res.errorMsg ? res.errorMsg :
162 "Error getting authentication status. - try reloading");
166 Pman.Login.check.defer(1000, Pman.Login, [ true ]);
171 success : function(res) // check successfull...
175 this.sending = false;
178 return this.failure(res);
180 if (!res.data || !res.data.id) {
181 return this.failure(res);
184 this.fillAuth(res.data);
190 Pman.loadUserInterface();
192 if (Pman.Login.callback) {
193 Pman.Login.callback();
199 fillAuth: function(au) {
200 this.startAuthCheck();
201 this.authUserId = au.id;
203 this.lastChecked = new Date();
204 // if login is used on other applicaitons..
205 if (Pman.fireEvent) { Pman.fireEvent('authrefreshed', au); }
208 //Pman.Tab.FaxQueue.newMaxId(au.faxMax);
209 //Pman.Tab.FaxTab.setTitle(au.faxNumPending);
211 //this.switchLang(Roo.state.Manager.get('Pman.Login.lang', 'en'));
212 Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, au.lang);
213 this.switchLang(au.lang);
216 // open system... - -on setyp..
217 if (this.authUserId < 0) {
218 Roo.bootstrap.MessageBox.alert("Warning",
219 "This is an open system - please set up a admin user with a password.");
222 //Pman.onload(); // which should do nothing if it's a re-auth result...
228 intervalID : false, /// the login refresher...
232 startAuthCheck : function() // starter for timeout checking..
234 if (Pman.Login.intervalID) { // timer already in place...
238 Pman.Login.intervalID = window.setInterval(function() {
239 Pman.Login.check(false);
240 }, 120000); // every 120 secs = 2mins..
249 window.onbeforeunload = function() { }; // false does not work for IE..
250 Pman.Login.authUserId = -1;
252 url: baseURL + '/Login.html',
257 failure : function() {
258 Roo.bootstrap.MessageBox.alert("Error", "Error logging out. - continuing anyway.", function() {
259 document.location = baseURL + '?ts=' + Math.random();
263 success : function() {
264 Pman.Login.authUserId = -1;
265 Pman.Login.checkFails =0;
267 document.location = baseURL + '?ts=' + Math.random();
273 switchLang : function (lang) {
274 if (!lang || !lang.length) {
277 if (typeof(_T.en) == 'undefined') {
279 Roo.apply(_T.en, _T);
282 if (typeof(_T[lang]) == 'undefined') {
283 Roo.bootstrap.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
288 Roo.apply(_T, _T[lang]);
289 // just need to set the text values for everything...
293 function formLabel(name, val) {
295 var lbl = Pman.Login.form.findField( name ).el.dom.parentNode.parentNode;
296 if (lbl.getElementsByTagName('label').length) {
297 lbl = lbl.getElementsByTagName('label')[0];
299 lbl = lbl.parentNode.getElementsByTagName('label')[0];
305 formLabel('password', "Password"+':');
306 formLabel('username', "Email Address"+':');
307 formLabel('lang', "Language"+':');
308 this.dialog.setTitle("Login");
309 this.dialog.buttons[0].setText("Forgot Password");
310 this.dialog.buttons[1].setText("Login");
316 inGroup : function(g)
318 return this.authUser && this.authUser.groups &&
319 this.authUser.groups.indexOf(g) > -1;
323 return this.authUser && this.authUser.company_id_comptype &&
324 this.authUser.company_id_comptype == 'OWNER';
328 * Depreciated = use Pman.I18n
331 i18nList: function (type, codes)
334 return Pman.I18n.listToNames(type, codes);
336 i18n: function(type, code)
338 return Pman.I18n.toName(type, code);
342 openSystemCreateUser : function(data)
344 if (!data || !data.id) {
345 //Roo.log("Force Admin");
346 Pman.Dialog.BAdminStaff.show(
349 company_id : Pman.Login.authUser.company_id* 1,
350 company_id_name : Pman.Login.authUser.company_id_name
353 Pman.Login.openSystemCreateUser(data);
358 Roo.state.Manager.set('Pman.Login.username', data.email),
359 window.onbeforeunload = false;
360 document.location = baseURL + '?ts=' + Math.random();
362 openSystemCreateCompany: function(data)
365 if (Pman.Login.authUser.company_id * 1 > 0) {
367 Pman.Login.openSystemCreateUser(data);
370 if (!data || !data.id) {
371 Pman.Dialog.BAdminCompany.show( { id : 0, comptype: 'OWNER' }, function(data) {
372 Roo.log("company dialog returned");
374 //forceCompany(data);
375 Pman.Login.openSystemCreateCompany(data);
379 Pman.Login.authUser.company_id_id = data.id;
380 Pman.Login.authUser.company_id = data.id;
381 Pman.Login.authUser.company_id_name = data.name;
382 Roo.log("forcing admin");
383 this.openSystemCreateUser();
387 openSystem : function()
390 Roo.bootstrap.MessageBox.alert("Error", "Admin accounts have not been created - use the old admin at present");
392 // create company if company_id is -1
393 if (Pman.Login.authUser.company_id* 1 < 0) {
394 this.openSystemCreateCompany();
398 // create user if id is -1
399 if (Pman.Login.authUser.id* 1 < 0) {
400 this.openSystemCreateUser();
409 * Does the authenticated user have permission to see this.
411 * @param {String} name the [Module].[permission] to check for
412 * @param {Char} lvl - which type of permission to use (eg. S=show...)
413 * @returns {Boolean} tue indicates permission allowed
415 hasPerm: function(name, lvl) {
417 (typeof(this.authUser) != 'object')
419 (typeof(this.authUser.perms) != 'object')
421 (typeof(this.authUser.perms[name]) != 'string')
426 return this.authUser.perms[name].indexOf(lvl) > -1;
431 * Is there a permission defined for this (used by module registration.)
433 * @param {String} name the [Module].[permission] to check for
434 * @returns {Boolean} tue indicates permission exists.
436 hasPermExists: function(name) {
438 (typeof(this.authUser) != 'object')
440 (typeof(this.authUser.perms) != 'object')
442 (typeof(this.authUser.perms[name]) != 'string')