Commit 38f28d3e authored by Maxym Borodenko's avatar Maxym Borodenko
Browse files

Updated MeController

parent 309ac333
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -15,9 +15,15 @@
*/
package org.genesys.catalog.server.controller.api.v0.me;
import java.util.UUID;
import org.genesys.blocks.security.SecurityContextUtil;
import org.genesys.blocks.security.UserException;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.model.traits.DescriptorList;
import org.genesys.catalog.model.user.User;
import org.genesys.catalog.service.UserService;
import org.genesys.catalog.service.DatasetService;
import org.genesys.catalog.service.DescriptorListService;
import org.genesys.catalog.service.DescriptorService;
......@@ -33,7 +39,9 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -63,14 +71,38 @@ public class MeController {
private DescriptorListService descriptorListService;
@Autowired
@Qualifier("tokenServices")
private DefaultTokenServices defaultTokenServices;
@PostMapping(value = "/token", consumes = "text/plain")
public Object removeToken(@RequestBody final String token) {
defaultTokenServices.revokeToken(token);
return token;
}
@Qualifier("tokenServices")
private DefaultTokenServices defaultTokenServices;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private UserService userService;
@PostMapping(value = "/token", consumes = "text/plain")
public Object removeToken(@RequestBody final String token) {
defaultTokenServices.revokeToken(token);
return token;
}
@GetMapping(value = "/profile")
public User getProfile() {
final User currentUser = SecurityContextUtil.getMe();
return userService.getUser(UUID.fromString(currentUser.getUuid()));
}
@PostMapping(value = "/password")
public String changePassword(@RequestParam(name = "old", required = true) final String oldPassword,
@RequestParam(name = "new", required = true) final String newPassword) throws UserException {
final User currentUser = SecurityContextUtil.getMe();
if (passwordEncoder.matches(oldPassword, currentUser.getPassword())) {
userService.changePassword(currentUser, newPassword);
return "OK";
}
throw new UserException("Your old password was entered incorrectly. Please enter it again.");
}
@PostMapping(value = "/partners")
public Page<Partner> myPartners(@RequestParam(name = "p", required = false, defaultValue = "0") final int page,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment