// file = UserManager /* * UserManager.js * * Contributors: * Flavien Gateuil (flavien@freephonie.org) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /** * @fileoverview This file describes the different functions to manage a user * * @author Flavien Gateuil * @version 0.0 * @since MapFreebox V0.0 */ var xhrUM = null; /** * The user's management object */ function UserManager() {} UserManager.Version = "0.0"; UserManager.Team = "Map Freebox"; /** * The generic method which performs an AJAX call to the server * * @param {String} url The url to call * @param {JSONObject} params The parameters to send with the request * @param {function} callback The callback function which treat the server's response * @param {function} UMCallback (Optional) The default callback function * @see {@link http://www.json.org/js.html} JSONObject * @private */ UserManager.performAction = function(url, params, callback, UMCallback) { var first = true; var data = ""; for (key in params) { if (first) { data += key + "=" + params[key]; first = false; } else { data += "&" + key + "=" + params[key]; } } if (xhrUM) { xhrUM.abort(); xhrUM = null; } xhrUM = GXmlHttp.create(); xhrUM.open('POST', url, true); xhrUM.onreadystatechange = function() { var responseXML = null; if (xhrUM.readyState == 4) { /* 4 : complete state */ if (xhrUM.status == 200) { /* 200 : HTTP code ok */ /* Load data */ var JSONObject = eval("(" + xhrUM.responseText + ")"); //eval("var JSONObject = " + xhrUM.responseText); if (!JSONObject) { JSONObject = { Status : { code : 500, request : "manageuser" }, Infos : { reasons : "The server returned a ill-formed JSON object" } }; } if (UMCallback) { UMCallback(callback, JSONObject); } else { callback(JSONObject); } //responseXML = GXml.parse(xhrUM.responseText); } //XML2JSON(responseXML, callback); } } xhrUM.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); xhrUM.send(data); }; /** * Connect a user with the specified login and password. * The response from the server is returned as parameter to the callback function. * * @param {String} login The user's login * @param {String} password The user's password * @param {function} callback The function which will handle the JSON object returned after connection * @see {@link http://www.json.org/js.html} JSONObject */ UserManager.connect = function(login, password, callback) { var JSONParams = { type : CONNECT, login : login, pass : password}; this.performAction(USER_MANAGEMENT, JSONParams, callback, UserManager.defaultCallbackConnect); }; /** * Disconnect the current user. * The response from the server is returned as parameter to the callback function. * * @param {function} callback The function which will handle the JSON object returned after disconnection * @see {@link http://www.json.org/js.html} JSONObject */ UserManager.disconnect = function(callback) { var JSONParams = { type : DISCONNECT }; this.performAction(USER_MANAGEMENT, JSONParams, callback, UserManager.defaultCallbackDisconnect); }; /** * Ask the server if the user is still connected * The response from the server is returned as parameter to the callback function. * * @param {function} callback The function which will handle the JSON object returned by the server * @see {@link http://www.json.org/js.html} JSONObject * @private */ UserManager.checkConnection = function(callback) { var JSONParams = { type : CHECK_CONNECTION }; this.performAction(USER_MANAGEMENT, JSONParams, callback); }; /** * Register a new user with the specified parameters * * @param {String} login The user's login * @param {String} password The user's password * @param {String} mail The user's mail * @param {String} URLSite The user's website * @param {int} department The user's department * @param {function} callback The function which will handle the JSON object returned after registration */ UserManager.register = function(login, password, mail, URLSite, department, ffw, callback) { var JSONParams = { type : REGISTER, login : login, pass : password, mail : mail, site : URLSite, dep : department, ffw : ffw }; this.performAction(USER_MANAGEMENT, JSONParams, callback, UserManager.defaultCallbackRegister); }; /** * Updates the user's informations * * @param {String} login The user's login * @param {String} oldpass The former user's password * @param {String} newpass The new user's password * @param {String} mail The user's mail * @param {String} URLSite The user's website * @param {int} department The user's department * @param {function} callback The function which will handle the JSON object returned after update */ UserManager.update = function(pass, newpass, mail, URLSite, department, ffw, callback) { var JSONParams = ""; if (newpass != "") { JSONParams = { type : UPDATE, pass : pass, new_pass : newpass, mail : mail, site : URLSite, dep : department, ffw : ffw }; } else { JSONParams = { type : UPDATE, pass : pass, mail : mail, site : URLSite, dep : department, ffw : ffw }; } this.performAction(USER_MANAGEMENT, JSONParams, callback, UserManager.defaultCallbackRegister); }; /** * The default connection callback function * * @param {function} callback The callback function which will treat the data from the server after connection * @param {JSONObject} JSONObject The server's connection response * @see {@link http://www.json.org/js.html} JSONObject * @private */ UserManager.defaultCallbackConnect = function(callback, JSONObject) { if ((JSONObject.Status.code == 200) && (JSONObject.UserInfos.connected)) { UserManager.currentUser = new MapUser(JSONObject.UserInfos.login, JSONObject.UserInfos.email, JSONObject.UserInfos.website, JSONObject.UserInfos.department, JSONObject.UserInfos.id); } if (callback) { callback(JSONObject); } }; /** * The default disconnection callback function * * @param {function} callback The callback function which will treat the data from the server after disconnection * @param {JSONObject} JSONObject The server's disconnection response * @see {@link http://www.json.org/js.html} JSONObject * @private */ UserManager.defaultCallbackDisconnect = function(callback, JSONObject) { UserManager.currentUser = null; if (callback) { callback(JSONObject); } }; /** * The default registration callback function * * @param {function} callback The callback function which will treat the data from the server after registration * @param {JSONObject} JSONObject The server's registration response * @see {@link http://www.json.org/js.html} JSONObject * @private */ UserManager.defaultCallbackRegister = function(callback, JSONObject) { if (callback) { callback(JSONObject); } }; /** * The default update callback function * * @param {function} callback The callback function which will treat the data from the server after update * @param {JSONObject} JSONObject The server's update response * @see {@link http://www.json.org/js.html} JSONObject * @private */ UserManager.defaultCallbackUpdate = function(callback, JSONObject) { if (callback) { callback(JSONObject); } }; /** * The current user * @private */ UserManager.currentUser = null;