Search in sources :

Example 11 with TokenContext

use of de.ids_mannheim.korap.security.context.TokenContext in project Kustvakt by KorAP.

the class ShibbolethUserController method updateSettings.

@Deprecated
@POST
@Path("settings")
@Consumes({ MediaType.APPLICATION_JSON })
@ResourceFilters({ AuthenticationFilter.class, PiwikFilter.class, BlockingFilter.class })
public Response updateSettings(@Context SecurityContext context, @Context Locale locale, Map settings) {
    TokenContext ctx = (TokenContext) context.getUserPrincipal();
    if (settings == null)
        return Response.notModified().build();
    try {
        User user = controller.getUser(ctx.getUsername());
        if (User.UserFactory.isDemo(ctx.getUsername()))
            return Response.notModified().build();
        Userdata data = controller.getUserData(user, UserSettingProcessor.class);
        // todo: check setting only within the scope of user settings permissions; not foundry range. Latter is part of
        // frontend which only displays available foundries and
        // SecurityManager.findbyId(us.getDefaultConstfoundry(), user, Foundry.class);
        // SecurityManager.findbyId(us.getDefaultLemmafoundry(), user, Foundry.class);
        // SecurityManager.findbyId(us.getDefaultPOSfoundry(), user, Foundry.class);
        // SecurityManager.findbyId(us.getDefaultRelfoundry(), user, Foundry.class);
        Userdata new_data = new UserSettingProcessor(user.getId());
        new_data.readQuietly((Map<String, Object>) settings, false);
        data.update(new_data);
        controller.updateUserData(data);
    } catch (KustvaktException e) {
        jlog.error("Exception encountered!", e);
        throw kustvaktResponseHandler.throwit(e);
    }
    return Response.ok().build();
}
Also used : TokenContext(de.ids_mannheim.korap.security.context.TokenContext) User(de.ids_mannheim.korap.user.User) KorAPUser(de.ids_mannheim.korap.user.KorAPUser) KustvaktException(de.ids_mannheim.korap.exceptions.KustvaktException) Userdata(de.ids_mannheim.korap.user.Userdata) UserSettingProcessor(de.ids_mannheim.korap.user.UserSettingProcessor) Path(javax.ws.rs.Path) ResourceFilters(com.sun.jersey.spi.container.ResourceFilters) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 12 with TokenContext

use of de.ids_mannheim.korap.security.context.TokenContext in project Kustvakt by KorAP.

the class ShibbolethUserController method updateQueries.

// fixme: if policy allows, foreign user might be allowed to change search!
@POST
@Path("queries")
@Consumes(MediaType.APPLICATION_JSON)
@ResourceFilters({ AuthenticationFilter.class, PiwikFilter.class, BlockingFilter.class })
public Response updateQueries(@Context SecurityContext context, String json) {
    TokenContext ctx = (TokenContext) context.getUserPrincipal();
    Collection<UserQuery> add = new HashSet<>();
    try {
        User user = controller.getUser(ctx.getUsername());
        List<UserQuery> userQuieres = new ArrayList<>();
        JsonNode nodes = JsonUtils.readTree(json);
        Iterator<JsonNode> node = nodes.elements();
        while (node.hasNext()) {
            JsonNode cursor = node.next();
            UserQuery query = new UserQuery(cursor.path("id").asInt(), user.getId());
            query.setQueryLanguage(cursor.path("queryLanguage").asText());
            query.setQuery(cursor.path("query").asText());
            query.setDescription(cursor.path("description").asText());
            userQuieres.add(query);
        }
    // 1: add all that are new, update all that are retained, delete the rest
    // Set<UserQuery> resources = ResourceFinder
    // .search(user, UserQuery.class);
    // 
    // add.addAll(userQuieres);
    // add.removeAll(resources);
    // Collection<UserQuery> update = new HashSet<>(userQuieres);
    // update.retainAll(resources);
    // resources.removeAll(userQuieres);
    // 
    // if (!update.isEmpty()) {
    // resourceHandler.updateResources(user,
    // update.toArray(new UserQuery[update.size()]));
    // }
    // if (!add.isEmpty()) {
    // resourceHandler.storeResources(user,
    // add.toArray(new UserQuery[add.size()]));
    // }
    // if (!resources.isEmpty()) {
    // resourceHandler.deleteResources(user,
    // resources.toArray(new UserQuery[resources.size()]));
    // }
    } catch (KustvaktException e) {
        jlog.error("Exception encountered!", e);
        throw kustvaktResponseHandler.throwit(e);
    }
    try {
        return Response.ok(JsonUtils.toJSON(add)).build();
    } catch (KustvaktException e) {
        throw kustvaktResponseHandler.throwit(e);
    }
}
Also used : TokenContext(de.ids_mannheim.korap.security.context.TokenContext) User(de.ids_mannheim.korap.user.User) KorAPUser(de.ids_mannheim.korap.user.KorAPUser) KustvaktException(de.ids_mannheim.korap.exceptions.KustvaktException) UserQuery(de.ids_mannheim.korap.user.UserQuery) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) ResourceFilters(com.sun.jersey.spi.container.ResourceFilters) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 13 with TokenContext

