Preference Service

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@9538 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Roy Wetherall
2008-06-23 09:48:22 +00:00
parent b9fe333e4d
commit 1f3e2a47c1
11 changed files with 274 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
<webscript>
<shortname>Preferences</shortname>
<description>Delete a users preferences</description>
<url>/api/people/{userid}/preferences?pf={preferencefilter?}</url>
<format default="json"/>
<authentication>user</authentication>
<transaction>required</transaction>
</webscript>

View File

@@ -0,0 +1,14 @@
function main()
{
// Try and get the person
var userid = url.templateArgs.userid;
var person = people.getPerson(userid);
if (person == null)
{
// 404 since person resource could not be found
status.setCode(status.STATUS_NOT_FOUND, "The user " + userid + " could not be found");
return;
}
}
main();

View File

@@ -0,0 +1,8 @@
<webscript>
<shortname>Preferences</shortname>
<description>Get the preferences for a user</description>
<url>/api/people/{userid}/preferences?pf={preferencefilter?}</url>
<format default="json"/>
<authentication>user</authentication>
<transaction>required</transaction>
</webscript>

View File

@@ -0,0 +1,20 @@
function main()
{
// Try and get the person
var userid = url.templateArgs.userid;
var person = people.getPerson(userid);
if (person == null)
{
// 404 since person resource could not be found
status.setCode(status.STATUS_NOT_FOUND, "The user " + userid + " could not be found");
return;
}
// Get the preferences for the person
var preferences = preferenceService.getPreferences(userid, args["pf"]);
// Convert the preferences to JSON and place in the model
model.preferences = jsonUtils.toJSONString(preferences);
}
main();

View File

@@ -0,0 +1,8 @@
<webscript>
<shortname>Preferences</shortname>
<description>Set a users preferences</description>
<url>/api/people/{userid}/preferences</url>
<format default="json"/>
<authentication>user</authentication>
<transaction>required</transaction>
</webscript>

View File

@@ -0,0 +1,20 @@
function main()
{
// Try and get the person
var userid = url.templateArgs.userid;
var person = people.getPerson(userid);
if (person == null)
{
// 404 since person resource could not be found
status.setCode(status.STATUS_NOT_FOUND, "The user " + userid + " could not be found");
return;
}
// Convert the passed json into a native JS object
var preferences = jsonUtils.toObject(json);
// Set the preferences
preferenceService.setPreferences(userid, preferences);
}
main();

View File

@@ -272,4 +272,9 @@
</property>
</bean>
<!-- Switch on debugger by setting log4j.logger.org.alfresco.repo.web.scripts.AlfrescoRhinoScriptDebugger to DEBUG -->
<bean id="rhinoScriptDebugger" class="org.alfresco.repo.web.scripts.AlfrescoRhinoScriptDebugger" init-method="start"/>
</beans>

View File

@@ -0,0 +1,190 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* 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.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.web.scripts.preference;
import java.util.ArrayList;
import java.util.List;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.repo.site.SiteModel;
import org.alfresco.repo.web.scripts.BaseWebScriptTest;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.util.GUID;
import org.alfresco.util.PropertyMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.mock.web.MockHttpServletResponse;
/**
* Unit test to test preference Web Script API
*
* @author Roy Wetherall
*/
public class PreferenceServiceTest extends BaseWebScriptTest
{
private AuthenticationService authenticationService;
private AuthenticationComponent authenticationComponent;
private PersonService personService;
private static final String USER_ONE = "PreferenceTestOne" + System.currentTimeMillis();
private static final String USER_BAD = "PreferenceTestBad" + System.currentTimeMillis();
private static final String URL = "/api/people/" + USER_ONE + "/preferences";;
@Override
protected void setUp() throws Exception
{
super.setUp();
this.authenticationService = (AuthenticationService)getServer().getApplicationContext().getBean("AuthenticationService");
this.authenticationComponent = (AuthenticationComponent)getServer().getApplicationContext().getBean("authenticationComponent");
this.personService = (PersonService)getServer().getApplicationContext().getBean("PersonService");
// Create users
createUser(USER_ONE);
createUser(USER_BAD);
// Do tests as user one
this.authenticationComponent.setCurrentUser(USER_ONE);
}
private void createUser(String userName)
{
if (this.authenticationService.authenticationExists(userName) == false)
{
this.authenticationService.createAuthentication(userName, "PWD".toCharArray());
PropertyMap ppOne = new PropertyMap(4);
ppOne.put(ContentModel.PROP_USERNAME, userName);
ppOne.put(ContentModel.PROP_FIRSTNAME, "firstName");
ppOne.put(ContentModel.PROP_LASTNAME, "lastName");
ppOne.put(ContentModel.PROP_EMAIL, "email@email.com");
ppOne.put(ContentModel.PROP_JOBTITLE, "jobTitle");
this.personService.createPerson(ppOne);
}
}
@Override
protected void tearDown() throws Exception
{
super.tearDown();
this.authenticationComponent.setCurrentUser("admin");
}
public void testPreferences() throws Exception
{
// Get the preferences before they have been set
MockHttpServletResponse resp = getRequest(URL, 200);
JSONObject jsonResult = new JSONObject(resp.getContentAsString());
assertNotNull(jsonResult);
assertFalse(jsonResult.keys().hasNext());
// Set some preferences
JSONObject jsonObject = getPreferenceObj();
jsonObject.put("comp1", getPreferenceObj());
resp = postRequest(URL, 200, jsonObject.toString(), "application/json");
assertEquals(0, resp.getContentLength());
// Get the preferences
resp = getRequest(URL, 200);
jsonResult = new JSONObject(resp.getContentAsString());
assertNotNull(jsonResult);
assertTrue(jsonResult.keys().hasNext());
checkJSONObject(jsonResult);
checkJSONObject(jsonResult.getJSONObject("comp1"));
// Update some of the preferences
jsonObject.put("stringValue", "updated");
jsonObject.put("comp2", getPreferenceObj());
resp = postRequest(URL, 200, jsonObject.toString(), "application/json");
assertEquals(0, resp.getContentLength());
// Get the preferences
resp = getRequest(URL, 200);
jsonResult = new JSONObject(resp.getContentAsString());
assertNotNull(jsonResult);
assertTrue(jsonResult.keys().hasNext());
jsonObject.put("stringValue", "updated");
jsonObject.put("numberValue", 10);
jsonObject.put("numberValue2", 3.142);
checkJSONObject(jsonResult.getJSONObject("comp1"));
checkJSONObject(jsonResult.getJSONObject("comp2"));
// Filter the preferences retrieved
resp = getRequest(URL + "?pf=comp2", 200);
jsonResult = new JSONObject(resp.getContentAsString());
assertNotNull(jsonResult);
assertTrue(jsonResult.keys().hasNext());
checkJSONObject(jsonResult.getJSONObject("comp2"));
assertFalse(jsonResult.has("comp1"));
assertFalse(jsonResult.has("stringValue"));
// Clear some of the preferences
// Clear all the preferences
// Test trying to update another user's permissions
postRequest("/api/people/" + USER_BAD + "/preferences", 500, jsonObject.toString(), "application/json");
// Test error conditions
getRequest("/api/people/noExistUser/preferences", 404);
}
private JSONObject getPreferenceObj()
throws JSONException
{
JSONObject jsonObject = new JSONObject();
jsonObject.put("stringValue", "value");
jsonObject.put("numberValue", 10);
jsonObject.put("numberValue2", 3.142);
return jsonObject;
}
private void checkJSONObject(JSONObject jsonObject)
throws JSONException
{
assertEquals("value", jsonObject.get("stringValue"));
assertEquals(10, jsonObject.get("numberValue"));
assertEquals(3.142, jsonObject.get("numberValue2"));
}
}