use of org.olat.basesecurity.BaseSecurity in project openolat by klemens.
the class UserWebService method update.
/**
* Update an user
* @response.representation.qname {http://www.example.com}userVO
* @response.representation.mediaType application/xml, application/json
* @response.representation.doc The user
* @response.representation.example {@link org.olat.user.restapi.Examples#SAMPLE_USERVO}
* @response.representation.200.qname {http://www.example.com}userVO
* @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_USERVO}
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @response.representation.404.doc The identity not found
* @response.representation.406.qname {http://www.example.com}errorVO
* @response.representation.406.mediaType application/xml, application/json
* @response.representation.406.doc The list of validation errors
* @response.representation.406.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_ERRORVOes}
* @param identityKey The user key identifier
* @param user The user datas
* @param request The HTTP request
* @return <code>User</code> object. The operation status (success or fail)
*/
@POST
@Path("{identityKey}")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response update(@PathParam("identityKey") Long identityKey, UserVO user, @Context HttpServletRequest request) {
try {
if (user == null) {
return Response.serverError().status(Status.NO_CONTENT).build();
}
if (!isUserManager(request)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
BaseSecurity baseSecurity = BaseSecurityManager.getInstance();
Identity retrievedIdentity = baseSecurity.loadIdentityByKey(identityKey, false);
if (retrievedIdentity == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
User retrievedUser = retrievedIdentity.getUser();
List<ErrorVO> errors = validateUser(retrievedUser, user, request);
if (errors.isEmpty()) {
if (StringHelper.containsNonWhitespace(user.getExternalId()) && !user.getExternalId().equals(retrievedIdentity.getExternalId())) {
retrievedIdentity = baseSecurity.setExternalId(retrievedIdentity, user.getExternalId());
retrievedUser = retrievedIdentity.getUser();
}
String oldEmail = retrievedUser.getEmail();
post(retrievedUser, user, getLocale(request));
UserManager.getInstance().updateUser(retrievedUser);
BaseSecurityManager.getInstance().deleteInvalidAuthenticationsByEmail(oldEmail);
return Response.ok(get(retrievedIdentity, true, true)).build();
}
// content not ok
ErrorVO[] errorVos = new ErrorVO[errors.size()];
errors.toArray(errorVos);
return Response.ok(errorVos).status(Status.NOT_ACCEPTABLE).build();
} catch (Exception e) {
log.error("Error updating an user", e);
return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
}
}
use of org.olat.basesecurity.BaseSecurity in project openolat by klemens.
the class UserAuthenticationWebService method changePassword.
/**
* Change the password of a user.
*
* @response.representation.200.doc The password successfully changed
* @response.representation.304.doc The password was not changed
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @response.representation.404.doc The identity or the authentication not found
* @param username The username of the user to change the password
* @param newPassword The new password
* @param request The HTTP request
* @return <code>Response</code> object. The operation status (success or fail)
*/
@POST
@Path("password")
public Response changePassword(@PathParam("username") String username, @FormParam("newPassword") String newPassword, @Context HttpServletRequest request) {
if (!isAdmin(request)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
Identity doer = getIdentity(request);
if (doer == null) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
BaseSecurity baseSecurity = BaseSecurityManager.getInstance();
Identity identity = baseSecurity.findIdentityByName(username);
if (identity == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
OLATAuthManager authManager = CoreSpringFactory.getImpl(OLATAuthManager.class);
boolean ok = authManager.changePassword(doer, identity, newPassword);
return (ok ? Response.ok() : Response.notModified()).build();
}
use of org.olat.basesecurity.BaseSecurity in project openolat by klemens.
the class QuestionPoolWebService method addAuthor.
/**
* Add an author to the question item.
*
* @response.representation.200.doc The user is an author of the question item
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @response.representation.404.doc The question item or the user not found
* @param itemKey The question item identifier
* @param identityKey The user identifier
* @param httpRequest The HTTP request
* @return It returns 200 if the user is added as author of the question item
*/
@PUT
@Path("{itemKey}/authors/{identityKey}")
public Response addAuthor(@PathParam("itemKey") Long itemKey, @PathParam("identityKey") Long identityKey, @Context HttpServletRequest httpRequest) {
if (!isQuestionPoolManager(httpRequest)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
QPoolService poolService = CoreSpringFactory.getImpl(QPoolService.class);
QuestionItem item = poolService.loadItemById(itemKey);
if (item == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
BaseSecurity securityManager = CoreSpringFactory.getImpl(BaseSecurity.class);
Identity author = securityManager.loadIdentityByKey(identityKey, false);
if (author == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
List<Identity> authors = Collections.singletonList(author);
List<QuestionItemShort> items = Collections.singletonList(item);
poolService.addAuthors(authors, items);
return Response.ok().build();
}
use of org.olat.basesecurity.BaseSecurity in project openolat by klemens.
the class QuestionPoolWebService method removeAuthor.
/**
* Remove an author to the question item.
*
* @response.representation.200.doc The user was successfully removed as author of the question item
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @response.representation.404.doc The question item or the user not found
* @param itemKey The question item identifier
* @param identityKey The user identifier
* @param httpRequest The HTTP request
* @return It returns 200 if the user is removed as author of the question item
*/
@DELETE
@Path("{itemKey}/authors/{identityKey}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response removeAuthor(@PathParam("itemKey") Long itemKey, @PathParam("identityKey") Long identityKey, @Context HttpServletRequest httpRequest) {
if (!isQuestionPoolManager(httpRequest)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
QPoolService poolService = CoreSpringFactory.getImpl(QPoolService.class);
QuestionItem item = poolService.loadItemById(itemKey);
if (item == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
BaseSecurity securityManager = CoreSpringFactory.getImpl(BaseSecurity.class);
Identity author = securityManager.loadIdentityByKey(identityKey, false);
if (author == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
List<Identity> authors = Collections.singletonList(author);
List<QuestionItemShort> items = Collections.singletonList(item);
poolService.removeAuthors(authors, items);
return Response.ok().build();
}
use of org.olat.basesecurity.BaseSecurity in project openolat by klemens.
the class CatalogTest method setUp.
@Before
@Override
public void setUp() throws Exception {
super.setUp();
id1 = JunitTestHelper.createAndPersistIdentityAsUser("rest-catalog-one");
JunitTestHelper.createAndPersistIdentityAsUser("rest-catalog-two");
BaseSecurity securityManager = BaseSecurityManager.getInstance();
admin = securityManager.findIdentityByName("administrator");
// create a catalog
root1 = catalogManager.getRootCatalogEntries().get(0);
entry1 = catalogManager.createCatalogEntry();
entry1.setType(CatalogEntry.TYPE_NODE);
entry1.setName("Entry-1");
entry1.setDescription("Entry-description-1");
entry1.setOwnerGroup(securityManager.createAndPersistSecurityGroup());
catalogManager.addCatalogEntry(root1, entry1);
DBFactory.getInstance().intermediateCommit();
entry1 = catalogManager.loadCatalogEntry(entry1);
securityManager.addIdentityToSecurityGroup(admin, entry1.getOwnerGroup());
subEntry11 = catalogManager.createCatalogEntry();
subEntry11.setType(CatalogEntry.TYPE_NODE);
subEntry11.setName("Sub-entry-11");
subEntry11.setDescription("Sub-entry-description-11");
catalogManager.addCatalogEntry(entry1, subEntry11);
subEntry12 = catalogManager.createCatalogEntry();
subEntry12.setType(CatalogEntry.TYPE_NODE);
subEntry12.setName("Sub-entry-12");
subEntry12.setDescription("Sub-entry-description-12");
catalogManager.addCatalogEntry(entry1, subEntry12);
entry2 = catalogManager.createCatalogEntry();
entry2.setType(CatalogEntry.TYPE_NODE);
entry2.setName("Entry-2");
entry2.setDescription("Entry-description-2");
catalogManager.addCatalogEntry(root1, entry2);
entryToMove1 = catalogManager.createCatalogEntry();
entryToMove1.setType(CatalogEntry.TYPE_NODE);
entryToMove1.setName("Entry-1-to-move");
entryToMove1.setDescription("Entry-description-1-to-move");
catalogManager.addCatalogEntry(root1, entryToMove1);
entryToMove2 = catalogManager.createCatalogEntry();
entryToMove2.setType(CatalogEntry.TYPE_NODE);
entryToMove2.setName("Entry-2-to-move");
entryToMove2.setDescription("Entry-description-2-to-move");
catalogManager.addCatalogEntry(root1, entryToMove2);
subEntry13move = catalogManager.createCatalogEntry();
subEntry13move.setType(CatalogEntry.TYPE_NODE);
subEntry13move.setName("Sub-entry-13-move target");
subEntry13move.setDescription("Sub-entry-description-13-move target");
catalogManager.addCatalogEntry(root1, subEntry13move);
DBFactory.getInstance().intermediateCommit();
}
Aggregations