-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #279 from kbss-cvut/development
[3.1.2] Release
- Loading branch information
Showing
33 changed files
with
1,029 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/cz/cvut/kbss/termit/dto/PasswordChangeDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package cz.cvut.kbss.termit.dto; | ||
|
||
import cz.cvut.kbss.jopa.model.annotations.Id; | ||
import cz.cvut.kbss.jopa.model.annotations.OWLClass; | ||
import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty; | ||
import cz.cvut.kbss.jopa.vocabulary.DC; | ||
import cz.cvut.kbss.termit.util.Vocabulary; | ||
|
||
import java.io.Serializable; | ||
import java.net.URI; | ||
|
||
@OWLClass(iri = Vocabulary.ONTOLOGY_IRI_TERMIT + "/password-change") | ||
public class PasswordChangeDto implements Serializable { | ||
@Id | ||
private URI uri; | ||
|
||
@OWLDataProperty(iri = DC.Terms.IDENTIFIER) | ||
private String token; | ||
|
||
@OWLDataProperty(iri = Vocabulary.s_p_ma_heslo) | ||
private String newPassword; | ||
|
||
public URI getUri() { | ||
return uri; | ||
} | ||
|
||
public void setUri(URI uri) { | ||
this.uri = uri; | ||
} | ||
|
||
public String getToken() { | ||
return token; | ||
} | ||
|
||
public void setToken(String token) { | ||
this.token = token; | ||
} | ||
|
||
public String getNewPassword() { | ||
return newPassword; | ||
} | ||
|
||
public void setNewPassword(String newPassword) { | ||
this.newPassword = newPassword; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/cz/cvut/kbss/termit/dto/PasswordChangeRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package cz.cvut.kbss.termit.dto; | ||
|
||
import cz.cvut.kbss.termit.model.UserAccount; | ||
import cz.cvut.kbss.termit.model.util.HasIdentifier; | ||
|
||
import java.io.Serializable; | ||
import java.net.URI; | ||
import java.time.Instant; | ||
|
||
public class PasswordChangeRequestDto implements Serializable, HasIdentifier { | ||
private URI uri; | ||
|
||
/** | ||
* Associated user account. | ||
*/ | ||
private UserAccount userAccount; | ||
|
||
/** | ||
* Token value. | ||
*/ | ||
private String token; | ||
|
||
/** | ||
* Token creation timestamp. | ||
*/ | ||
private Instant createdAt; | ||
|
||
@Override | ||
public URI getUri() { | ||
return uri; | ||
} | ||
|
||
@Override | ||
public void setUri(URI uri) { | ||
this.uri = uri; | ||
} | ||
|
||
public UserAccount getUserAccount() { | ||
return userAccount; | ||
} | ||
|
||
public void setUserAccount(UserAccount userAccount) { | ||
this.userAccount = userAccount; | ||
} | ||
|
||
public String getToken() { | ||
return token; | ||
} | ||
|
||
public void setToken(String token) { | ||
this.token = token; | ||
} | ||
|
||
public Instant getCreatedAt() { | ||
return createdAt; | ||
} | ||
|
||
public void setCreatedAt(Instant createdAt) { | ||
this.createdAt = createdAt; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/cz/cvut/kbss/termit/exception/InvalidPasswordChangeRequestException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package cz.cvut.kbss.termit.exception; | ||
|
||
public class InvalidPasswordChangeRequestException extends AuthorizationException { | ||
|
||
private final String messageId; | ||
public InvalidPasswordChangeRequestException(String message, String messageId) { | ||
super(message); | ||
this.messageId = messageId; | ||
} | ||
|
||
public String getMessageId() { | ||
return messageId; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/cz/cvut/kbss/termit/model/PasswordChangeRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package cz.cvut.kbss.termit.model; | ||
|
||
import cz.cvut.kbss.jopa.model.annotations.OWLClass; | ||
import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty; | ||
import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty; | ||
import cz.cvut.kbss.jopa.model.annotations.ParticipationConstraints; | ||
import cz.cvut.kbss.jopa.vocabulary.DC; | ||
import cz.cvut.kbss.termit.util.Vocabulary; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
import java.time.Instant; | ||
|
||
@OWLClass(iri = Vocabulary.s_c_pozadavek_na_zmenu_hesla) | ||
public class PasswordChangeRequest extends AbstractEntity { | ||
/** | ||
* Associated user account. | ||
*/ | ||
@NotNull | ||
@ParticipationConstraints(nonEmpty = true) | ||
@OWLObjectProperty(iri = DC.Terms.SUBJECT) | ||
private UserAccount userAccount; | ||
|
||
/** | ||
* Token value. | ||
*/ | ||
@NotNull | ||
@ParticipationConstraints(nonEmpty = true) | ||
@OWLDataProperty(iri = DC.Terms.IDENTIFIER, simpleLiteral = true) | ||
private String token; | ||
|
||
/** | ||
* Token creation timestamp. | ||
*/ | ||
@NotNull | ||
@ParticipationConstraints(nonEmpty = true) | ||
@OWLDataProperty(iri = DC.Terms.CREATED) | ||
private Instant createdAt; | ||
|
||
public UserAccount getUserAccount() { | ||
return userAccount; | ||
} | ||
|
||
public void setUserAccount(UserAccount userAccount) { | ||
this.userAccount = userAccount; | ||
} | ||
|
||
public String getToken() { | ||
return token; | ||
} | ||
|
||
public void setToken(String token) { | ||
this.token = token; | ||
} | ||
|
||
public Instant getCreatedAt() { | ||
return createdAt; | ||
} | ||
|
||
public void setCreatedAt(Instant createdAt) { | ||
this.createdAt = createdAt; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/cz/cvut/kbss/termit/persistence/dao/PasswordChangeRequestDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cz.cvut.kbss.termit.persistence.dao; | ||
|
||
import cz.cvut.kbss.jopa.model.EntityManager; | ||
import cz.cvut.kbss.termit.exception.PersistenceException; | ||
import cz.cvut.kbss.termit.model.PasswordChangeRequest; | ||
import cz.cvut.kbss.termit.util.Configuration; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
@Repository | ||
public class PasswordChangeRequestDao extends BaseDao<PasswordChangeRequest> { | ||
|
||
private final Configuration.Persistence persistenceConfig; | ||
|
||
@Autowired | ||
public PasswordChangeRequestDao(EntityManager em, Configuration configuration) { | ||
super(PasswordChangeRequest.class, em); | ||
this.persistenceConfig = configuration.getPersistence(); | ||
} | ||
|
||
public List<PasswordChangeRequest> findAllByUsername(String username) { | ||
Objects.requireNonNull(username); | ||
try { | ||
return em.createQuery("SELECT DISTINCT t FROM " + type.getSimpleName() + " t WHERE t.userAccount.username = :username", type) | ||
.setParameter("username", username, persistenceConfig.getLanguage()) | ||
.getResultList(); | ||
} catch (RuntimeException e) { | ||
throw new PersistenceException(e); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/cz/cvut/kbss/termit/rest/PasswordChangeController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package cz.cvut.kbss.termit.rest; | ||
|
||
import cz.cvut.kbss.jsonld.JsonLd; | ||
import cz.cvut.kbss.termit.dto.PasswordChangeDto; | ||
import cz.cvut.kbss.termit.service.business.PasswordChangeService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
|
||
@ConditionalOnProperty(prefix = "termit.security", name = "provider", havingValue = "internal", matchIfMissing = true) | ||
@Tag(name = "Password reset", description = "User forgotten password reset API") | ||
@RestController | ||
@RequestMapping("/password") | ||
public class PasswordChangeController { | ||
private static final Logger LOG = LoggerFactory.getLogger(PasswordChangeController.class); | ||
|
||
private final PasswordChangeService passwordChangeService; | ||
|
||
@Autowired | ||
public PasswordChangeController(PasswordChangeService passwordChangeService) { | ||
this.passwordChangeService = passwordChangeService; | ||
} | ||
|
||
@Operation(description = "Requests a password reset for the specified username.") | ||
@ApiResponses({ | ||
@ApiResponse(responseCode = "204", description = "Password reset request accepted, email sent"), | ||
@ApiResponse(responseCode = "404", description = "User with the specified username not found.") | ||
}) | ||
@PreAuthorize("permitAll()") | ||
@PostMapping(consumes = {MediaType.TEXT_PLAIN_VALUE}) | ||
public ResponseEntity<Void> requestPasswordReset( | ||
@Parameter(description = "Username of the user") @RequestBody String username) { | ||
LOG.info("Password reset requested for user {}.", username); | ||
passwordChangeService.requestPasswordReset(username); | ||
return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
} | ||
|
||
@Operation(description = "Changes the password for the specified user.") | ||
@ApiResponses({ | ||
@ApiResponse(responseCode = "204", description = "Password changed"), | ||
@ApiResponse(responseCode = "409", description = "Invalid or expired token") | ||
}) | ||
@PreAuthorize("permitAll()") | ||
@PutMapping(consumes = {MediaType.APPLICATION_JSON_VALUE, JsonLd.MEDIA_TYPE}) | ||
public ResponseEntity<Void> changePassword( | ||
@Parameter( | ||
description = "Token with URI for password reset") @RequestBody PasswordChangeDto passwordChangeDto) { | ||
LOG.info("Password change requested with token {}", passwordChangeDto.getToken()); | ||
passwordChangeService.changePassword(passwordChangeDto); | ||
return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.