/** * corpo-account-api.js * Javascript API for managing the User Accounts of a Corporate Space. */ var SparkomCorpoAccountApi = function() { var SERVER_ROOT = "https://www.spark-angels.com/panserver3"; function caa_setServerRoot(serverRoot) { SERVER_ROOT = serverRoot; } var CORPO_ID = 0; var API_KEY = ""; function caa_setCorpo(corpoId, apiKey) { CORPO_ID = corpoId; API_KEY = apiKey; } var API_URL = "/corpo_action"; /** * Creates a new Corpo Account. All parameters are mandatory. * The profileSpec is a list of Group Names, separated by spaces. * * The callback function is invoked asynchronously with a single argument "data", * that contains the following fields: * - "ret": Numeric Return code, 0=0K. * - "msg": "OK" or an error message. */ function caa_createAccount(emailLogin, password, displayName, profileSpec, callback) { caa_postJSON(SERVER_ROOT + API_URL, { json: true, corpoid: CORPO_ID, akey: API_KEY, cmd: "CREATE_USER", login: emailLogin, pass: password, dname: displayName, profile: profileSpec }, callback); } /** * Updates an existing Corpo Account. The "emailLogin" parameter is mandatory. * The password, displayName and/or profileSpec fields will be updated IF THE * GIVEN VALUE IS NOT null or "". * The profileSpec is a list of Group Names, separated by spaces. It fully * replaces the previously set one. * * The callback function is invoked asynchronously with a single argument "data", * that contains the following fields: * - "ret": Numeric Return code, 0=0K. * - "msg": "OK" or an error message. */ function caa_updateAccount(emailLogin, password, displayName, profileSpec, callback) { var params = { json: true, corpoid: CORPO_ID, akey: API_KEY, cmd: "UPDATE_USER", login: emailLogin }; if((password != null) && (password != "")) { params.pass = password; } if((displayName != null) && (displayName != "")) { params.dname = displayName; } if((profileSpec != null) && (profileSpec != "")) { params.profile = profileSpec; } caa_postJSON(SERVER_ROOT + API_URL, params, callback); } /** * Deletes an existing Corpo Account. The "emailLogin" parameter is mandatory. * * The callback function is invoked asynchronously with a single argument "data", * that contains the following fields: * - "ret": Numeric Return code, 0=0K. * - "msg": "OK" or an error message. */ function caa_deleteAccount(emailLogin, callback) { var params = { json: true, corpoid: CORPO_ID, akey: API_KEY, cmd: "DELETE_USER", login: emailLogin }; caa_postJSON(SERVER_ROOT + API_URL, params, callback); } /** * Audit an existing Corpo Account. The "emailLogin" parameter is mandatory. * The password, displayName and/or profileSpec fields will be verified IF THE * GIVEN VALUE IS NOT null or "". * If the given values match the ones stored on the Server, OK will be returned. * * The callback function is invoked asynchronously with a single argument "data", * that contains the following fields: * - "ret": Numeric Return code, 0=0K. * - "msg": "OK" or an error message. */ function caa_auditAccount(emailLogin, password, displayName, profileSpec, callback) { var params = { json: true, corpoid: CORPO_ID, akey: API_KEY, cmd: "AUDIT_USER", login: emailLogin }; if((password != null) && (password != "")) { params.pass = password; } if((displayName != null) && (displayName != "")) { params.dname = displayName; } if((profileSpec != null) && (profileSpec != "")) { params.profile = profileSpec; } caa_postJSON(SERVER_ROOT + API_URL, params, callback); } /** * Retrieves all Defined "Corpo Entities Groups". * * The callback function is invoked asynchronously with a single argument "data", * that contains the following fields: * - "ret": Numeric Return code, 0=0K. * - "msg": "OK" or an error message. * - "groups": An Array of Entities Group Names. */ function caa_retrieveAllGroups(callback) { var params = { json: true, corpoid: CORPO_ID, akey: API_KEY, cmd: "RETRIEVE_ALL_GROUPS" }; caa_postJSON(SERVER_ROOT + API_URL, params, callback); } /** * Lists the Helper Accounts associated with a "Corpo Entities Group". * * Takes the following parameters: * "group": Display Name of the Corpo Entities Group. * "firstResult": For pagination, use 0 if no pagination. Ordering by emailLogin. * "maxResults": For pagination, use 0 if no pagination. * "callback": Function that will receive the result. * * The callback function is invoked asynchronously with a single argument "data", * that contains the following fields: * - "ret": Numeric Return code, 0=0K. * - "msg": "OK" or an error message. * - "numaccounts": Total Numeric count of Accounts associated with this Group. * - "accounts": Array of returned accounts, each is an Object with the fields: * - "eml": The EmailLogin of the Account * - "dname": The DisplayName of the Account */ function caa_listGroupMembers(group, firstResult, maxResults, callback) { var params = { json: true, corpoid: CORPO_ID, akey: API_KEY, cmd: "LIST_GROUP_MEMBERS", group: group }; if((firstResult != null) && (firstResult != "")) { params.first = firstResult; } else { params.first = 0; } if((maxResults != null) && (maxResults != "")) { params.max = maxResults; } else { params.max = 0; } caa_postJSON(SERVER_ROOT + API_URL, params, callback); } // JSON POST action with callback and error indication function caa_postJSON(jsonUrl, params, callback) { $.post(jsonUrl, params, function(data) { if(callback) { callback(data); } }, "json"); } return { setServerRoot: caa_setServerRoot, setCorpo: caa_setCorpo, createAccount: caa_createAccount, updateAccount: caa_updateAccount, deleteAccount: caa_deleteAccount, auditAccount: caa_auditAccount, retrieveAllGroups: caa_retrieveAllGroups, listGroupMembers: caa_listGroupMembers } }();