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 = 1 for periodic checking that sesion is valid.
12 * passwordRequest = email request password
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
237 rendered : function(dlg) {
238 this.form = this.items[0];
239 this.form.dialog = dlg;
240 this.buttons[0].form = this.form;
241 this.buttons[0].dialog = dlg
242 this.buttons[1].form = this.form;
243 this.buttons[1].dialog = dlg
254 actionfailed : function(f, act) {
255 // form can return { errors: .... }
257 //act.result.errors // invalid form element list...
258 //act.result.errorMsg// invalid form element list...
260 this.dialog.el.unmask();
261 Roo.MessageBox.alert("Error", act.result.errorMsg ? act.result.errorMsg :
262 "Login failed - communication error - try again.");
265 actioncomplete: function(re, act) {
267 Roo.state.Manager.set(
268 this.dialog.realm + '.' + this.dialog.params.username,
269 this.form.findField(this.dialog.params.username).getValue()
271 Roo.state.Manager.set(
272 this.dialog.realm + '.lang',
273 Pman.Login.form.findField('lang').getValue()
276 this.dialog.fillAuth(act.result.data);
280 if (Roo.get('loading-mask')) {
281 Roo.get('loading-mask').show();
283 Roo.XComponent.build();
293 fieldLabel: "Email Address",
296 autoCreate : {tag: "input", type: "text", size: "20"}
301 fieldLabel: "Password",
302 inputType: 'password',
305 autoCreate : {tag: "input", type: "text", size: "20"},
307 specialkey : function(e,ev) {
308 if (ev.keyCode == 13) {
309 this.form.dialog.el.mask("Logging in");
310 this.form.doAction('submit', {
311 url: this.form.dialog.url,
312 method: this.form.dialog.method,
321 fieldLabel: "Language",
324 xtype : 'SimpleStore',
325 fields: ['lang', 'ldisp'],
328 [ 'zh_HK' , '\u7E41\u4E2D' ],
329 [ 'zh_CN', '\u7C21\u4E2D' ]
336 displayField:'ldisp',
340 triggerAction: 'all',
341 emptyText:'Select a Language...',
344 select : function(cb, rec, ix) {
345 this.form.switchLang(rec.data.lang);
359 label : "Forgot Password",
363 var n = this.form.findField('username').getValue();
365 Roo.MessageBox.alert("Error", "Fill in your email address");
373 method: this.dialog.method,
374 success: function(response, opts) { // check successfull...
376 var res = Pman.processResponse(response);
377 if (!res.success) { // error!
378 Roo.MessageBox.alert("Error" ,
379 res.errorMsg ? res.errorMsg : "Problem Requesting Password Reset");
382 Roo.MessageBox.alert("Notice" ,
383 "Please check you email for the Password Reset message");
385 failure : function() {
386 Roo.MessageBox.alert("Error" , "Problem Requesting Password Reset");
399 this.dialog.el.mask("Logging in");
400 this.form.doAction('submit', {
402 method: this.dialog.method
408 Pman.Login = new Roo.util.Observable({
421 authUser: { id : false },
431 intervalID : false, /// the login refresher...
439 this.dialog.addButton("Forgot Password", function()
442 var n = _this.form.findField('username').getValue();
444 Roo.MessageBox.alert("Error", "Fill in your email address");
448 url: baseURL + '/Login.js',
453 success: function(response, opts) { // check successfull...
455 var res = Pman.processResponse(response);
456 if (!res.success) { // error!
457 Roo.MessageBox.alert("Error" , res.errorMsg ? res.errorMsg : "Problem Requesting Password Reset");
460 Roo.MessageBox.alert("Notice" , "Please check you email for the Password Reset message");
462 failure : function() {
463 Roo.MessageBox.alert("Error" , "Problem Requesting Password Reset");
469 this.dialog.addButton("Login", function()
471 Pman.Login.dialog.el.mask("Logging in");
472 Pman.Login.form.doAction('submit', {
473 url: baseURL + '/Login',
477 this.layout = this.dialog.getLayout();
478 this.layout.beginUpdate();
480 //layout.add('center', new Roo.ContentPanel('center', {title: 'The First Tab'}));
481 // generate some other tabs
482 this.form = new Roo.form.Form({
486 actionfailed : function(f, act) {
487 // form can return { errors: .... }
489 //act.result.errors // invalid form element list...
490 //act.result.errorMsg// invalid form element list...
492 Pman.Login.dialog.el.unmask();
493 Roo.MessageBox.alert("Error", act.result.errorMsg ? act.result.errorMsg :
494 "Login failed - communication error - try again.");
497 actioncomplete: function(re, act) {
499 Roo.state.Manager.set('Pman.Login.username.'+appNameShort, Pman.Login.form.findField('username').getValue() );
500 Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, Pman.Login.form.findField('lang').getValue() );
501 Pman.Login.fillAuth(act.result.data);
503 Pman.Login.dialog.hide();
504 if (Roo.get('loading-mask')) {
505 //Roo.get('loading').show();
506 Roo.get('loading-mask').show();
530 var ef = this.dialog.getLayout().getEl().createChild({tag: 'div'});
531 ef.dom.style.margin = 10;
533 this.form.render(ef.dom);
534 // logoprefix comes from base config.
535 Pman.Login.form.el.createChild({
537 src: rootURL + '/Pman/'+appNameShort + '/templates/images/logo.gif',
538 style: 'margin-bottom: 10px;'
540 Pman.Login.form.el.dom.firstChild
543 var vp = this.dialog.getLayout().add('center', new Roo.ContentPanel(ef, {
545 //title: 'Org Details',
552 this.layout.endUpdate();
554 this.fireEvent('render', this);
561 resizeToLogo : function()
563 var sz = Roo.get(Pman.Login.form.el.query('img')[0]).getSize();
565 this.resizeToLogo.defer(1000,this);
568 var w = Ext.lib.Dom.getViewWidth() - 100;
569 var h = Ext.lib.Dom.getViewHeight() - 100;
570 Pman.Login.dialog.resizeTo(Math.max(350, Math.min(sz.width + 30, w)),Math.min(sz.height+200, h));
571 Pman.Login.dialog.center();
576 show: function (modal)
581 modal = modal || false;
582 if (Pman.Login.authUserId < 0) { // logout!?
586 if (Pman.Login.intervalID) {
588 window.clearInterval(Pman.Login.intervalID);
589 Pman.Login.intervalID = false;
596 if (Roo.get('loading')) {
597 Roo.get('loading').remove();
599 if (Roo.get('loading-mask')) {
600 Roo.get('loading-mask').hide();
603 //incomming._node = tnode;
605 this.dialog.modal = !modal;
607 this.dialog.el.unmask();
608 this.resizeToLogo.defer(1000,this);
611 this.form.setValues({
612 'username' : Roo.state.Manager.get('Pman.Login.username.'+appNameShort, ''),
613 'lang' : Roo.state.Manager.get('Pman.Login.lang.'+appNameShort, 'en')
615 Pman.Login.switchLang(Roo.state.Manager.get('Pman.Login.lang.'+appNameShort, ''));
616 if (this.form.findField('username').getValue().length > 0 ){
617 this.form.findField('password').focus();
619 this.form.findField('username').focus();
629 window.onbeforeunload = function() { }; // false does not work for IE..
630 Pman.Login.authUserId = -1;
632 url: baseURL + '/Login.html',
637 failure : function() {
638 Roo.MessageBox.alert("Error", "Error logging out. - continuing anyway.", function() {
639 document.location = baseURL + '?ts=' + Math.random();
643 success : function() {
644 Pman.Login.authUserId = -1;
645 Pman.Login.checkFails =0;
647 document.location = baseURL + '?ts=' + Math.random();
653 switchLang : function (lang) {
657 if (typeof(_T.en) == 'undefined') {
659 Roo.apply(_T.en, _T);
662 if (typeof(_T[lang]) == 'undefined') {
663 Roo.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
668 Roo.apply(_T, _T[lang]);
669 // just need to set the text values for everything...
673 function formLabel(name, val) {
675 var lbl = Pman.Login.form.findField( name ).el.dom.parentNode.parentNode;
676 if (lbl.getElementsByTagName('label').length) {
677 lbl = lbl.getElementsByTagName('label')[0];
679 lbl = lbl.parentNode.getElementsByTagName('label')[0];
685 formLabel('password', "Password"+':');
686 formLabel('username', "Email Address"+':');
687 formLabel('lang', "Language"+':');
688 this.dialog.setTitle("Login");
689 this.dialog.buttons[0].setText("Forgot Password");
690 this.dialog.buttons[1].setText("Login");
696 inGroup : function(g)
698 return this.authUser && this.authUser.groups &&
699 this.authUser.groups.indexOf(g) > -1;
703 return this.authUser && this.authUser.company_id_comptype &&
704 this.authUser.company_id_comptype == 'OWNER';
708 * Depreciated = use Pman.I18n
711 i18nList: function (type, codes)
714 return Pman.I18n.listToNames(type, codes);
716 i18n: function(type, code)
718 return Pman.I18n.toName(type, code);