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 return Pman.Dialog.Login.show({},
89 Pman.Login.success(data);
100 check: function(again) // called every so often to refresh cookie etc..
102 if (again) { // could be undefined..
103 Pman.Login.checkFails++;
105 Pman.Login.checkFails = 0;
110 if ( Pman.Login.checkFails > 4) {
111 //Pman.Preview.disable();
112 Roo.bootstrap.MessageBox.alert("Error",
113 "Error getting authentication status. - try reloading, or wait a while", function() {
114 _this.sending = false;
119 _this.check.defer(10000, _this, [ true ]); // check in 10 secs.
125 url: baseURL + '/Login',
130 success: Pman.Login.success,
131 failure : Pman.Login.failure,
140 failure : function (res) // called if login 'check' fails.. (causes re-check)
143 this.sending = false;
146 if ( Pman.Login.checkFails > 2) {
147 //Pman.Preview.disable();
148 Roo.bootstrap.MessageBox.alert("Error", res.errorMsg ? res.errorMsg :
149 "Error getting authentication status. - try reloading");
153 Pman.Login.check.defer(1000, Pman.Login, [ true ]);
158 success : function(res) // check successfull...
160 this.sending = false;
163 return this.failure(res);
165 if (!res.data || !res.data.id) {
166 return this.failure(res);
169 this.fillAuth(res.data);
175 Pman.loadUserInterface();
177 if (Pman.Login.callback) {
178 Pman.Login.callback();
184 fillAuth: function(au) {
186 //pause auth check in open system
188 this.startAuthCheck();
191 this.authUserId = au.id;
193 this.lastChecked = new Date();
194 // if login is used on other applicaitons..
195 if (Pman.fireEvent) { Pman.fireEvent('authrefreshed', au); }
198 //Pman.Tab.FaxQueue.newMaxId(au.faxMax);
199 //Pman.Tab.FaxTab.setTitle(au.faxNumPending);
201 //this.switchLang(Roo.state.Manager.get('Pman.Login.lang', 'en'));
202 Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, au.lang);
203 this.switchLang(au.lang);
205 // open system... - -on setyp..
206 if (this.authUserId < 0) {
207 Roo.bootstrap.MessageBox.alert(
209 "This is an open system - please set up a admin user with a password."
213 //Pman.onload(); // which should do nothing if it's a re-auth result...
217 intervalID : false, /// the login refresher...
221 startAuthCheck : function() // starter for timeout checking..
223 if (Pman.Login.intervalID) { // timer already in place...
227 Pman.Login.intervalID = window.setInterval(function() {
228 Pman.Login.check(false);
229 }, 120000); // every 120 secs = 2mins..
238 window.onbeforeunload = function() { }; // false does not work for IE..
239 Pman.Login.authUserId = -1;
241 url: baseURL + '/Login.html',
246 failure : function() {
247 Roo.bootstrap.MessageBox.alert("Error", "Error logging out. - continuing anyway.", function() {
248 document.location = baseURL + '?ts=' + Math.random();
252 success : function() {
253 Pman.Login.authUserId = -1;
254 Pman.Login.checkFails =0;
256 document.location = baseURL + '?ts=' + Math.random();
262 switchLang : function (lang) {
263 if (!lang || !lang.length) {
266 if (typeof(_T.en) == 'undefined') {
268 Roo.apply(_T.en, _T);
271 if (typeof(_T[lang]) == 'undefined') {
272 Roo.bootstrap.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
277 Roo.apply(_T, _T[lang]);
278 // just need to set the text values for everything...
282 function formLabel(name, val) {
284 var lbl = Pman.Login.form.findField( name ).el.dom.parentNode.parentNode;
285 if (lbl.getElementsByTagName('label').length) {
286 lbl = lbl.getElementsByTagName('label')[0];
288 lbl = lbl.parentNode.getElementsByTagName('label')[0];
294 formLabel('password', "Password"+':');
295 formLabel('username', "Email Address"+':');
296 formLabel('lang', "Language"+':');
297 this.dialog.setTitle("Login");
298 this.dialog.buttons[0].setText("Forgot Password");
299 this.dialog.buttons[1].setText("Login");
305 inGroup : function(g)
307 return this.authUser && this.authUser.groups &&
308 this.authUser.groups.indexOf(g) > -1;
312 return this.authUser && this.authUser.company_id_comptype &&
313 this.authUser.company_id_comptype == 'OWNER';
317 * Depreciated = use Pman.I18n
320 i18nList: function (type, codes)
323 return Pman.I18n.listToNames(type, codes);
325 i18n: function(type, code)
327 return Pman.I18n.toName(type, code);
331 openSystemCreateUser : function(data)
333 if (!data || !data.id) {
334 //Roo.log("Force Admin");
335 Pman.Dialog.BAdminStaff.show(
338 company_id : Pman.Login.authUser.company_id* 1,
339 company_id_name : Pman.Login.authUser.company_id_name,
340 core_group : 'Administrators'
343 Pman.Login.openSystemCreateUser(data);
348 Roo.state.Manager.set('Pman.Login.username', data.email),
349 window.onbeforeunload = false;
350 document.location = baseURL + '?ts=' + Math.random();
352 openSystemCreateCompany: function(data)
355 if (Pman.Login.authUser.company_id * 1 > 0) {
357 Pman.Login.openSystemCreateUser(data);
360 if (!data || !data.id) {
361 Pman.Dialog.BAdminCompany.show( { id : 0, comptype: 'OWNER' }, function(data) {
362 Roo.log("company dialog returned");
364 //forceCompany(data);
365 Pman.Login.openSystemCreateCompany(data);
369 Pman.Login.authUser.company_id_id = data.id;
370 Pman.Login.authUser.company_id = data.id;
371 Pman.Login.authUser.company_id_name = data.name;
372 Roo.log("forcing admin");
373 this.openSystemCreateUser();
377 openSystem : function()
379 Roo.bootstrap.MessageBox.alert("Error", "Admin accounts have not been created - use the old admin at present");
381 if (Pman.Login.authUser.company_id* 1 <= 0) {
382 this.openSystemCreateCompany();
386 // create user if id is -1
387 if (Pman.Login.authUser.id* 1 <= 0) {
388 this.openSystemCreateUser();
395 * Does the authenticated user have permission to see this.
397 * @param {String} name the [Module].[permission] to check for
398 * @param {Char} lvl - which type of permission to use (eg. S=show...)
399 * @returns {Boolean} tue indicates permission allowed
401 hasPerm: function(name, lvl) {
403 (typeof(this.authUser) != 'object')
405 (typeof(this.authUser.perms) != 'object')
407 (typeof(this.authUser.perms[name]) != 'string')
412 return this.authUser.perms[name].indexOf(lvl) > -1;
417 * Is there a permission defined for this (used by module registration.)
419 * @param {String} name the [Module].[permission] to check for
420 * @returns {Boolean} tue indicates permission exists.
422 hasPermExists: function(name) {
424 (typeof(this.authUser) != 'object')
426 (typeof(this.authUser.perms) != 'object')
428 (typeof(this.authUser.perms[name]) != 'string')