Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master3' into master2
Browse files Browse the repository at this point in the history
# Conflicts:
#	KariaMain/src/main/java/com/jhipster/demo/store/domain/KariaUser.java
#	KariaMain/src/main/java/com/jhipster/demo/store/repository/KariaUserSqlHelper.java
#	KariaMain/src/main/java/com/jhipster/demo/store/repository/rowmapper/KariaUserRowMapper.java
#	KariaMain/src/main/java/com/jhipster/demo/store/service/UserService.java
#	KariaMain/src/test/java/com/jhipster/demo/store/web/rest/KariaUserResourceIT.java
  • Loading branch information
MoetezWelhazi committed Apr 21, 2024
2 parents dd61b76 + d79a448 commit baf71d9
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 7 deletions.
5 changes: 5 additions & 0 deletions KariaMain/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.twilio.sdk</groupId>
<artifactId>twilio</artifactId>
<version>8.8.0</version>
</dependency>
<dependency>
<groupId>tech.jhipster</groupId>
<artifactId>jhipster-framework</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http)
.pathMatchers("/api/authenticate").permitAll()
.pathMatchers("/api/register").permitAll()
.pathMatchers("/api/activate").permitAll()
.pathMatchers("/api/forgot-password").permitAll()
.pathMatchers("/api/validate-code").permitAll()
.pathMatchers("/api/reset-password").permitAll()
.pathMatchers("/api/account/reset-password/init").permitAll()
.pathMatchers("/api/account/reset-password/finish").permitAll()
.pathMatchers("/api/admin/**").hasAuthority(AuthoritiesConstants.ADMIN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ interface KariaUserRepositoryInternal {

Mono<KariaUser> findOneWithEagerRelationships(Long id);

Mono<KariaUser> findByPhone(String phone);
Flux<KariaUser> findAllWithEagerRelationships();

Flux<KariaUser> findAllWithEagerRelationships(Pageable page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ public Mono<KariaUser> findById(Long id) {
Comparison whereClause = Conditions.isEqual(entityTable.column("id"), Conditions.just(id.toString()));
return createQuery(null, whereClause).one();
}

public Mono<KariaUser> findByPhone(String phone) {
Comparison whereClause = Conditions.isEqual(entityTable.column("phone"), Conditions.just(phone));
return createQuery(null, whereClause).one();
}
@Override
public Mono<KariaUser> findOneWithEagerRelationships(Long id) {
return findById(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public static List<Expression> getColumns(Table table, String columnPrefix) {
columns.add(Column.aliased("address_line_2", table, columnPrefix + "_address_line_2"));
columns.add(Column.aliased("city", table, columnPrefix + "_city"));
columns.add(Column.aliased("role", table, columnPrefix + "_role"));

columns.add(Column.aliased("user_id", table, columnPrefix + "_user_id"));
return columns;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public Mono<KariaUser> update(KariaUser kariaUser) {
return kariaUserRepository.save(kariaUser);
}


/**
* Partially update a kariaUser.
*
Expand Down Expand Up @@ -133,7 +134,11 @@ public Mono<KariaUser> findOne(Long id) {
log.debug("Request to get KariaUser : {}", id);
return kariaUserRepository.findOneWithEagerRelationships(id);
}

@Transactional(readOnly = true)
public Mono<KariaUser> findOneByPhone(String phone) {
log.debug("Request to get KariaUser : {}", phone);
return kariaUserRepository.findByPhone(phone);
}
/**
* Delete the kariaUser by id.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@
import com.jhipster.demo.store.domain.enumeration.Gender;
import com.jhipster.demo.store.domain.enumeration.RoleEnum;
import com.jhipster.demo.store.repository.AuthorityRepository;
import com.jhipster.demo.store.repository.KariaUserRepository;
import com.jhipster.demo.store.repository.UserRepository;
import com.jhipster.demo.store.security.AuthoritiesConstants;
import com.jhipster.demo.store.security.SecurityUtils;
import com.jhipster.demo.store.service.dto.AdminUserDTO;
import com.jhipster.demo.store.service.dto.PhoneVerification;
import com.jhipster.demo.store.service.dto.UserDTO;

import java.security.SecureRandom;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
Expand All @@ -35,8 +42,11 @@
*/
@Service
public class UserService {

private final HashMap<String, PhoneVerification> phoneCodes = new HashMap<>();
private final Logger log = LoggerFactory.getLogger(UserService.class);
private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
private static final int CODE_LENGTH = 4;

private final UserRepository userRepository;

Expand Down Expand Up @@ -97,6 +107,69 @@ public Mono<User> requestPasswordReset(String mail) {
.flatMap(this::saveUser);
}

private boolean checkPhoneNumber(String phoneNumber){
for (int digit = 0 ; digit < phoneNumber.length(); digit++){
if (phoneNumber.charAt(digit)< '0' || phoneNumber.charAt(digit) > '9')
return false;
}
return true;
}
@Transactional
public Mono<Boolean> sendMessage(String phoneNumber) {
return kariaUserService.findOneByPhone(phoneNumber).switchIfEmpty(Mono.empty()).flatMap(user ->{

if (user.getPhone().equals(phoneNumber) && phoneNumber.length() == 8 && checkPhoneNumber(phoneNumber)) {
Twilio.init(System.getenv("TWILIO_ACCOUNT_SID"), System.getenv("TWILIO_AUTH_TOKEN"));
StringBuilder codeBuilder = new StringBuilder();
for (int i = 0; i < CODE_LENGTH; i++) {
int randomIndex = SECURE_RANDOM.nextInt(CHARACTERS.length());
codeBuilder.append(CHARACTERS.charAt(randomIndex));
}
String code = codeBuilder.toString();
phoneCodes.put(user.getPhone(),new PhoneVerification(code));
Message.creator(new PhoneNumber("+216"+phoneNumber),
new PhoneNumber("+16205518972"), "Your code is :" + code).create();
return Mono.just(true);
}
return Mono.just(false);
});
}
@Transactional
public Mono<Boolean> resetPassword(String phoneNumber,String password) {
AtomicBoolean isVerified = new AtomicBoolean(false);
return kariaUserService
.findOneByPhone(phoneNumber)
.flatMap(kariaUser -> {
if (phoneCodes.get(kariaUser.getPhone()).getVerified()){
isVerified.set(true);
}

return Mono.just(kariaUser);
})
.flatMap(kariaUser -> {
log.debug(kariaUser.getUserId().toString());
return userRepository.findById(kariaUser.getUserId());})
.map(user -> {

if (isVerified.get()) {
String encodedPassword = passwordEncoder.encode(user.getPassword());
user.setPassword(password);
phoneCodes.remove(phoneNumber);
}
return user;
})
.flatMap(this::saveUser)
.flatMap(user ->{
return Mono.just(isVerified.get());
});
}
public boolean checkCode( String code,String phoneNumber) {
if(phoneCodes.get(phoneNumber) != null && code.equals(phoneCodes.get(phoneNumber).getCode())){
phoneCodes.get(phoneNumber).setVerified(true);
return true;
}
return false;
}
@Transactional
public Mono<User> registerUser(AdminUserDTO userDTO, String password, String phoneNumber) {
KariaUser kariaUser = new KariaUser();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.jhipster.demo.store.service.dto;

import java.io.Serializable;

public class CodeDTO implements Serializable {
private static final long serialVersionUID = 1L;
private String code;
private String phoneNumber;

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public CodeDTO(String code, String phoneNumber) {
this.code = code;
this.phoneNumber = phoneNumber;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.jhipster.demo.store.service.dto;

public class MessageDTO {
private String message;

private int status;
public String getMessage() {
return message;
}
public int getStatus(){
return status;
}

public void setMessage(String message) {
this.message = message;
}
private void setStatus(int status){
this.status = status;
}
public MessageDTO(String message,int status) {
this.message = message;
this.status = status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.jhipster.demo.store.service.dto;

import java.io.Serializable;

public class PasswordDTO implements Serializable {
private String password;
private String phoneNumber;

public PasswordDTO(String password, String phoneNumber) {
this.password = password;
this.phoneNumber = phoneNumber;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.jhipster.demo.store.service.dto;

import java.io.Serializable;

public class PhoneNumberDTO implements Serializable {

private static final long serialVersionUID = 1L;

private String phoneNumber;

public PhoneNumberDTO() {
// Empty constructor needed for Jackson.
}

public PhoneNumberDTO(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public String getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.jhipster.demo.store.service.dto;

import java.io.Serializable;

public class PhoneVerification implements Serializable {
private String code;
private boolean verified = false;

public PhoneVerification(String code) {
this.code = code;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}
public boolean getVerified(){
return this.verified;
}
public void setVerified(boolean verified){
this.verified = verified;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package com.jhipster.demo.store.web.rest;

import com.jhipster.demo.store.domain.User;
import com.jhipster.demo.store.repository.UserRepository;
import com.jhipster.demo.store.security.SecurityUtils;
import com.jhipster.demo.store.service.MailService;
import com.jhipster.demo.store.service.UserService;
import com.jhipster.demo.store.service.dto.AdminUserDTO;
import com.jhipster.demo.store.service.dto.PasswordChangeDTO;
import com.jhipster.demo.store.service.dto.*;
import com.jhipster.demo.store.web.rest.errors.*;
import com.jhipster.demo.store.web.rest.vm.KeyAndPasswordVM;
import com.jhipster.demo.store.web.rest.vm.ManagedUserVM;
import jakarta.mail.Message;
import jakarta.validation.Valid;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

Expand Down Expand Up @@ -140,7 +142,28 @@ public Mono<Void> changePassword(@RequestBody PasswordChangeDTO passwordChangeDt
}
return userService.changePassword(passwordChangeDto.getCurrentPassword(), passwordChangeDto.getNewPassword());
}
@PostMapping(path = "/reset-password")
public Mono<ResponseEntity<MessageDTO>> changePassword(@RequestBody PasswordDTO passwordDTO) {
if (isPasswordLengthInvalid(passwordDTO.getPassword())) {
throw new InvalidPasswordException();
}
return userService.resetPassword(passwordDTO.getPhoneNumber(),passwordDTO.getPassword())
.map(bool -> ResponseEntity.status(bool ? 200 : 400)
.body(new MessageDTO (bool ? "Password reset successfully" : "error while resetting password",bool ? 200 : 400)));
}
@PostMapping(path = "/forgot-password")
public Mono<ResponseEntity<MessageDTO>> forgotPassword(@RequestBody PhoneNumberDTO phoneNumberDTO) {
return userService.sendMessage(phoneNumberDTO.getPhoneNumber())
.thenReturn("Password reset instructions sent to " + phoneNumberDTO.getPhoneNumber())
.map(message -> ResponseEntity.ok().body(new MessageDTO(message,200)))
.defaultIfEmpty(ResponseEntity.badRequest().body(new MessageDTO("Failed to send password reset instructions",400)));
}
@PostMapping(path = "/validate-code")
public ResponseEntity<MessageDTO> validateCode(@RequestBody CodeDTO codeDTO) {

boolean bool = userService.checkCode(codeDTO.getCode(),codeDTO.getPhoneNumber());
return ResponseEntity.status(bool ? 200 : 400).body(new MessageDTO(bool ? "Code is correct !" : "Invalid code!",bool ? 200 : 400));
}
/**
* {@code POST /account/reset-password/init} : Send an email to reset the password of the user.
*
Expand Down

0 comments on commit baf71d9

Please sign in to comment.