use of org.olat.basesecurity.BaseSecurity in project openolat by klemens.
the class CoreSpringFactoryTest method testGetImpl.
@Test
public void testGetImpl() {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
BaseSecurity securityManager = CoreSpringFactory.getImpl(BaseSecurity.class);
Assert.assertNotNull(securityManager);
}
log.info("Get bean by impl takes (ms): " + (System.currentTimeMillis() - start));
long start2 = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
BaseSecurity securityManager = (BaseSecurity) CoreSpringFactory.getBean("baseSecurityManager");
Assert.assertNotNull(securityManager);
}
log.info("Get by by ID takes (ms): " + (System.currentTimeMillis() - start2));
}
use of org.olat.basesecurity.BaseSecurity in project openolat by klemens.
the class UserAuthenticationWebService method getAuthenticationTokenList.
/**
* Returns all user authentications
* @response.representation.200.qname {http://www.example.com}authenticationVO
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc The list of all users in the OLAT system
* @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_AUTHVOes}
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @response.representation.404.doc The identity not found
* @param username The username of the user to retrieve authentication
* @param request The HTTP request
* @return
*/
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getAuthenticationTokenList(@PathParam("username") String username, @Context HttpServletRequest request) {
if (!isUserManager(request)) {
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();
}
List<Authentication> authentications = baseSecurity.getAuthentications(identity);
AuthenticationVO[] vos = new AuthenticationVO[authentications.size()];
int count = 0;
for (Authentication authentication : authentications) {
vos[count++] = ObjectFactory.get(authentication, false);
}
return Response.ok(vos).build();
}
use of org.olat.basesecurity.BaseSecurity in project openolat by klemens.
the class UserAuthenticationWebService method create.
/**
* Creates and persists an authentication
* @response.representation.qname {http://www.example.com}authenticationVO
* @response.representation.mediaType application/xml, application/json
* @response.representation.doc An authentication to save
* @response.representation.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_AUTHVO}
* @response.representation.200.qname {http://www.example.com}authenticationVO
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc The saved authentication
* @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_AUTHVO}
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @response.representation.404.doc The identity not found
* @response.representation.406.doc Cannot create the authentication for an unkown reason
* @response.representation.409.doc Cannot create the authentication because the authentication username is already used by someone else within the same provider
* @param username The username of the user
* @param authenticationVO The authentication object to persist
* @param request The HTTP request
* @return the saved authentication
*/
@PUT
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response create(@PathParam("username") String username, AuthenticationVO authenticationVO, @Context HttpServletRequest request) {
if (!RestSecurityHelper.isUserManager(request)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
BaseSecurity baseSecurity = BaseSecurityManager.getInstance();
Identity identity = baseSecurity.loadIdentityByKey(authenticationVO.getIdentityKey(), false);
if (identity == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
if (!identity.getName().equals(username)) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
String provider = authenticationVO.getProvider();
String authUsername = authenticationVO.getAuthUsername();
String credentials = authenticationVO.getCredential();
Authentication currentAuthentication = baseSecurity.findAuthenticationByAuthusername(authUsername, provider);
if (currentAuthentication != null) {
if (!currentAuthentication.getIdentity().equals(identity)) {
ErrorVO error = new ErrorVO();
error.setCode("unkown:409");
error.setTranslation("Authentication name used by: " + currentAuthentication.getIdentity().getUser().getEmail());
return Response.serverError().status(Status.CONFLICT).entity(error).build();
}
}
Authentication authentication = baseSecurity.createAndPersistAuthentication(identity, provider, authUsername, credentials, null);
if (authentication == null) {
return Response.serverError().status(Status.NOT_ACCEPTABLE).build();
}
log.audit("New authentication created for " + authUsername + " with provider " + provider);
AuthenticationVO savedAuth = ObjectFactory.get(authentication, true);
return Response.ok(savedAuth).build();
}
use of org.olat.basesecurity.BaseSecurity in project openolat by klemens.
the class UserAuthenticationWebService method delete.
/**
* Deletes an authentication from the system
* @response.representation.200.doc The authentication successfully deleted
* @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
* @param authKey The authentication key identifier
* @param request The HTTP request
* @return <code>Response</code> object. The operation status (success or
* fail)
*/
@DELETE
@Path("{authKey}")
public Response delete(@PathParam("username") String username, @PathParam("authKey") Long authKey, @Context HttpServletRequest request) {
if (!isUserManager(request)) {
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();
}
List<Authentication> authentications = baseSecurity.getAuthentications(identity);
for (Authentication authentication : authentications) {
if (authKey.equals(authentication.getKey())) {
baseSecurity.deleteAuthentication(authentication);
return Response.ok().build();
}
}
return Response.serverError().status(Status.NOT_FOUND).build();
}
use of org.olat.basesecurity.BaseSecurity in project openolat by klemens.
the class CatalogWebService method removeOwner.
/**
* Remove an owner of the local sub tree
* @response.representation.200.qname {http://www.example.com}userVO
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc The catalog entry
* @response.representation.200.example {@link org.olat.user.restapi.Examples#SAMPLE_USERVOes}
* @response.representation.401.doc Not authorized
* @response.representation.404.doc The path could not be resolved to a valid catalog entry
* @param path The path
* @param identityKey The id of the user
* @param httpRquest The HTTP request
* @return The response
*/
@DELETE
@Path("{path:.*}/owners/{identityKey}")
public Response removeOwner(@PathParam("path") List<PathSegment> path, @PathParam("identityKey") Long identityKey, @Context HttpServletRequest httpRequest) {
Long key = getCatalogEntryKeyFromPath(path);
if (key == null) {
return Response.serverError().status(Status.NOT_ACCEPTABLE).build();
}
CatalogEntry ce = catalogManager.loadCatalogEntry(key);
if (ce == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
if (!isAuthor(httpRequest) && !canAdminSubTree(ce, httpRequest)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
BaseSecurity securityManager = BaseSecurityManager.getInstance();
Identity identity = securityManager.loadIdentityByKey(identityKey, false);
if (identity == null) {
return Response.ok().build();
}
SecurityGroup sg = ce.getOwnerGroup();
if (sg == null) {
return Response.ok().build();
}
Identity id = getUserRequest(httpRequest).getIdentity();
LockResult lock = CoordinatorManager.getInstance().getCoordinator().getLocker().acquireLock(lockRes, id, LOCK_TOKEN);
if (!lock.isSuccess()) {
return getLockedResponse(lock, httpRequest);
}
try {
securityManager.removeIdentityFromSecurityGroup(identity, ce.getOwnerGroup());
} catch (Exception e) {
throw new WebApplicationException(e);
} finally {
CoordinatorManager.getInstance().getCoordinator().getLocker().releaseLock(lock);
}
return Response.ok().build();
}
Aggregations