Skip to content

Commit 886d421

Browse files
committed
Added permissions to replace calls to isAdmin() on the current user services.
1 parent 5116b3d commit 886d421

File tree

19 files changed

+135
-74
lines changed

19 files changed

+135
-74
lines changed

server/src/main/java/com/objectcomputing/checkins/services/employee_hours/EmployeeHoursServicesImpl.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public EmployeeHoursServicesImpl(CurrentUserServices currentUserServices,
3939
@RequiredPermission(Permission.CAN_UPLOAD_HOURS)
4040
public EmployeeHoursResponseDTO save(CompletedFileUpload file) {
4141
MemberProfile currentUser = currentUserServices.getCurrentUser();
42-
boolean isAdmin = currentUserServices.isAdmin();
4342
List<EmployeeHours> employeeHoursList = new ArrayList<>();
4443
Set<EmployeeHours> employeeHours = new HashSet<>();
4544
EmployeeHoursResponseDTO responseDTO = new EmployeeHoursResponseDTO();
@@ -63,17 +62,17 @@ public EmployeeHoursResponseDTO save(CompletedFileUpload file) {
6362
@Override
6463
public Set<EmployeeHours> findByFields(String employeeId) {
6564
MemberProfile currentUser = currentUserServices.getCurrentUser();
66-
boolean isAdmin = currentUserServices.isAdmin();
65+
boolean canViewAll = currentUserServices.hasPermission(Permission.CAN_VIEW_ALL_UPLOADED_HOURS);
6766

6867
Set<EmployeeHours> employeeHours = new HashSet<>();
6968
employeehourRepo.findAll().forEach(employeeHours::add);
7069

7170
if(employeeId !=null) {
72-
validate((!isAdmin && currentUser!=null&& !currentUser.getEmployeeId().equals(employeeId)),
71+
validate((!canViewAll && currentUser!=null && !currentUser.getEmployeeId().equals(employeeId)),
7372
NOT_AUTHORIZED_MSG);
7473
employeeHours.retainAll(employeehourRepo.findByEmployeeId(employeeId));
7574
} else {
76-
validate(!isAdmin, NOT_AUTHORIZED_MSG);
75+
validate(!canViewAll, NOT_AUTHORIZED_MSG);
7776
}
7877

7978

server/src/main/java/com/objectcomputing/checkins/services/feedback_template/FeedbackTemplateServicesImpl.java

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

3+
import com.objectcomputing.checkins.services.permissions.Permission;
34
import com.objectcomputing.checkins.exceptions.BadArgException;
45
import com.objectcomputing.checkins.exceptions.NotFoundException;
56
import com.objectcomputing.checkins.exceptions.PermissionException;
@@ -93,12 +94,12 @@ public FeedbackTemplate getById(UUID id) {
9394
@Override
9495
public List<FeedbackTemplate> findByFields(@Nullable UUID creatorId, @Nullable String title) {
9596
UUID currentUserId = currentUserServices.getCurrentUser().getId();
96-
boolean isAdmin = currentUserServices.isAdmin();
97+
boolean canAdminister = hasAdministerPermission();
9798
List <FeedbackTemplate> allTemplates = feedbackTemplateRepository.searchByValues(Util.nullSafeUUIDToString(creatorId), title);
9899
return allTemplates
99100
.stream()
100-
.filter(template -> template.getIsPublic() || isAdmin || template.getCreatorId().equals(currentUserId))
101-
.filter(template -> !template.getIsReview() || isAdmin)
101+
.filter(template -> template.getIsPublic() || canAdminister || template.getCreatorId().equals(currentUserId))
102+
.filter(template -> !template.getIsReview() || canAdminister)
102103
.toList();
103104
}
104105

@@ -113,12 +114,14 @@ public boolean setAdHocInactiveByCreator(@Nullable UUID creatorId) {
113114

114115
public boolean updateIsPermitted(UUID creatorId) {
115116
UUID currentUserId = currentUserServices.getCurrentUser().getId();
116-
boolean isAdmin = currentUserServices.isAdmin();
117-
return isAdmin || currentUserId.equals(creatorId);
117+
return hasAdministerPermission() || currentUserId.equals(creatorId);
118118
}
119119

120120
public boolean deleteIsPermitted(UUID creatorId) {
121121
return updateIsPermitted(creatorId);
122122
}
123123

124+
private boolean hasAdministerPermission() {
125+
return currentUserServices.hasPermission(Permission.CAN_ADMINISTER_FEEDBACK_TEMPLATES);
126+
}
124127
}

server/src/main/java/com/objectcomputing/checkins/services/feedback_template/template_question/TemplateQuestionServicesImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.objectcomputing.checkins.services.feedback_template.template_question;
2-
2+
import com.objectcomputing.checkins.services.permissions.Permission;
33
import com.objectcomputing.checkins.exceptions.BadArgException;
44
import com.objectcomputing.checkins.exceptions.NotFoundException;
55
import com.objectcomputing.checkins.exceptions.PermissionException;
@@ -139,9 +139,8 @@ public List<TemplateQuestion> findByFields(UUID templateId) {
139139

140140
// only admins or the creator of the template can add questions to it
141141
public boolean createIsPermitted(UUID templateCreatorId) {
142-
boolean isAdmin = currentUserServices.isAdmin();
143142
UUID currentUserId = currentUserServices.getCurrentUser().getId();
144-
return currentUserId != null && (isAdmin || currentUserId.equals(templateCreatorId));
143+
return currentUserId != null && (currentUserServices.hasPermission(Permission.CAN_ADMINISTER_FEEDBACK_TEMPLATES) || currentUserId.equals(templateCreatorId));
145144
}
146145

147146
public boolean updateIsPermitted(UUID templateCreatorId) {

server/src/main/java/com/objectcomputing/checkins/services/file/FileServicesBaseImpl.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ abstract protected FileInfoDTO uploadSingleFile(
5858

5959
@Override
6060
public Set<FileInfoDTO> findFiles(@Nullable UUID checkInID) {
61-
boolean isAdmin = currentUserServices.isAdmin();
62-
validate(checkInID == null && !isAdmin, NOT_AUTHORIZED_MSG);
61+
boolean canAdminister = hasAdministerPermission();
62+
validate(checkInID == null && !canAdminister, NOT_AUTHORIZED_MSG);
6363

6464
try {
6565
Set<FileInfoDTO> result = new HashSet<>();
66-
if (checkInID == null && isAdmin) {
66+
if (checkInID == null && canAdminister) {
6767
getCheckinDocuments(result, Collections.emptySet());
6868
} else if (checkInID != null) {
6969
validate(!checkInServices.accessGranted(checkInID, currentUserServices.getCurrentUser().getId()),
@@ -89,14 +89,14 @@ public Set<FileInfoDTO> findFiles(@Nullable UUID checkInID) {
8989
@Override
9090
public java.io.File downloadFiles(@NotNull String uploadDocId) {
9191
MemberProfile currentUser = currentUserServices.getCurrentUser();
92-
boolean isAdmin = currentUserServices.isAdmin();
92+
boolean canAdminister = hasAdministerPermission();
9393

9494
CheckinDocument cd = checkinDocumentServices.getFindByUploadDocId(uploadDocId);
9595
validate(cd == null, String.format("Unable to find record with id %s", uploadDocId));
9696

9797
CheckIn associatedCheckin = checkInServices.read(cd.getCheckinsId());
9898

99-
if(!isAdmin) {
99+
if(!canAdminister) {
100100
validate((!currentUser.getId().equals(associatedCheckin.getTeamMemberId()) && !currentUser.getId().equals(associatedCheckin.getPdlId())), NOT_AUTHORIZED_MSG);
101101
}
102102
try {
@@ -120,12 +120,12 @@ public java.io.File downloadFiles(@NotNull String uploadDocId) {
120120
@Override
121121
public FileInfoDTO uploadFile(@NotNull UUID checkInID, @NotNull CompletedFileUpload file) {
122122
MemberProfile currentUser = currentUserServices.getCurrentUser();
123-
boolean isAdmin = currentUserServices.isAdmin();
123+
boolean canAdminister = hasAdministerPermission();
124124
validate((file.getFilename() == null || file.getFilename().equals("")), "Please select a valid file before uploading.");
125125

126126
CheckIn checkIn = checkInServices.read(checkInID);
127127
validate(checkIn == null, "Unable to find checkin record with id %s", checkInID);
128-
if(!isAdmin) {
128+
if(!canAdminister) {
129129
validate((!currentUser.getId().equals(checkIn.getTeamMemberId()) && !currentUser.getId().equals(checkIn.getPdlId())), "You are not authorized to perform this operation");
130130
validate(checkIn.isCompleted(), NOT_AUTHORIZED_MSG);
131131
}
@@ -175,4 +175,8 @@ protected void validate(boolean isError, String message, Object... args) {
175175
throw new FileRetrievalException(String.format(message, args));
176176
}
177177
}
178+
179+
protected boolean hasAdministerPermission() {
180+
return currentUserServices.hasPermission(Permission.CAN_ADMINISTER_CHECKIN_DOCUMENTS);
181+
}
178182
}

server/src/main/java/com/objectcomputing/checkins/services/file/FileServicesImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ protected FileInfoDTO uploadSingleFile(CompletedFileUpload file, String director
147147
public FileInfoDTO uploadDocument(String directoryName, String name, String text) {
148148
final String GOOGLE_DOC_TYPE = "application/vnd.google-apps.document";
149149
MemberProfile currentUser = currentUserServices.getCurrentUser();
150-
boolean isAdmin = currentUserServices.isAdmin();
151-
validate(!isAdmin, "You are not authorized to perform this operation");
150+
validate(!hasAdministerPermission(), "You are not authorized to perform this operation");
152151

153152
try {
154153
Drive drive = googleApiAccess.getDrive();

server/src/main/java/com/objectcomputing/checkins/services/guild/GuildServicesImpl.java

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

3+
import com.objectcomputing.checkins.services.permissions.Permission;
34
import com.objectcomputing.checkins.Environments;
45
import com.objectcomputing.checkins.configuration.CheckInsConfiguration;
56
import com.objectcomputing.checkins.exceptions.BadArgException;
@@ -135,8 +136,8 @@ public GuildResponseDTO read(@NotNull UUID guildId) {
135136

136137
public GuildResponseDTO update(GuildUpdateDTO guildDTO) {
137138
MemberProfile currentUser = currentUserServices.getCurrentUser();
138-
boolean isAdmin = currentUserServices.isAdmin();
139-
if (isAdmin || (currentUser != null &&
139+
boolean canAdminister = hasAdministerPermission();
140+
if (canAdminister || (currentUser != null &&
140141
!guildMemberServices.findByFields(guildDTO.getId(), currentUser.getId(), true).isEmpty())) {
141142
// Guild newGuildEntity = null;
142143
GuildResponseDTO updated= null;
@@ -236,9 +237,9 @@ public Set<GuildResponseDTO> findByFields(String name, UUID memberId) {
236237

237238
public boolean delete(@NotNull UUID id) {
238239
MemberProfile currentUser = currentUserServices.getCurrentUser();
239-
boolean isAdmin = currentUserServices.isAdmin();
240+
boolean canAdminister = hasAdministerPermission();
240241

241-
if (isAdmin || (currentUser != null && !guildMemberRepo.search(nullSafeUUIDToString(id), nullSafeUUIDToString(currentUser.getId()), true).isEmpty())) {
242+
if (canAdminister || (currentUser != null && !guildMemberRepo.search(nullSafeUUIDToString(id), nullSafeUUIDToString(currentUser.getId()), true).isEmpty())) {
242243
guildMemberHistoryRepository.deleteByGuildId(id);
243244
guildMemberRepo.deleteByGuildId(id.toString());
244245
guildsRepo.deleteById(id);
@@ -341,4 +342,8 @@ public void emailGuildLeaders(Set<String> guildLeadersEmails, Guild guild) {
341342
String body = "Congratulations, you have been assigned as a guild leader of " + guild.getName();
342343
emailSender.sendEmail(null, null, subject, body, guildLeadersEmails.toArray(new String[0]));
343344
}
345+
346+
private boolean hasAdministerPermission() {
347+
return currentUserServices.hasPermission(Permission.CAN_ADMINISTER_GUILDS);
348+
}
344349
}

server/src/main/java/com/objectcomputing/checkins/services/guild/member/GuildMemberServicesImpl.java

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

3+
import com.objectcomputing.checkins.services.permissions.Permission;
34
import com.objectcomputing.checkins.configuration.CheckInsConfiguration;
45
import com.objectcomputing.checkins.exceptions.BadArgException;
56
import com.objectcomputing.checkins.exceptions.NotFoundException;
@@ -76,11 +77,11 @@ public GuildMember save(@Valid @NotNull GuildMember guildMember, boolean sendEma
7677
throw new BadArgException(String.format("Member %s already exists in guild %s", memberId, guildId));
7778
}
7879
// only allow admins to create guild leads
79-
else if (!currentUserServices.isAdmin() && Boolean.TRUE.equals(guildMember.getLead())) {
80+
else if (!hasAdministerPermission() && Boolean.TRUE.equals(guildMember.getLead())) {
8081
throw new BadArgException(NOT_AUTHORIZED_MSG);
8182
}
8283
// only admins and leads can add members to guilds unless a user adds themself
83-
else if (!currentUserServices.isAdmin() && !guildMember.getMemberId().equals(currentUser.getId()) && !isLead){
84+
else if (!hasAdministerPermission() && !guildMember.getMemberId().equals(currentUser.getId()) && !isLead){
8485
throw new PermissionException(NOT_AUTHORIZED_MSG);
8586
}
8687

@@ -103,7 +104,7 @@ public GuildMember read(@NotNull UUID id) {
103104

104105
public GuildMember update(@NotNull @Valid GuildMember guildMember) {
105106
MemberProfile currentUser = currentUserServices.getCurrentUser();
106-
boolean isAdmin = currentUserServices.isAdmin();
107+
boolean canAdminister = hasAdministerPermission();
107108

108109
final UUID id = guildMember.getId();
109110
final UUID guildId = guildMember.getGuildId();
@@ -122,7 +123,7 @@ public GuildMember update(@NotNull @Valid GuildMember guildMember) {
122123
throw new BadArgException(String.format("Member %s doesn't exist", memberId));
123124
} else if (guildMemberRepo.findByGuildIdAndMemberId(guildMember.getGuildId(), guildMember.getMemberId()).isEmpty()) {
124125
throw new BadArgException(String.format("Member %s is not part of guild %s", memberId, guildId));
125-
} else if (!isAdmin && guildLeads.stream().noneMatch(o -> o.getMemberId().equals(currentUser.getId()))) {
126+
} else if (!canAdminister && guildLeads.stream().noneMatch(o -> o.getMemberId().equals(currentUser.getId()))) {
126127
throw new BadArgException(NOT_AUTHORIZED_MSG);
127128
}
128129
GuildMember guildMemberUpdate = guildMemberRepo.update(guildMember);
@@ -161,7 +162,7 @@ public void delete(@NotNull UUID id, boolean sendEmail) {
161162
throw new BadArgException("At least one guild lead must be present in the guild at all times");
162163
}
163164
// if the current user is not an admin, is not the same as the member in the request, and is not a lead in the guild -> don't delete
164-
if (!currentUserServices.isAdmin() && !guildMember.getMemberId().equals(currentUser.getId()) && !currentUserIsLead) {
165+
if (!hasAdministerPermission() && !guildMember.getMemberId().equals(currentUser.getId()) && !currentUserIsLead) {
165166
throw new PermissionException(NOT_AUTHORIZED_MSG);
166167
}
167168

@@ -211,4 +212,8 @@ private String constructEmailContent (GuildMember guildMember, boolean isAdded){
211212
private static GuildMemberHistory buildGuildMemberHistory(UUID guildId, UUID memberId, String change, LocalDateTime date) {
212213
return new GuildMemberHistory(guildId,memberId,change,date);
213214
}
215+
216+
private boolean hasAdministerPermission() {
217+
return currentUserServices.hasPermission(Permission.CAN_ADMINISTER_GUILDS);
218+
}
214219
}

server/src/main/java/com/objectcomputing/checkins/services/kudos/kudos_recipient/KudosRecipientServicesImpl.java

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

3+
import com.objectcomputing.checkins.services.permissions.Permission;
34
import com.objectcomputing.checkins.notifications.email.EmailSender;
45
import com.objectcomputing.checkins.notifications.email.MailJetFactory;
56
import com.objectcomputing.checkins.exceptions.BadArgException;
@@ -54,8 +55,7 @@ public KudosRecipient save(KudosRecipient kudosRecipient) {
5455
new NotFoundException("No kudos with id %s"));
5556

5657
boolean isKudosCreator = currentUserServices.getCurrentUser().getId().equals(kudos.getSenderId());
57-
boolean isAdmin = currentUserServices.isAdmin();
58-
if (!isAdmin && !isKudosCreator) {
58+
if (!currentUserServices.hasPermission(Permission.CAN_ADMINISTER_KUDOS) && !isKudosCreator) {
5959
throw new PermissionException(NOT_AUTHORIZED_MSG);
6060
}
6161

server/src/main/java/com/objectcomputing/checkins/services/memberprofile/MemberProfileServicesImpl.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public MemberProfile getById(@NotNull UUID id) {
6767
throw new NotFoundException("No member profile for id " + id);
6868
}
6969
MemberProfile memberProfile = optional.get();
70-
if (!currentUserServices.isAdmin()) {
70+
if (!hasAdministerPermission()) {
7171
memberProfile.clearBirthYear();
7272
}
7373
return memberProfile;
@@ -91,7 +91,7 @@ public Set<MemberProfile> findByValues(@Nullable String firstName,
9191
@Nullable Boolean terminated) {
9292
Set<MemberProfile> memberProfiles = new HashSet<>(memberProfileRepository.search(firstName, null, lastName, null, title,
9393
nullSafeUUIDToString(pdlId), workEmail, nullSafeUUIDToString(supervisorId), terminated));
94-
if (!currentUserServices.isAdmin()) {
94+
if (!hasAdministerPermission()) {
9595
for (MemberProfile memberProfile : memberProfiles) {
9696
memberProfile.clearBirthYear();
9797
}
@@ -199,7 +199,7 @@ public List<MemberProfile> findAll() {
199199
@Cacheable
200200
public List<MemberProfile> getSupervisorsForId(UUID id) {
201201
List<MemberProfile> supervisorsForId = memberProfileRepository.findSupervisorsForId(id);
202-
if (!currentUserServices.isAdmin()) {
202+
if (!hasAdministerPermission()) {
203203
for (MemberProfile memberProfile : supervisorsForId) {
204204
memberProfile.clearBirthYear();
205205
}
@@ -211,7 +211,7 @@ public List<MemberProfile> getSupervisorsForId(UUID id) {
211211
@Cacheable
212212
public List<MemberProfile> getSubordinatesForId(UUID id) {
213213
List<MemberProfile> subordinatesForId = memberProfileRepository.findSubordinatesForId(id);
214-
if (!currentUserServices.isAdmin()) {
214+
if (!hasAdministerPermission()) {
215215
for (MemberProfile memberProfile : subordinatesForId) {
216216
memberProfile.clearBirthYear();
217217
}
@@ -270,4 +270,8 @@ public void updateLastSeen(UUID id) {
270270
memberProfileRepository.update(memberProfile);
271271
}
272272
}
273+
274+
private boolean hasAdministerPermission() {
275+
return currentUserServices.hasPermission(Permission.CAN_EDIT_ALL_ORGANIZATION_MEMBERS);
276+
}
273277
}

server/src/main/java/com/objectcomputing/checkins/services/memberprofile/retentionreport/RetentionReportServicesImpl.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ public RetentionReportServicesImpl(MemberProfileServices memberProfileServices,
2727
@Override
2828
@RequiredPermission(Permission.CAN_VIEW_RETENTION_REPORT)
2929
public RetentionReportResponseDTO report(RetentionReportDTO request) {
30-
if (!currentUserServices.isAdmin()) {
31-
throw new PermissionException("Requires admin privileges");
32-
}
33-
3430
RetentionReportResponseDTO response;
3531
List<MemberProfile> memberProfiles = memberProfileServices.findAll();
3632
LocalDate periodStartDate = getIntervalStartDate(request.getStartDate(), request.getFrequency());

0 commit comments

Comments
 (0)