Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MGR-102 show LDAP settings and users
- Loading branch information
1 parent
a34832a
commit 38a3f00
Showing
9 changed files
with
342 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<datasources xmlns="http://www.appng.org/schema/platform" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.appng.org/schema/platform http://www.appng.org/schema/platform/appng-platform.xsd"> | ||
|
||
<datasource id="ldapUsers"> | ||
<config> | ||
<title id="ldap.users" /> | ||
<params /> | ||
<meta-data bindClass="org.appng.application.manager.business.LdapUsers.LdapUser"> | ||
<field name="name" type="text"> | ||
<label id="name" /> | ||
</field> | ||
<field name="realname" type="text"> | ||
<label id="realname" /> | ||
</field> | ||
<field name="type" type="text"> | ||
<label id="type" /> | ||
</field> | ||
<field name="dn" type="text" displayLength="80"> | ||
<label id="distinguishedName" /> | ||
</field> | ||
</meta-data> | ||
</config> | ||
<bean id="ldapUsers"> | ||
</bean> | ||
</datasource> | ||
|
||
<datasource id="ldapSettings"> | ||
<config> | ||
<title id="ldap.settings" /> | ||
<meta-data bindClass="org.appng.api.model.SimpleProperty"> | ||
<field name="name" type="text"> | ||
<label id="name" /> | ||
</field> | ||
<field name="value" type="text"> | ||
<label id="value" /> | ||
</field> | ||
</meta-data> | ||
</config> | ||
<bean id="ldapUsers"> | ||
<option name="mode" value="settings" /> | ||
</bean> | ||
</datasource> | ||
|
||
</datasources> |
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
118 changes: 118 additions & 0 deletions
118
src/main/java/org/appng/application/manager/business/LdapUsers.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,118 @@ | ||
/* | ||
* Copyright 2011-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.appng.application.manager.business; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.appng.api.DataContainer; | ||
import org.appng.api.DataProvider; | ||
import org.appng.api.Environment; | ||
import org.appng.api.FieldProcessor; | ||
import org.appng.api.Options; | ||
import org.appng.api.Request; | ||
import org.appng.api.model.Application; | ||
import org.appng.api.model.Properties; | ||
import org.appng.api.model.Property; | ||
import org.appng.api.model.SimpleProperty; | ||
import org.appng.api.model.Site; | ||
import org.appng.api.model.UserType; | ||
import org.appng.application.manager.MessageConstants; | ||
import org.appng.core.domain.SubjectImpl; | ||
import org.appng.core.service.CoreService; | ||
import org.appng.core.service.LdapService; | ||
import org.springframework.stereotype.Component; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
|
||
@Component | ||
public class LdapUsers implements DataProvider { | ||
|
||
private CoreService coreService; | ||
|
||
private LdapUsers(CoreService coreService) { | ||
this.coreService = coreService; | ||
} | ||
|
||
@Override | ||
public DataContainer getData(Site site, Application application, Environment environment, Options options, | ||
Request request, FieldProcessor fp) { | ||
|
||
DataContainer dataContainer = new DataContainer(fp); | ||
String mode = options.getString("mode", "value"); | ||
LdapService ldapService = new LdapService(); | ||
|
||
Properties siteProps = site.getProperties(); | ||
if ("settings".equals(mode)) { | ||
List<Property> ldapProps = new ArrayList<>(); | ||
SimpleProperty ldapHost = getProperty(siteProps, LdapService.LDAP_HOST); | ||
ldapProps.add(ldapHost); | ||
ldapProps.add(getProperty(siteProps, LdapService.LDAP_DOMAIN)); | ||
ldapProps.add(getProperty(siteProps, LdapService.LDAP_GROUP_BASE_DN)); | ||
SimpleProperty ldapUser = getProperty(siteProps, LdapService.LDAP_USER); | ||
ldapProps.add(ldapUser); | ||
ldapProps.add(getProperty(siteProps, LdapService.LDAP_ID_ATTRIBUTE)); | ||
ldapProps.add(getProperty(siteProps, LdapService.LDAP_PRINCIPAL_SCHEME)); | ||
ldapProps.add(getProperty(siteProps, LdapService.LDAP_START_TLS)); | ||
dataContainer.setItems(ldapProps); | ||
char[] ldapPw = siteProps.getString(LdapService.LDAP_PASSWORD, StringUtils.EMPTY).toCharArray(); | ||
boolean adminLoginOk = ldapService.loginUser(site, ldapUser.getString(), ldapPw); | ||
if (!adminLoginOk) { | ||
fp.addErrorMessage(request.getMessage(MessageConstants.LDAP_NOT_WORKING, ldapHost.getString())); | ||
} | ||
} else { | ||
|
||
List<LdapUser> users = new ArrayList<>(); | ||
|
||
List<SubjectImpl> globalGroups = coreService.getSubjectsByType(UserType.GLOBAL_GROUP); | ||
for (SubjectImpl globalGroup : globalGroups) { | ||
List<SubjectImpl> membersOfGroup = ldapService.getMembersOfGroup(site, globalGroup.getName()); | ||
users.addAll(membersOfGroup.stream().map(s -> new LdapUser(s.getName(), s.getEmail(), s.getRealname(), | ||
UserType.GLOBAL_GROUP, globalGroup.getName())).collect(Collectors.toList())); | ||
} | ||
List<SubjectImpl> globalUsers = coreService.getSubjectsByType(UserType.GLOBAL_USER); | ||
String userDn = siteProps.getString(LdapService.LDAP_ID_ATTRIBUTE) + "=%s," | ||
+ siteProps.getString(LdapService.LDAP_USER_BASE_DN); | ||
users.addAll(globalUsers.stream().map(s -> { | ||
return new LdapUser(s.getName(), s.getEmail(), s.getRealname(), s.getUserType(), | ||
String.format(userDn, s.getName())); | ||
}).collect(Collectors.toList())); | ||
|
||
dataContainer.setPage(users, fp.getPageable(), true); | ||
} | ||
return dataContainer; | ||
} | ||
|
||
private SimpleProperty getProperty(Properties siteProps, String propName) { | ||
SimpleProperty prop = new SimpleProperty(propName, siteProps.getString(propName)); | ||
prop.determineType(); | ||
return prop; | ||
} | ||
|
||
@Data | ||
@AllArgsConstructor | ||
public class LdapUser { | ||
private String name; | ||
private String email; | ||
private String realname; | ||
private UserType type; | ||
private String groupOrDn; | ||
} | ||
|
||
} |
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
59 changes: 59 additions & 0 deletions
59
src/test/java/org/appng/application/manager/business/LdapUsersTest.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,59 @@ | ||
/* | ||
* Copyright 2011-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.appng.application.manager.business; | ||
|
||
import java.util.List; | ||
|
||
import org.appng.api.model.Property; | ||
import org.appng.api.model.SimpleProperty; | ||
import org.appng.api.support.CallableDataSource; | ||
import org.appng.core.service.LdapService; | ||
import org.appng.testsupport.validation.WritingXmlValidator; | ||
import org.junit.FixMethodOrder; | ||
import org.junit.Test; | ||
import org.junit.runners.MethodSorters; | ||
|
||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) | ||
public class LdapUsersTest extends AbstractTest { | ||
|
||
static { | ||
WritingXmlValidator.writeXml = false; | ||
} | ||
|
||
@Test | ||
public void testUsers() throws Exception { | ||
CallableDataSource ldapUsers = getDataSource("ldapUsers").getCallableDataSource(); | ||
ldapUsers.perform(""); | ||
validate(ldapUsers.getDatasource()); | ||
} | ||
|
||
@Test | ||
public void testSettings() throws Exception { | ||
CallableDataSource ldapUsers = getDataSource("ldapSettings").getCallableDataSource(); | ||
ldapUsers.perform(""); | ||
validate(ldapUsers.getDatasource()); | ||
} | ||
|
||
@Override | ||
protected List<Property> getSiteProperties(String prefix) { | ||
List<Property> siteProperties = super.getSiteProperties(prefix); | ||
siteProperties.add(new SimpleProperty(prefix + LdapService.LDAP_START_TLS, "false")); | ||
siteProperties.add(new SimpleProperty(prefix + LdapService.LDAP_HOST, "ldap://localhost:389")); | ||
siteProperties.add(new SimpleProperty(prefix + LdapService.LDAP_PRINCIPAL_SCHEME, "DN")); | ||
return siteProperties; | ||
} | ||
|
||
} |
Oops, something went wrong.