diff --git a/src/main/java/org/olat/user/PreferencesImpl.java b/src/main/java/org/olat/user/PreferencesImpl.java index 4e83576c3648a85729864f78ebc5b168e14307ad..9630df8958292d6c8fed1167cd7f03f7a74ce596 100644 --- a/src/main/java/org/olat/user/PreferencesImpl.java +++ b/src/main/java/org/olat/user/PreferencesImpl.java @@ -46,7 +46,7 @@ public class PreferencesImpl implements Preferences { private String language; private String fontsize; private String notificationInterval; - boolean informSessionTimeout; + private boolean informSessionTimeout; private String receiveRealMail; private boolean presenceMessagesPublic; diff --git a/src/main/java/org/olat/user/restapi/Examples.java b/src/main/java/org/olat/user/restapi/Examples.java index e4a61d3c7e1d9ee50750be74bb9ed1afa03cd1a5..b7fcb1863275809763439e8f87907c165016cb0d 100644 --- a/src/main/java/org/olat/user/restapi/Examples.java +++ b/src/main/java/org/olat/user/restapi/Examples.java @@ -35,6 +35,8 @@ public class Examples { public static final UserVOes SAMPLE_USERVOes = new UserVOes(); public static final RolesVO SAMPLE_ROLESVO = new RolesVO(); + + public static final PreferencesVO SAMPLE_PREFERENCESVO = new PreferencesVO(); static { SAMPLE_USERVO.setKey(345l); @@ -48,5 +50,7 @@ public class Examples { SAMPLE_USERVOes.setUsers(new UserVO[]{SAMPLE_USERVO}); SAMPLE_ROLESVO.setAuthor(true); + + SAMPLE_PREFERENCESVO.setLanguage("de"); } } diff --git a/src/main/java/org/olat/user/restapi/PreferencesVO.java b/src/main/java/org/olat/user/restapi/PreferencesVO.java new file mode 100644 index 0000000000000000000000000000000000000000..1f5bcbcb9e21cd7f0d775fe750b8f02a7640ea3e --- /dev/null +++ b/src/main/java/org/olat/user/restapi/PreferencesVO.java @@ -0,0 +1,60 @@ +/** + * <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 diff --git a/src/main/java/org/olat/user/restapi/UserPropertyVO.java b/src/main/java/org/olat/user/restapi/UserPropertyVO.java index 1d321ff2b9ab9214c3a1604276e2ee779e7531cd..bd058d06baff562554cf8dcd92eb463a5f7d8e85 100644 --- a/src/main/java/org/olat/user/restapi/UserPropertyVO.java +++ b/src/main/java/org/olat/user/restapi/UserPropertyVO.java @@ -70,5 +70,12 @@ public class UserPropertyVO { public void setValue(String 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 diff --git a/src/main/java/org/olat/user/restapi/UserWebService.java b/src/main/java/org/olat/user/restapi/UserWebService.java index 8028594a7263ffd17dfd12237831dab9ee188276..e8dd3b01df0e3a3519fc610d6999e0fa3a00d736 100644 --- a/src/main/java/org/olat/user/restapi/UserWebService.java +++ b/src/main/java/org/olat/user/restapi/UserWebService.java @@ -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.Translator; import org.olat.core.id.Identity; +import org.olat.core.id.Preferences; import org.olat.core.id.Roles; import org.olat.core.id.User; import org.olat.core.id.UserConstants; @@ -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 @Path("{identityKey}/roles") @Consumes({MediaType.APPLICATION_XML ,MediaType.APPLICATION_JSON}) @@ -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 diff --git a/src/test/java/org/olat/restapi/UserMgmtTest.java b/src/test/java/org/olat/restapi/UserMgmtTest.java index ba810dea23900d94875943c734c97187af23ce14..1999a47b86504de6a17a9eb82ed43946c8e64da8 100644 --- a/src/test/java/org/olat/restapi/UserMgmtTest.java +++ b/src/test/java/org/olat/restapi/UserMgmtTest.java @@ -107,6 +107,8 @@ import org.olat.restapi.support.vo.GroupVOes; import org.olat.test.JunitTestHelper; import org.olat.test.OlatJerseyTestCase; 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.UserVO; import org.springframework.beans.factory.annotation.Autowired; @@ -139,6 +141,8 @@ public class UserMgmtTest extends OlatJerseyTestCase { private BusinessGroupService businessGroupService; @Autowired private BaseSecurity securityManager; + @Autowired + private UserManager userManager; @Before @Override @@ -670,6 +674,63 @@ public class UserMgmtTest extends OlatJerseyTestCase { 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 public void testUserForums() throws IOException, URISyntaxException { RestConnection conn = new RestConnection();