-
Notifications
You must be signed in to change notification settings - Fork 1
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 #31 from szymonpoltorak/roles
Roles
- Loading branch information
Showing
27 changed files
with
558 additions
and
22 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
social-app-backend/src/main/java/razepl/dev/socialappbackend/admin/AdminController.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,52 @@ | ||
package razepl.dev.socialappbackend.admin; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.*; | ||
import razepl.dev.socialappbackend.admin.interfaces.IAdminController; | ||
import razepl.dev.socialappbackend.admin.interfaces.IAdminService; | ||
import razepl.dev.socialappbackend.auth.apicalls.AuthResponse; | ||
import razepl.dev.socialappbackend.auth.apicalls.RegisterRequest; | ||
import razepl.dev.socialappbackend.auth.interfaces.AuthInterface; | ||
import razepl.dev.socialappbackend.home.data.UserData; | ||
|
||
import java.util.List; | ||
|
||
import static razepl.dev.socialappbackend.admin.Constants.ADMIN_MAPPING; | ||
import static razepl.dev.socialappbackend.admin.Constants.ENDPOINT_MATCHER; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping(value = ADMIN_MAPPING) | ||
public class AdminController implements IAdminController { | ||
private final IAdminService adminService; | ||
private final AuthInterface authInterface; | ||
|
||
@Override | ||
@GetMapping(value = ENDPOINT_MATCHER) | ||
public final List<UserData> getUsersList() { | ||
return adminService.getUsersList(); | ||
} | ||
|
||
@Override | ||
@DeleteMapping(value = ENDPOINT_MATCHER) | ||
@ResponseStatus(value = HttpStatus.NO_CONTENT) | ||
public final void deleteUser(@RequestParam String email) { | ||
adminService.deleteUser(email); | ||
} | ||
|
||
@Override | ||
@PatchMapping(value = ENDPOINT_MATCHER) | ||
public final void updateUsersName(@RequestParam String email, @RequestParam String newName) { | ||
adminService.updateUsersName(email, newName); | ||
} | ||
|
||
@Override | ||
@PostMapping(value = ENDPOINT_MATCHER) | ||
@ResponseStatus(value = HttpStatus.CREATED) | ||
public final AuthResponse createUser(@RequestBody RegisterRequest registerRequest) { | ||
return authInterface.registerUser(registerRequest); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
social-app-backend/src/main/java/razepl/dev/socialappbackend/admin/AdminService.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,51 @@ | ||
package razepl.dev.socialappbackend.admin; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import razepl.dev.socialappbackend.admin.interfaces.IAdminService; | ||
import razepl.dev.socialappbackend.entities.user.interfaces.UserRepository; | ||
import razepl.dev.socialappbackend.home.data.UserData; | ||
import razepl.dev.socialappbackend.home.interfaces.DataServiceInterface; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class AdminService implements IAdminService { | ||
private final UserRepository userRepository; | ||
private final DataServiceInterface dataService; | ||
|
||
@Override | ||
public final List<UserData> getUsersList() { | ||
log.info("Getting users list"); | ||
|
||
return userRepository | ||
.findAll() | ||
.stream() | ||
.map(dataService::buildUserData) | ||
.toList(); | ||
} | ||
|
||
@Override | ||
public final void deleteUser(String email) { | ||
log.info("Deleting user with email: {}", email); | ||
|
||
userRepository.deleteByEmail(email); | ||
} | ||
|
||
@Override | ||
public final void updateUsersName(String email, String newName) { | ||
log.info("Updating user with email: {}", email); | ||
log.info("New name: {}", newName); | ||
|
||
userRepository | ||
.findByEmail(email) | ||
.ifPresent(user -> { | ||
user.setName(newName); | ||
|
||
userRepository.save(user); | ||
}); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
social-app-backend/src/main/java/razepl/dev/socialappbackend/admin/Constants.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,9 @@ | ||
package razepl.dev.socialappbackend.admin; | ||
|
||
final class Constants { | ||
static final String ENDPOINT_MATCHER = "/users"; | ||
static final String ADMIN_MAPPING = "/api/admin"; | ||
|
||
private Constants() { | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...-backend/src/main/java/razepl/dev/socialappbackend/admin/interfaces/IAdminController.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,42 @@ | ||
package razepl.dev.socialappbackend.admin.interfaces; | ||
|
||
import razepl.dev.socialappbackend.auth.apicalls.AuthResponse; | ||
import razepl.dev.socialappbackend.auth.apicalls.RegisterRequest; | ||
import razepl.dev.socialappbackend.home.data.UserData; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Represents the controller interface for admin-related operations. | ||
*/ | ||
public interface IAdminController { | ||
/** | ||
* Retrieves a list of user data for all users. | ||
* | ||
* @return The list of user data. | ||
*/ | ||
List<UserData> getUsersList(); | ||
|
||
/** | ||
* Deletes a user with the specified email. | ||
* | ||
* @param email - The email of the user to delete. | ||
*/ | ||
void deleteUser(String email); | ||
|
||
/** | ||
* Updates the name of a user with the specified email. | ||
* | ||
* @param email - The email of the user to update. | ||
* @param newName - The new name for the user. | ||
*/ | ||
void updateUsersName(String email, String newName); | ||
|
||
/** | ||
* Creates a new user based on the provided registration request. | ||
* | ||
* @param registerRequest - The registration request containing user details. | ||
* @return The authentication response for the created user. | ||
*/ | ||
AuthResponse createUser(RegisterRequest registerRequest); | ||
} |
32 changes: 32 additions & 0 deletions
32
...app-backend/src/main/java/razepl/dev/socialappbackend/admin/interfaces/IAdminService.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,32 @@ | ||
package razepl.dev.socialappbackend.admin.interfaces; | ||
|
||
import razepl.dev.socialappbackend.home.data.UserData; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Represents the service interface for admin-related operations. | ||
*/ | ||
public interface IAdminService { | ||
/** | ||
* Retrieves a list of user data for all users. | ||
* | ||
* @return The list of user data. | ||
*/ | ||
List<UserData> getUsersList(); | ||
|
||
/** | ||
* Deletes a user with the specified email. | ||
* | ||
* @param email - The email of the user to delete. | ||
*/ | ||
void deleteUser(String email); | ||
|
||
/** | ||
* Updates the name of a user with the specified email. | ||
* | ||
* @param email - The email of the user to update. | ||
* @param newName - The new name for the user. | ||
*/ | ||
void updateUsersName(String email, String newName); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package razepl.dev.socialappbackend.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
|
@@ -14,10 +15,13 @@ | |
import org.springframework.web.cors.CorsConfiguration; | ||
import org.springframework.web.cors.CorsConfigurationSource; | ||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | ||
import razepl.dev.socialappbackend.config.enums.Role; | ||
import razepl.dev.socialappbackend.config.interfaces.AppConfigInterface; | ||
import razepl.dev.socialappbackend.entities.user.User; | ||
import razepl.dev.socialappbackend.entities.user.interfaces.UserRepository; | ||
import razepl.dev.socialappbackend.exceptions.AuthManagerInstanceException; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
import static razepl.dev.socialappbackend.config.constants.CorsConfig.*; | ||
|
@@ -81,4 +85,32 @@ public AuthenticationManager authenticationManager(AuthenticationConfiguration c | |
throw new AuthManagerInstanceException("Could not create authManager bean!"); | ||
} | ||
} | ||
|
||
@Bean | ||
@Override | ||
public CommandLineRunner commandLineRunner() { | ||
return args -> { | ||
User admin = User | ||
.builder() | ||
.name("Admin") | ||
.surname("admin") | ||
.role(Role.ADMIN) | ||
.email("[email protected]") | ||
.password(passwordEncoder().encode("Ab!#$123zncA")) | ||
.dateOfBirth(LocalDate.parse("2000-01-01")) | ||
.build(); | ||
userRepository.save(admin); | ||
|
||
User moderator = User | ||
.builder() | ||
.name("moderator") | ||
.surname("moderator") | ||
.role(Role.MODERATOR) | ||
.email("[email protected]") | ||
.password(passwordEncoder().encode("Ab!#$789asdhjaksA")) | ||
.dateOfBirth(LocalDate.parse("2000-01-01")) | ||
.build(); | ||
userRepository.save(moderator); | ||
}; | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
social-app-backend/src/main/java/razepl/dev/socialappbackend/config/enums/Permissions.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,19 @@ | ||
package razepl.dev.socialappbackend.config.enums; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum Permissions { | ||
ADMIN_READ("admin:read"), | ||
ADMIN_WRITE("admin:write"), | ||
ADMIN_DELETE("admin:delete"), | ||
ADMIN_UPDATE("admin:update"), | ||
MODERATOR_READ("moderator:read"), | ||
MODERATOR_WRITE("moderator:write"), | ||
MODERATOR_DELETE("moderator:delete"), | ||
MODERATOR_UPDATE("moderator:update"); | ||
|
||
private final String permission; | ||
} |
Oops, something went wrong.