| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.service.full; |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 2 | |
| 3 | import com.fasterxml.jackson.databind.JsonNode; |
| 4 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 5 | import com.sun.jersey.spi.container.ContainerRequest; |
| 6 | import com.sun.jersey.spi.container.ResourceFilters; |
| 7 | import de.ids_mannheim.korap.config.BeanConfiguration; |
| 8 | import de.ids_mannheim.korap.config.Scopes; |
| 9 | import de.ids_mannheim.korap.config.URIParam; |
| 10 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 11 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 12 | import de.ids_mannheim.korap.interfaces.AuthenticationManagerIface; |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 13 | import de.ids_mannheim.korap.user.*; |
| 14 | import de.ids_mannheim.korap.utils.JsonUtils; |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 15 | import de.ids_mannheim.korap.utils.StringUtils; |
| 16 | import de.ids_mannheim.korap.utils.TimeUtils; |
| 17 | import de.ids_mannheim.korap.web.KustvaktServer; |
| 18 | import de.ids_mannheim.korap.web.filter.AuthFilter; |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 19 | import de.ids_mannheim.korap.web.filter.BlockingFilter; |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 20 | import de.ids_mannheim.korap.web.filter.DefaultFilter; |
| 21 | import de.ids_mannheim.korap.web.filter.PiwikFilter; |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 22 | import de.ids_mannheim.korap.web.utils.FormRequestWrapper; |
| 23 | import de.ids_mannheim.korap.web.utils.KustvaktResponseHandler; |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 24 | import org.slf4j.Logger; |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 25 | import org.slf4j.LoggerFactory; |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 26 | |
| 27 | import javax.ws.rs.*; |
| 28 | import javax.ws.rs.core.*; |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 29 | import java.util.*; |
| 30 | |
| 31 | /** |
| 32 | * @author hanl |
| 33 | * @date 29/01/2014 |
| 34 | */ |
| 35 | @Path(KustvaktServer.API_VERSION + "/user") |
| 36 | @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") |
| 37 | @ResourceFilters({ PiwikFilter.class }) |
| 38 | public class UserService { |
| 39 | |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 40 | private static Logger jlog = LoggerFactory.getLogger(UserService.class); |
| 41 | // private static Logger jlog = KustvaktLogger |
| 42 | // .getLogger(KustvaktLogger.SECURITY_LOG); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 43 | private AuthenticationManagerIface controller; |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 44 | |
| 45 | private |
| 46 | @Context |
| 47 | UriInfo info; |
| 48 | |
| 49 | public UserService() { |
| 50 | this.controller = BeanConfiguration.getBeans() |
| 51 | .getAuthenticationManager(); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | // fixme: json contains password in clear text. Encrypt request? |
| 55 | // fixme: should also collect service exception, not just db exception! |
| 56 | @POST |
| 57 | @Path("register") |
| 58 | @Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
| 59 | public Response signUp( |
| 60 | @HeaderParam(ContainerRequest.USER_AGENT) String agent, |
| 61 | @HeaderParam(ContainerRequest.HOST) String host, |
| Michael Hanl | e17eaa5 | 2016-01-22 20:55:05 +0100 | [diff] [blame] | 62 | @Context Locale locale, |
| 63 | MultivaluedMap<String, String> form_values) { |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 64 | Map<String, String> wrapper = FormRequestWrapper |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 65 | .toMap(form_values, true); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 66 | |
| 67 | wrapper.put(Attributes.HOST, host); |
| 68 | wrapper.put(Attributes.USER_AGENT, agent); |
| 69 | UriBuilder uriBuilder; |
| 70 | User user; |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 71 | if (wrapper.get(Attributes.EMAIL) == null) |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 72 | throw KustvaktResponseHandler |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 73 | .throwit(StatusCodes.ILLEGAL_ARGUMENT, "parameter missing", |
| 74 | "email"); |
| 75 | |
| 76 | try { |
| 77 | uriBuilder = info.getBaseUriBuilder(); |
| 78 | uriBuilder.path(KustvaktServer.API_VERSION).path("user") |
| 79 | .path("confirm"); |
| 80 | |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 81 | user = controller.createUserAccount(wrapper, true); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 82 | |
| 83 | }catch (KustvaktException e) { |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 84 | throw KustvaktResponseHandler.throwit(e); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 85 | } |
| 86 | URIParam uri = user.getField(URIParam.class); |
| 87 | if (uri.hasValues()) { |
| 88 | uriBuilder.queryParam(Attributes.QUERY_PARAM_URI, |
| 89 | uri.getUriFragment()) |
| 90 | .queryParam(Attributes.QUERY_PARAM_USER, |
| 91 | user.getUsername()); |
| 92 | jlog.info("registration was successful for user '{}'", |
| 93 | form_values.get(Attributes.USERNAME)); |
| 94 | Map object = new HashMap(); |
| 95 | object.put("confirm_uri", uriBuilder.build()); |
| 96 | object.put("uri_expiration", |
| 97 | TimeUtils.format(uri.getUriExpiration())); |
| 98 | return Response.ok(JsonUtils.toJSON(object)).build(); |
| 99 | }else { |
| 100 | // todo: return error or warning |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 101 | throw KustvaktResponseHandler.throwit(StatusCodes.ILLEGAL_ARGUMENT, |
| 102 | "failed to validate uri paramter", "confirmation fragment"); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | } |
| 106 | |
| Michael Hanl | e17eaa5 | 2016-01-22 20:55:05 +0100 | [diff] [blame] | 107 | //todo: password update in special function? --> password reset only! |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 108 | @POST |
| 109 | @Path("update") |
| 110 | @Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
| 111 | @ResourceFilters({ AuthFilter.class, DefaultFilter.class, |
| 112 | PiwikFilter.class }) |
| 113 | public Response updateAccount(@Context SecurityContext ctx, String json) { |
| 114 | TokenContext context = (TokenContext) ctx.getUserPrincipal(); |
| 115 | try { |
| 116 | User user = controller.getUser(context.getUsername()); |
| 117 | |
| 118 | JsonNode node = JsonUtils.readTree(json); |
| 119 | KorAPUser ident = (KorAPUser) user; |
| 120 | KorAPUser values = User.UserFactory.toUser(json); |
| 121 | // user = controller |
| 122 | // .checkPasswordAllowance(ident, values.getPassword(), |
| 123 | // node.path("new_password").asText()); |
| Michael Hanl | e17eaa5 | 2016-01-22 20:55:05 +0100 | [diff] [blame] | 124 | // controller.updateAccount(user); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 125 | }catch (KustvaktException e) { |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 126 | throw KustvaktResponseHandler.throwit(e); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 127 | } |
| 128 | return Response.ok().build(); |
| 129 | } |
| 130 | |
| 131 | @GET |
| 132 | @Path("confirm") |
| 133 | @Produces(MediaType.TEXT_HTML) |
| 134 | public Response confirmRegistration(@QueryParam("uri") String uritoken, |
| 135 | @Context Locale locale, @QueryParam("user") String username) { |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 136 | if (uritoken == null || uritoken.isEmpty()) |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 137 | throw KustvaktResponseHandler |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 138 | .throwit(StatusCodes.ILLEGAL_ARGUMENT, "parameter missing", |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 139 | "uri parameter"); |
| 140 | if (username == null || username.isEmpty()) |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 141 | throw KustvaktResponseHandler |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 142 | .throwit(StatusCodes.ILLEGAL_ARGUMENT, "parameter missing", |
| 143 | "Username"); |
| 144 | |
| 145 | try { |
| 146 | controller.confirmRegistration(uritoken, username); |
| 147 | }catch (KustvaktException e) { |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 148 | e.printStackTrace(); |
| 149 | throw KustvaktResponseHandler.throwit(e); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 150 | } |
| 151 | return Response.ok("success").build(); |
| 152 | } |
| 153 | |
| 154 | // todo: auditing! |
| 155 | @POST |
| 156 | @Path("requestReset") |
| 157 | @Produces(MediaType.TEXT_HTML) |
| 158 | @Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
| 159 | public Response requestPasswordReset(@Context Locale locale, String json) { |
| 160 | JsonNode node = JsonUtils.readTree(json); |
| 161 | StringBuilder builder = new StringBuilder(); |
| 162 | String username, email; |
| 163 | username = node.path(Attributes.USERNAME).asText(); |
| 164 | email = node.path(Attributes.EMAIL).asText(); |
| 165 | |
| 166 | // deprecated --> depends on the client! |
| 167 | // String url = config.getMailProperties() |
| 168 | // .getProperty("korap.frontend.url", ""); |
| 169 | // if (url.isEmpty()) |
| 170 | // return Response.ok("URLException: Missing source URL").build(); |
| 171 | |
| 172 | // URIUtils utils = new URIUtils(info); |
| 173 | // may inject the actual REST url in a redirect request?! |
| 174 | // UriBuilder uriBuilder = UriBuilder.fromUri(url).fragment("reset"); |
| 175 | Object[] objects; |
| 176 | try { |
| 177 | builder.append("?"); |
| 178 | // just append the endpint fragment plus the query parameter. |
| 179 | // the address by which the data is handled depends on the frontend |
| 180 | objects = controller.validateResetPasswordRequest(username, email); |
| 181 | builder.append(Attributes.QUERY_PARAM_URI).append("=") |
| 182 | .append(objects[0]); |
| 183 | builder.append(Attributes.QUERY_PARAM_USER).append("=") |
| 184 | .append(username); |
| 185 | }catch (KustvaktException e) { |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 186 | jlog.error("Eoxception encountered!", e); |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 187 | throw KustvaktResponseHandler.throwit(e); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | ObjectNode obj = JsonUtils.createObjectNode(); |
| 191 | obj.put(Attributes.URI, builder.toString()); |
| 192 | obj.put(Attributes.URI_EXPIRATION, String.valueOf(objects[1])); |
| 193 | return Response.ok(JsonUtils.toJSON(obj)).build(); |
| 194 | } |
| 195 | |
| 196 | @POST |
| 197 | @Path("reset") |
| 198 | @Produces(MediaType.TEXT_HTML) |
| 199 | @Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
| 200 | public Response resetPassword( |
| 201 | @QueryParam(Attributes.QUERY_PARAM_URI) String uri, |
| 202 | @QueryParam(Attributes.QUERY_PARAM_USER) String username, |
| 203 | @Context HttpHeaders headers, String passphrase) { |
| 204 | try { |
| 205 | controller.resetPassword(uri, username, passphrase); |
| 206 | }catch (KustvaktException e) { |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 207 | jlog.error("Exception encountered!", e); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 208 | return Response.notModified().build(); |
| 209 | } |
| 210 | return Response.ok().build(); |
| 211 | } |
| 212 | |
| 213 | @GET |
| 214 | @Path("info") |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 215 | @ResourceFilters({ AuthFilter.class, DefaultFilter.class, PiwikFilter.class, |
| 216 | BlockingFilter.class }) |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 217 | public Response getStatus(@Context SecurityContext context, |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 218 | @QueryParam("scopes") String scopes) { |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 219 | TokenContext ctx = (TokenContext) context.getUserPrincipal(); |
| 220 | User user; |
| 221 | try { |
| 222 | user = controller.getUser(ctx.getUsername()); |
| Michael Hanl | 7d92561 | 2016-01-28 16:59:30 +0100 | [diff] [blame^] | 223 | Userdata data = controller.getUserData(user, Userdetails2.class); |
| Michael Hanl | c2a9f62 | 2016-01-28 16:40:06 +0100 | [diff] [blame] | 224 | user.addUserData(data); |
| 225 | |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 226 | Set<String> base_scope = StringUtils.toSet(scopes, " "); |
| 227 | if (scopes != null) |
| 228 | base_scope.retainAll(StringUtils.toSet(scopes)); |
| 229 | scopes = StringUtils.toString(base_scope); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 230 | }catch (KustvaktException e) { |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 231 | throw KustvaktResponseHandler.throwit(e); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 232 | } |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 233 | Scopes m = Scopes.mapScopes(scopes, user.getDetails()); |
| 234 | return Response.ok(m.toEntity()).build(); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | @GET |
| 238 | @Path("settings") |
| 239 | @ResourceFilters({ AuthFilter.class, DefaultFilter.class, |
| 240 | PiwikFilter.class }) |
| 241 | public Response getUserSettings(@Context SecurityContext context, |
| 242 | @Context Locale locale) { |
| 243 | TokenContext ctx = (TokenContext) context.getUserPrincipal(); |
| Michael Hanl | 7d92561 | 2016-01-28 16:59:30 +0100 | [diff] [blame^] | 244 | String result; |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 245 | try { |
| Michael Hanl | 7d92561 | 2016-01-28 16:59:30 +0100 | [diff] [blame^] | 246 | User user = controller.getUser(ctx.getUsername()); |
| 247 | Userdata data = controller.getUserData(user, UserSettings2.class); |
| 248 | data.addField(Attributes.USERNAME, ctx.getUsername()); |
| 249 | result = data.data(); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 250 | }catch (KustvaktException e) { |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 251 | jlog.error("Exception encountered!", e); |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 252 | throw KustvaktResponseHandler.throwit(e); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 253 | } |
| Michael Hanl | 7d92561 | 2016-01-28 16:59:30 +0100 | [diff] [blame^] | 254 | return Response.ok(result).build(); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // todo: test |
| 258 | @POST |
| 259 | @Path("settings") |
| 260 | @Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
| 261 | @ResourceFilters({ AuthFilter.class, DefaultFilter.class, |
| 262 | PiwikFilter.class }) |
| 263 | public Response updateSettings(@Context SecurityContext context, |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 264 | @Context Locale locale, MultivaluedMap<String, String> form) { |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 265 | TokenContext ctx = (TokenContext) context.getUserPrincipal(); |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 266 | Map<String, String> settings = FormRequestWrapper.toMap(form, false); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 267 | |
| 268 | try { |
| 269 | User user = controller.getUser(ctx.getUsername()); |
| Michael Hanl | 7d92561 | 2016-01-28 16:59:30 +0100 | [diff] [blame^] | 270 | if (user.isDemo()) |
| 271 | return Response.notModified().build(); |
| 272 | |
| 273 | Userdata data = controller.getUserData(user, UserSettings2.class); |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 274 | // todo: check setting only within the scope of user settings permissions; not foundry range. Latter is part of |
| 275 | // frontend which only displays available foundries and |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 276 | // SecurityManager.findbyId(us.getDefaultConstfoundry(), user, Foundry.class); |
| 277 | // SecurityManager.findbyId(us.getDefaultLemmafoundry(), user, Foundry.class); |
| 278 | // SecurityManager.findbyId(us.getDefaultPOSfoundry(), user, Foundry.class); |
| 279 | // SecurityManager.findbyId(us.getDefaultRelfoundry(), user, Foundry.class); |
| Michael Hanl | 7d92561 | 2016-01-28 16:59:30 +0100 | [diff] [blame^] | 280 | Userdata new_data = new UserSettings2(user.getId()); |
| 281 | new_data.setData(JsonUtils.toJSON(settings)); |
| 282 | data.update(new_data); |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 283 | |
| Michael Hanl | 7d92561 | 2016-01-28 16:59:30 +0100 | [diff] [blame^] | 284 | controller.updateUserData(data); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 285 | }catch (KustvaktException e) { |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 286 | jlog.error("Exception encountered!", e); |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 287 | throw KustvaktResponseHandler.throwit(e); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | return Response.ok().build(); |
| 291 | } |
| 292 | |
| 293 | @GET |
| 294 | @Path("details") |
| 295 | @ResourceFilters({ AuthFilter.class, DefaultFilter.class, |
| 296 | PiwikFilter.class }) |
| 297 | public Response getDetails(@Context SecurityContext context, |
| 298 | @Context Locale locale) { |
| 299 | TokenContext ctx = (TokenContext) context.getUserPrincipal(); |
| 300 | User user; |
| 301 | try { |
| 302 | user = controller.getUser(ctx.getUsername()); |
| Michael Hanl | 7d92561 | 2016-01-28 16:59:30 +0100 | [diff] [blame^] | 303 | Userdata data = controller.getUserData(user, Userdetails2.class); |
| Michael Hanl | c2a9f62 | 2016-01-28 16:40:06 +0100 | [diff] [blame] | 304 | user.addUserData(data); |
| 305 | |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 306 | }catch (KustvaktException e) { |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 307 | jlog.error("Exception encountered!", e); |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 308 | throw KustvaktResponseHandler.throwit(e); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| Michael Hanl | e17eaa5 | 2016-01-22 20:55:05 +0100 | [diff] [blame] | 311 | Map m = user.getDetails().toMap(); |
| 312 | m.put(Attributes.USERNAME, ctx.getUsername()); |
| 313 | return Response.ok(JsonUtils.toJSON(m)).build(); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | @POST |
| 317 | @Path("details") |
| 318 | @Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
| 319 | @ResourceFilters({ AuthFilter.class, DefaultFilter.class, |
| 320 | PiwikFilter.class }) |
| 321 | public Response updateDetails(@Context SecurityContext context, |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 322 | @Context Locale locale, MultivaluedMap form) { |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 323 | TokenContext ctx = (TokenContext) context.getUserPrincipal(); |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 324 | |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 325 | Map<String, String> wrapper = FormRequestWrapper.toMap(form, true); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 326 | |
| 327 | try { |
| 328 | User user = controller.getUser(ctx.getUsername()); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 329 | if (user.isDemo()) |
| 330 | return Response.notModified().build(); |
| Michael Hanl | 7d92561 | 2016-01-28 16:59:30 +0100 | [diff] [blame^] | 331 | |
| 332 | Userdetails2 new_data = new Userdetails2(user.getId()); |
| 333 | new_data.setData(JsonUtils.toJSON(wrapper)); |
| 334 | |
| 335 | Userdetails2 det = controller.getUserData(user, Userdetails2.class); |
| 336 | det.update(new_data); |
| 337 | controller.updateUserData(det); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 338 | }catch (KustvaktException e) { |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 339 | jlog.error("Exception encountered!", e); |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 340 | throw KustvaktResponseHandler.throwit(e); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | return Response.ok().build(); |
| 344 | } |
| 345 | |
| 346 | //fixme: if policy allows, foreign user might be allowed to change search! |
| 347 | @POST |
| 348 | @Path("queries") |
| 349 | @Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
| 350 | @ResourceFilters({ AuthFilter.class, DefaultFilter.class, |
| 351 | PiwikFilter.class }) |
| 352 | public Response updateQueries(@Context SecurityContext context, |
| 353 | String json) { |
| 354 | TokenContext ctx = (TokenContext) context.getUserPrincipal(); |
| 355 | Collection<UserQuery> add = new HashSet<>(); |
| 356 | try { |
| 357 | User user = controller.getUser(ctx.getUsername()); |
| 358 | List<UserQuery> userQuieres = new ArrayList<>(); |
| 359 | JsonNode nodes = JsonUtils.readTree(json); |
| 360 | Iterator<JsonNode> node = nodes.elements(); |
| 361 | while (node.hasNext()) { |
| 362 | JsonNode cursor = node.next(); |
| 363 | UserQuery query = new UserQuery(cursor.path("id").asInt(), |
| 364 | user.getId()); |
| 365 | query.setQueryLanguage(cursor.path("queryLanguage").asText()); |
| 366 | query.setQuery(cursor.path("query").asText()); |
| 367 | query.setDescription(cursor.path("description").asText()); |
| 368 | userQuieres.add(query); |
| 369 | } |
| 370 | |
| 371 | //1: add all that are new, update all that are retained, delete the rest |
| 372 | // Set<UserQuery> resources = ResourceFinder |
| 373 | // .search(user, UserQuery.class); |
| 374 | // |
| 375 | // add.addAll(userQuieres); |
| 376 | // add.removeAll(resources); |
| 377 | // Collection<UserQuery> update = new HashSet<>(userQuieres); |
| 378 | // update.retainAll(resources); |
| 379 | // resources.removeAll(userQuieres); |
| 380 | // |
| 381 | // if (!update.isEmpty()) { |
| 382 | // resourceHandler.updateResources(user, |
| 383 | // update.toArray(new UserQuery[update.size()])); |
| 384 | // } |
| 385 | // if (!add.isEmpty()) { |
| 386 | // resourceHandler.storeResources(user, |
| 387 | // add.toArray(new UserQuery[add.size()])); |
| 388 | // } |
| 389 | // if (!resources.isEmpty()) { |
| 390 | // resourceHandler.deleteResources(user, |
| 391 | // resources.toArray(new UserQuery[resources.size()])); |
| 392 | // } |
| 393 | }catch (KustvaktException e) { |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 394 | jlog.error("Exception encountered!", e); |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 395 | throw KustvaktResponseHandler.throwit(e); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 396 | } |
| 397 | return Response.ok(JsonUtils.toJSON(add)).build(); |
| 398 | } |
| 399 | |
| 400 | @DELETE |
| 401 | @ResourceFilters({ AuthFilter.class, DefaultFilter.class, |
| 402 | PiwikFilter.class }) |
| 403 | public Response deleteUser(@Context SecurityContext context) { |
| 404 | TokenContext ctx = (TokenContext) context.getUserPrincipal(); |
| 405 | try { |
| 406 | User user = controller.getUser(ctx.getUsername()); |
| 407 | if (user.isDemo()) |
| 408 | return Response.notModified().build(); |
| 409 | controller.deleteAccount(user); |
| 410 | }catch (KustvaktException e) { |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 411 | jlog.error("Exception encountered!", e); |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 412 | throw KustvaktResponseHandler.throwit(e); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 413 | } |
| 414 | return Response.ok().build(); |
| 415 | } |
| 416 | |
| 417 | @GET |
| 418 | @Path("queries") |
| 419 | @ResourceFilters({ AuthFilter.class, DefaultFilter.class, |
| 420 | PiwikFilter.class }) |
| 421 | public Response getQueries(@Context SecurityContext context, |
| 422 | @Context Locale locale) { |
| 423 | TokenContext ctx = (TokenContext) context.getUserPrincipal(); |
| 424 | String queryStr; |
| 425 | try { |
| 426 | User user = controller.getUser(ctx.getUsername()); |
| 427 | // Set<UserQuery> queries = ResourceFinder |
| 428 | // .search(user, UserQuery.class); |
| 429 | // queryStr = JsonUtils.toJSON(queries); |
| 430 | //todo: |
| 431 | queryStr = ""; |
| 432 | }catch (KustvaktException e) { |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 433 | jlog.error("Exception encountered!", e); |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 434 | throw KustvaktResponseHandler.throwit(e); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 435 | } |
| 436 | return Response.ok(queryStr).build(); |
| 437 | } |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 438 | } |