From 4f41ba64990efb470d443142d9d1dcf2119f8d73 Mon Sep 17 00:00:00 2001 From: Philipp Haussleiter Date: Wed, 8 Feb 2017 22:52:15 +0100 Subject: [PATCH 1/2] using real DNs for LdapNodes. --- .../com/innoq/ldap/connector/LdapEntry.java | 1 - .../com/innoq/ldap/connector/LdapGroup.java | 6 ++--- .../com/innoq/ldap/connector/LdapHelper.java | 6 +++++ .../com/innoq/ldap/connector/LdapNode.java | 23 +++++++--------- .../com/innoq/ldap/connector/TestGroup.java | 27 +++++++++++++++++++ 5 files changed, 45 insertions(+), 18 deletions(-) diff --git a/ldap-connector/src/main/java/com/innoq/ldap/connector/LdapEntry.java b/ldap-connector/src/main/java/com/innoq/ldap/connector/LdapEntry.java index 5f1663f..0bd3d5b 100644 --- a/ldap-connector/src/main/java/com/innoq/ldap/connector/LdapEntry.java +++ b/ldap-connector/src/main/java/com/innoq/ldap/connector/LdapEntry.java @@ -21,7 +21,6 @@ public class LdapEntry extends LdapNode implements Comparable { public LdapEntry(String cn, String owner) { super(); - this.cn = cn; this.owner = owner; } diff --git a/ldap-connector/src/main/java/com/innoq/ldap/connector/LdapGroup.java b/ldap-connector/src/main/java/com/innoq/ldap/connector/LdapGroup.java index 4c8d034..7ee8437 100644 --- a/ldap-connector/src/main/java/com/innoq/ldap/connector/LdapGroup.java +++ b/ldap-connector/src/main/java/com/innoq/ldap/connector/LdapGroup.java @@ -95,7 +95,7 @@ public boolean isEmpty() { @Override public int hashCode() { int hash = 7; - hash = 79 * hash + (this.cn != null ? this.cn.hashCode() : 0); + hash = 79 * hash + (get("cn") != null ? get("cn").hashCode() : 0); hash = 79 * hash + (this.attributes != null ? this.attributes.hashCode() : 0); return hash; } @@ -109,7 +109,7 @@ public boolean equals(Object obj) { return false; } final LdapGroup other = (LdapGroup) obj; - if ((this.cn == null) ? (other.cn != null) : !this.cn.equals(other.cn)) { + if ((get("cn") == null) ? (other.get("cn") != null) : !get("cn").equals(get("cn"))) { return false; } return true; @@ -125,7 +125,7 @@ public int compareTo(LdapGroup t) { @Override public String toString() { - return this.cn + " # " + attributes.size(); + return get("cn") + " # " + attributes.size(); } @Override diff --git a/ldap-connector/src/main/java/com/innoq/ldap/connector/LdapHelper.java b/ldap-connector/src/main/java/com/innoq/ldap/connector/LdapHelper.java index 4c4985f..3c2ed65 100644 --- a/ldap-connector/src/main/java/com/innoq/ldap/connector/LdapHelper.java +++ b/ldap-connector/src/main/java/com/innoq/ldap/connector/LdapHelper.java @@ -618,6 +618,7 @@ public boolean checkCredentials(final String uid, final String password) { */ public LdapUser getUserTemplate(String uid) { LdapUser user = new LdapUser(uid, this); + user.set("dn", getDNForNode(user)); for (String oc : userObjectClasses) { user.addObjectClass(oc.trim()); } @@ -662,6 +663,7 @@ public LdapUser getUserTemplate(String uid) { */ public LdapGroup getGroupTemplate(String cn) { LdapGroup group = new LdapGroup(cn, this); + group.set("dn", getDNForNode(group)); for (String oc : groupObjectClasses) { group.addObjectClass(oc.trim()); } @@ -917,6 +919,10 @@ private List buildMemberChangeSets(List miLi } for (LdapUser member : oldLdapGroup.getUsers()) { if (!newLdapGroup.getUsers().contains(member)) { + if(oldLdapGroup.getUsers().size() == 1) { + Logger.error("Group "+oldLdapGroup.getName()+" has only one member left. Cannot remove "+member.getName()); + return miList; + } a = new BasicAttribute(groupMemberAttribut, member.getDn()); miList.add(new ModificationItem(DirContext.REMOVE_ATTRIBUTE, a)); } diff --git a/ldap-connector/src/main/java/com/innoq/ldap/connector/LdapNode.java b/ldap-connector/src/main/java/com/innoq/ldap/connector/LdapNode.java index 75887e3..7acc04f 100644 --- a/ldap-connector/src/main/java/com/innoq/ldap/connector/LdapNode.java +++ b/ldap-connector/src/main/java/com/innoq/ldap/connector/LdapNode.java @@ -33,8 +33,7 @@ public class LdapNode implements Node { protected Set keys; protected Set objectClasses; protected String name; - protected String dn; - protected String cn; + private String dn; /** * Basic Constructor. @@ -52,9 +51,8 @@ public LdapNode() { @Override public String get(String key) { if("dn".equals(key)) { - return dn; - } - if (key != null + return this.dn; + } else if (key != null && attributes != null && getKeys().contains(key) && attributes.get(key) != null) { @@ -82,9 +80,9 @@ public boolean isEmpty() { */ @Override public void set(String key, String value) { - if("dn".equals(key)) { - dn = value; - } else if (value != null + if("dn".equals(key)) { + this.dn = value; + } else if (value != null && !value.isEmpty() && key != null && !key.isEmpty()) { @@ -112,11 +110,11 @@ public void setAttributes(BasicAttributes attributes) { } public void setCn(String cn) { - this.cn = cn; + set("cn", cn); } public String getCn() { - return this.cn == null ? "" : this.cn; + return get("cn"); } public boolean isNew() { @@ -177,14 +175,11 @@ public void addObjectClass(final String objectClass) { @Override public String getName() { - return name; + return getCn(); } @Override public String getDn() { - if (dn == null) { - dn = LdapHelper.getInstance().getDNForNode(this); - } return dn; } diff --git a/ldap-connector/src/test/java/com/innoq/ldap/connector/TestGroup.java b/ldap-connector/src/test/java/com/innoq/ldap/connector/TestGroup.java index 8a583ce..9df3ba4 100644 --- a/ldap-connector/src/test/java/com/innoq/ldap/connector/TestGroup.java +++ b/ldap-connector/src/test/java/com/innoq/ldap/connector/TestGroup.java @@ -144,6 +144,33 @@ public void testRemoveUserFromGroup() throws Exception { Utils.removeTestUsers(users); } + @Test + public void testRemoveUserFromGroupNotAllowed() throws Exception { + LdapGroup g1 = Utils.createTestGroup(CN); + assertTrue(g1.getUsers().size() == 1); + Node principal = HELPER.getPrincipal(); + LdapUser principalUser = (LdapUser) principal; + g1.rmUser(principalUser); + HELPER.setGroup(g1); + LdapGroup g2 = Utils.createTestGroup(CN); + assertTrue(g2.getUsers().size() == 1); + Utils.removeTestGroup(g1); + } + + @Test + public void testLoadPrincipalUserFromGroup() throws Exception { + LdapGroup g1 = Utils.createTestGroup(CN); + assertTrue(g1.getUsers().size() == 1); + Node principal = HELPER.getPrincipal(); + LdapUser principalUser = (LdapUser) principal; + Set users = g1.getUsers(); + assertTrue(users.size() == 1); + for(LdapUser user : users) { + assertEquals(principalUser.getDn(), user.getDn()); + } + Utils.removeTestGroup(g1); + } + @Test public void testAddUsersToGroup() throws Exception { testUser2 = HELPER.getUserTemplate("U4_" + System.currentTimeMillis()); From 36128650f740a565e43168b0961fca9307d74d6a Mon Sep 17 00:00:00 2001 From: Philipp Haussleiter Date: Wed, 8 Feb 2017 23:07:37 +0100 Subject: [PATCH 2/2] Code Cleanup - removing obsolete code - updating File Headers --- NOTICE | 2 +- README.md | 2 +- ldap-connector/conf/dependencies.yml | 18 --- ldap-connector/conf/messages | 19 --- ldap-connector/conf/routes | 22 ---- ldap-connector/pom.xml | 4 +- .../com/innoq/ldap/connector/LdapEntry.java | 2 +- .../innoq/ldap/connector/LdapException.java | 2 +- .../com/innoq/ldap/connector/LdapGroup.java | 2 +- .../com/innoq/ldap/connector/LdapHelper.java | 2 +- .../com/innoq/ldap/connector/LdapKeys.java | 2 +- .../com/innoq/ldap/connector/LdapNode.java | 2 +- .../ldap/connector/LdapQueryBuilder.java | 2 +- .../com/innoq/ldap/connector/LdapUser.java | 2 +- .../java/com/innoq/ldap/package-info.java | 4 + .../main/java/com/innoq/ldap/util/App.java | 2 +- .../ldap/connector/TestQueryBuilder.java | 2 +- .../com/innoq/ldap/connector/TestUser.java | 2 +- .../java/com/innoq/ldap/connector/Utils.java | 2 +- .../resources/test.ldap.properties.template | 2 +- model/pom.xml | 2 +- .../java/com/innoq/liqid/model/Helper.java | 2 +- .../main/java/com/innoq/liqid/model/Node.java | 2 +- .../com/innoq/liqid/model/QueryBuilder.java | 2 +- pom.xml | 2 +- utils/pom.xml | 2 +- .../com/innoq/liqid/utils/Configuration.java | 2 +- .../com/innoq/liqid/utils/KeyValueStore.java | 103 ---------------- .../com/innoq/liqid/utils/ObjectCache.java | 114 ------------------ .../java/com/innoq/liqid/utils/SHACrypt.java | 2 +- 30 files changed, 29 insertions(+), 301 deletions(-) delete mode 100644 ldap-connector/conf/dependencies.yml delete mode 100644 ldap-connector/conf/messages delete mode 100644 ldap-connector/conf/routes create mode 100644 ldap-connector/src/main/java/com/innoq/ldap/package-info.java delete mode 100644 utils/src/main/java/com/innoq/liqid/utils/KeyValueStore.java delete mode 100644 utils/src/main/java/com/innoq/liqid/utils/ObjectCache.java diff --git a/NOTICE b/NOTICE index e43b76e..e89ee87 100644 --- a/NOTICE +++ b/NOTICE @@ -1,2 +1,2 @@ innoQ LDAP Client Library - Copyright (C) 2012 innoQ Deutschland GmbH + Copyright (C) 2017 innoQ Deutschland GmbH diff --git a/README.md b/README.md index 4425c17..fdc4e19 100644 --- a/README.md +++ b/README.md @@ -160,7 +160,7 @@ For more Examples have a look into the # Legal - Copyright (C) 2012 innoQ Deutschland GmbH + Copyright (C) 2017 innoQ Deutschland GmbH Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/ldap-connector/conf/dependencies.yml b/ldap-connector/conf/dependencies.yml deleted file mode 100644 index 2bbcf53..0000000 --- a/ldap-connector/conf/dependencies.yml +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright (C) 2012 innoQ Deutschland GmbH -# -# 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. - -self: play -> @MODULE@ @VERSION@ - -require: - - play diff --git a/ldap-connector/conf/messages b/ldap-connector/conf/messages deleted file mode 100644 index 18e94a2..0000000 --- a/ldap-connector/conf/messages +++ /dev/null @@ -1,19 +0,0 @@ -# Copyright (C) 2012 innoQ Deutschland GmbH -# -# 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. -# -# Default ldap messages -# You can specialize this file for each language. -# For exemple, for french create a messages.fr file - -#ldap.name=ldap diff --git a/ldap-connector/conf/routes b/ldap-connector/conf/routes deleted file mode 100644 index f564ca6..0000000 --- a/ldap-connector/conf/routes +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright (C) 2012 innoQ Deutschland GmbH -# -# 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. -# -# This file defines all module routes (Higher priority routes first) -# -# import these routes in the main app as : -# * / module:ldap -# -# ~~~~ - -GET /? Module.index diff --git a/ldap-connector/pom.xml b/ldap-connector/pom.xml index 4117fb4..34d5426 100644 --- a/ldap-connector/pom.xml +++ b/ldap-connector/pom.xml @@ -1,6 +1,6 @@