use of de.ids_mannheim.korap.security.context.TokenContext in project Kustvakt by KorAP.

the class UserGroupController method addMemberRoles.

/**
 * Adds roles of an active member of a user-group. Only user-group
 * admins and system admins are allowed.
 *
 * @param securityContext
 * @param groupName
 *            a group name
 * @param memberUsername
 *            a username of a group member
 * @param roleId
 *            a role id or multiple role ids
 * @return if successful, HTTP response status OK
 */
@POST
@Path("@{groupName}/role/add")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response addMemberRoles(@Context SecurityContext securityContext, @PathParam("groupName") String groupName, @FormParam("memberUsername") String memberUsername, @FormParam("roleId") List<Integer> roleIds) {
    TokenContext context = (TokenContext) securityContext.getUserPrincipal();
    try {
        scopeService.verifyScope(context, OAuth2Scope.ADD_USER_GROUP_MEMBER_ROLE);
        service.addMemberRoles(context.getUsername(), groupName, memberUsername, roleIds);
        return Response.ok("SUCCESS").build();
    } catch (KustvaktException e) {
        throw kustvaktResponseHandler.throwit(e);
    }
}
Also used : TokenContext(de.ids_mannheim.korap.security.context.TokenContext) KustvaktException(de.ids_mannheim.korap.exceptions.KustvaktException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 14 with TokenContext

use of de.ids_mannheim.korap.security.context.TokenContext in project Kustvakt by KorAP.

the class UserGroupController method subscribeToGroup.

/**
 * Handles requests to accept membership invitation. Only invited
 * users can subscribe to the corresponding user-group.
 *
 * @param securityContext
 * @param groupName
 *            a group name
 * @return if successful, HTTP response status OK
 */
@POST
@Path("@{groupName}/subscribe")
public Response subscribeToGroup(@Context SecurityContext securityContext, @PathParam("groupName") String groupName) {
    TokenContext context = (TokenContext) securityContext.getUserPrincipal();
    try {
        scopeService.verifyScope(context, OAuth2Scope.ADD_USER_GROUP_MEMBER);
        service.acceptInvitation(groupName, context.getUsername());
        return Response.ok("SUCCESS").build();
    } catch (KustvaktException e) {
        throw kustvaktResponseHandler.throwit(e);
    }
}
Also used : TokenContext(de.ids_mannheim.korap.security.context.TokenContext) KustvaktException(de.ids_mannheim.korap.exceptions.KustvaktException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 15 with TokenContext

use of de.ids_mannheim.korap.security.context.TokenContext in project Kustvakt by KorAP.

the class UserGroupController method removeUserFromGroup.

/**
 * Removes a user-group member. Group owner cannot be removed.
 * Only group admins, system admins and the member himself can
 * remove a member.
 *
 * @param securityContext
 * @param memberUsername
 *            a username of a group member
 * @param groupName
 *            a group name
 * @return if successful, HTTP response status OK
 */
@DELETE
@Path("@{groupName}/~{memberUsername}")
public Response removeUserFromGroup(@Context SecurityContext securityContext, @PathParam("memberUsername") String memberUsername, @PathParam("groupName") String groupName) {
    TokenContext context = (TokenContext) securityContext.getUserPrincipal();
    try {
        scopeService.verifyScope(context, OAuth2Scope.DELETE_USER_GROUP_MEMBER);
        service.deleteGroupMember(memberUsername, groupName, context.getUsername());
        return Response.ok().build();
    } catch (KustvaktException e) {
        throw kustvaktResponseHandler.throwit(e);
    }
}
Also used : TokenContext(de.ids_mannheim.korap.security.context.TokenContext) KustvaktException(de.ids_mannheim.korap.exceptions.KustvaktException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Aggregations

TokenContext (de.ids_mannheim.korap.security.context.TokenContext)62 KustvaktException (de.ids_mannheim.korap.exceptions.KustvaktException)54 Path (javax.ws.rs.Path)40 Consumes (javax.ws.rs.Consumes)21 POST (javax.ws.rs.POST)19 User (de.ids_mannheim.korap.user.User)16 ResourceFilters (com.sun.jersey.spi.container.ResourceFilters)15 GET (javax.ws.rs.GET)11 KorAPUser (de.ids_mannheim.korap.user.KorAPUser)10 DELETE (javax.ws.rs.DELETE)10 Produces (javax.ws.rs.Produces)9 Userdata (de.ids_mannheim.korap.user.Userdata)5 ZonedDateTime (java.time.ZonedDateTime)5 HashMap (java.util.HashMap)4 PUT (javax.ws.rs.PUT)4 AuthorizationData (de.ids_mannheim.korap.authentication.http.AuthorizationData)3 FormRequestWrapper (de.ids_mannheim.korap.web.utils.FormRequestWrapper)3 HashSet (java.util.HashSet)3 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)3 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)3