Skip to content

Commit

Permalink
Be able to update unofficial post name yourself (not requiring admin)
Browse files Browse the repository at this point in the history
  • Loading branch information
Portals committed Jun 10, 2024
1 parent 5ff2b66 commit b848f6f
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ public ModelAndView getGroups(

public record Member(String name, String post, UUID userId) {}

public static class MyMembershipsForm {

private MyMembershipsForm() {
this.postNames = new HashMap<>();
}

private Map<String, String> postNames;

public Map<String, String> getPostNames() {
return postNames;
}

public void setPostNames(Map<String, String> postNames) {
this.postNames = postNames;
}
}

@GetMapping("/groups/{id}")
public ModelAndView getGroup(
@RequestHeader(value = "HX-Request", required = false) boolean htmxRequest,
Expand Down Expand Up @@ -96,14 +113,27 @@ public ModelAndView getGroup(
boolean canEditImages = false;
if (SecurityContextHolder.getContext().getAuthentication()
instanceof UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken) {
UUID myUserId = UUID.fromString(usernamePasswordAuthenticationToken.getName());

canEditImages =
group.get().groupMembers().stream()
.anyMatch(
groupMember ->
groupMember
.user()
.id()
.equals(UUID.fromString(usernamePasswordAuthenticationToken.getName())));
.anyMatch(groupMember -> groupMember.user().id().equals(myUserId));

List<GroupFacade.GroupMemberDTO> myGroupMembers =
group.get().groupMembers().stream()
.filter(groupMember -> groupMember.user().id().equals(myUserId))
.toList();
mv.addObject("myMembers", myGroupMembers);

MyMembershipsForm form = new MyMembershipsForm();

myGroupMembers.forEach(
groupMember -> {
form.postNames.put(
groupMember.post().id().toString(), groupMember.unofficialPostName());
});

mv.addObject("myMembershipsForm", form);
}

mv.addObject("canEditImages", canEditImages);
Expand Down Expand Up @@ -392,4 +422,21 @@ public ModelAndView deleteGroup(

return new ModelAndView("redirect:/groups");
}

@PutMapping("/groups/{groupId}/my-posts")
public ModelAndView updateUnofficialPostNames(
@PathVariable("groupId") UUID groupId, MyMembershipsForm myMembershipsForm) {
if (SecurityContextHolder.getContext().getAuthentication()
instanceof UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken) {
UUID myUserId = UUID.fromString(usernamePasswordAuthenticationToken.getName());

myMembershipsForm.postNames.forEach(
(postId, newUnofficialPostName) -> {
groupFacade.changeUnofficialPostName(
groupId, UUID.fromString(postId), myUserId, newUnofficialPostName);
});
}

return new ModelAndView("redirect:/groups/" + groupId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ public static AccessChecker isAdmin() {
};
}

public static AccessChecker isMe(UserId userId) {
return (clientRepository, userRepository) -> {
if (AuthenticationExtractor.getAuthentication()
instanceof UserAuthentication userAuthenticated) {
return userAuthenticated.gammaUser().id().equals(userId);
}

return false;
};
}

public static AccessChecker passwordCheck(String password) {
return (clientRepository, userRepository) -> {
if (AuthenticationExtractor.getAuthentication()
Expand Down
36 changes: 36 additions & 0 deletions app/src/main/java/it/chalmers/gamma/app/group/GroupFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,42 @@ public void setMembers(UUID groupId, List<ShallowMember> newMembers)
}
}

@Transactional
public void changeUnofficialPostName(
UUID groupId, UUID postId, UUID userId, String newUnofficialPostName) {
accessGuard.requireEither(isAdmin(), isMe(new UserId(userId)));

Group group =
this.groupRepository
.get(new GroupId(groupId))
.orElseThrow(GroupNotFoundRuntimeException::new);

List<GroupMember> groupMembers = new ArrayList<>(group.groupMembers());
boolean found = false;
for (int i = 0; i < groupMembers.size() && !found; i++) {
GroupMember groupMember = groupMembers.get(i);
if (groupMember.user().id().value().equals(userId)
&& groupMember.post().id().value().equals(postId)) {
GroupMember newGroupMember =
groupMember.withUnofficialPostName(new UnofficialPostName(newUnofficialPostName));
groupMembers.set(i, newGroupMember);
found = true;
}
}

if (!found) {
throw new PostNotFoundRuntimeException();
}

group = group.withGroupMembers(groupMembers);
try {
this.groupRepository.save(group);
} catch (GroupRepository.GroupNameAlreadyExistsException e) {
LOGGER.error("GroupAlreadyExistsException when just trying to update withGroupMembers", e);
throw new UnexpectedRuntimeException();
}
}

@Transactional
public void delete(UUID id) throws GroupNotFoundRuntimeException {
accessGuard.require(isAdmin());
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/resources/templates/pages/group-details.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@
</footer>
</article>

<article th:if="${myMembers.size() > 0}">
<header>
Change unofficial post name for your posts
</header>

<form id="update-unofficial-post-name" th:object="${myMembershipsForm}" th:action="|/groups/${group.id()}/my-posts|" th:method="put">
<th:block th:each="myMember : ${myMembers}">
<div th:replace="~{common/input :: textInput2(field='postNames['+${myMember.post().id()}+']', label=${myMember.post().enName()})}"></div>
</th:block>
</form>

<footer>
<button class="outline contained" form="update-unofficial-post-name">
Update unofficial post names
</button>
</footer>
</article>

<th:block th:with="groupId=${group.id()}">
<article th:fragment="group-avatar">
<header>
Expand Down

0 comments on commit b848f6f

Please sign in to comment.