Skip to content

Commit a30fd25

Browse files
committed
Added the ability to check permissions on the current user and use that for the kudos services to check for administer permissions (instead of using the admin role).
1 parent b786c08 commit a30fd25

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

server/src/main/java/com/objectcomputing/checkins/services/kudos/KudosServicesImpl.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.objectcomputing.checkins.services.kudos;
22

3+
import com.objectcomputing.checkins.services.permissions.Permission;
34
import com.objectcomputing.checkins.configuration.CheckInsConfiguration;
45
import com.objectcomputing.checkins.notifications.email.EmailSender;
56
import com.objectcomputing.checkins.notifications.email.MailJetFactory;
@@ -147,7 +148,7 @@ public KudosResponseDTO getById(UUID id) {
147148

148149
if (kudos.getDateApproved() == null) {
149150
// If not yet approved, only admins and the sender can access the kudos
150-
if (!currentUserServices.isAdmin() && !isSender) {
151+
if (!hasAdministerKudosPermission() && !isSender) {
151152
throw new PermissionException(NOT_AUTHORIZED_MSG);
152153
}
153154
} else {
@@ -158,7 +159,7 @@ public KudosResponseDTO getById(UUID id) {
158159
.stream()
159160
.anyMatch(recipient -> recipient.getMemberId().equals(currentUserId));
160161

161-
if (!currentUserServices.isAdmin() && !isSender && !isRecipient) {
162+
if (!hasAdministerKudosPermission() && !isSender && !isRecipient) {
162163
throw new PermissionException(NOT_AUTHORIZED_MSG);
163164
}
164165
}
@@ -188,7 +189,7 @@ public List<KudosResponseDTO> findByValues(@Nullable UUID recipientId, @Nullable
188189
} else if (senderId != null) {
189190
return findAllFromMember(senderId);
190191
} else {
191-
if (!currentUserServices.isAdmin()) {
192+
if (!hasAdministerKudosPermission()) {
192193
throw new PermissionException(NOT_AUTHORIZED_MSG);
193194
}
194195

@@ -207,7 +208,7 @@ public List<KudosResponseDTO> getRecent() {
207208
}
208209

209210
private List<KudosResponseDTO> findByPending(boolean isPending) {
210-
if (!currentUserServices.isAdmin()) {
211+
if (!hasAdministerKudosPermission()) {
211212
throw new PermissionException(NOT_AUTHORIZED_MSG);
212213
}
213214

@@ -227,10 +228,10 @@ private List<KudosResponseDTO> findByPending(boolean isPending) {
227228

228229

229230
private List<KudosResponseDTO> findAllToMember(UUID memberId) {
230-
boolean isAdmin = currentUserServices.isAdmin();
231231
UUID currentUserId = currentUserServices.getCurrentUser().getId();
232232

233-
if (!currentUserId.equals(memberId) && !isAdmin) {
233+
if (!currentUserId.equals(memberId) &&
234+
!hasAdministerKudosPermission()) {
234235
throw new PermissionException("You are not authorized to retrieve the kudos another user has received");
235236
}
236237

@@ -253,10 +254,10 @@ private List<KudosResponseDTO> findAllToMember(UUID memberId) {
253254

254255
private List<KudosResponseDTO> findAllFromMember(UUID senderId) {
255256

256-
boolean isAdmin = currentUserServices.isAdmin();
257257
UUID currentUserId = currentUserServices.getCurrentUser().getId();
258258

259-
if (!currentUserId.equals(senderId) && !isAdmin) {
259+
if (!currentUserId.equals(senderId) &&
260+
!hasAdministerKudosPermission()) {
260261
throw new PermissionException("You are not authorized to retrieve the kudos another user has sent");
261262
}
262263

@@ -378,4 +379,8 @@ private void slackApprovedKudos(Kudos kudos) {
378379
LOG.error("Unable to POST to Slack: " + httpResponse.reason());
379380
}
380381
}
382+
383+
private boolean hasAdministerKudosPermission() {
384+
return currentUserServices.hasPermission(Permission.CAN_ADMINISTER_KUDOS);
385+
}
381386
}

server/src/main/java/com/objectcomputing/checkins/services/memberprofile/currentuser/CurrentUserServices.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.objectcomputing.checkins.services.memberprofile.currentuser;
22

33
import com.objectcomputing.checkins.services.memberprofile.MemberProfile;
4+
import com.objectcomputing.checkins.services.permissions.Permission;
45
import com.objectcomputing.checkins.services.role.RoleType;
56

67
public interface CurrentUserServices {
@@ -9,6 +10,8 @@ public interface CurrentUserServices {
910

1011
boolean hasRole(RoleType role);
1112

13+
boolean hasPermission(Permission permission);
14+
1215
boolean isAdmin();
1316

1417
MemberProfile getCurrentUser();

server/src/main/java/com/objectcomputing/checkins/services/memberprofile/currentuser/CurrentUserServicesImpl.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22

33
import com.objectcomputing.checkins.exceptions.AlreadyExistsException;
44
import com.objectcomputing.checkins.exceptions.NotFoundException;
5+
import com.objectcomputing.checkins.services.permissions.Permission;
56
import com.objectcomputing.checkins.services.memberprofile.MemberProfile;
67
import com.objectcomputing.checkins.services.memberprofile.MemberProfileRepository;
78
import com.objectcomputing.checkins.services.role.Role;
89
import com.objectcomputing.checkins.services.role.RoleServices;
910
import com.objectcomputing.checkins.services.role.RoleType;
1011
import com.objectcomputing.checkins.services.role.member_roles.MemberRoleServices;
12+
import com.objectcomputing.checkins.services.role.role_permissions.RolePermissionServices;
1113
import io.micronaut.security.authentication.Authentication;
1214
import io.micronaut.security.utils.SecurityService;
1315
import jakarta.inject.Singleton;
1416
import jakarta.validation.constraints.NotNull;
1517

1618
import java.time.LocalDate;
1719
import java.util.Optional;
20+
import java.util.List;
1821

1922
@Singleton
2023
public class CurrentUserServicesImpl implements CurrentUserServices {
@@ -23,14 +26,18 @@ public class CurrentUserServicesImpl implements CurrentUserServices {
2326
private final SecurityService securityService;
2427
private final RoleServices roleServices;
2528
private final MemberRoleServices memberRoleServices;
29+
private final RolePermissionServices rolePermissionServices;
2630

2731
public CurrentUserServicesImpl(MemberProfileRepository memberProfileRepository,
2832
RoleServices roleServices,
29-
SecurityService securityService, MemberRoleServices memberRoleServices) {
33+
SecurityService securityService,
34+
MemberRoleServices memberRoleServices,
35+
RolePermissionServices rolePermissionServices) {
3036
this.memberProfileRepo = memberProfileRepository;
3137
this.roleServices = roleServices;
3238
this.securityService = securityService;
3339
this.memberRoleServices = memberRoleServices;
40+
this.rolePermissionServices = rolePermissionServices;
3441
}
3542

3643
@Override
@@ -48,6 +55,14 @@ public boolean hasRole(RoleType role) {
4855
return securityService.hasRole(role.toString());
4956
}
5057

58+
@Override
59+
public boolean hasPermission(Permission permission) {
60+
List<Permission> userPermissions =
61+
rolePermissionServices.findUserPermissions(getCurrentUser().getId());
62+
return userPermissions.stream().map(Permission::name)
63+
.anyMatch(str -> str.equals(permission.name()));
64+
}
65+
5166
@Override
5267
public boolean isAdmin() {
5368
return hasRole(RoleType.ADMIN);

0 commit comments

Comments
 (0)