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);
71 if (res.data.id*1 < 0) {
72 this.fillAuth(res.data);
73 return this.openSystem();
77 if (!res.data.id) { // id=0 == login failure.
79 if(window.location.pathname.substr(baseURL.length).match(/\/PasswordReset\//)){
80 Pman.Dialog.BAdminPasswordReset.show({}, function(){
81 window.location.href = baseURL;
89 return Pman.Dialog.Login.show(
92 Pman.Login.success(data);
96 this.success(res.data);
98 failure : function(res)
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...
173 this.sending = false;
176 return this.failure(res);
178 if (!res.data || !res.data.id) {
179 return this.failure(res);
182 this.fillAuth(res.data);
188 Pman.loadUserInterface();
190 if (Pman.Login.callback) {
191 Pman.Login.callback();
197 fillAuth: function(au) {
199 //pause auth check in open system
201 this.startAuthCheck();
204 this.authUserId = au.id;
206 this.lastChecked = new Date();
207 // if login is used on other applicaitons..
208 if (Pman.fireEvent) { Pman.fireEvent('authrefreshed', au); }
211 //Pman.Tab.FaxQueue.newMaxId(au.faxMax);
212 //Pman.Tab.FaxTab.setTitle(au.faxNumPending);
214 //this.switchLang(Roo.state.Manager.get('Pman.Login.lang', 'en'));
215 Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, au.lang);
216 this.switchLang(au.lang);
218 // open system... - -on setyp..
219 if (this.authUserId < 0) {
220 Roo.bootstrap.MessageBox.alert(
222 "This is an open system - please set up a admin user with a password."
226 //Pman.onload(); // which should do nothing if it's a re-auth result...
230 intervalID : false, /// the login refresher...
234 startAuthCheck : function() // starter for timeout checking..
236 if (Pman.Login.intervalID) { // timer already in place...
240 Pman.Login.intervalID = window.setInterval(function() {
241 Pman.Login.check(false);
242 }, 120000); // every 120 secs = 2mins..
251 window.onbeforeunload = function() { }; // false does not work for IE..
252 Pman.Login.authUserId = -1;
254 url: baseURL + '/Login.html',
259 failure : function() {
260 Roo.bootstrap.MessageBox.alert("Error", "Error logging out. - continuing anyway.", function() {
261 document.location = baseURL + '?ts=' + Math.random();
265 success : function() {
266 Pman.Login.authUserId = -1;
267 Pman.Login.checkFails =0;
269 document.location = baseURL + '?ts=' + Math.random();
275 switchLang : function (lang) {
276 if (!lang || !lang.length) {
279 if (typeof(_T.en) == 'undefined') {
281 Roo.apply(_T.en, _T);
284 if (typeof(_T[lang]) == 'undefined') {
285 Roo.bootstrap.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
290 Roo.apply(_T, _T[lang]);
291 // just need to set the text values for everything...
295 function formLabel(name, val) {
297 var lbl = Pman.Login.form.findField( name ).el.dom.parentNode.parentNode;
298 if (lbl.getElementsByTagName('label').length) {
299 lbl = lbl.getElementsByTagName('label')[0];
301 lbl = lbl.parentNode.getElementsByTagName('label')[0];
307 formLabel('password', "Password"+':');
308 formLabel('username', "Email Address"+':');
309 formLabel('lang', "Language"+':');
310 this.dialog.setTitle("Login");
311 this.dialog.buttons[0].setText("Forgot Password");
312 this.dialog.buttons[1].setText("Login");
318 inGroup : function(g)
320 return this.authUser && this.authUser.groups &&
321 this.authUser.groups.indexOf(g) > -1;
325 return this.authUser && this.authUser.company_id_comptype &&
326 this.authUser.company_id_comptype == 'OWNER';
330 * Depreciated = use Pman.I18n
333 i18nList: function (type, codes)
336 return Pman.I18n.listToNames(type, codes);
338 i18n: function(type, code)
340 return Pman.I18n.toName(type, code);
344 openSystemCreateUser : function(data)
346 if (!data || !data.id) {
347 //Roo.log("Force Admin");
348 Pman.Dialog.BAdminStaff.show(
351 company_id : Pman.Login.authUser.company_id* 1,
352 company_id_name : Pman.Login.authUser.company_id_name,
353 role : 'Administrators'
356 Pman.Login.openSystemCreateUser(data);
361 Roo.state.Manager.set('Pman.Login.username', data.email),
362 window.onbeforeunload = false;
363 document.location = baseURL + '?ts=' + Math.random();
366 openSystemCreateCompany: function(data)
368 if (Pman.Login.authUser.company_id * 1 > 0) {
370 Pman.Login.openSystemCreateUser(data);
373 if (!data || !data.id) {
374 Pman.Dialog.BAdminCompany.show( { id : 0, comptype: 'OWNER' }, function(data) {
375 Roo.log("company dialog returned");
377 //forceCompany(data);
378 Pman.Login.openSystemCreateCompany(data);
382 Pman.Login.authUser.company_id_id = data.id;
383 Pman.Login.authUser.company_id = data.id;
384 Pman.Login.authUser.company_id_name = data.name;
385 Roo.log("forcing admin");
386 this.openSystemCreateUser();
389 openSystem : function()
391 Roo.bootstrap.MessageBox.alert("Error", "Admin accounts have not been created - use the old admin at present");
394 url: baseURL + '/Login',
397 check_owner_company: true
399 success: function(res) { // check successfull...
402 this.openSystemCreateUser();
407 this.openSystemCreateCompany();
412 Roo.bootstrap.MessageBox.alert(
414 "There are more than 1 company in the system. please fix the data"
419 failure : function(res)
421 Roo.bootstrap.MessageBox.alert(
423 "Invalid params for check owner company"
431 * Does the authenticated user have permission to see this.
433 * @param {String} name the [Module].[permission] to check for
434 * @param {Char} lvl - which type of permission to use (eg. S=show...)
435 * @returns {Boolean} tue indicates permission allowed
437 hasPerm: function(name, lvl) {
439 (typeof(this.authUser) != 'object')
441 (typeof(this.authUser.perms) != 'object')
443 (typeof(this.authUser.perms[name]) != 'string')
448 return this.authUser.perms[name].indexOf(lvl) > -1;
453 * Is there a permission defined for this (used by module registration.)
455 * @param {String} name the [Module].[permission] to check for
456 * @returns {Boolean} tue indicates permission exists.
458 hasPermExists: function(name) {
460 (typeof(this.authUser) != 'object')
462 (typeof(this.authUser.perms) != 'object')
464 (typeof(this.authUser.perms[name]) != 'string')