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.
78 return Pman.Dialog.Login.show(
81 Pman.Login.success(data);
85 this.success(res.data);
87 failure : function(res)
89 return Pman.Dialog.Login.show({},
91 Pman.Login.success(data);
102 check: function(again) // called every so often to refresh cookie etc..
104 if (again) { // could be undefined..
105 Pman.Login.checkFails++;
107 Pman.Login.checkFails = 0;
112 if ( Pman.Login.checkFails > 4) {
113 //Pman.Preview.disable();
114 Roo.bootstrap.MessageBox.alert("Error",
115 "Error getting authentication status. - try reloading, or wait a while", function() {
116 _this.sending = false;
121 _this.check.defer(10000, _this, [ true ]); // check in 10 secs.
127 url: baseURL + '/Login',
132 success: Pman.Login.success,
133 failure : Pman.Login.failure,
142 failure : function (res) // called if login 'check' fails.. (causes re-check)
145 this.sending = false;
148 if ( Pman.Login.checkFails > 2) {
149 //Pman.Preview.disable();
150 Roo.bootstrap.MessageBox.alert("Error", res.errorMsg ? res.errorMsg :
151 "Error getting authentication status. - try reloading");
155 Pman.Login.check.defer(1000, Pman.Login, [ true ]);
160 success : function(res) // check successfull...
162 this.sending = false;
165 return this.failure(res);
167 if (!res.data || !res.data.id) {
168 return this.failure(res);
171 this.fillAuth(res.data);
177 Pman.loadUserInterface();
179 if (Pman.Login.callback) {
180 Pman.Login.callback();
186 fillAuth: function(au) {
188 //pause auth check in open system
190 this.startAuthCheck();
193 this.authUserId = au.id;
195 this.lastChecked = new Date();
196 // if login is used on other applicaitons..
197 if (Pman.fireEvent) { Pman.fireEvent('authrefreshed', au); }
200 //Pman.Tab.FaxQueue.newMaxId(au.faxMax);
201 //Pman.Tab.FaxTab.setTitle(au.faxNumPending);
203 //this.switchLang(Roo.state.Manager.get('Pman.Login.lang', 'en'));
204 Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, au.lang);
205 this.switchLang(au.lang);
207 // open system... - -on setyp..
208 if (this.authUserId < 0) {
209 Roo.bootstrap.MessageBox.alert(
211 "This is an open system - please set up a admin user with a password."
215 //Pman.onload(); // which should do nothing if it's a re-auth result...
219 intervalID : false, /// the login refresher...
223 startAuthCheck : function() // starter for timeout checking..
225 if (Pman.Login.intervalID) { // timer already in place...
229 Pman.Login.intervalID = window.setInterval(function() {
230 Pman.Login.check(false);
231 }, 120000); // every 120 secs = 2mins..
240 window.onbeforeunload = function() { }; // false does not work for IE..
241 Pman.Login.authUserId = -1;
243 url: baseURL + '/Login.html',
248 failure : function() {
249 Roo.bootstrap.MessageBox.alert("Error", "Error logging out. - continuing anyway.", function() {
250 document.location = baseURL + '?ts=' + Math.random();
254 success : function() {
255 Pman.Login.authUserId = -1;
256 Pman.Login.checkFails =0;
258 document.location = baseURL + '?ts=' + Math.random();
264 switchLang : function (lang) {
265 if (!lang || !lang.length) {
268 if (typeof(_T.en) == 'undefined') {
270 Roo.apply(_T.en, _T);
273 if (typeof(_T[lang]) == 'undefined') {
274 Roo.bootstrap.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
279 Roo.apply(_T, _T[lang]);
280 // just need to set the text values for everything...
284 function formLabel(name, val) {
286 var lbl = Pman.Login.form.findField( name ).el.dom.parentNode.parentNode;
287 if (lbl.getElementsByTagName('label').length) {
288 lbl = lbl.getElementsByTagName('label')[0];
290 lbl = lbl.parentNode.getElementsByTagName('label')[0];
296 formLabel('password', "Password"+':');
297 formLabel('username', "Email Address"+':');
298 formLabel('lang', "Language"+':');
299 this.dialog.setTitle("Login");
300 this.dialog.buttons[0].setText("Forgot Password");
301 this.dialog.buttons[1].setText("Login");
307 inGroup : function(g)
309 return this.authUser && this.authUser.groups &&
310 this.authUser.groups.indexOf(g) > -1;
314 return this.authUser && this.authUser.company_id_comptype &&
315 this.authUser.company_id_comptype == 'OWNER';
319 * Depreciated = use Pman.I18n
322 i18nList: function (type, codes)
325 return Pman.I18n.listToNames(type, codes);
327 i18n: function(type, code)
329 return Pman.I18n.toName(type, code);
333 openSystemCreateUser : function(data)
335 if (!data || !data.id) {
336 //Roo.log("Force Admin");
337 Pman.Dialog.BAdminStaff.show(
340 company_id : Pman.Login.authUser.company_id* 1,
341 company_id_name : Pman.Login.authUser.company_id_name,
342 role : 'Administrators'
345 Pman.Login.openSystemCreateUser(data);
350 Roo.state.Manager.set('Pman.Login.username', data.email),
351 window.onbeforeunload = false;
352 document.location = baseURL + '?ts=' + Math.random();
355 openSystemCreateCompany: function(data)
357 if (Pman.Login.authUser.company_id * 1 > 0) {
359 Pman.Login.openSystemCreateUser(data);
362 if (!data || !data.id) {
363 Pman.Dialog.BAdminCompany.show( { id : 0, comptype: 'OWNER' }, function(data) {
364 Roo.log("company dialog returned");
366 //forceCompany(data);
367 Pman.Login.openSystemCreateCompany(data);
371 Pman.Login.authUser.company_id_id = data.id;
372 Pman.Login.authUser.company_id = data.id;
373 Pman.Login.authUser.company_id_name = data.name;
374 Roo.log("forcing admin");
375 this.openSystemCreateUser();
378 openSystem : function()
380 Roo.bootstrap.MessageBox.alert("Error", "Admin accounts have not been created - use the old admin at present");
383 url: baseURL + '/Login',
386 check_owner_company: true
388 success: function(res) { // check successfull...
391 this.openSystemCreateUser();
396 this.openSystemCreateCompany();
401 Roo.bootstrap.MessageBox.alert(
403 "There are more than 1 company in the system. please fix the data"
408 failure : function(res)
410 Roo.bootstrap.MessageBox.alert(
412 "Invalid params for check owner company"
420 * Does the authenticated user have permission to see this.
422 * @param {String} name the [Module].[permission] to check for
423 * @param {Char} lvl - which type of permission to use (eg. S=show...)
424 * @returns {Boolean} tue indicates permission allowed
426 hasPerm: function(name, lvl) {
428 (typeof(this.authUser) != 'object')
430 (typeof(this.authUser.perms) != 'object')
432 (typeof(this.authUser.perms[name]) != 'string')
437 return this.authUser.perms[name].indexOf(lvl) > -1;
442 * Is there a permission defined for this (used by module registration.)
444 * @param {String} name the [Module].[permission] to check for
445 * @returns {Boolean} tue indicates permission exists.
447 hasPermExists: function(name) {
449 (typeof(this.authUser) != 'object')
451 (typeof(this.authUser.perms) != 'object')
453 (typeof(this.authUser.perms[name]) != 'string')