-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
application/src/main/java/net/furizon/backend/feature/user/UserSession.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package net.furizon.backend.feature.user; | ||
|
||
import lombok.Data; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.time.OffsetDateTime; | ||
import java.util.UUID; | ||
|
||
@Data | ||
public class UserSession { | ||
@NotNull | ||
private final UUID sessionId; | ||
|
||
@Nullable | ||
private final String userAgent; | ||
|
||
@NotNull | ||
private final OffsetDateTime createdAt; | ||
|
||
@NotNull | ||
private final OffsetDateTime lastUsageAt; | ||
} |
19 changes: 18 additions & 1 deletion
19
...user/controller/SimpleUserController.java → ...ature/user/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,38 @@ | ||
package net.furizon.backend.feature.user.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import net.furizon.backend.feature.user.UserSession; | ||
import net.furizon.backend.feature.user.usecase.GetUserSessionsUseCase; | ||
import net.furizon.backend.infrastructure.security.FurizonUser; | ||
import net.furizon.backend.infrastructure.usecase.UseCaseExecutor; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/users") | ||
@RequiredArgsConstructor | ||
public class SimpleUserController { | ||
public class UserController { | ||
private final UseCaseExecutor executor; | ||
|
||
@GetMapping("/me") | ||
public FurizonUser getMe( | ||
@AuthenticationPrincipal @NotNull FurizonUser user | ||
) { | ||
return user; | ||
} | ||
|
||
@GetMapping("/me/sessions") | ||
public List<UserSession> getMeSessions( | ||
@AuthenticationPrincipal @NotNull FurizonUser user | ||
) { | ||
return executor.execute( | ||
GetUserSessionsUseCase.class, | ||
new GetUserSessionsUseCase.Input(user.getUserId()) | ||
); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...cation/src/main/java/net/furizon/backend/feature/user/usecase/GetUserSessionsUseCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package net.furizon.backend.feature.user.usecase; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import net.furizon.backend.feature.user.UserSession; | ||
import net.furizon.backend.infrastructure.security.session.finder.SessionFinder; | ||
import net.furizon.backend.infrastructure.usecase.UseCase; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class GetUserSessionsUseCase implements UseCase<GetUserSessionsUseCase.Input, List<UserSession>> { | ||
private final SessionFinder sessionFinder; | ||
|
||
@Override | ||
public @NotNull List<UserSession> executor(@NotNull Input input) { | ||
return sessionFinder.getUserSessions(input.userId) | ||
.stream() | ||
.map(session -> | ||
new UserSession( | ||
session.getId(), | ||
session.getUserAgent(), | ||
session.getCreatedAt(), | ||
session.getModifiedAt() | ||
) | ||
) | ||
.toList(); | ||
} | ||
|
||
public record Input(long userId) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...ucture/security/session/action/clearNewestUserSessions/ClearNewestUserSessionsAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package net.furizon.backend.infrastructure.security.session.action.clearNewestUserSessions; | ||
|
||
public interface ClearNewestUserSessionsAction { | ||
void invoke(long userId); | ||
} |
37 changes: 37 additions & 0 deletions
37
...re/security/session/action/clearNewestUserSessions/JooqClearNewestUserSessionsAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package net.furizon.backend.infrastructure.security.session.action.clearNewestUserSessions; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import net.furizon.backend.infrastructure.security.SecurityConfig; | ||
import net.furizon.jooq.infrastructure.command.SqlCommand; | ||
import org.jooq.util.postgres.PostgresDSL; | ||
import org.springframework.stereotype.Component; | ||
|
||
import static net.furizon.jooq.generated.Tables.SESSIONS; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class JooqClearNewestUserSessionsAction implements ClearNewestUserSessionsAction { | ||
private final SqlCommand sqlCommand; | ||
|
||
private final SecurityConfig config; | ||
|
||
@Override | ||
public void invoke(long userId) { | ||
sqlCommand.execute( | ||
PostgresDSL.deleteFrom(SESSIONS) | ||
.where(SESSIONS.USER_ID.eq(userId)) | ||
.and( | ||
SESSIONS.CREATED_AT.eq( | ||
PostgresDSL | ||
.select(SESSIONS.CREATED_AT) | ||
.from(SESSIONS) | ||
.where(SESSIONS.USER_ID.eq(userId)) | ||
.orderBy(SESSIONS.CREATED_AT) | ||
.limit(1) | ||
// -1 because we are planing to insert one more session after the clean | ||
.offset(config.getSession().getMaxAllowedSessionsSize() - 1) | ||
) | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters