From 7a1f3844180045ad85f5d692ba04bcd175fc658e Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Fri, 25 May 2012 14:38:09 +0000 Subject: [PATCH] change signature to be able to pass the application url for reset password. git-svn-id: https://svn.apache.org/repos/asf/archiva/redback/redback-core/trunk@1342645 13f79535-47bb-0310-9956-ffa450edef68 --- .../redback/rest/api/services/UserService.java | 8 +++++--- .../redback/rest/services/DefaultUserService.java | 11 +++++++++-- .../redback/rest/services/UserServiceTest.java | 9 ++++++--- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/UserService.java b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/UserService.java index 331f8d8d..40a54146 100644 --- a/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/UserService.java +++ b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/UserService.java @@ -24,6 +24,7 @@ import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants import org.apache.archiva.redback.rest.api.model.Operation; import org.apache.archiva.redback.rest.api.model.Permission; import org.apache.archiva.redback.rest.api.model.RegistrationKey; +import org.apache.archiva.redback.rest.api.model.ResetPasswordRequest; import org.apache.archiva.redback.rest.api.model.User; import org.apache.archiva.redback.rest.api.model.UserRegistrationRequest; @@ -204,16 +205,17 @@ public interface UserService Boolean validateUserFromKey( @PathParam( "key" ) String key ) throws RedbackServiceException; - @Path( "resetPassword/{user}" ) - @GET + @Path( "resetPassword" ) + @POST @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } ) + @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @RedbackAuthorization( noRestriction = true, noPermission = true ) /** * * @param user username for send a password reset email * @since 1.4 */ - Boolean resetPassword( @PathParam( "user" ) String user ) + Boolean resetPassword( ResetPasswordRequest resetPasswordRequest ) throws RedbackServiceException; @Path( "getUserPermissions/{userName}" ) diff --git a/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/DefaultUserService.java b/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/DefaultUserService.java index f967d6e3..a694a4f8 100644 --- a/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/DefaultUserService.java +++ b/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/DefaultUserService.java @@ -43,6 +43,7 @@ import org.apache.archiva.redback.rest.api.model.ErrorMessage; import org.apache.archiva.redback.rest.api.model.Operation; import org.apache.archiva.redback.rest.api.model.Permission; import org.apache.archiva.redback.rest.api.model.RegistrationKey; +import org.apache.archiva.redback.rest.api.model.ResetPasswordRequest; import org.apache.archiva.redback.rest.api.model.Resource; import org.apache.archiva.redback.rest.api.model.User; import org.apache.archiva.redback.rest.api.model.UserRegistrationRequest; @@ -501,9 +502,10 @@ public class DefaultUserService return Boolean.FALSE; } - public Boolean resetPassword( String username ) + public Boolean resetPassword( ResetPasswordRequest resetPasswordRequest ) throws RedbackServiceException { + String username = resetPasswordRequest.getUsername(); if ( StringUtils.isEmpty( username ) ) { throw new RedbackServiceException( new ErrorMessage( "username.cannot.be.empty" ) ); @@ -520,8 +522,13 @@ public class DefaultUserService AuthenticationKey authkey = keyManager.createKey( username, "Password Reset Request", policy.getUserValidationSettings().getEmailValidationTimeout() ); - mailer.sendPasswordResetEmail( Arrays.asList( user.getEmail() ), authkey, getBaseUrl() ); + String applicationUrl = resetPasswordRequest.getApplicationUrl(); + if ( StringUtils.isBlank( applicationUrl ) ) + { + applicationUrl = getBaseUrl(); + } + mailer.sendPasswordResetEmail( Arrays.asList( user.getEmail() ), authkey, applicationUrl ); log.info( "password reset request for username {}", username ); } catch ( UserNotFoundException e ) diff --git a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/UserServiceTest.java b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/UserServiceTest.java index 41c8fcde..dbb17eb6 100644 --- a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/UserServiceTest.java +++ b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/UserServiceTest.java @@ -21,6 +21,7 @@ package org.apache.archiva.redback.rest.services; import org.apache.archiva.redback.rest.api.model.Operation; import org.apache.archiva.redback.rest.api.model.Permission; +import org.apache.archiva.redback.rest.api.model.ResetPasswordRequest; import org.apache.archiva.redback.rest.api.model.User; import org.apache.archiva.redback.rest.api.model.UserRegistrationRequest; import org.apache.archiva.redback.rest.api.services.UserService; @@ -283,14 +284,16 @@ public class UserServiceTest assertTrue( service.validateUserFromKey( key ) ); - assertTrue( service.resetPassword( "toto" ) ); + assertTrue( service.resetPassword( new ResetPasswordRequest( "toto", "http://foo.fr/bar" ) ) ); emailMessages = assertService.getEmailMessageSended(); assertEquals( 2, emailMessages.size() ); assertEquals( "toto@toto.fr", emailMessages.get( 1 ).getTos().get( 0 ) ); - assertTrue( emailMessages.get( 1 ).getText().contains( "Password Reset" ) ); - assertTrue( emailMessages.get( 1 ).getText().contains( "Username: toto" ) ); + String messageContent = emailMessages.get( 1 ).getText(); + + assertThat( messageContent ).contains( "Password Reset" ).contains( "Username: toto" ).contains( + "http://foo.fr/bar" ); }