Skip to content

Commit

Permalink
added first login logic
Browse files Browse the repository at this point in the history
  • Loading branch information
SundarakrishnanN committed Jun 12, 2024
1 parent 243dfe2 commit 8d37d67
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.ieeervce.gatekeeper.controller;

import org.ieeervce.gatekeeper.dto.User.UserResponseDTO;
import org.ieeervce.gatekeeper.exception.IncorrectPasswordException;
import org.ieeervce.gatekeeper.exception.InvalidDataException;
import org.ieeervce.gatekeeper.exception.ItemNotFoundException;
import org.ieeervce.gatekeeper.dto.User.UserDTO;
Expand All @@ -12,6 +13,7 @@
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.ieeervce.gatekeeper.service.UserService;
Expand Down Expand Up @@ -78,5 +80,15 @@ public UserResponseDTO getUserByEmail(@PathVariable String email) throws ItemNot
.map(user->modelMapper.map(user, UserResponseDTO.class))
.orElseThrow(()->new ItemNotFoundException("User Not Found"));
}
@PutMapping()
public ResponseEntity userPasswordUpdate(String currentPassword, String newPassword) throws IncorrectPasswordException
{
User user= userService.getUserByEmail(getRequesterDetails()).get();
if(!user.getPassword().equals(passwordEncoder.encode(currentPassword)))
throw new IncorrectPasswordException("Wrong password");
userService.updatePassword(user,passwordEncoder.encode(newPassword));

return (ResponseEntity) ResponseEntity.ok();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class UserResponseDTO {
private boolean unsubscribed;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private boolean firstLogin;
private List<RequestDTO> approvedRequests;
private List<RequestDTO> pendingRequests;
private List<RequestDTO> rejectedRequests;
Expand Down Expand Up @@ -112,6 +113,14 @@ public List<RequestDTO> getRejectedRequests() {
public void setRejectedRequests(List<RequestDTO> rejectedRequests) {
this.rejectedRequests = rejectedRequests;
}

public boolean isFirstLogin() {
return firstLogin;
}

public void setFirstLogin(boolean firstLogin) {
this.firstLogin = firstLogin;
}
}


11 changes: 11 additions & 0 deletions src/main/java/org/ieeervce/gatekeeper/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public class User {
@Column(nullable = false)
private boolean enabled;

@Column(nullable = false)
private boolean firstLogin=true;

@ManyToMany
@JoinTable(
name = "pendingRequests",
Expand Down Expand Up @@ -184,4 +187,12 @@ public void setSociety(Society society){
public void setRole(Role role){
this.role= role;
}

public boolean isFirstLogin() {
return firstLogin;
}

public void setFirstLogin(boolean firstLogin) {
this.firstLogin = firstLogin;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.ieeervce.gatekeeper.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.BAD_REQUEST)
public class IncorrectPasswordException extends Exception{
public IncorrectPasswordException(String message)
{
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,9 @@ public void removePendingRequests(RequestForm requestForm, List<Role> requestHie
}
}
}

public void updatePassword(User user, String newEncodedPassword) {
user.setPassword(newEncodedPassword);
user.setFirstLogin(false);
}
}

0 comments on commit 8d37d67

Please sign in to comment.