-
Notifications
You must be signed in to change notification settings - Fork 29
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 #12 from devondragon:issue-11-Enhance-Roles-and-Pr…
…iveleges Issue-11-Enhance-Roles-and-Priveleges
- Loading branch information
Showing
14 changed files
with
344 additions
and
118 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,4 +123,6 @@ gradle-app.setting | |
!/config/README.md | ||
!/config/*.example | ||
|
||
application-local.yml | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
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
81 changes: 81 additions & 0 deletions
81
src/main/java/com/digitalsanctuary/spring/user/service/RolePrivilegeSetupService.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,81 @@ | ||
package com.digitalsanctuary.spring.user.service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import javax.transaction.Transactional; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.context.event.ContextRefreshedEvent; | ||
import org.springframework.stereotype.Component; | ||
import com.digitalsanctuary.spring.user.persistence.model.Privilege; | ||
import com.digitalsanctuary.spring.user.persistence.model.Role; | ||
import com.digitalsanctuary.spring.user.persistence.repository.PrivilegeRepository; | ||
import com.digitalsanctuary.spring.user.persistence.repository.RoleRepository; | ||
import com.digitalsanctuary.spring.user.util.RolesAndPrivilegesConfig; | ||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Data | ||
@Component | ||
public class RolePrivilegeSetupService implements ApplicationListener<ContextRefreshedEvent> { | ||
private boolean alreadySetup = false; | ||
|
||
@Autowired | ||
private RolesAndPrivilegesConfig rolesAndPrivilegesConfig; | ||
|
||
|
||
@Autowired | ||
private RoleRepository roleRepository; | ||
|
||
@Autowired | ||
private PrivilegeRepository privilegeRepository; | ||
|
||
|
||
@Override | ||
@Transactional | ||
public void onApplicationEvent(final ContextRefreshedEvent event) { | ||
if (alreadySetup) { | ||
return; | ||
} | ||
|
||
log.debug("rolesAndPrivilegesConfig: " + rolesAndPrivilegesConfig); | ||
|
||
for (Map.Entry<String, List<String>> entry : rolesAndPrivilegesConfig.getRolesAndPrivileges().entrySet()) { | ||
final String roleName = entry.getKey(); | ||
final List<String> privileges = entry.getValue(); | ||
if (roleName != null && privileges != null) { | ||
final List<Privilege> privilegesList = new ArrayList<Privilege>(); | ||
for (String privilegeName : privileges) { | ||
privilegesList.add(createPrivilegeIfNotFound(privilegeName)); | ||
} | ||
createRoleIfNotFound(roleName, privilegesList); | ||
} | ||
|
||
} | ||
alreadySetup = true; | ||
} | ||
|
||
@Transactional | ||
Privilege createPrivilegeIfNotFound(final String name) { | ||
Privilege privilege = privilegeRepository.findByName(name); | ||
if (privilege == null) { | ||
privilege = new Privilege(name); | ||
privilege = privilegeRepository.save(privilege); | ||
} | ||
return privilege; | ||
} | ||
|
||
@Transactional | ||
Role createRoleIfNotFound(final String name, final Collection<Privilege> privileges) { | ||
Role role = roleRepository.findByName(name); | ||
if (role == null) { | ||
role = new Role(name); | ||
} | ||
role.setPrivileges(privileges); | ||
role = roleRepository.save(role); | ||
return role; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/digitalsanctuary/spring/user/util/RolesAndPrivilegesConfig.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,38 @@ | ||
package com.digitalsanctuary.spring.user.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Configuration | ||
@ConfigurationProperties(prefix = "user") | ||
public class RolesAndPrivilegesConfig { | ||
private Map<String, List<String>> rolesAndPrivileges = new HashMap<String, List<String>>(); | ||
|
||
private List<String> roleHierarchy = new ArrayList<String>(); | ||
|
||
public String getRoleHierarchyString() { | ||
StringBuffer roleHierarchyStringBuf = new StringBuffer(); | ||
if (roleHierarchy != null && !roleHierarchy.isEmpty()) { | ||
|
||
for (String roleRelationship : roleHierarchy) { | ||
roleHierarchyStringBuf.append(roleRelationship); | ||
roleHierarchyStringBuf.append("\n"); | ||
} | ||
} | ||
// If we have built a list of hierarchy relationships, then return it. | ||
if (roleHierarchyStringBuf.length() > 0) { | ||
return roleHierarchyStringBuf.toString(); | ||
} else { | ||
// otherwise, return null. | ||
return null; | ||
} | ||
|
||
} | ||
|
||
} |
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 was deleted.
Oops, something went wrong.
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,32 @@ | ||
debug: true | ||
|
||
logging: | ||
level: | ||
com: | ||
digitalsanctuary: | ||
spring: DEBUG | ||
org: | ||
springframework: | ||
security: DEBUG | ||
web: | ||
filter: | ||
CommonsRequestLoggingFilter: DEBUG | ||
nodeValue: DEBUG | ||
spring: | ||
mvc: | ||
log-request-details: true | ||
thymeleaf: | ||
cache: false | ||
devtools: | ||
restart: | ||
enabled: true | ||
|
||
server: | ||
servlet: | ||
session: | ||
cookie: | ||
secure: false | ||
user: | ||
audit: | ||
flushOnWrite: true | ||
|
File renamed without changes.
Oops, something went wrong.