-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #371 from Consdata/IKC-394-user-groups-permissions
IKC-394 User groups permissions
- Loading branch information
Showing
48 changed files
with
808 additions
and
40 deletions.
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
22 changes: 22 additions & 0 deletions
22
...nd/src/main/java/com/consdata/kouncil/config/security/DefaultUserPermissionsReloader.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,22 @@ | ||
package com.consdata.kouncil.config.security; | ||
|
||
import com.consdata.kouncil.notifications.Notification; | ||
import com.consdata.kouncil.notifications.NotificationAction; | ||
import com.consdata.kouncil.notifications.NotificationType; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.messaging.simp.SimpMessagingTemplate; | ||
|
||
@RequiredArgsConstructor | ||
public class DefaultUserPermissionsReloader implements UserPermissionsReloader { | ||
|
||
private final SimpMessagingTemplate eventSender; | ||
|
||
@Override | ||
public void reloadPermissions() { | ||
Notification notification = new Notification(); | ||
notification.setMessage("User permissions were updated. You have to re-login."); | ||
notification.setType(NotificationType.PUSH_WITH_ACTION_REQUIRED); | ||
notification.setAction(NotificationAction.LOGOUT); | ||
eventSender.convertAndSend("/notifications", notification); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...l-backend/src/main/java/com/consdata/kouncil/config/security/UserPermissionsReloader.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,6 @@ | ||
package com.consdata.kouncil.config.security; | ||
|
||
public interface UserPermissionsReloader { | ||
|
||
void reloadPermissions(); | ||
} |
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
67 changes: 67 additions & 0 deletions
67
...n/java/com/consdata/kouncil/config/security/inmemory/InMemoryUserPermissionsReloader.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,67 @@ | ||
package com.consdata.kouncil.config.security.inmemory; | ||
|
||
import static com.consdata.kouncil.config.security.inmemory.InMemoryConst.ADMIN_CONFIG; | ||
import static com.consdata.kouncil.config.security.inmemory.InMemoryConst.ADMIN_USERNAME; | ||
import static com.consdata.kouncil.config.security.inmemory.InMemoryConst.EDITOR_CONFIG; | ||
import static com.consdata.kouncil.config.security.inmemory.InMemoryConst.EDITOR_USERNAME; | ||
import static com.consdata.kouncil.config.security.inmemory.InMemoryConst.SUPERUSER_CONFIG; | ||
import static com.consdata.kouncil.config.security.inmemory.InMemoryConst.SUPERUSER_USERNAME; | ||
import static com.consdata.kouncil.config.security.inmemory.InMemoryConst.VIEWER_CONFIG; | ||
import static com.consdata.kouncil.config.security.inmemory.InMemoryConst.VIEWER_USERNAME; | ||
|
||
import com.consdata.kouncil.KouncilRuntimeException; | ||
import com.consdata.kouncil.config.security.DefaultUserPermissionsReloader; | ||
import com.consdata.kouncil.security.UserRolesMapping; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.Set; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.messaging.simp.SimpMessagingTemplate; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.User; | ||
import org.springframework.security.provisioning.UserDetailsManager; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@ConditionalOnProperty(prefix = "kouncil.auth", name = "active-provider", havingValue = "inmemory") | ||
public class InMemoryUserPermissionsReloader extends DefaultUserPermissionsReloader { | ||
|
||
private final UserDetailsManager userDetailsService; | ||
private final UserRolesMapping userRolesMapping; | ||
|
||
public InMemoryUserPermissionsReloader(SimpMessagingTemplate eventSender, UserDetailsManager userDetailsService, | ||
UserRolesMapping userRolesMapping) { | ||
super(eventSender); | ||
this.userDetailsService = userDetailsService; | ||
this.userRolesMapping = userRolesMapping; | ||
} | ||
|
||
@Override | ||
public void reloadPermissions() { | ||
super.reloadPermissions(); | ||
List.of(ADMIN_USERNAME, EDITOR_USERNAME, VIEWER_USERNAME, SUPERUSER_USERNAME).forEach(user -> { | ||
try { | ||
String[] fileContent = Files.readString(getPath(user)).split(";"); | ||
userDetailsService.updateUser(User.withUsername(user) | ||
.password(String.format("{noop}%s", fileContent[0])) | ||
.authorities(userRolesMapping.mapToKouncilRoles(Set.of(fileContent[1].split(",")))) | ||
.build()); | ||
} catch (IOException e) { | ||
throw new KouncilRuntimeException(e); | ||
} | ||
}); | ||
} | ||
|
||
private Path getPath(String username) { | ||
return switch (username) { | ||
case ADMIN_USERNAME -> Paths.get(ADMIN_CONFIG); | ||
case EDITOR_USERNAME -> Paths.get(EDITOR_CONFIG); | ||
case VIEWER_USERNAME -> Paths.get(VIEWER_CONFIG); | ||
case SUPERUSER_USERNAME -> Paths.get(SUPERUSER_CONFIG); | ||
default -> throw new IllegalStateException(String.format("Can't find user: %s", SecurityContextHolder.getContext().getAuthentication().getName())); | ||
}; | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
kouncil-backend/src/main/java/com/consdata/kouncil/notifications/Notification.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,16 @@ | ||
package com.consdata.kouncil.notifications; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
public class Notification { | ||
|
||
private String message; | ||
private NotificationType type; | ||
private NotificationAction action; | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
kouncil-backend/src/main/java/com/consdata/kouncil/notifications/NotificationAction.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,6 @@ | ||
package com.consdata.kouncil.notifications; | ||
|
||
public enum NotificationAction { | ||
|
||
LOGOUT | ||
} |
6 changes: 6 additions & 0 deletions
6
kouncil-backend/src/main/java/com/consdata/kouncil/notifications/NotificationType.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,6 @@ | ||
package com.consdata.kouncil.notifications; | ||
|
||
public enum NotificationType { | ||
PUSH_WITH_ACTION_REQUIRED, | ||
PUSH | ||
} |
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
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
Oops, something went wrong.