This commit is contained in:
Mohamed Karray
2018-09-20 11:51:10 +02:00
parent abe63140dd
commit b80e9666aa
13 changed files with 40 additions and 40 deletions

View File

@@ -10,7 +10,7 @@ import javax.ws.rs.ext.Provider;
public class InvalidPasswordExceptionMapper implements ExceptionMapper<InvalidPasswordException> {
@Override
public Response toResponse(InvalidPasswordException exception) {
return Response.status(Response.Status.UNAUTHORIZED)
return Response.status(Response.Status.BAD_REQUEST)
.entity(exception.getMessage())
.build();
}

View File

@@ -24,6 +24,8 @@ import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.util.function.Consumer;
import static sonia.scm.user.InvalidPasswordException.PASSWORD_NOT_MATCHED;
/**
* RESTful Web Service Resource to get currently logged in users.
@@ -87,7 +89,7 @@ public class MeResource {
private Consumer<User> getOldOriginalPasswordChecker(String oldPassword) {
return user -> {
if (!user.getPassword().equals(passwordService.encryptPassword(oldPassword))) {
throw new InvalidPasswordException("The password is invalid");
throw new InvalidPasswordException(PASSWORD_NOT_MATCHED);
}
};
}

View File

@@ -23,6 +23,7 @@ public abstract class MeToUserDtoMapper extends UserToUserDtoMapper{
private ResourceLinks resourceLinks;
@Override
@AfterMapping
protected void appendLinks(User user, @MappingTarget UserDto target) {
Links.Builder linksBuilder = linkingTo().self(resourceLinks.me().self());

View File

@@ -1,7 +1,5 @@
package sonia.scm.api.v2.resources;
import de.otto.edison.hal.HalRepresentation;
import de.otto.edison.hal.Links;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@@ -10,16 +8,10 @@ import org.hibernate.validator.constraints.NotEmpty;
@Getter
@Setter
@ToString
public class PasswordChangeDto extends HalRepresentation {
public class PasswordChangeDto {
private String oldPassword;
@NotEmpty
private String newPassword;
@Override
@SuppressWarnings("squid:S1185") // We want to have this method available in this package
protected HalRepresentation add(Links links) {
return super.add(links);
}
}

View File

@@ -100,8 +100,8 @@ class ResourceLinks {
private final LinkBuilder meLinkBuilder;
private UserLinks userLinks;
MeLinks(ScmPathInfo uriInfo, UserLinks user) {
meLinkBuilder = new LinkBuilder(uriInfo, MeResource.class);
MeLinks(ScmPathInfo pathInfo, UserLinks user) {
meLinkBuilder = new LinkBuilder(pathInfo, MeResource.class);
userLinks = user;
}

View File

@@ -1,9 +1,10 @@
package sonia.scm.api.v2.resources;
import org.mapstruct.AfterMapping;
import org.mapstruct.Context;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.MappingTarget;
import sonia.scm.user.User;
// Mapstruct does not support parameterized (i.e. non-default) constructors. Thus, we need to use field injection.
@@ -11,21 +12,24 @@ import sonia.scm.user.User;
@Mapper
public abstract class UserDtoToUserMapper extends BaseDtoMapper {
@Mapping(source = "password", target = "password", qualifiedByName = "getUsedPassword")
@Mapping(target = "creationDate", ignore = true)
public abstract User map(UserDto userDto, @Context String usedPassword);
/**
* depends on the use case the right password will be mapped.
* The given Password in the context parameter will be set.
* The mapper consumer have the control of what password should be set.
* </p>
* eg. for update user action the password will be set to the original password
* for create user and change password actions the password is the user input
*
* @param usedPassword the password to be mapped
* @return the password to be mapped
* @param usedPassword the password to be set
* @param user the target
*/
@Named("getUsedPassword")
String getUsedPassword(String password, @Context String usedPassword) {
return usedPassword;
@AfterMapping
void overridePassword(@MappingTarget User user, @Context String usedPassword) {
user.setPassword(usedPassword);
}
}

View File

@@ -120,7 +120,7 @@ public class UserResource {
@Consumes(VndMediaType.PASSWORD_CHANGE)
@StatusCodes({
@ResponseCode(code = 204, condition = "update success"),
@ResponseCode(code = 400, condition = "Invalid body, e.g. illegal change of id/user name"),
@ResponseCode(code = 400, condition = "Invalid body, e.g. the user type is not xml or the given oldPassword do not match the stored one"),
@ResponseCode(code = 401, condition = "not authenticated / invalid credentials"),
@ResponseCode(code = 403, condition = "not authorized, the current user does not have the \"user\" privilege"),
@ResponseCode(code = 404, condition = "not found, no user with the specified id/name available"),

View File

@@ -31,11 +31,6 @@ public abstract class UserToUserDtoMapper extends BaseMapper<User, UserDto> {
@Inject
private ResourceLinks resourceLinks;
@VisibleForTesting
void setResourceLinks(ResourceLinks resourceLinks) {
this.resourceLinks = resourceLinks;
}
@AfterMapping
protected void appendLinks(User user, @MappingTarget UserDto target) {
Links.Builder linksBuilder = linkingTo().self(resourceLinks.user().self(target.getName()));