From 2ea4b47b3f93508ae4429a5d529f55ca18d33a29 Mon Sep 17 00:00:00 2001 From: SamuelNguyen2121 <2032373@champlaincollege.qc.ca> Date: Mon, 16 Oct 2023 17:03:25 -0400 Subject: [PATCH 1/7] Create files for update role form --- .../auth/update-role-form/role-update.component.js | 0 .../auth/update-role-form/role-update.config.js | 0 .../auth/update-role-form/role-update.controller.js | 0 .../auth/update-role-form/role-update.template.html | 10 ++++++++++ 4 files changed, 10 insertions(+) create mode 100644 api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.component.js create mode 100644 api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.config.js create mode 100644 api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.controller.js create mode 100644 api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html diff --git a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.component.js b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.component.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.config.js b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.config.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.controller.js b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.controller.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html new file mode 100644 index 0000000000..de155ea51d --- /dev/null +++ b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html @@ -0,0 +1,10 @@ + + + + + Title + + + + + \ No newline at end of file From c09e6aeb38c53f7a27f13f4542926d7fb2c7108c Mon Sep 17 00:00:00 2001 From: SamuelNguyen2121 <2032373@champlaincollege.qc.ca> Date: Mon, 16 Oct 2023 23:30:31 -0400 Subject: [PATCH 2/7] Created update role button and unfinished update form --- api-gateway/src/main/resources/static/index.html | 6 ++++++ .../src/main/resources/static/scripts/app.js | 2 +- .../auth/admin-panel/admin-panel.template.html | 1 + .../update-role-form/role-update.component.js | 11 +++++++++++ .../auth/update-role-form/role-update.config.js | 16 ++++++++++++++++ .../update-role-form/role-update.controller.js | 12 ++++++++++++ .../auth/update-role-form/role-update.service.js | 8 ++++++++ .../update-role-form/role-update.template.html | 15 +++++---------- 8 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.service.js diff --git a/api-gateway/src/main/resources/static/index.html b/api-gateway/src/main/resources/static/index.html index 311e4f68b7..684cee39f2 100755 --- a/api-gateway/src/main/resources/static/index.html +++ b/api-gateway/src/main/resources/static/index.html @@ -198,6 +198,12 @@ + + + + + + diff --git a/api-gateway/src/main/resources/static/scripts/app.js b/api-gateway/src/main/resources/static/scripts/app.js index 5974f59b8d..daf9660120 100644 --- a/api-gateway/src/main/resources/static/scripts/app.js +++ b/api-gateway/src/main/resources/static/scripts/app.js @@ -12,7 +12,7 @@ const petClinicApp = angular.module('petClinicApp', [ 'ui.router', 'layoutNav', 'layoutFooter', 'layoutWelcome', 'ownerList', 'ownerDetails', 'ownerForm', 'ownerRegister', 'petRegister', 'petForm' , 'visits', 'visit', 'visitList' , 'vetList','vetForm','vetDetails', 'billForm', 'billUpdateForm', 'loginForm', 'rolesDetails', 'signupForm', 'productDetailsInfo', 'billDetails', 'billsByOwnerId', 'billHistory','billsByVetId','inventoryList', 'inventoryForm', 'productForm','inventoryProductList', 'inventoryUpdateForm', 'productUpdateForm', - 'verification' , 'adminPanel','resetPwdForm','forgotPwdForm','petTypeList', 'petDetails','userDetails','managerForm']); + 'verification' , 'adminPanel','resetPwdForm','forgotPwdForm','petTypeList', 'petDetails','userDetails','managerForm','userModule']); diff --git a/api-gateway/src/main/resources/static/scripts/auth/admin-panel/admin-panel.template.html b/api-gateway/src/main/resources/static/scripts/auth/admin-panel/admin-panel.template.html index 9724f37d14..bff8396f61 100644 --- a/api-gateway/src/main/resources/static/scripts/auth/admin-panel/admin-panel.template.html +++ b/api-gateway/src/main/resources/static/scripts/auth/admin-panel/admin-panel.template.html @@ -25,6 +25,7 @@

Users

