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 from IDM.
 22  *  @name_ LoginIDM
 23  *  @class Show buttons and form to login and logout from IDM.
 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.LoginIDM = Ext.extend(gxp.plugins.Tool, 
 28  /** 
 29 * @lends framework.plugins.LoginIDM.prototype 
 30 */
 31 {
 32     /**
 33     * framework_LoginIDM plugin type.
 34     * @public
 35     * @type String
 36     */
 37     ptype: "framework_LoginIDM",
 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     /** 
 72     * the name of the services backend
 73     * @public
 74     * @type String
 75     */
 76     serveltName: 'POIManagingServlet',
 77     
 78      /** 
 79     * the authenticate user
 80     * @public
 81     * @type Ext.data.Store
 82     */
 83     userAuthDS: null,
 84     
 85     /** 
 86     * login url
 87     * @public
 88     * @type String
 89     */
 90     loginUrl: 'login.jsp',
 91     
 92     /** 
 93     * logout url
 94     * @public
 95     * @type String
 96     */
 97     logoutUrl: 'logout.jsp',
 98     
 99    
100     /**
101      * Adds the action to be runned on click event
102      * @private
103      */
104     addActions: function() {
105         this.logged = false; //initially user is not logged
106         var map = this.target.mapPanel.map;
107         var _self = this;
108         var action = new GeoExt.Action({
109             iconCls: "gxp-icon-logoff",
110             tooltip: "Login",
111             map: map,
112             handler: function() {
113                 if (_self.logged) {
114                     _self.logoff(_self);
115                 } else {                    
116                     _self.login(_self);
117                 }
118             }
119         });
120         var obj = framework.plugins.LoginIDM.superclass.addActions.apply(this, [action]);
121         this.loginCFISC();
122         return obj;
123     },
124     
125     /** 
126      * logoff method. 
127      * @public
128      */    
129     logoff: function(self) {
130         self.actions[0].items[0].setIconClass("gxp-icon-logoff");
131         self.actions[0].items[0].setTooltip("Login");
132         self.logged = false;
133         self.fireEvent('logged_out');
134         document.location = self.logoutUrl;
135     },
136  
137     /**
138      * send login request to the server
139      * @public
140      */ 
141     login: function(self) {
142         var idmInfo = new Ext.Panel({
143             title: self.mapInfoText,
144             html:self.htmlText?self.htmlText: 
145                     '<div class="gx-info-panel">' +
146                     '<div class="logoidm"></div>' +
147                     '<h1>Area riservata</h1>' +
148                     '<h2>Accedi ai servizi</h2>'+
149                     '<p>'+
150                     'Per accedere ai servizi dedicati devi essere registrato all\'<b>Identity Management RAS</b> '+
151                     'ed essere stato abilitato all\'utilizzo delle funzionalità riservate.<br />'+
152                     '<br /><small><b>nota:</b> Cliccando sul pulsante "Accedi" si aprirà la pagina del sistema di autenticazione unico, una volta effettuato l\'accesso '+
153                     'verrai reindirizzato dinuovo all\'applicativo.</small>'+
154                     '</p></div>',
155             height: 'auto',
156             width: 'auto',
157             buttons: [{
158                 text: 'Annulla',
159                     handler: function() {
160                       this.accWin.hide();
161                     },
162                     scope:self
163                 }, {
164                     text: 'Accedi',
165                     handler: function() {
166                       window.location = this.loginUrl;
167                     },
168                     scope:self
169             }]
170         });
171 
172         this.accWin = new Ext.Window({
173             title: "Accesso riservato",                            
174             constrainHeader: true,
175             modal: true,
176             layout: "fit",
177             width: 300,
178             height: 350,
179             items: [idmInfo]
180         });
181         this.accWin.show();
182                         
183         
184     },
185     
186     /**
187      * send login request to the server
188      * @public
189      */
190     loginCFISC: function() {
191         var self_ = this;
192         Ext.Ajax.request({
193             url: this.baseUrl + this.serveltName + '?method=getUser',
194             success: function(response) {
195                 this.userAuthDS = Ext.decode(response.responseText);
196                 for(var user in this.userAuthDS){
197                     if (this.userAuthDS[user]) {
198                         this.actions[0].items[0].setIconClass("gxp-icon-login");
199                         this.actions[0].items[0].setTooltip("Logoff");
200                         this.fireEvent('logged_in_auth');
201                         this.logged = true;
202                         return;
203                     }
204                 }
205                
206             },
207             failure: function(response) {
208                //console.log('server-side failure with status code ' + response.status);
209             },scope:this
210          });
211             
212         
213     }
214 
215 });
216 
217 Ext.preg(framework.plugins.LoginIDM.prototype.ptype, framework.plugins.LoginIDM);
218