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
14 * Affects: (this id="????" elements)
15 * loading (removed) (used to indicate application is loading)
16 * loading-mask (hides) (used to hide application when it's building loading)
22 * Myapp.login = Roo.Login({
36 * Ext.apply(_T, _T[lang]);
40 Roo.Login = function(cfg)
47 Roo.each(this.items, function(e) {
50 Roo.Document.on('ready', function() {
54 Roo.Login.superclass.constructor.call(this);
58 Roo.extend(Roo.Login, Roo.LayoutDialog, {
61 * @cfg {String} method
62 * Method used to query for login details.
68 * URL to query login data. - eg. baseURL + '/Login.php'
74 * The user data - if user.id < 0 then login will be bypassed. (used for inital setup situation.
80 onLoad : function() // called on page load...
84 if (Roo.get('loading')) { // clear any loading indicator..
85 Roo.get('loading').remove();
88 this.switchLang('en'); // set the language to english..
91 success: function(response, opts) { // check successfull...
93 var res = Pman.processResponse(response);
95 if (!res.success) { // error!
97 //console.log('call failure');
98 return this.failure(response,opts);
101 if (!res.data.id) { // id=0 == login failure.
102 return this.show(true);
106 //console.log(success);
107 this.fillAuth(res.data);
109 Roo.XComponent.build();
117 check: function(cfg) // called every so often to refresh cookie etc..
119 if (cfg.again) { // could be undefined..
126 if ( this.checkFails > 4) {
127 Roo.MessageBox.alert("Error",
128 "Error getting authentication status. - try reloading, or wait a while", function() {
129 _this.sending = false;
134 _this.check.defer(10000, _this, [ cfg ]); // check in 10 secs.
145 success: cfg.success || this.success,
146 failure : cfg.failure || this.failure,
156 window.onbeforeunload = function() { }; // false does not work for IE..
165 failure : function() {
166 Roo.MessageBox.alert("Error", "Error logging out. - continuing anyway.", function() {
167 document.location = baseURL + '?ts=' + Math.random();
171 success : function() {
172 Pman.Login.authUserId = -1;
173 Pman.Login.checkFails =0;
175 document.location = baseURL + '?ts=' + Math.random();
184 success : function(response, opts) // check successfull...
186 this.sending = false;
187 var res = Pman.processResponse(response);
189 return this.failure(response, opts);
191 if (!res.data || !res.data.id) {
192 return this.failure(response,opts);
195 this.fillAuth(res.data);
202 failure : function (response, opts) // called if login 'check' fails.. (causes re-check)
205 this.sending = false;
206 var res = Pman.processResponse(response);
208 if ( Pman.Login.checkFails > 2) {
210 Roo.MessageBox.alert("Error", res.errorMsg ? res.errorMsg :
211 "Error getting authentication status. - try reloading");
214 opts.callCfg.again = true;
215 this.check.defer(1000, this, [ opts.callCfg ]);
221 fillAuth: function(au) {
222 this.startAuthCheck();
223 this.authUserId = au.id;
225 this.lastChecked = new Date();
226 this.fireEvent('refreshed', au);
227 //Pman.Tab.FaxQueue.newMaxId(au.faxMax);
228 //Pman.Tab.FaxTab.setTitle(au.faxNumPending);
230 //this.switchLang(Roo.state.Manager.get('Pman.Login.lang', 'en'));
231 Roo.state.Manager.set( this.realm + 'lang' , au.lang);
232 this.switchLang(au.lang);
235 // open system... - -on setyp..
236 if (this.authUserId < 0) {
237 Roo.MessageBox.alert("Warning",
238 "This is an open system - please set up a admin user with a password.");
241 //Pman.onload(); // which should do nothing if it's a re-auth result...
246 startAuthCheck : function() // starter for timeout checking..
248 if (Pman.Login.intervalID) { // timer already in place...
252 Pman.Login.intervalID = window.setInterval(function() {
253 Pman.Login.check(false);
254 }, 120000); // every 120 secs = 2mins..
271 center: { // needed??
274 // tabPosition: 'top',
277 alwaysShowTabs: false
281 rendered : function(dlg) {
282 this.form = this.items[0];
283 this.form.dialog = dlg;
284 this.buttons[0].form = this.form;
285 this.buttons[0].dialog = dlg
286 this.buttons[1].form = this.form;
287 this.buttons[1].dialog = dlg;
289 // logoprefix comes from base config.
290 //Pman.Login.form.el.createChild({
292 // src: rootURL + '/Pman/'+appNameShort + '/templates/images/logo.gif',
293 // style: 'margin-bottom: 10px;'
302 //this.resizeToLogo.defer(1000,this);
303 //var sz = Roo.get(Pman.Login.form.el.query('img')[0]).getSize();
305 // this.resizeToLogo.defer(1000,this);
308 var w = Ext.lib.Dom.getViewWidth() - 100;
309 var h = Ext.lib.Dom.getViewHeight() - 100;
310 this.resizeTo(Math.max(350, Math.min(sz.width + 30, w)),Math.min(sz.height+200, h));
317 if (this.user.id < 0) { // used for inital setup situations.
321 if (this.intervalID) {
323 window.clearInterval(this.intervalID);
324 this.intervalID = false;
328 if (Roo.get('loading')) {
329 Roo.get('loading').remove();
331 if (Roo.get('loading-mask')) {
332 Roo.get('loading-mask').hide();
335 //incomming._node = tnode;
337 //this.dialog.modal = !modal;
338 //this.dialog.show();
339 this.dialog.el.unmask();
342 this.form.setValues({
343 'username' : Roo.state.Manager.get(this.realm + '.username', ''),
344 'lang' : Roo.state.Manager.get(this.realm + '.lang', 'en')
347 Pman.Login.switchLang(Roo.state.Manager.get(this.realm + '.lang', ''));
348 if (this.form.findField('username').getValue().length > 0 ){
349 this.form.findField('password').focus();
351 this.form.findField('username').focus();
359 xtype : 'ContentPanel',
370 style : 'margin : 10px;',
372 actionfailed : function(f, act) {
373 // form can return { errors: .... }
375 //act.result.errors // invalid form element list...
376 //act.result.errorMsg// invalid form element list...
378 this.dialog.el.unmask();
379 Roo.MessageBox.alert("Error", act.result.errorMsg ? act.result.errorMsg :
380 "Login failed - communication error - try again.");
383 actioncomplete: function(re, act) {
385 Roo.state.Manager.set(
386 this.dialog.realm + '.' + this.dialog.params.username,
387 this.form.findField(this.dialog.params.username).getValue()
389 Roo.state.Manager.set(
390 this.dialog.realm + '.lang',
391 Pman.Login.form.findField('lang').getValue()
394 this.dialog.fillAuth(act.result.data);
398 if (Roo.get('loading-mask')) {
399 Roo.get('loading-mask').show();
401 Roo.XComponent.build();
411 fieldLabel: "Email Address",
414 autoCreate : {tag: "input", type: "text", size: "20"}
419 fieldLabel: "Password",
420 inputType: 'password',
423 autoCreate : {tag: "input", type: "text", size: "20"},
425 specialkey : function(e,ev) {
426 if (ev.keyCode == 13) {
427 this.form.dialog.el.mask("Logging in");
428 this.form.doAction('submit', {
429 url: this.form.dialog.url,
430 method: this.form.dialog.method,
439 fieldLabel: "Language",
442 xtype : 'SimpleStore',
443 fields: ['lang', 'ldisp'],
446 [ 'zh_HK' , '\u7E41\u4E2D' ],
447 [ 'zh_CN', '\u7C21\u4E2D' ]
454 displayField:'ldisp',
458 triggerAction: 'all',
459 emptyText:'Select a Language...',
462 select : function(cb, rec, ix) {
463 this.form.switchLang(rec.data.lang);
477 text : "Forgot Password",
481 var n = this.form.findField('username').getValue();
483 Roo.MessageBox.alert("Error", "Fill in your email address");
491 method: this.dialog.method,
492 success: function(response, opts) { // check successfull...
494 var res = Pman.processResponse(response);
495 if (!res.success) { // error!
496 Roo.MessageBox.alert("Error" ,
497 res.errorMsg ? res.errorMsg : "Problem Requesting Password Reset");
500 Roo.MessageBox.alert("Notice" ,
501 "Please check you email for the Password Reset message");
503 failure : function() {
504 Roo.MessageBox.alert("Error" , "Problem Requesting Password Reset");
517 this.dialog.el.mask("Logging in");
518 this.form.doAction('submit', {
520 method: this.dialog.method
529 Pman.Login = new Roo.util.Observable({
542 authUser: { id : false },
552 intervalID : false, /// the login refresher...
563 switchLang : function (lang) {
567 if (typeof(_T.en) == 'undefined') {
569 Roo.apply(_T.en, _T);
572 if (typeof(_T[lang]) == 'undefined') {
573 Roo.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
578 Roo.apply(_T, _T[lang]);
579 // just need to set the text values for everything...
583 function formLabel(name, val) {
585 var lbl = Pman.Login.form.findField( name ).el.dom.parentNode.parentNode;
586 if (lbl.getElementsByTagName('label').length) {
587 lbl = lbl.getElementsByTagName('label')[0];
589 lbl = lbl.parentNode.getElementsByTagName('label')[0];
595 formLabel('password', "Password"+':');
596 formLabel('username', "Email Address"+':');
597 formLabel('lang', "Language"+':');
598 this.dialog.setTitle("Login");
599 this.dialog.buttons[0].setText("Forgot Password");
600 this.dialog.buttons[1].setText("Login");
606 inGroup : function(g)
608 return this.authUser && this.authUser.groups &&
609 this.authUser.groups.indexOf(g) > -1;
613 return this.authUser && this.authUser.company_id_comptype &&
614 this.authUser.company_id_comptype == 'OWNER';
618 * Depreciated = use Pman.I18n
621 i18nList: function (type, codes)
624 return Pman.I18n.listToNames(type, codes);
626 i18n: function(type, code)
628 return Pman.I18n.toName(type, code);