1 /** 2 * @fileOverview 3 * Copyright (c) 2013 Regione Autonoma della Sardegna 4 * Published under the GPL license.<br> 5 * See <a href="https://sardegnageoportale.it/webgis/license.txt">https://sardegnageoportale.it/webgis/license.txt</a> 6 * for the full text of the license. 7 * @version 0.1 8 */ 9 10 /** 11 * @require plugins/Tool.js 12 * @require GeoExt/widgets/Action.js 13 */ 14 15 /** 16 * @namespace framework.plugins 17 */ 18 Ext.ns("framework.plugins"); 19 20 /** 21 * Show buttons and form to login and logout. 22 * @name_ Login 23 * @class Show buttons and form to login and logout. 24 * @constructor 25 * @extends <a target="_blank" href="http://gxp.opengeo.org/master/doc/lib/plugins/Tool.html">gxp.plugins.Tool</a> 26 */ 27 framework.plugins.Login = Ext.extend(gxp.plugins.Tool, 28 /** 29 * @lends framework.plugins.Login.prototype 30 */ 31 { 32 /** 33 * framework_login plugin type. 34 * @public 35 * @type String 36 */ 37 ptype: "framework_login", 38 39 /** 40 * the base Ajax Url for ajax request 41 * default "" 42 * @public 43 * @type String 44 */ 45 baseUrl: "", 46 47 /** 48 * name field for username 49 * default "username" 50 * @public 51 * @type String 52 */ 53 userFieldName:'username', 54 55 /** 56 * name field for username 57 * default "password" 58 * @public 59 * @type String 60 */ 61 pwdFieldName:'password', 62 63 /** 64 * name field for username 65 * default "Codice Fiscale" 66 * @public 67 * @type String 68 */ 69 codFiscFieldName:'codicefiscale', 70 71 serveltName: 'POIManagingServlet', 72 baseUrl: '', 73 userAuthDS: null, 74 75 /** 76 * Adds the action to be runned on click event 77 * @private 78 */ 79 addActions: function() { 80 this.logged = false; //initially user is not logged 81 var map = this.target.mapPanel.map; 82 var _self = this; 83 var action = new GeoExt.Action({ 84 iconCls: "gxp-icon-logoff", 85 tooltipText: "Login", 86 map: map, 87 handler: function() { 88 if (_self.logged) { 89 _self.logoff(_self); 90 } else { 91 _self.showLoginWindow(); 92 } 93 } 94 }); 95 return framework.plugins.Login.superclass.addActions.apply(this, [action]); 96 }, 97 98 /** 99 * logoff method. 100 * @public 101 */ 102 logoff: function(self) { 103 self.actions[0].items[0].setIconClass("gxp-icon-logoff"); 104 self.logged = false; 105 self.fireEvent('logged_out'); 106 }, 107 108 /** 109 * Shows a login form. 110 * @public 111 */ 112 showLoginWindow: function() { 113 var _self = this; 114 this.loginWindow = new Ext.FormPanel({ 115 labelWidth: 80, 116 url: this.baseUrl + 'Login', 117 frame: true, 118 title: 'Accesso utente', 119 defaultType: 'textfield', 120 monitorValid: true, 121 // Specific attributes for the text fields for username / password. 122 // The "name" attribute defines the name of variables sent to the server. 123 items: [/*{ 124 id: 'userName', 125 fieldLabel: 'Nome utente', 126 name: this.userFieldName, 127 allowBlank: false, 128 listeners: { 129 specialkey: function(field, event) { 130 if (event.getKey() === event.ENTER) { 131 _self.login(); 132 } 133 } 134 } 135 },*/ { 136 id: 'codFisc', 137 fieldLabel: 'Username', 138 name: this.codFiscFieldName, 139 inputType: 'codFisc', 140 allowBlank: true, 141 listeners: { 142 specialkey: function(field, event) { 143 if (event.getKey() === event.ENTER) { 144 // _self.login(); 145 _self.loginCFISC(); 146 } 147 } 148 } 149 }, { 150 id: 'loginPwd', 151 fieldLabel: 'Password', 152 name: this.pwdFieldName, 153 inputType: 'password', 154 allowBlank: false, 155 listeners: { 156 specialkey: function(field, event) { 157 if (event.getKey() === event.ENTER) { 158 // _self.login(); 159 _self.loginCFISC(); 160 } 161 } 162 } 163 }], 164 // All the magic happens after the user clicks the button 165 buttons: [ 166 { 167 text: 'Annulla', 168 handler: function() { 169 _self.win.hide(); 170 _self.win.destroy(); 171 } 172 }, 173 { 174 text: 'Login', 175 formBind: true, 176 // Function that fires when user clicks the button 177 handler: function() { 178 //_self.login(); 179 _self.loginCFISC(); 180 } 181 }] 182 }); 183 184 // This just creates a window to wrap the login form. 185 // The login object is passed to the items collection. 186 this.win = new Ext.Window({ 187 layout: 'fit', 188 width: 250, 189 height: 150, 190 closable: false, 191 resizable: false, 192 plain: true, 193 border: false, 194 items: [this.loginWindow] 195 }); 196 197 this.win.show(); 198 }, 199 200 /** 201 * Requests a digest based on client ip to server side servlet 202 * @public 203 * @param {function} onSuccess function called when digest value is received. 204 * @param {Strng} text password 205 */ 206 getDigest: function(text, onSuccess) { 207 Ext.Ajax.request({ 208 method: 'POST', 209 url: this.baseUrl + 'Digest', 210 params: {txt: text}, 211 success: function(response, opts) { 212 var obj = Ext.util.JSON.decode(response.responseText); 213 if (Ext.isDefined(obj.value)) { 214 onSuccess(obj.value); 215 } else { 216 Ext.MessageBox.alert('Errore', obj.error); 217 } 218 }, 219 failure: function(response, opts) { 220 Ext.MessageBox.alert('Errore', 'server-side failure with status code ' + response.status); 221 console.log('server-side failure with status code ' + response.status); 222 } 223 }); 224 }, 225 226 /** 227 * send login request to the server 228 * @public 229 */ 230 login: function() { 231 var pwd = Ext.getCmp('loginPwd'); 232 if (Ext.isDefined(pwd.getValue())) { 233 var _self = this; 234 _self.getDigest(pwd.getValue(), function(value) { 235 pwd.setValue(value); 236 _self.loginWindow.getForm().submit({ 237 method: 'POST', 238 waitTitle: 'Connessione al server', 239 waitMsg: 'Invio richiesta...', 240 // Functions that fire (success or failure) when the server responds. 241 // The one that executes is determined by the 242 // response that comes from Login servlet as seen below. The server would 243 // actually respond with valid JSON, 244 // something like: response.write "{ success: true}" or 245 // response.write "{ success: false, errors: { reason: 'Login failed. Try again.' }}" 246 // depending on the logic contained within your server script. 247 // If a success occurs, the user is notified with an alert messagebox, 248 // and when they click "OK", they are redirected to whatever page 249 // you define as redirect. 250 251 success: function() { 252 _self.actions[0].items[0].setIconClass("gxp-icon-login"); 253 _self.fireEvent('logged_in'); 254 _self.logged = true; 255 if (Ext.getCmp('codFisc').getValue()) { 256 _self.loginCFISC(Ext.getCmp('codFisc')); 257 } 258 _self.win.hide(); 259 _self.win.destroy(); 260 }, 261 // Failure function, see comment above re: success and failure. 262 // You can see here, if login fails, it throws a messagebox 263 // at the user telling him / her as much. 264 265 failure: function(form, action) { 266 if (action.failureType == 'server') { 267 obj = Ext.util.JSON.decode(action.response.responseText); 268 Ext.Msg.alert('Errore login', obj.errors.reason); 269 } else { 270 Ext.Msg.alert('Attenzione', 'Il server per l\'autenticazione<BR/> non è raggiungibile: ' + action.response.responseText); 271 } 272 _self.loginWindow.getForm().reset(); 273 } 274 }); 275 }); 276 } else { 277 Ext.Msg.alert('Attenzione', 'Non è stata inserita alcuna password'); 278 } 279 }, 280 281 /** 282 * send login request to the server 283 * @public 284 */ 285 loginCFISC: function() { 286 287 var codFisc = Ext.getCmp('codFisc'); 288 var pwd = Ext.getCmp('loginPwd'); 289 290 if (Ext.isDefined(codFisc.getValue()) && Ext.isDefined(pwd.getValue())) { 291 292 this.userAuthDS = new Ext.data.Store({ 293 url: this.baseUrl+this.serveltName, 294 reader: new Ext.data.JsonReader({ 295 id: 'myid' 296 }, [ 297 {name: 'myid', mapping: 'myid'}, 298 {name: 'firstname', mapping:'firstname'}, 299 {name: 'lastname', mapping: 'lastname'}, 300 {name: 'usertype', mapping:'usertype.description'}, 301 {name: 'codfiscale', mapping: 'codfiscale'}, 302 {name: 'provinciaInteresse', mapping: 'provinciaInteresse'}, 303 {name: 'comuneInteresse', mapping: 'comuneInteresse'}, 304 {name: 'comuneInteresse', mapping: 'comuneInteresse'}, 305 {name: 'validFlag', mapping: 'validFlag'} 306 ]), 307 listeners: { 308 load: function() { 309 if (this.userAuthDS.data.items[0].data.validFlag) { 310 this.actions[0].items[0].setIconClass("gxp-icon-login"); 311 this.fireEvent('logged_in_auth'); 312 313 this.logged = true; 314 315 this.win.hide(); 316 this.win.destroy(); 317 318 } 319 else 320 Ext.Msg.alert('Accesso negato','Utenza non abilitata'); 321 }, 322 loadexception: function( misc ){ 323 Ext.Msg.alert('Errore','Utenza non presente nel sistema o credenziali non corrette.'); 324 // do something about the record exception 325 }, 326 scope:this 327 } 328 }); 329 330 this.userAuthDS.load({ 331 params: { 332 method: 'getUser', 333 codfiscale: codFisc.getValue(), 334 password: pwd.getValue() 335 } 336 }); 337 338 } 339 } 340 341 }); 342 343 Ext.preg(framework.plugins.Login.prototype.ptype, framework.plugins.Login); 344