-
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.
Change the way leaders are selected for request acceptance (#244)
Co-authored-by: witold.brzozowski <[email protected]> Co-authored-by: mateusz.uzarek <[email protected]>
- Loading branch information
1 parent
bb0723b
commit 0339204
Showing
27 changed files
with
1,010 additions
and
483 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; | ||
} | ||
} |
218 changes: 121 additions & 97 deletions
218
src/main/java/info/fingo/urlopia/config/ad/ActiveDirectorySearcher.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,97 +1,121 @@ | ||
package info.fingo.urlopia.config.ad; | ||
|
||
import info.fingo.urlopia.config.authentication.LDAPConnectionService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
|
||
import javax.naming.NamingException; | ||
import javax.naming.directory.DirContext; | ||
import javax.naming.directory.SearchControls; | ||
import javax.naming.directory.SearchResult; | ||
import java.util.Collections; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
@ConditionalOnProperty(name = "ad.configuration.enabled", havingValue = "true", matchIfMissing = true) | ||
public class ActiveDirectorySearcher { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ActiveDirectorySearcher.class); | ||
|
||
private final StringBuilder filter = new StringBuilder("(&"); | ||
private final String mainContainer; | ||
private final LDAPConnectionService ldapConnectionService; | ||
|
||
public ActiveDirectorySearcher(String mainContainer, | ||
LDAPConnectionService ldapConnectionService) { | ||
this.mainContainer = mainContainer; | ||
this.ldapConnectionService = ldapConnectionService; | ||
} | ||
|
||
public ActiveDirectorySearcher objectClass(ActiveDirectoryObjectClass objectClass) { | ||
var value = String.format("(objectClass=%s)", objectClass.name()); | ||
filter.append(value); | ||
return this; | ||
} | ||
|
||
public ActiveDirectorySearcher memberOf(String group) { | ||
var value = String.format("(memberOf=%s)", group); | ||
filter.append(value); | ||
return this; | ||
} | ||
|
||
public ActiveDirectorySearcher mail(String mail) { | ||
var value = String.format("(mail=%s)", mail); | ||
filter.append(value); | ||
return this; | ||
} | ||
|
||
public ActiveDirectorySearcher name(String name) { | ||
var value = String.format("(name=%s)", name); | ||
filter.append(value); | ||
return this; | ||
} | ||
|
||
public ActiveDirectorySearcher distinguishedName(String distinguishedName) { | ||
var value = String.format("(distinguishedName=%s)", distinguishedName); | ||
filter.append(value); | ||
return this; | ||
} | ||
|
||
public ActiveDirectorySearcher isDisabled(){ | ||
var builder = new StringBuilder("(|"); | ||
for (var disableKey: ActiveDirectoryUtils.DISABLED_STATUS){ | ||
var value = String.format("(%s=%s)",Attribute.USER_ACCOUNT_CONTROL.getKey(), disableKey); | ||
builder.append(value); | ||
} | ||
builder.append(")"); | ||
filter.append(builder); | ||
return this; | ||
} | ||
|
||
public List<SearchResult> search() { | ||
var filter = this.filter.append(")").toString(); | ||
List<SearchResult> result = new LinkedList<>(); | ||
|
||
var controls = new SearchControls(); | ||
controls.setSearchScope(SearchControls.SUBTREE_SCOPE); | ||
|
||
// connecting to AD and getting data | ||
DirContext ad = null; | ||
try { | ||
ad = ldapConnectionService.getContext(); | ||
result = Collections.list(ad.search(mainContainer, filter, controls)); | ||
} catch (NamingException e) { | ||
LOGGER.error("Exception when trying to search in Active Directory", e); | ||
} finally { | ||
try { | ||
if (ad != null) { | ||
ad.close(); | ||
} | ||
} catch (NamingException e) { | ||
LOGGER.error("Exception when trying to close the LDAP connection", e); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
package info.fingo.urlopia.config.ad; | ||
|
||
import info.fingo.urlopia.config.authentication.LDAPConnectionService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
|
||
import javax.naming.NamingException; | ||
import javax.naming.directory.DirContext; | ||
import javax.naming.directory.SearchControls; | ||
import javax.naming.directory.SearchResult; | ||
import java.util.Collections; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@ConditionalOnProperty(name = "ad.configuration.enabled", havingValue = "true", matchIfMissing = true) | ||
public class ActiveDirectorySearcher { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ActiveDirectorySearcher.class); | ||
|
||
private final StringBuilder filter = new StringBuilder("(&"); | ||
private final String mainContainer; | ||
private final LDAPConnectionService ldapConnectionService; | ||
|
||
public ActiveDirectorySearcher(String mainContainer, | ||
LDAPConnectionService ldapConnectionService) { | ||
this.mainContainer = mainContainer; | ||
this.ldapConnectionService = ldapConnectionService; | ||
} | ||
|
||
public ActiveDirectorySearcher objectClass(ActiveDirectoryObjectClass objectClass) { | ||
var value = String.format("(objectClass=%s)", objectClass.getKey()); | ||
filter.append(value); | ||
return this; | ||
} | ||
|
||
public ActiveDirectorySearcher objectClasses(List<ActiveDirectoryObjectClass> objectClasses) { | ||
var value = objectClasses.stream() | ||
.map(objClass -> String.format("(objectClass=%s)", objClass.getKey())) | ||
.collect(Collectors.joining("", "(|", ")")); | ||
filter.append(value); | ||
return this; | ||
} | ||
|
||
public ActiveDirectorySearcher memberOf(String group) { | ||
var value = String.format("(memberOf=%s)", group); | ||
filter.append(value); | ||
return this; | ||
} | ||
|
||
public ActiveDirectorySearcher principalName(String principalName) { | ||
var value = String.format("(userPrincipalName=%s)", principalName); | ||
filter.append(value); | ||
return this; | ||
} | ||
|
||
public ActiveDirectorySearcher mail(String mail) { | ||
var value = String.format("(mail=%s)", mail); | ||
filter.append(value); | ||
return this; | ||
} | ||
|
||
public ActiveDirectorySearcher name(String name) { | ||
var value = String.format("(name=%s)", name); | ||
filter.append(value); | ||
return this; | ||
} | ||
|
||
public ActiveDirectorySearcher distinguishedName(String distinguishedName) { | ||
var value = String.format("(distinguishedName=%s)", distinguishedName); | ||
filter.append(value); | ||
return this; | ||
} | ||
|
||
public ActiveDirectorySearcher excludeDistinguishedName(String distinguishedName) { | ||
var value = String.format("(!(distinguishedName=%s))", distinguishedName); | ||
filter.append(value); | ||
return this; | ||
} | ||
|
||
public ActiveDirectorySearcher isDisabled(){ | ||
var builder = new StringBuilder("(|"); | ||
for (var disableKey: ActiveDirectoryUtils.DISABLED_STATUS){ | ||
var value = String.format("(%s=%s)",Attribute.USER_ACCOUNT_CONTROL.getKey(), disableKey); | ||
builder.append(value); | ||
} | ||
builder.append(")"); | ||
filter.append(builder); | ||
return this; | ||
} | ||
|
||
public List<SearchResult> search() { | ||
var controls = new SearchControls(); | ||
controls.setSearchScope(SearchControls.SUBTREE_SCOPE); | ||
return search(controls); | ||
} | ||
|
||
public List<SearchResult> search(SearchControls controls) { | ||
var filter = this.filter.append(")").toString(); | ||
List<SearchResult> result = new LinkedList<>(); | ||
|
||
// connecting to AD and getting data | ||
DirContext ad = null; | ||
try { | ||
ad = ldapConnectionService.getContext(); | ||
result = Collections.list(ad.search(mainContainer, filter, controls)); | ||
} catch (NamingException e) { | ||
LOGGER.error("Exception when trying to search in Active Directory", e); | ||
} finally { | ||
try { | ||
if (ad != null) { | ||
ad.close(); | ||
} | ||
} catch (NamingException e) { | ||
LOGGER.error("Exception when trying to close the LDAP connection", e); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} |
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; | ||
} | ||
|
||
} |
Oops, something went wrong.