1 //<script type="text/javascript">
5 * A generic Login Dialog..... - only one needed in theory!?!?
7 * Fires XComponent builder on success...
10 * username,password, lang = for login actions.
11 * check = for periodic checking that sesion is valid.
16 * Myapp.login = Roo.Login({
30 * Ext.apply(_T, _T[lang]);
34 Roo.Login = function(cfg)
41 Roo.each(this.items, function(e) {
44 Roo.Document.on('ready', function() {
48 Roo.Login.superclass.constructor.call(this);
52 Roo.extend(Roo.Login, Roo.LayoutDialog, {
55 * @cfg {String} method
56 * Method used to query for login details.
62 * URL to query login data. - eg. baseURL + '/Login.php'
66 onLoad : function() // called on page load...
70 if (Roo.get('loading')) { // clear any loading indicator..
71 Roo.get('loading').remove();
74 this.switchLang('en'); // set the language to english..
77 success: function(response, opts) { // check successfull...
79 var res = Pman.processResponse(response);
81 if (!res.success) { // error!
83 //console.log('call failure');
84 return this.failure(response,opts);
87 if (!res.data.id) { // id=0 == login failure.
88 return this.show(true);
92 //console.log(success);
93 this.fillAuth(res.data);
95 Roo.XComponent.build();
103 check: function(cfg) // called every so often to refresh cookie etc..
105 if (cfg.again) { // could be undefined..
112 if ( this.checkFails > 4) {
113 Roo.MessageBox.alert("Error",
114 "Error getting authentication status. - try reloading, or wait a while", function() {
115 _this.sending = false;
120 _this.check.defer(10000, _this, [ cfg ]); // check in 10 secs.
131 success: cfg.success || this.success,
132 failure : cfg.failure || this.failure,
140 success : function(response, opts) // check successfull...
142 this.sending = false;
143 var res = Pman.processResponse(response);
145 return this.failure(response, opts);
147 if (!res.data || !res.data.id) {
148 return this.failure(response,opts);
151 this.fillAuth(res.data);
158 failure : function (response, opts) // called if login 'check' fails.. (causes re-check)
161 this.sending = false;
162 var res = Pman.processResponse(response);
164 if ( Pman.Login.checkFails > 2) {
166 Roo.MessageBox.alert("Error", res.errorMsg ? res.errorMsg :
167 "Error getting authentication status. - try reloading");
170 opts.callCfg.again = true;
171 this.check.defer(1000, this, [ opts.callCfg ]);
177 fillAuth: function(au) {
178 this.startAuthCheck();
179 this.authUserId = au.id;
181 this.lastChecked = new Date();
182 this.fireEvent('refreshed', au);
183 //Pman.Tab.FaxQueue.newMaxId(au.faxMax);
184 //Pman.Tab.FaxTab.setTitle(au.faxNumPending);
186 //this.switchLang(Roo.state.Manager.get('Pman.Login.lang', 'en'));
187 Roo.state.Manager.set( this.realm + 'lang' , au.lang);
188 this.switchLang(au.lang);
191 // open system... - -on setyp..
192 if (this.authUserId < 0) {
193 Roo.MessageBox.alert("Warning",
194 "This is an open system - please set up a admin user with a password.");
197 //Pman.onload(); // which should do nothing if it's a re-auth result...
202 startAuthCheck : function() // starter for timeout checking..
204 if (Pman.Login.intervalID) { // timer already in place...
208 Pman.Login.intervalID = window.setInterval(function() {
209 Pman.Login.check(false);
210 }, 120000); // every 120 secs = 2mins..
230 // tabPosition: 'top',
233 alwaysShowTabs: false
236 rendered : function(dlg) {
237 this.items[0].dialog = dlg;
248 actionfailed : function(f, act) {
249 // form can return { errors: .... }
251 //act.result.errors // invalid form element list...
252 //act.result.errorMsg// invalid form element list...
254 this.dialog.el.unmask();
255 Roo.MessageBox.alert("Error", act.result.errorMsg ? act.result.errorMsg :
256 "Login failed - communication error - try again.");
259 actioncomplete: function(re, act) {
261 Roo.state.Manager.set(
262 this.dialog.realm + '.' + this.dialog.params.username,
263 this.form.findField(this.dialog.params.username).getValue()
265 Roo.state.Manager.set(
266 this.dialog.realm + '.lang',
267 Pman.Login.form.findField('lang').getValue()
270 this.dialog.fillAuth(act.result.data);
274 if (Roo.get('loading-mask')) {
275 Roo.get('loading-mask').show();
277 Roo.XComponent.build();
287 fieldLabel: "Email Address",
290 autoCreate : {tag: "input", type: "text", size: "20"}
295 fieldLabel: "Password",
296 inputType: 'password',
299 autoCreate : {tag: "input", type: "text", size: "20"},
301 specialkey : function(e,ev) {
302 if (ev.keyCode == 13) {
303 this.form.dialog.el.mask("Logging in");
304 this.form.doAction('submit', {
305 url: this.form.dialog.url,
306 method: this.form.dialog.method,
315 fieldLabel: "Language",
318 xtype : 'SimpleStore',
319 fields: ['lang', 'ldisp'],
322 [ 'zh_HK' , '\u7E41\u4E2D' ],
323 [ 'zh_CN', '\u7C21\u4E2D' ]
330 displayField:'ldisp',
334 triggerAction: 'all',
335 emptyText:'Select a Language...',
338 select : function(cb, rec, ix) {
339 this.form.switchLang(rec.data.lang);
353 label : "Forgot Password",
357 var n = _this.form.findField('username').getValue();
359 Roo.MessageBox.alert("Error", "Fill in your email address");
363 url: baseURL + '/Login.js',
368 success: function(response, opts) { // check successfull...
370 var res = Pman.processResponse(response);
371 if (!res.success) { // error!
372 Roo.MessageBox.alert("Error" , res.errorMsg ? res.errorMsg : "Problem Requesting Password Reset");
375 Roo.MessageBox.alert("Notice" , "Please check you email for the Password Reset message");
377 failure : function() {
378 Roo.MessageBox.alert("Error" , "Problem Requesting Password Reset");
387 Pman.Login = new Roo.util.Observable({
400 authUser: { id : false },
410 intervalID : false, /// the login refresher...
418 this.dialog.addButton("Forgot Password", function()
421 var n = _this.form.findField('username').getValue();
423 Roo.MessageBox.alert("Error", "Fill in your email address");
427 url: baseURL + '/Login.js',
432 success: function(response, opts) { // check successfull...
434 var res = Pman.processResponse(response);
435 if (!res.success) { // error!
436 Roo.MessageBox.alert("Error" , res.errorMsg ? res.errorMsg : "Problem Requesting Password Reset");
439 Roo.MessageBox.alert("Notice" , "Please check you email for the Password Reset message");
441 failure : function() {
442 Roo.MessageBox.alert("Error" , "Problem Requesting Password Reset");
448 this.dialog.addButton("Login", function()
450 Pman.Login.dialog.el.mask("Logging in");
451 Pman.Login.form.doAction('submit', {
452 url: baseURL + '/Login',
456 this.layout = this.dialog.getLayout();
457 this.layout.beginUpdate();
459 //layout.add('center', new Roo.ContentPanel('center', {title: 'The First Tab'}));
460 // generate some other tabs
461 this.form = new Roo.form.Form({
465 actionfailed : function(f, act) {
466 // form can return { errors: .... }
468 //act.result.errors // invalid form element list...
469 //act.result.errorMsg// invalid form element list...
471 Pman.Login.dialog.el.unmask();
472 Roo.MessageBox.alert("Error", act.result.errorMsg ? act.result.errorMsg :
473 "Login failed - communication error - try again.");
476 actioncomplete: function(re, act) {
478 Roo.state.Manager.set('Pman.Login.username.'+appNameShort, Pman.Login.form.findField('username').getValue() );
479 Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, Pman.Login.form.findField('lang').getValue() );
480 Pman.Login.fillAuth(act.result.data);
482 Pman.Login.dialog.hide();
483 if (Roo.get('loading-mask')) {
484 //Roo.get('loading').show();
485 Roo.get('loading-mask').show();
509 var ef = this.dialog.getLayout().getEl().createChild({tag: 'div'});
510 ef.dom.style.margin = 10;
512 this.form.render(ef.dom);
513 // logoprefix comes from base config.
514 Pman.Login.form.el.createChild({
516 src: rootURL + '/Pman/'+appNameShort + '/templates/images/logo.gif',
517 style: 'margin-bottom: 10px;'
519 Pman.Login.form.el.dom.firstChild
522 var vp = this.dialog.getLayout().add('center', new Roo.ContentPanel(ef, {
524 //title: 'Org Details',
531 this.layout.endUpdate();
533 this.fireEvent('render', this);
540 resizeToLogo : function()
542 var sz = Roo.get(Pman.Login.form.el.query('img')[0]).getSize();
544 this.resizeToLogo.defer(1000,this);
547 var w = Ext.lib.Dom.getViewWidth() - 100;
548 var h = Ext.lib.Dom.getViewHeight() - 100;
549 Pman.Login.dialog.resizeTo(Math.max(350, Math.min(sz.width + 30, w)),Math.min(sz.height+200, h));
550 Pman.Login.dialog.center();
555 show: function (modal)
560 modal = modal || false;
561 if (Pman.Login.authUserId < 0) { // logout!?
565 if (Pman.Login.intervalID) {
567 window.clearInterval(Pman.Login.intervalID);
568 Pman.Login.intervalID = false;
575 if (Roo.get('loading')) {
576 Roo.get('loading').remove();
578 if (Roo.get('loading-mask')) {
579 Roo.get('loading-mask').hide();
582 //incomming._node = tnode;
584 this.dialog.modal = !modal;
586 this.dialog.el.unmask();
587 this.resizeToLogo.defer(1000,this);
590 this.form.setValues({
591 'username' : Roo.state.Manager.get('Pman.Login.username.'+appNameShort, ''),
592 'lang' : Roo.state.Manager.get('Pman.Login.lang.'+appNameShort, 'en')
594 Pman.Login.switchLang(Roo.state.Manager.get('Pman.Login.lang.'+appNameShort, ''));
595 if (this.form.findField('username').getValue().length > 0 ){
596 this.form.findField('password').focus();
598 this.form.findField('username').focus();
608 window.onbeforeunload = function() { }; // false does not work for IE..
609 Pman.Login.authUserId = -1;
611 url: baseURL + '/Login.html',
616 failure : function() {
617 Roo.MessageBox.alert("Error", "Error logging out. - continuing anyway.", function() {
618 document.location = baseURL + '?ts=' + Math.random();
622 success : function() {
623 Pman.Login.authUserId = -1;
624 Pman.Login.checkFails =0;
626 document.location = baseURL + '?ts=' + Math.random();
632 switchLang : function (lang) {
636 if (typeof(_T.en) == 'undefined') {
638 Roo.apply(_T.en, _T);
641 if (typeof(_T[lang]) == 'undefined') {
642 Roo.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
647 Roo.apply(_T, _T[lang]);
648 // just need to set the text values for everything...
652 function formLabel(name, val) {
654 var lbl = Pman.Login.form.findField( name ).el.dom.parentNode.parentNode;
655 if (lbl.getElementsByTagName('label').length) {
656 lbl = lbl.getElementsByTagName('label')[0];
658 lbl = lbl.parentNode.getElementsByTagName('label')[0];
664 formLabel('password', "Password"+':');
665 formLabel('username', "Email Address"+':');
666 formLabel('lang', "Language"+':');
667 this.dialog.setTitle("Login");
668 this.dialog.buttons[0].setText("Forgot Password");
669 this.dialog.buttons[1].setText("Login");
675 inGroup : function(g)
677 return this.authUser && this.authUser.groups &&
678 this.authUser.groups.indexOf(g) > -1;
682 return this.authUser && this.authUser.company_id_comptype &&
683 this.authUser.company_id_comptype == 'OWNER';
687 * Depreciated = use Pman.I18n
690 i18nList: function (type, codes)
693 return Pman.I18n.listToNames(type, codes);
695 i18n: function(type, code)
697 return Pman.I18n.toName(type, code);