Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add activity log #16

Merged
merged 6 commits into from
Nov 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update
haiphucnguyen committed Nov 27, 2024
commit 220f4d4bda12f16aa63635ddcbe36128099abbec
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
import io.flexwork.modules.teams.repository.TeamRoleRepository;
import io.flexwork.modules.teams.service.dto.TeamDTO;
import io.flexwork.modules.teams.service.event.NewUsersAddedIntoTeamEvent;
import io.flexwork.modules.teams.service.event.RemoveUserOutOfTeamEvent;
import io.flexwork.modules.teams.service.mapper.TeamMapper;
import io.flexwork.modules.usermanagement.domain.User;
import io.flexwork.modules.usermanagement.domain.UserTeam;
@@ -181,8 +182,10 @@ public void removeUserFromTeam(Long userId, Long teamId) {
// Remove the team from the user's teams set
if (user.getTeams().contains(team)) {
user.getTeams().remove(team);
userRepository.save(user); // Save the updated user
userRepository.save(user);
}

eventPublisher.publishEvent(new RemoveUserOutOfTeamEvent(this, teamId, userId));
}

public String getUserRoleInTeam(Long userId, Long teamId) {
Original file line number Diff line number Diff line change
@@ -16,5 +16,8 @@ public class NewUsersAddedIntoTeamEvent extends ApplicationEvent {
public NewUsersAddedIntoTeamEvent(
Object source, List<Long> userIds, Long teamId, String roleName) {
super(source);
this.userIds = userIds;
this.teamId = teamId;
this.roleName = roleName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.flexwork.modules.teams.service.event;

import lombok.Getter;
import org.springframework.context.ApplicationEvent;

@Getter
public class RemoveUserOutOfTeamEvent extends ApplicationEvent {
private Long teamId;

private Long userId;

public RemoveUserOutOfTeamEvent(Object source, Long teamId, Long userId) {
super(source);
this.teamId = teamId;
this.userId = userId;
}
}
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@@ -34,6 +35,7 @@ public NewTeamRequestCreatedEventListener(
this.activityLogRepository = activityLogRepository;
}

@Async("taskExecutor")
@Transactional
@EventListener
public void onNewTeamRequestCreated(NewTeamRequestCreatedEvent event) {
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.flexwork.modules.teams.service.listener;

import static j2html.TagCreator.*;

import com.flexwork.platform.utils.Obfuscator;
import io.flexwork.modules.collab.domain.ActivityLog;
import io.flexwork.modules.collab.domain.EntityType;
import io.flexwork.modules.collab.repository.ActivityLogRepository;
@@ -9,27 +12,35 @@
import io.flexwork.modules.usermanagement.domain.User;
import io.flexwork.modules.usermanagement.repository.UserRepository;
import io.flexwork.security.SecurityUtils;
import j2html.tags.specialized.DivTag;
import jakarta.persistence.EntityNotFoundException;
import jakarta.transaction.Transactional;
import java.util.List;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class NewUserAddedIntoTeamEventListener {

private ActivityLogRepository activityLogRepository;
private TeamRepository teamRepository;
private UserRepository userRepository;

public NewUserAddedIntoTeamEventListener(ActivityLogRepository activityLogRepository) {
public NewUserAddedIntoTeamEventListener(
ActivityLogRepository activityLogRepository,
TeamRepository teamRepository,
UserRepository userRepository) {
this.activityLogRepository = activityLogRepository;
this.teamRepository = teamRepository;
this.userRepository = userRepository;
}

@Async("taskExecutor")
@EventListener
@Transactional
public void onNewUsersAddedIntoTeam(NewUsersAddedIntoTeamEvent event) {
ActivityLog activityLog = new ActivityLog();
activityLog.setEntityType(EntityType.Team);
activityLog.setEntityId(event.getTeamId());

Team team =
teamRepository
.findById(event.getTeamId())
@@ -39,10 +50,43 @@ public void onNewUsersAddedIntoTeam(NewUsersAddedIntoTeamEvent event) {
"Not found team id " + event.getTeamId()));
List<User> allUsers = userRepository.findAllById(event.getUserIds());

activityLog.setCreatedBy(
SecurityUtils.getCurrentUserLogin()
.map(userKey -> User.builder().id(userKey.getId()).build())
.orElse(null));
DivTag message = div();

// Add message prefix
message.withText("The following users have been added to the ")
.with(b(team.getName()))
.withText(" team as ")
.with(b(event.getRoleName() + "s"))
.withText(": ");

// Construct user list
message.with(
ul().with(
allUsers.stream()
.map(
user ->
li().with(
a(user.getFirstName()
+ " "
+ user
.getLastName())
.withHref(
"/portal/users/"
+ Obfuscator
.obfuscate(
user
.getId()))
.withTarget(
"_blank")))
.toList()));

ActivityLog activityLog =
ActivityLog.builder()
.entityId(team.getId())
.entityType(EntityType.Team)
.content(message.render())
.createdBy(SecurityUtils.getCurrentUserAuditorLogin())
.build();
activityLogRepository.save(activityLog);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package io.flexwork.modules.teams.service.listener;

import static j2html.TagCreator.a;
import static j2html.TagCreator.div;

import com.flexwork.platform.utils.Obfuscator;
import io.flexwork.modules.collab.domain.ActivityLog;
import io.flexwork.modules.collab.domain.EntityType;
import io.flexwork.modules.collab.repository.ActivityLogRepository;
import io.flexwork.modules.teams.domain.Team;
import io.flexwork.modules.teams.repository.TeamRepository;
import io.flexwork.modules.teams.service.event.RemoveUserOutOfTeamEvent;
import io.flexwork.modules.usermanagement.domain.User;
import io.flexwork.modules.usermanagement.repository.UserRepository;
import io.flexwork.security.SecurityUtils;
import jakarta.persistence.EntityNotFoundException;
import jakarta.transaction.Transactional;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class RemoveUserOutOfTeamEventListener {

private final TeamRepository teamRepository;

private final UserRepository userRepository;

private final ActivityLogRepository activityLogRepository;

public RemoveUserOutOfTeamEventListener(
TeamRepository teamRepository,
UserRepository userRepository,
ActivityLogRepository activityLogRepository) {
this.teamRepository = teamRepository;
this.userRepository = userRepository;
this.activityLogRepository = activityLogRepository;
}

@Async("taskExecutor")
@EventListener
@Transactional
public void onRemoveUserOutOfTeam(RemoveUserOutOfTeamEvent event) {
Team team =
teamRepository
.findById(event.getTeamId())
.orElseThrow(
() ->
new EntityNotFoundException(
"Not found team id " + event.getTeamId()));

User user =
userRepository
.findById(event.getUserId())
.orElseThrow(
() ->
new EntityNotFoundException(
"Not found user id " + event.getUserId()));

String content =
div().withText("User ")
.with(
a(user.getFirstName() + " " + user.getLastName())
.withHref(
"/portal/users/"
+ Obfuscator.obfuscate(user.getId()))
.withTarget("_blank"))
.withText(" is no longer part of the ")
.with(
a(team.getName())
.withHref(
"/portal/teams/"
+ Obfuscator.obfuscate(team.getId()))
.withTarget("_blank") // Opens the link in a new tab
)
.withText(" team.")
.render();

ActivityLog activityLog =
ActivityLog.builder()
.entityId(team.getId())
.entityType(EntityType.Team)
.content(content)
.createdBy(SecurityUtils.getCurrentUserAuditorLogin())
.build();
activityLogRepository.save(activityLog);
}
}
7 changes: 7 additions & 0 deletions server/src/main/java/io/flexwork/security/SecurityUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.flexwork.security;

import io.flexwork.modules.usermanagement.AuthoritiesConstants;
import io.flexwork.modules.usermanagement.domain.User;
import io.flexwork.modules.usermanagement.service.dto.FwUserDetails;
import io.flexwork.modules.usermanagement.service.dto.UserKey;
import java.util.Arrays;
@@ -35,6 +36,12 @@ public static Optional<UserKey> getCurrentUserLogin() {
return Optional.ofNullable(extractPrincipal(securityContext.getAuthentication()));
}

public static User getCurrentUserAuditorLogin() {
return SecurityUtils.getCurrentUserLogin()
.map(userKey -> User.builder().id(userKey.getId()).build())
.orElse(null);
}

private static UserKey extractPrincipal(Authentication authentication) {
if (authentication == null) {
return null;