-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add synchronization to new ad structure
- Loading branch information
mateusz.uzarek
committed
Oct 3, 2024
1 parent
f758d11
commit cf45a92
Showing
18 changed files
with
579 additions
and
110 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
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
14 changes: 13 additions & 1 deletion
14
src/main/java/info/fingo/urlopia/config/ad/ActiveDirectoryObjectClass.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,5 +1,17 @@ | ||
package info.fingo.urlopia.config.ad; | ||
|
||
public enum ActiveDirectoryObjectClass { | ||
Person, Group | ||
PERSON("person"), | ||
GROUP("group"), | ||
ORGANIZATIONAL_UNIT("organizationalUnit"); | ||
|
||
private final String key; | ||
|
||
ActiveDirectoryObjectClass(String key) { | ||
this.key = key; | ||
} | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
src/main/java/info/fingo/urlopia/config/ad/tree/ActiveDirectoryNode.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,57 @@ | ||
package info.fingo.urlopia.config.ad.tree; | ||
|
||
import info.fingo.urlopia.config.ad.ActiveDirectoryUtils; | ||
import info.fingo.urlopia.config.ad.Attribute; | ||
|
||
import javax.naming.directory.SearchResult; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class ActiveDirectoryNode { | ||
|
||
private final String relativeDN; | ||
private final SearchResult object; | ||
private final Map<String, ActiveDirectoryNode> children; | ||
|
||
protected ActiveDirectoryNode(SearchResult object) { | ||
this.relativeDN = getRDN(object); | ||
this.object = object; | ||
this.children = new HashMap<>(); | ||
} | ||
|
||
protected ActiveDirectoryNode(String distinguishedName) { | ||
this.relativeDN = getRDN(distinguishedName); | ||
this.object = null; | ||
this.children = new HashMap<>(); | ||
} | ||
|
||
public void add(ActiveDirectoryNode child) { | ||
children.put(child.relativeDN, child); | ||
} | ||
|
||
public Optional<ActiveDirectoryNode> getChild(String childRelativeDistinguishedName) { | ||
return Optional.ofNullable(children.get(childRelativeDistinguishedName)); | ||
} | ||
|
||
private static String getRDN(SearchResult object) { | ||
var distinguishedName = ActiveDirectoryUtils.pickAttribute(object, Attribute.DISTINGUISHED_NAME); | ||
return getRDN(distinguishedName); | ||
} | ||
|
||
private static String getRDN(String distinguishedName) { | ||
return distinguishedName.split(",", 2)[0]; | ||
} | ||
|
||
public List<SearchResult> getDirectChildrenObjects() { | ||
return children.values().stream() | ||
.map(child -> child.object) | ||
.toList(); | ||
} | ||
|
||
public SearchResult getObject() { | ||
return object; | ||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/info/fingo/urlopia/config/ad/tree/ActiveDirectoryTree.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,69 @@ | ||
package info.fingo.urlopia.config.ad.tree; | ||
|
||
import info.fingo.urlopia.config.ad.ActiveDirectoryUtils; | ||
import info.fingo.urlopia.config.ad.Attribute; | ||
|
||
import javax.naming.directory.SearchResult; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class ActiveDirectoryTree { | ||
|
||
private final String mainContainerDn; | ||
private final ActiveDirectoryNode root; | ||
|
||
public ActiveDirectoryTree(String mainContainerDn) { | ||
this.mainContainerDn = mainContainerDn; | ||
this.root = new ActiveDirectoryNode(mainContainerDn); | ||
} | ||
|
||
public void put(SearchResult object) { | ||
var childDn = ActiveDirectoryUtils.pickAttribute(object, Attribute.DISTINGUISHED_NAME); | ||
var parentDn = ActiveDirectoryUtils.getParentDN(childDn); | ||
var parentNode = searchNode(parentDn); | ||
parentNode.ifPresentOrElse( | ||
pNode -> { | ||
var childNode = new ActiveDirectoryNode(object); | ||
pNode.add(childNode); | ||
}, | ||
() -> { | ||
throw ActiveDirectoryTreeException.missingParent(childDn); | ||
} | ||
); | ||
} | ||
|
||
public List<SearchResult> searchDirectChildrenObjectsOf(String distinguishedName) { | ||
return searchNode(distinguishedName) | ||
.map(ActiveDirectoryNode::getDirectChildrenObjects) | ||
.orElse(List.of()); | ||
} | ||
|
||
public Optional<SearchResult> search(String distinguishedName) { | ||
return searchNode(distinguishedName).map(ActiveDirectoryNode::getObject); | ||
} | ||
|
||
private Optional<ActiveDirectoryNode> searchNode(String distinguishedName) { | ||
var relativeDn = ActiveDirectoryUtils.getRelativeDN(distinguishedName, mainContainerDn); | ||
if (relativeDn.isBlank()) { | ||
return Optional.of(root); | ||
} | ||
var dnParts = Arrays.stream(relativeDn.split(",")).toList(); | ||
return searchNode(root, dnParts); | ||
} | ||
|
||
private Optional<ActiveDirectoryNode> searchNode(ActiveDirectoryNode node, | ||
List<String> dnParts) { | ||
if (dnParts.isEmpty()) { | ||
return Optional.of(node); | ||
} | ||
var topDnPartIdx = dnParts.size() - 1; | ||
var topDnPart = dnParts.get(topDnPartIdx); | ||
var topNode = node.getChild(topDnPart); | ||
var bottomDnParts = dnParts.stream() | ||
.limit(topDnPartIdx) | ||
.toList(); | ||
return topNode.flatMap(n -> searchNode(n, bottomDnParts)); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/info/fingo/urlopia/config/ad/tree/ActiveDirectoryTreeException.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,15 @@ | ||
package info.fingo.urlopia.config.ad.tree; | ||
|
||
public class ActiveDirectoryTreeException extends RuntimeException { | ||
|
||
private static final String MISSING_PARENT = "Missing parent node for object %s"; | ||
|
||
private ActiveDirectoryTreeException(String message) { | ||
super(message); | ||
} | ||
|
||
public static ActiveDirectoryTreeException missingParent(String child) { | ||
return new ActiveDirectoryTreeException(MISSING_PARENT.formatted(child)); | ||
} | ||
|
||
} |
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
48 changes: 48 additions & 0 deletions
48
src/main/java/info/fingo/urlopia/team/ActiveDirectoryTeamLeaderProvider.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,48 @@ | ||
package info.fingo.urlopia.team; | ||
|
||
import info.fingo.urlopia.config.ad.ActiveDirectoryUtils; | ||
import info.fingo.urlopia.config.ad.Attribute; | ||
import info.fingo.urlopia.config.ad.tree.ActiveDirectoryTree; | ||
import info.fingo.urlopia.user.User; | ||
import info.fingo.urlopia.user.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.naming.directory.SearchResult; | ||
import java.util.Optional; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@ConditionalOnProperty(name = "ad.configuration.enabled", havingValue = "true", matchIfMissing = true) | ||
public class ActiveDirectoryTeamLeaderProvider { | ||
|
||
private final UserRepository userRepository; | ||
|
||
public Optional<User> getTeamLeader(String adTeamDN, | ||
ActiveDirectoryTree adTeamsTree) { | ||
return adTeamsTree.search(adTeamDN) | ||
.flatMap(adTeam -> { | ||
var managedBy = getManagedBy(adTeam); | ||
return managedBy | ||
.map(this::getUser) | ||
.orElseGet(() -> checkParentTeam(adTeamDN, adTeamsTree)); | ||
}); | ||
} | ||
|
||
private Optional<String> getManagedBy(SearchResult adTeam) { | ||
return Optional.ofNullable(ActiveDirectoryUtils.pickAttribute(adTeam, Attribute.MANAGED_BY)) | ||
.filter(managedBy -> !managedBy.isBlank()); | ||
} | ||
|
||
private Optional<User> checkParentTeam(String adTeamDN, | ||
ActiveDirectoryTree adTeamsTree) { | ||
var parentTeamDN = ActiveDirectoryUtils.getParentDN(adTeamDN); | ||
return getTeamLeader(parentTeamDN, adTeamsTree); | ||
} | ||
|
||
private Optional<User> getUser(String userDN) { | ||
System.out.println("repo:" + userDN); | ||
return userRepository.findFirstByAdName(userDN); | ||
} | ||
} |
Oops, something went wrong.