-
Notifications
You must be signed in to change notification settings - Fork 7
[NAE-2241] Anonymous access refactor #408
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
base: release/7.0.0-rev10
Are you sure you want to change the base?
Changes from all commits
be10d44
83606bc
69d7f85
8cd0f08
c24bbaf
2c4a084
9ce5fd7
57e9f59
140a35d
e260dbe
d4abf76
5dfe8c6
8d3923f
b6b4550
b4933a9
1a11c5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package com.netgrif.application.engine.auth.web; | ||
| package com.netgrif.application.engine.auth.web; | ||
|
|
||
| import com.netgrif.application.engine.adapter.spring.common.web.responsebodies.ResponseMessage; | ||
| import com.netgrif.application.engine.adapter.spring.petrinet.service.ProcessRoleService; | ||
|
|
@@ -8,10 +8,7 @@ | |
| import com.netgrif.application.engine.auth.web.requestbodies.UserSearchRequestBody; | ||
| import com.netgrif.application.engine.auth.web.responsebodies.PreferencesResource; | ||
| import com.netgrif.application.engine.auth.web.responsebodies.User; | ||
| import com.netgrif.application.engine.objects.auth.domain.AbstractUser; | ||
| import com.netgrif.application.engine.objects.auth.domain.Authority; | ||
| import com.netgrif.application.engine.objects.auth.domain.LoggedUser; | ||
| import com.netgrif.application.engine.objects.auth.domain.Realm; | ||
| import com.netgrif.application.engine.objects.auth.domain.*; | ||
| import com.netgrif.application.engine.objects.preferences.Preferences; | ||
| import com.netgrif.application.engine.objects.workflow.domain.ProcessResourceId; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
|
|
@@ -30,6 +27,7 @@ | |
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.access.prepost.PreAuthorize; | ||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.security.core.context.SecurityContextHolder; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.*; | ||
|
|
@@ -112,10 +110,10 @@ public ResponseEntity<Page<User>> getAllUsers(@PathVariable String realmId, Page | |
| }) | ||
| @GetMapping(value = "/me", produces = MediaType.APPLICATION_JSON_VALUE) | ||
| public ResponseEntity<User> getLoggedUser(Authentication auth, Locale locale) { | ||
| LoggedUser loggedUser = (LoggedUser) auth.getPrincipal(); | ||
| LoggedUser loggedUser = resolveAuthenticationToken(auth); | ||
| AbstractUser user; | ||
| try { | ||
| user = userService.findById(loggedUser.getStringId(), loggedUser.getRealmId()); | ||
| user = resolveLoggedUser(loggedUser); | ||
| if (user == null) { | ||
| return ResponseEntity | ||
| .status(HttpStatus.UNAUTHORIZED).build(); | ||
|
|
@@ -294,11 +292,14 @@ public ResponseEntity<ResponseMessage> assignAuthorityToUser(@PathVariable("real | |
| }) | ||
| @GetMapping(value = "/preferences", produces = MediaType.APPLICATION_JSON_VALUE) | ||
| public ResponseEntity<PreferencesResource> preferences(Authentication auth) { | ||
| String userId = ((LoggedUser) auth.getPrincipal()).getStringId(); | ||
| Preferences preferences = preferencesService.get(userId); | ||
| LoggedUser loggedUser = resolveAuthenticationToken(auth); | ||
| if (loggedUser == null) { | ||
| return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); | ||
| } | ||
| Preferences preferences = preferencesService.get(loggedUser.getStringId()); | ||
|
|
||
| if (preferences == null) { | ||
| preferences = new com.netgrif.application.engine.adapter.spring.preferences.Preferences(userId); | ||
| preferences = new com.netgrif.application.engine.adapter.spring.preferences.Preferences(loggedUser.getStringId()); | ||
| } | ||
| PreferencesResource preferencesResource = PreferencesResource.withPreferences(preferences); | ||
|
|
||
|
|
@@ -337,4 +338,25 @@ private boolean realmExists(String realmId) { | |
| Optional<Realm> realm = realmService.getRealmById(realmId); | ||
| return realm.isPresent(); | ||
| } | ||
|
|
||
| private LoggedUser resolveAuthenticationToken(Authentication auth) { | ||
| if (auth != null) { | ||
| return (LoggedUser) auth.getPrincipal(); | ||
| } | ||
| auth = SecurityContextHolder.getContext().getAuthentication(); | ||
| if (auth != null) { | ||
| return (LoggedUser) auth.getPrincipal(); | ||
| } | ||
| return null; | ||
| } | ||
|
Comment on lines
+342
to
+351
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Check all calls to resolveAuthenticationToken and their context
rg -n "resolveAuthenticationToken" --type java -B 5 -A 5Repository: netgrif/application-engine Length of output: 4910 Remove redundant fallback to Both usages of 🤖 Prompt for AI Agents |
||
|
|
||
| private AbstractUser resolveLoggedUser(LoggedUser loggedUser) { | ||
| if (loggedUser == null) { | ||
| return null; | ||
| } | ||
| if (loggedUser.isAnonymous()) { | ||
| return ActorTransformer.toUser(loggedUser); | ||
| } | ||
| return userService.findById(loggedUser.getStringId(), loggedUser.getRealmId()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,20 @@ | ||
| package com.netgrif.application.engine.configuration; | ||
|
|
||
| import com.netgrif.application.engine.auth.service.DefaultLoggedUserFactory; | ||
| import com.netgrif.application.engine.objects.auth.domain.ActorTransformer; | ||
| import jakarta.annotation.PostConstruct; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| @RequiredArgsConstructor | ||
| public class LoggedUserConfiguration { | ||
|
|
||
| private final ActorTransformer.LoggedUserFactory loggedUserFactory; | ||
| private final ActorTransformer.UserFactory userFactory; | ||
|
|
||
| @PostConstruct | ||
| public void initializeLoggedUserFactory() { | ||
| ActorTransformer.setLoggedUserFactory(new DefaultLoggedUserFactory()); | ||
| ActorTransformer.setLoggedUserFactory(loggedUserFactory); | ||
| ActorTransformer.setUserFactory(userFactory); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick | 🔵 Trivial
Consider optimizing authority lookup with a Set.
The current implementation recreates
Arrays.stream(authority)for each authority in the user's set, resulting in O(n×m) complexity. Converting the varargs to aSetfirst would improve this to O(n) with O(1) lookups.Also, consider renaming the parameter to
authorities(plural) for clarity with varargs.♻️ Proposed optimization
`@Override` public boolean hasAnyAuthority(String... authority) { // TODO: impersonation // LoggedUser loggedUser = userService.getLoggedUserFromContext().getSelfOrImpersonated(); LoggedUser loggedUser = userService.getLoggedUserFromContext(); - return loggedUser.getAuthoritySet().stream().anyMatch(it -> Arrays.stream(authority).anyMatch(a -> it.getAuthority().equals(a))); + Set<String> authoritySet = Set.of(authority); + return loggedUser.getAuthoritySet().stream().anyMatch(it -> authoritySet.contains(it.getAuthority())); }📝 Committable suggestion
🤖 Prompt for AI Agents