Skip to content
Snippets Groups Projects
Commit da12a07b authored by srosse's avatar srosse
Browse files

CL-232: enhance REST API to set the preferred language of a user

parent 33730b16
No related branches found
No related tags found
No related merge requests found
...@@ -46,7 +46,7 @@ public class PreferencesImpl implements Preferences { ...@@ -46,7 +46,7 @@ public class PreferencesImpl implements Preferences {
private String language; private String language;
private String fontsize; private String fontsize;
private String notificationInterval; private String notificationInterval;
boolean informSessionTimeout; private boolean informSessionTimeout;
private String receiveRealMail; private String receiveRealMail;
private boolean presenceMessagesPublic; private boolean presenceMessagesPublic;
......
...@@ -35,6 +35,8 @@ public class Examples { ...@@ -35,6 +35,8 @@ public class Examples {
public static final UserVOes SAMPLE_USERVOes = new UserVOes(); public static final UserVOes SAMPLE_USERVOes = new UserVOes();
public static final RolesVO SAMPLE_ROLESVO = new RolesVO(); public static final RolesVO SAMPLE_ROLESVO = new RolesVO();
public static final PreferencesVO SAMPLE_PREFERENCESVO = new PreferencesVO();
static { static {
SAMPLE_USERVO.setKey(345l); SAMPLE_USERVO.setKey(345l);
...@@ -48,5 +50,7 @@ public class Examples { ...@@ -48,5 +50,7 @@ public class Examples {
SAMPLE_USERVOes.setUsers(new UserVO[]{SAMPLE_USERVO}); SAMPLE_USERVOes.setUsers(new UserVO[]{SAMPLE_USERVO});
SAMPLE_ROLESVO.setAuthor(true); SAMPLE_ROLESVO.setAuthor(true);
SAMPLE_PREFERENCESVO.setLanguage("de");
} }
} }
/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.user.restapi;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import org.olat.core.id.Preferences;
/**
*
* Initial date: 07.08.2013<br>
* @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "preferencesVO")
public class PreferencesVO {
private String language;
public PreferencesVO() {
//make JAXB happy
}
public PreferencesVO(Preferences preferences) {
language = preferences.getLanguage();
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
@Override
public String toString() {
return "UserPreferencesVO[language=" + language + "]";
}
}
\ No newline at end of file
...@@ -70,5 +70,12 @@ public class UserPropertyVO { ...@@ -70,5 +70,12 @@ public class UserPropertyVO {
public void setValue(String value) { public void setValue(String value) {
this.value = value; this.value = value;
} }
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("userPropertyVo[").append(name).append(":").append(value).append("]");
return sb.toString();
}
} }
\ No newline at end of file
...@@ -68,6 +68,7 @@ import org.olat.core.gui.components.form.ValidationError; ...@@ -68,6 +68,7 @@ import org.olat.core.gui.components.form.ValidationError;
import org.olat.core.gui.translator.PackageTranslator; import org.olat.core.gui.translator.PackageTranslator;
import org.olat.core.gui.translator.Translator; import org.olat.core.gui.translator.Translator;
import org.olat.core.id.Identity; import org.olat.core.id.Identity;
import org.olat.core.id.Preferences;
import org.olat.core.id.Roles; import org.olat.core.id.Roles;
import org.olat.core.id.User; import org.olat.core.id.User;
import org.olat.core.id.UserConstants; import org.olat.core.id.UserConstants;
...@@ -276,7 +277,18 @@ public class UserWebService { ...@@ -276,7 +277,18 @@ public class UserWebService {
} }
} }
/**
* Update the roles of a user given its unique key identifier
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc The user
* @response.representation.200.example {@link org.olat.user.restapi.Examples#SAMPLE_ROLESVO}
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @response.representation.404.doc The identity not found
* @param identityKey The user key identifier of the user being searched
* @param roles The updated roles
* @param httpRequest The HTTP request
* @return an xml or json representation of a the roles being search.
*/
@POST @POST
@Path("{identityKey}/roles") @Path("{identityKey}/roles")
@Consumes({MediaType.APPLICATION_XML ,MediaType.APPLICATION_JSON}) @Consumes({MediaType.APPLICATION_XML ,MediaType.APPLICATION_JSON})
...@@ -300,6 +312,71 @@ public class UserWebService { ...@@ -300,6 +312,71 @@ public class UserWebService {
} }
} }
/**
* Retrieves the preferences of a user given its unique key identifier
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc The preferences
* @response.representation.200.example {@link org.olat.user.restapi.Examples#SAMPLE_PREFERENCESVO}
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @response.representation.404.doc The identity not found
* @param identityKey The user key identifier of the user being searched
* @param httpRequest The HTTP request
* @return an xml or json representation of a the roles being search.
*/
@GET
@Path("{identityKey}/preferences")
@Produces({MediaType.APPLICATION_XML ,MediaType.APPLICATION_JSON})
public Response getUserPreferences(@PathParam("identityKey") Long identityKey, @Context HttpServletRequest request) {
boolean isUserManager = isUserManager(request);
if(!isUserManager) {
return Response.serverError().status(Status.FORBIDDEN).build();
}
Identity identity = BaseSecurityManager.getInstance().loadIdentityByKey(identityKey, false);
if(identity == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
Preferences prefs = identity.getUser().getPreferences();
return Response.ok(new PreferencesVO(prefs)).build();
}
/**
* Update the preferences of a user given its unique key identifier
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc The user
* @response.representation.200.example {@link org.olat.user.restapi.Examples#SAMPLE_PREFERENCESVO}
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @response.representation.404.doc The identity not found
* @param identityKey The user key identifier of the user being searched
* @param preferences The updated preferences
* @param httpRequest The HTTP request
* @return an xml or json representation of a the roles being search.
*/
@POST
@Path("{identityKey}/preferences")
@Consumes({MediaType.APPLICATION_XML ,MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML ,MediaType.APPLICATION_JSON})
public Response updatePreferences(@PathParam("identityKey") Long identityKey, PreferencesVO preferences, @Context HttpServletRequest request) {
try {
boolean isUserManager = isUserManager(request);
if(!isUserManager) {
return Response.serverError().status(Status.FORBIDDEN).build();
}
Identity identity = BaseSecurityManager.getInstance().loadIdentityByKey(identityKey, false);
if(identity == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
Preferences prefs = identity.getUser().getPreferences();
prefs.setLanguage(preferences.getLanguage());
UserManager.getInstance().updateUserFromIdentity(identity);
return Response.ok(new PreferencesVO(prefs)).build();
} catch (Throwable e) {
throw new WebApplicationException(e);
}
}
/** /**
* Retrieves an user given its unique key identifier * Retrieves an user given its unique key identifier
......
...@@ -107,6 +107,8 @@ import org.olat.restapi.support.vo.GroupVOes; ...@@ -107,6 +107,8 @@ import org.olat.restapi.support.vo.GroupVOes;
import org.olat.test.JunitTestHelper; import org.olat.test.JunitTestHelper;
import org.olat.test.OlatJerseyTestCase; import org.olat.test.OlatJerseyTestCase;
import org.olat.user.DisplayPortraitManager; import org.olat.user.DisplayPortraitManager;
import org.olat.user.UserManager;
import org.olat.user.restapi.PreferencesVO;
import org.olat.user.restapi.RolesVO; import org.olat.user.restapi.RolesVO;
import org.olat.user.restapi.UserVO; import org.olat.user.restapi.UserVO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -139,6 +141,8 @@ public class UserMgmtTest extends OlatJerseyTestCase { ...@@ -139,6 +141,8 @@ public class UserMgmtTest extends OlatJerseyTestCase {
private BusinessGroupService businessGroupService; private BusinessGroupService businessGroupService;
@Autowired @Autowired
private BaseSecurity securityManager; private BaseSecurity securityManager;
@Autowired
private UserManager userManager;
@Before @Before
@Override @Override
...@@ -670,6 +674,63 @@ public class UserMgmtTest extends OlatJerseyTestCase { ...@@ -670,6 +674,63 @@ public class UserMgmtTest extends OlatJerseyTestCase {
conn.shutdown(); conn.shutdown();
} }
@Test
public void testGetPreferences() throws IOException, URISyntaxException {
//create an author
Identity prefsId = JunitTestHelper.createAndPersistIdentityAsAuthor("prefs-1-" + UUID.randomUUID().toString());
dbInstance.commitAndCloseSession();
prefsId.getUser().getPreferences().setLanguage("fr");
prefsId.getUser().getPreferences().setFontsize("11");
userManager.updateUserFromIdentity(prefsId);
dbInstance.commitAndCloseSession();
RestConnection conn = new RestConnection();
assertTrue(conn.login("administrator", "openolat"));
//get preferences of author
URI request = UriBuilder.fromUri(getContextURI()).path("/users/" + prefsId.getKey() + "/preferences").build();
HttpGet method = conn.createGet(request, MediaType.APPLICATION_JSON, true);
HttpResponse response = conn.execute(method);
assertEquals(200, response.getStatusLine().getStatusCode());
PreferencesVO prefsVo = conn.parse(response, PreferencesVO.class);
Assert.assertNotNull(prefsVo);
Assert.assertEquals("fr", prefsVo.getLanguage());
conn.shutdown();
}
@Test
public void testUpdatePreferences() throws IOException, URISyntaxException {
//create an author
Identity prefsId = JunitTestHelper.createAndPersistIdentityAsAuthor("prefs-1-" + UUID.randomUUID().toString());
dbInstance.commitAndCloseSession();
prefsId.getUser().getPreferences().setLanguage("de");
userManager.updateUserFromIdentity(prefsId);
dbInstance.commitAndCloseSession();
RestConnection conn = new RestConnection();
assertTrue(conn.login("administrator", "openolat"));
PreferencesVO prefsVo = new PreferencesVO();
prefsVo.setLanguage("fr");
//get roles of author
URI request = UriBuilder.fromUri(getContextURI()).path("/users/" + prefsId.getKey() + "/preferences").build();
HttpPost method = conn.createPost(request, MediaType.APPLICATION_JSON, true);
conn.addJsonEntity(method, prefsVo);
HttpResponse response = conn.execute(method);
assertEquals(200, response.getStatusLine().getStatusCode());
PreferencesVO modPrefs = conn.parse(response, PreferencesVO.class);
Assert.assertNotNull(modPrefs);
Assert.assertEquals("fr", prefsVo.getLanguage());
//double check
Identity reloadedPrefsId = securityManager.loadIdentityByKey(prefsId.getKey());
Assert.assertNotNull(reloadedPrefsId);
Assert.assertEquals("fr", reloadedPrefsId.getUser().getPreferences().getLanguage());
conn.shutdown();
}
@Test @Test
public void testUserForums() throws IOException, URISyntaxException { public void testUserForums() throws IOException, URISyntaxException {
RestConnection conn = new RestConnection(); RestConnection conn = new RestConnection();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment