Skip to content

Commit

Permalink
Merge pull request #48 from MiguelFerreira18/asvs-new-feats
Browse files Browse the repository at this point in the history
Asvs new feats
  • Loading branch information
MiguelFerreira18 authored May 17, 2024
2 parents d932956 + b7a0218 commit 02dde71
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 6 deletions.
Binary file modified Deliverables/v4-ASVS-checklist-en.xlsx
Binary file not shown.
30 changes: 30 additions & 0 deletions database_backups_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

if [ -f .env ]; then
source .env
fi

DATE=$(date +%F-%H%M%S)

# Backup directory on the host
BACKUP_DIR="/var/backups/database_backups"

# Database credentials and details
DB_HOST="${DESOFS_DB_HOST}" #name of the mysql container
DB_USER="${DESOFS_DB_USER}"
DB_PASSWORD="${DESOFS_DB_PASS}"
DB_NAME="${DESOFS_DB_DEV_DB_NAME}"
NETWORK="cozinha_na_cozinha" #name of the network where mysql container is running. You can check the list of the docker neworks using doocker network ls

# Docker image version of MySQL
MYSQL_IMAGE="mysql:latest"

# Backup filename
BACKUP_FILENAME="$BACKUP_DIR/$DB_NAME-$DATE.sql"

# Run mysqldump within a new Docker container
docker run --rm --network $NETWORK $MYSQL_IMAGE \\
/usr/bin/mysqldump -h $DB_HOST -u $DB_USER -p$DB_PASSWORD $DB_NAME > $BACKUP_FILENAME

# Compress the backup file
gzip $BACKUP_FILENAME
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@

import isep.ipp.pt.api.desofs.Dto.UserDTO.ControllerLayer.UserDTOResponse;
import isep.ipp.pt.api.desofs.Mapper.UserMapper.UserMapper;
import isep.ipp.pt.api.desofs.Model.UserModel.SignInRequest;
import isep.ipp.pt.api.desofs.Model.UserModel.User;
import isep.ipp.pt.api.desofs.Service.UserService.UserService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;

@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserMapper userMapper;

Expand All @@ -26,4 +29,13 @@ public ResponseEntity<UserDTOResponse> getUserInfo(@PathVariable Long userId){
if(userId < 0) return ResponseEntity.badRequest().build();
return ResponseEntity.ok(userMapper.fromUserToUserDTOResponse(userService.getUserById(userId)));
}
@DeleteMapping("/delete/data")
public ResponseEntity deleteUser(@RequestBody @Valid final SignInRequest request){
final Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(request.username(), request.password()));

if (authentication == null) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();

userService.deleteUser(request.username());
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ public List<Encomenda> findEncHistory(Long userId) {
return null;
}
}

@Override
public void deleteEncomendaByUserName(String userId) {
encomendaRepo.deleteEncomendaByUserName(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ public List<Review> getReviewsByUserId(Long userId) {
public void deleteAll() {
reviewRepo.deleteAll();
}

@Override
public void deleteReviewsByUserName(String username) {
reviewRepo.deleteReviewsByUserName(username);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import isep.ipp.pt.api.desofs.Model.UserModel.User;
import isep.ipp.pt.api.desofs.Repository.Interface.UserServiceRepo;
import isep.ipp.pt.api.desofs.Repository.Repo.EncomendaRepo;
import isep.ipp.pt.api.desofs.Repository.Repo.ReviewRepo;
import isep.ipp.pt.api.desofs.Repository.Repo.UserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
Expand All @@ -14,6 +16,10 @@ public class UserRepoImpl implements UserServiceRepo {

@Autowired
private UserRepo userRepo;
@Autowired
private ReviewRepo reviewRepo;
@Autowired
private EncomendaRepo encomendaRepo;

@Override
public User getUserById(Long userId) {
Expand Down Expand Up @@ -50,4 +56,11 @@ public User validateUser(String username, String password) {
return userRepo.validateUser(username, password);
}

@Override
public void deleteUser(String username) {
reviewRepo.deleteReviewsByUserName(username);
encomendaRepo.deleteEncomendaByUserName(username);
userRepo.deleteUser(username);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ public interface EncomendaServiceRepo {
void deleteAll();

List<Encomenda> findEncHistory(Long userId);
void deleteEncomendaByUserName(String userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public interface ReviewServiceRepo {
public List<Review> getReviewsByPacoteId(Long pacoteId);
public List<Review> getReviewsByUserId(Long userId);
public void deleteAll();
public void deleteReviewsByUserName(String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public interface UserServiceRepo {
public void deleteAll();
public User saveUser(User user);
public User validateUser(String username, String password);
public void deleteUser(String username);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ public interface EncomendaRepo extends CrudRepository<Encomenda, Long> {

@Query("SELECT e FROM Encomenda e WHERE e.user.userId = ?1 AND e.pacote.pacoteId = ?2 AND e.dataEncomenda = ?3")
Encomenda findByDateUserPackage(Long userId, Long pacoteId, LocalDateTime dataEncomenda);

@Query("DELETE FROM Encomenda e WHERE e.user.username = ?1")
void deleteEncomendaByUserName(String username);
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ public interface ReviewRepo extends CrudRepository<Review, Long>{
@Query("SELECT r FROM Review r WHERE r.user.userId = ?1")
public List<Review> getReviewsByUserId(Long userId);

@Query("DELETE FROM Review r WHERE r.user.username = ?1")
public void deleteReviewsByUserName(String username);


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ public interface UserRepo extends CrudRepository<User,Long> {

@Query("SELECT u FROM User u WHERE u.userId = ?1")
public User getUserById(Long userId);

@Query("DELETE FROM User u WHERE u.username = ?1")
public void deleteUser(String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.cors(cors -> cors.configurationSource(request -> {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOriginPatterns(Arrays.asList("*"));
config.setAllowedMethods(Arrays.asList("*"));
config.setAllowedMethods(Arrays.asList("GET", "POST", "DELETE", "OPTIONS"));
config.setAllowedHeaders(Arrays.asList("*"));
config.setExposedHeaders(Arrays.asList(HttpHeaders.AUTHORIZATION));
config.setAllowCredentials(true);
Expand All @@ -113,6 +113,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.requestMatchers("/encomenda/**").authenticated()
.requestMatchers("/user/info/**").authenticated()
.requestMatchers("/encomenda/**").authenticated()
.requestMatchers("/user/delete/data").authenticated()
.requestMatchers("/tipoReceita/**").authenticated()
.requestMatchers("/api-docs/**").permitAll()
.requestMatchers("/swagger-ui/**").permitAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ public interface UserService {
public void deleteAll();

public void saveUser(User user);
public void deleteUser(String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,9 @@ public void deleteAll() {
public void saveUser(User user) {
userRepo.saveUser(user);
}

@Override
public void deleteUser(String username) {
userRepo.deleteUser(username);
}
}

0 comments on commit 02dde71

Please sign in to comment.