{{user.email}} + diff --git a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.component.js b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.component.js index e69de29bb2..a59874098b 100644 --- a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.component.js +++ b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.component.js @@ -0,0 +1,11 @@ +'use strict'; + +angular.module('userModule') + .component('updateUserRoleComponent', { + templateUrl: 'scripts/auth/update-role-form/role-update.template.html', + controller: 'UpdateUserRoleController', + bindings: { + userId: '<', + onUpdate: '&' + } + }); \ No newline at end of file diff --git a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.config.js b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.config.js index e69de29bb2..af5abd257f 100644 --- a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.config.js +++ b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.config.js @@ -0,0 +1,16 @@ +'use strict'; + +angular.module('userModule', ['ui.router']) + .config(['$stateProvider', function($stateProvider) { + $stateProvider + .state('updateUserRole', { + parent: 'app', + url: '/users/:userId/updateRole', + component: 'updateUserRoleComponent', + resolve: { + userId: ['$stateParams', function($stateParams) { + return $stateParams.userId; + }] + } + }); + }]); \ No newline at end of file diff --git a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.controller.js b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.controller.js index e69de29bb2..2068eae61a 100644 --- a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.controller.js +++ b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.controller.js @@ -0,0 +1,12 @@ +'use strict'; + +angular.module('userModule') + .controller('UpdateUserRoleController', ['$scope', 'UserService', function($scope, UserService) { + $scope.roles = UserService.getAvailableRoles(); + $scope.selectedRole = {}; + + $scope.updateRole = function() { + // Here, you'll need to handle the role update logic as you see fit + $scope.onUpdate(); + }; + }]); \ No newline at end of file diff --git a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.service.js b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.service.js new file mode 100644 index 0000000000..fbf12f6add --- /dev/null +++ b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.service.js @@ -0,0 +1,8 @@ +'use strict'; + +angular.module('userModule').service('UserService', [function() { + this.getAvailableRoles = function() { + return ['ADMIN', 'VET', 'OWNER']; + }; + +}]); \ No newline at end of file diff --git a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html index de155ea51d..1ceda3594c 100644 --- a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html +++ b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html @@ -1,10 +1,5 @@ - - - - - Title - - - - - \ No newline at end of file +
+ + + +
\ No newline at end of file From 07ab18a07a1246140c4f49ac1a791c54c14d274b Mon Sep 17 00:00:00 2001 From: SamuelNguyen2121 <2032373@champlaincollege.qc.ca> Date: Tue, 17 Oct 2023 17:46:38 -0400 Subject: [PATCH 3/7] Update role form is working --- .../update-role-form/role-update.component.js | 3 +- .../role-update.controller.js | 46 +++++++++++++++++-- .../role-update.template.html | 8 ++-- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.component.js b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.component.js index a59874098b..c2726d3ee9 100644 --- a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.component.js +++ b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.component.js @@ -5,7 +5,6 @@ angular.module('userModule') templateUrl: 'scripts/auth/update-role-form/role-update.template.html', controller: 'UpdateUserRoleController', bindings: { - userId: '<', - onUpdate: '&' + userId: '<' } }); \ No newline at end of file diff --git a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.controller.js b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.controller.js index 2068eae61a..04a1da80f2 100644 --- a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.controller.js +++ b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.controller.js @@ -1,12 +1,50 @@ 'use strict'; angular.module('userModule') - .controller('UpdateUserRoleController', ['$scope', 'UserService', function($scope, UserService) { + .controller('UpdateUserRoleController', ['$scope', '$http', 'UserService', function($scope, $http, UserService) { + var ctrl = this; // Capture the controller instance + $scope.roles = UserService.getAvailableRoles(); $scope.selectedRole = {}; + // Use $onInit lifecycle hook to set $scope.userId + ctrl.$onInit = function() { + $scope.userId = ctrl.userId; + }; + + $scope.selectedRoles = {}; + $scope.updateRole = function() { - // Here, you'll need to handle the role update logic as you see fit - $scope.onUpdate(); + var rolesList = []; + for (var role in $scope.selectedRoles) { + if ($scope.selectedRoles[role]) { + rolesList.push(role); + } + } + + if (rolesList.length === 0) { + alert('Please select at least one role.'); + return; + } + + var rolesChangeRequest = { + roles: rolesList + }; + + // Send the PATCH request + $http({ + method: 'PATCH', + url: 'api/gateway/users/' + $scope.userId, + data: rolesChangeRequest, + headers: { + 'Content-Type': 'application/json', + // Add token headers if needed + } + }) + .then(function successCallback(response) { + alert('Roles updated successfully!'); + }, function errorCallback(response) { + alert('Failed to update roles. ' + response.data.message); + }); }; - }]); \ No newline at end of file + }]); diff --git a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html index 1ceda3594c..8bd289c5bf 100644 --- a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html +++ b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html @@ -1,5 +1,7 @@
- - - + +
+ {{role}} +
+
\ No newline at end of file From 218801f2abf0641daaab4021a64f0d49f4bf6b0d Mon Sep 17 00:00:00 2001 From: SamuelNguyen2121 <2032373@champlaincollege.qc.ca> Date: Thu, 19 Oct 2023 12:39:52 -0400 Subject: [PATCH 4/7] Make update form go back --- .../scripts/auth/admin-panel/admin-panel.template.html | 9 ++++++++- .../auth/update-role-form/role-update.controller.js | 3 ++- .../auth/update-role-form/role-update.template.html | 3 ++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/api-gateway/src/main/resources/static/scripts/auth/admin-panel/admin-panel.template.html b/api-gateway/src/main/resources/static/scripts/auth/admin-panel/admin-panel.template.html index bff8396f61..13d6c72de1 100644 --- a/api-gateway/src/main/resources/static/scripts/auth/admin-panel/admin-panel.template.html +++ b/api-gateway/src/main/resources/static/scripts/auth/admin-panel/admin-panel.template.html @@ -12,6 +12,7 @@

Users

Username Email + Roles Options @@ -23,9 +24,15 @@

Users

{{user.email}} + + + + {{ role.name }}, + + - + diff --git a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.controller.js b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.controller.js index 04a1da80f2..f08d2ac67c 100644 --- a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.controller.js +++ b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.controller.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('userModule') - .controller('UpdateUserRoleController', ['$scope', '$http', 'UserService', function($scope, $http, UserService) { + .controller('UpdateUserRoleController', ['$scope', '$http', 'UserService', '$state', function($scope, $http, UserService, $state) { var ctrl = this; // Capture the controller instance $scope.roles = UserService.getAvailableRoles(); @@ -43,6 +43,7 @@ angular.module('userModule') }) .then(function successCallback(response) { alert('Roles updated successfully!'); + $state.go('AdminPanel'); }, function errorCallback(response) { alert('Failed to update roles. ' + response.data.message); }); diff --git a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html index 8bd289c5bf..5a994df090 100644 --- a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html +++ b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html @@ -3,5 +3,6 @@
{{role}}
- + + \ No newline at end of file From 865c54ea0f0f9e34e97a2271bf3678968ce69e41 Mon Sep 17 00:00:00 2001 From: SamuelNguyen2121 <2032373@champlaincollege.qc.ca> Date: Fri, 20 Oct 2023 14:06:39 -0400 Subject: [PATCH 5/7] Allow only one role to be updated --- .../scripts/auth/admin-panel/admin-panel.template.html | 2 +- .../scripts/auth/update-role-form/role-update.controller.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/api-gateway/src/main/resources/static/scripts/auth/admin-panel/admin-panel.template.html b/api-gateway/src/main/resources/static/scripts/auth/admin-panel/admin-panel.template.html index 13d6c72de1..1cd6933c8d 100644 --- a/api-gateway/src/main/resources/static/scripts/auth/admin-panel/admin-panel.template.html +++ b/api-gateway/src/main/resources/static/scripts/auth/admin-panel/admin-panel.template.html @@ -12,7 +12,7 @@

Users

Username Email - Roles + Role Options diff --git a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.controller.js b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.controller.js index f08d2ac67c..753496bb79 100644 --- a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.controller.js +++ b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.controller.js @@ -27,6 +27,11 @@ angular.module('userModule') return; } + if (rolesList.length > 1) { + alert('Please select only one role.'); + return; + } + var rolesChangeRequest = { roles: rolesList }; From fd74a3e5bcba4408bada9334ff216b2af386c6a6 Mon Sep 17 00:00:00 2001 From: SamuelNguyen2121 <2032373@champlaincollege.qc.ca> Date: Sat, 21 Oct 2023 13:28:46 -0400 Subject: [PATCH 6/7] Modified update form styling --- .../update-role-form/role-update.template.html | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html index 5a994df090..be352a7b03 100644 --- a/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html +++ b/api-gateway/src/main/resources/static/scripts/auth/update-role-form/role-update.template.html @@ -1,8 +1,12 @@ -
- -
- {{role}} +

Update User Role

+
+
+ +
+ + +
- - -
\ No newline at end of file + + Cancel + From 889f5cc59d7615ab79e2d3c2f024e91dd37fda17 Mon Sep 17 00:00:00 2001 From: SamuelNguyen2121 <2032373@champlaincollege.qc.ca> Date: Sat, 21 Oct 2023 16:52:27 -0400 Subject: [PATCH 7/7] Fixed backend PR mistakes --- .../businesslayer/UserService.java | 2 +- .../authservice/datalayer/user/UserRepo.java | 1 - .../User/RolesChangeRequestDTO.java | 3 ++- .../User/UserControllerIntegrationTest.java | 22 +++++++++---------- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/auth-service/src/main/java/com/petclinic/authservice/businesslayer/UserService.java b/auth-service/src/main/java/com/petclinic/authservice/businesslayer/UserService.java index 8c391b806e..4928b32f37 100644 --- a/auth-service/src/main/java/com/petclinic/authservice/businesslayer/UserService.java +++ b/auth-service/src/main/java/com/petclinic/authservice/businesslayer/UserService.java @@ -63,5 +63,5 @@ public interface UserService { void updatePassword(String newPassword, String token); void processResetPassword(UserResetPwdWithTokenRequestModel resetRequest); - UserPasswordLessDTO updateUserRole(String id, RolesChangeRequestDTO roles, String token); + UserPasswordLessDTO updateUserRole(String userId, RolesChangeRequestDTO roles, String token); } diff --git a/auth-service/src/main/java/com/petclinic/authservice/datalayer/user/UserRepo.java b/auth-service/src/main/java/com/petclinic/authservice/datalayer/user/UserRepo.java index 2f86e56833..10a830a499 100644 --- a/auth-service/src/main/java/com/petclinic/authservice/datalayer/user/UserRepo.java +++ b/auth-service/src/main/java/com/petclinic/authservice/datalayer/user/UserRepo.java @@ -26,7 +26,6 @@ public interface UserRepo extends JpaRepository { Optional findByEmail(String email); - User findUserById(long id); Optional findByUsername(String username); diff --git a/auth-service/src/main/java/com/petclinic/authservice/presentationlayer/User/RolesChangeRequestDTO.java b/auth-service/src/main/java/com/petclinic/authservice/presentationlayer/User/RolesChangeRequestDTO.java index afde7a6c0e..7c59f390a5 100644 --- a/auth-service/src/main/java/com/petclinic/authservice/presentationlayer/User/RolesChangeRequestDTO.java +++ b/auth-service/src/main/java/com/petclinic/authservice/presentationlayer/User/RolesChangeRequestDTO.java @@ -6,11 +6,12 @@ import lombok.NoArgsConstructor; import java.util.List; +import java.util.Set; @Data @AllArgsConstructor @NoArgsConstructor @Builder(toBuilder = true) public class RolesChangeRequestDTO { - List roles; + Set roles; } diff --git a/auth-service/src/test/java/com/petclinic/authservice/presentationlayer/User/UserControllerIntegrationTest.java b/auth-service/src/test/java/com/petclinic/authservice/presentationlayer/User/UserControllerIntegrationTest.java index 455d4cd37d..a4020e1b1c 100644 --- a/auth-service/src/test/java/com/petclinic/authservice/presentationlayer/User/UserControllerIntegrationTest.java +++ b/auth-service/src/test/java/com/petclinic/authservice/presentationlayer/User/UserControllerIntegrationTest.java @@ -26,10 +26,7 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; -import java.util.Base64; -import java.util.Collections; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.stream.Collectors; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @@ -648,7 +645,7 @@ void deleteUser_ShouldThrowNotFoundException(){ @Test void updateUserRole_validUserId() { RolesChangeRequestDTO updatedUser = RolesChangeRequestDTO.builder() - .roles(Collections.singletonList("OWNER")) + .roles(Collections.singleton("OWNER")) .build(); String token = jwtTokenUtil.generateToken(userRepo.findAll().get(0)); @@ -666,16 +663,19 @@ void updateUserRole_validUserId() { .value(dto -> { assertNotNull(dto); List actualRoleNames = dto.getRoles().stream() - .map(Role::getName) // Assuming the Role object has a getName() method + .map(Role::getName) .toList(); - assertEquals(updatedUser.getRoles(), actualRoleNames); + + Set actualRolesSet = new HashSet<>(actualRoleNames); + + assertEquals(updatedUser.getRoles(), actualRolesSet); }); } @Test void updateUserRole_InvalidUserId() { RolesChangeRequestDTO updatedUser = RolesChangeRequestDTO.builder() - .roles(Collections.singletonList("OWNER")) + .roles(Collections.singleton("OWNER")) .build(); String token = jwtTokenUtil.generateToken(userRepo.findAll().get(0)); String invalidUserId = "invalidId"; @@ -694,7 +694,7 @@ void updateUserRole_InvalidUserId() { @Test void updateUserRole_NoCookie() { RolesChangeRequestDTO updatedUser = RolesChangeRequestDTO.builder() - .roles(Collections.singletonList("OWNER")) + .roles(Collections.singleton("OWNER")) .build(); webTestClient @@ -711,7 +711,7 @@ void updateUserRole_NoCookie() { void updateUserRole_cannotChangeOwnRoles() { String userId = "validUserId"; RolesChangeRequestDTO updatedUser = RolesChangeRequestDTO.builder() - .roles(Collections.singletonList("OWNER")) + .roles(Collections.singleton("OWNER")) .build(); String token = jwtTokenUtil.generateToken(userRepo.findAll().get(0)); @@ -730,7 +730,7 @@ void updateUserRole_cannotChangeOwnRoles() { @Test void updateUserRole_invalidRole() { RolesChangeRequestDTO updatedUser = RolesChangeRequestDTO.builder() - .roles(Collections.singletonList("NOT_OWNER")) + .roles(Collections.singleton("NOT_OWNER")) .build(); String token = jwtTokenUtil.generateToken(userRepo.findAll().get(0));