You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by du...@apache.org on 2013/10/29 10:04:54 UTC
git commit: updated refs/heads/master to 9300d4a
Updated Branches:
refs/heads/master cc4b612bf -> 9300d4a3b
Added an api call to import all the ldap users to the same domains(ou's) in cloudstack
TODO:
1. error handling of no domains present, nested hierarchy
2. handling the case when the api call fails for a specific user/users
3. test cases for LdapUserManager
Signed-off-by: Ian Duffy <ia...@ianduffy.ie>
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/9300d4a3
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/9300d4a3
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/9300d4a3
Branch: refs/heads/master
Commit: 9300d4a3baf7d51d520814839a1833c814c5a154
Parents: cc4b612
Author: Rajani Karuturi <ra...@gmail.com>
Authored: Wed Oct 23 10:32:05 2013 +0530
Committer: Ian Duffy <ia...@ianduffy.ie>
Committed: Tue Oct 29 09:04:33 2013 +0000
----------------------------------------------------------------------
api/src/com/cloud/user/DomainService.java | 3 +
client/tomcatconf/commands.properties.in | 1 +
.../api/command/LdapImportUsersCmd.java | 123 +++++++++++++
.../api/response/LdapUserResponse.java | 162 +++++++++--------
.../cloudstack/ldap/LdapConfiguration.java | 6 +-
.../apache/cloudstack/ldap/LdapManagerImpl.java | 9 +-
.../org/apache/cloudstack/ldap/LdapUser.java | 12 +-
.../apache/cloudstack/ldap/LdapUserManager.java | 175 +++++++++----------
.../ldap/LdapConfigurationSpec.groovy | 4 +-
.../ldap/LdapImportUsersCmdSpec.groovy | 71 ++++++++
.../cloudstack/ldap/LdapListUsersCmdSpec.groovy | 10 +-
.../cloudstack/ldap/LdapManagerImplSpec.groovy | 39 ++++-
.../ldap/LdapSearchUserCmdSpec.groovy | 5 +-
.../cloudstack/ldap/LdapUserResponseSpec.groovy | 18 ++
.../apache/cloudstack/ldap/LdapUserSpec.groovy | 31 ++--
.../src/com/cloud/user/DomainManagerImpl.java | 9 +
.../com/cloud/user/MockDomainManagerImpl.java | 6 +
17 files changed, 474 insertions(+), 210 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/9300d4a3/api/src/com/cloud/user/DomainService.java
----------------------------------------------------------------------
diff --git a/api/src/com/cloud/user/DomainService.java b/api/src/com/cloud/user/DomainService.java
index 7c302e3..f10728f 100644
--- a/api/src/com/cloud/user/DomainService.java
+++ b/api/src/com/cloud/user/DomainService.java
@@ -33,6 +33,9 @@ public interface DomainService {
Domain getDomain(String uuid);
+ Domain getDomainByName(String name, long parentId);
+
+
/**
* Return whether a domain is a child domain of a given domain.
*
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/9300d4a3/client/tomcatconf/commands.properties.in
----------------------------------------------------------------------
diff --git a/client/tomcatconf/commands.properties.in b/client/tomcatconf/commands.properties.in
index 1503ab8..81fd985 100644
--- a/client/tomcatconf/commands.properties.in
+++ b/client/tomcatconf/commands.properties.in
@@ -680,3 +680,4 @@ addLdapConfiguration=3
deleteLdapConfiguration=3
listLdapUsers=3
ldapCreateAccount=3
+importLdapUsers=3
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/9300d4a3/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapImportUsersCmd.java
----------------------------------------------------------------------
diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapImportUsersCmd.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapImportUsersCmd.java
new file mode 100644
index 0000000..f872247
--- /dev/null
+++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapImportUsersCmd.java
@@ -0,0 +1,123 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you 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.apache.cloudstack.api.command;
+
+import com.cloud.domain.Domain;
+import com.cloud.exception.*;
+import com.cloud.user.AccountService;
+import com.cloud.user.DomainService;
+import org.apache.cloudstack.api.*;
+import org.apache.cloudstack.api.response.LdapUserResponse;
+import org.apache.cloudstack.api.response.ListResponse;
+import org.apache.cloudstack.ldap.LdapManager;
+import org.apache.cloudstack.ldap.LdapUser;
+import org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException;
+import org.apache.log4j.Logger;
+import org.bouncycastle.util.encoders.Base64;
+
+import javax.inject.Inject;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+@APICommand(name = "importLdapUsers", description = "Import LDAP users", responseObject = LdapUserResponse.class, since = "4.3.0")
+public class LdapImportUsersCmd extends BaseListCmd {
+
+ public static final Logger s_logger = Logger.getLogger(LdapImportUsersCmd.class.getName());
+
+ private static final String s_name = "ldapuserresponse";
+
+ @Parameter(name = ApiConstants.TIMEZONE, type = CommandType.STRING,
+ description = "Specifies a timezone for this command. For more information on the timezone parameter, see Time Zone Format.")
+ private String timezone;
+
+ @Parameter(name = ApiConstants.ACCOUNT_TYPE, type = CommandType.SHORT, required = true,
+ description = "Type of the account. Specify 0 for user, 1 for root admin, and 2 for domain admin")
+ private Short accountType;
+
+ @Parameter(name = ApiConstants.ACCOUNT_DETAILS, type = CommandType.MAP, description = "details for account used to store specific parameters")
+ private Map<String, String> details;
+
+ @Inject
+ private LdapManager _ldapManager;
+
+ public LdapImportUsersCmd() {
+ super();
+ }
+
+ public LdapImportUsersCmd(final LdapManager ldapManager, final DomainService domainService, final AccountService accountService) {
+ super();
+ _ldapManager = ldapManager;
+ _domainService = domainService;
+ _accountService = accountService;
+ }
+
+ @Override
+ public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException,
+ NetworkRuleConflictException {
+ List<LdapUserResponse> ldapResponses = null;
+ final ListResponse<LdapUserResponse> response = new ListResponse<LdapUserResponse>();
+ try {
+ final List<LdapUser> users = _ldapManager.getUsers();
+ for (LdapUser user : users) {
+ Domain domain = _domainService.getDomainByName(user.getDomain(), Domain.ROOT_DOMAIN);
+
+ if (domain == null) {
+ domain = _domainService.createDomain(user.getDomain(), Domain.ROOT_DOMAIN, user.getDomain(), UUID.randomUUID().toString());
+ }
+ _accountService.createUserAccount(user.getUsername(), generatePassword(), user.getFirstname(), user.getLastname(), user.getEmail(), timezone, user.getUsername(),
+ accountType, domain.getId(), domain.getNetworkDomain(), details, UUID.randomUUID().toString(), UUID.randomUUID().toString());
+ }
+ ldapResponses = createLdapUserResponse(users);
+ } catch (final NoLdapUserMatchingQueryException ex) {
+ ldapResponses = new ArrayList<LdapUserResponse>();
+ } finally {
+ response.setResponses(ldapResponses);
+ response.setResponseName(getCommandName());
+ setResponseObject(response);
+ }
+ }
+
+ private List<LdapUserResponse> createLdapUserResponse(List<LdapUser> users) {
+ final List<LdapUserResponse> ldapResponses = new ArrayList<LdapUserResponse>();
+ for (final LdapUser user : users) {
+ final LdapUserResponse ldapResponse = _ldapManager.createLdapUserResponse(user);
+ ldapResponse.setObjectName("LdapUser");
+ ldapResponses.add(ldapResponse);
+ }
+ return ldapResponses;
+ }
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ private String generatePassword() throws ServerApiException {
+ try {
+ final SecureRandom randomGen = SecureRandom.getInstance("SHA1PRNG");
+ final byte bytes[] = new byte[20];
+ randomGen.nextBytes(bytes);
+ return Base64.encode(bytes).toString();
+ } catch (final NoSuchAlgorithmException e) {
+ throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate random password");
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/9300d4a3/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/response/LdapUserResponse.java
----------------------------------------------------------------------
diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/response/LdapUserResponse.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/response/LdapUserResponse.java
index 9b21c8f..1672703 100644
--- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/response/LdapUserResponse.java
+++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/response/LdapUserResponse.java
@@ -16,84 +16,94 @@
// under the License.
package org.apache.cloudstack.api.response;
-import org.apache.cloudstack.api.BaseResponse;
-
import com.cloud.serializer.Param;
import com.google.gson.annotations.SerializedName;
+import org.apache.cloudstack.api.BaseResponse;
public class LdapUserResponse extends BaseResponse {
- @SerializedName("email")
- @Param(description = "The user's email")
- private String email;
-
- @SerializedName("principal")
- @Param(description = "The user's principle")
- private String principal;
-
- @SerializedName("firstname")
- @Param(description = "The user's firstname")
- private String firstname;
-
- @SerializedName("lastname")
- @Param(description = "The user's lastname")
- private String lastname;
-
- @SerializedName("username")
- @Param(description = "The user's username")
- private String username;
-
- public LdapUserResponse() {
- super();
- }
-
- public LdapUserResponse(final String username, final String email,
- final String firstname, final String lastname,
- final String principal) {
- super();
- this.username = username;
- this.email = email;
- this.firstname = firstname;
- this.lastname = lastname;
- this.principal = principal;
- }
-
- public String getEmail() {
- return email;
- }
-
- public String getFirstname() {
- return firstname;
- }
-
- public String getLastname() {
- return lastname;
- }
-
- public String getPrincipal() {
- return principal;
- }
-
- public String getUsername() {
- return username;
- }
-
- public void setEmail(final String email) {
- this.email = email;
- }
-
- public void setFirstname(final String firstname) {
- this.firstname = firstname;
- }
-
- public void setLastname(final String lastname) {
- this.lastname = lastname;
- }
-
- public void setPrincipal(final String principal) {
- this.principal = principal;
- }
-
- public void setUsername(final String username) {
- this.username = username;
- }
+ @SerializedName("email")
+ @Param(description = "The user's email")
+ private String email;
+
+ @SerializedName("principal")
+ @Param(description = "The user's principle")
+ private String principal;
+
+ @SerializedName("firstname")
+ @Param(description = "The user's firstname")
+ private String firstname;
+
+ @SerializedName("lastname")
+ @Param(description = "The user's lastname")
+ private String lastname;
+
+ @SerializedName("username")
+ @Param(description = "The user's username")
+ private String username;
+
+ @SerializedName("domain")
+ @Param(description = "The user's domain")
+ private String domain;
+
+ public LdapUserResponse() {
+ super();
+ }
+
+ public LdapUserResponse(final String username, final String email, final String firstname, final String lastname, final String principal, String domain) {
+ super();
+ this.username = username;
+ this.email = email;
+ this.firstname = firstname;
+ this.lastname = lastname;
+ this.principal = principal;
+ this.domain = domain;
+ }
+
+ public String getEmail() {
+ return email;
+ }
+
+ public String getFirstname() {
+ return firstname;
+ }
+
+ public String getLastname() {
+ return lastname;
+ }
+
+ public String getPrincipal() {
+ return principal;
+ }
+
+ public String getUsername() {
+ return username;
+ }
+
+ public String getDomain() {
+ return domain;
+ }
+
+ public void setEmail(final String email) {
+ this.email = email;
+ }
+
+ public void setFirstname(final String firstname) {
+ this.firstname = firstname;
+ }
+
+ public void setLastname(final String lastname) {
+ this.lastname = lastname;
+ }
+
+ public void setPrincipal(final String principal) {
+ this.principal = principal;
+ }
+
+ public void setUsername(final String username) {
+ this.username = username;
+ }
+
+ public void setDomain(String domain) {
+ this.domain = domain;
+ }
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/9300d4a3/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapConfiguration.java
----------------------------------------------------------------------
diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapConfiguration.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapConfiguration.java
index 0cfb37c..a08dccb 100644
--- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapConfiguration.java
+++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapConfiguration.java
@@ -105,7 +105,7 @@ public class LdapConfiguration {
public String[] getReturnAttributes() {
return new String[] { getUsernameAttribute(), getEmailAttribute(),
- getFirstnameAttribute(), getLastnameAttribute() };
+ getFirstnameAttribute(), getLastnameAttribute(), getCommonNameAttribute() };
}
public int getScope() {
@@ -142,4 +142,8 @@ public class LdapConfiguration {
final String userObject = _configDao.getValue("ldap.user.object");
return userObject == null ? "inetOrgPerson" : userObject;
}
+
+ public String getCommonNameAttribute() {
+ return "cn";
+ }
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/9300d4a3/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManagerImpl.java
----------------------------------------------------------------------
diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManagerImpl.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManagerImpl.java
index 87406ad..90a79b3 100644
--- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManagerImpl.java
+++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManagerImpl.java
@@ -25,12 +25,7 @@ import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import org.apache.cloudstack.api.LdapValidator;
-import org.apache.cloudstack.api.command.LdapAddConfigurationCmd;
-import org.apache.cloudstack.api.command.LdapCreateAccountCmd;
-import org.apache.cloudstack.api.command.LdapDeleteConfigurationCmd;
-import org.apache.cloudstack.api.command.LdapListConfigurationCmd;
-import org.apache.cloudstack.api.command.LdapListUsersCmd;
-import org.apache.cloudstack.api.command.LdapUserSearchCmd;
+import org.apache.cloudstack.api.command.*;
import org.apache.cloudstack.api.response.LdapConfigurationResponse;
import org.apache.cloudstack.api.response.LdapUserResponse;
import org.apache.cloudstack.ldap.dao.LdapConfigurationDao;
@@ -136,6 +131,7 @@ public class LdapManagerImpl implements LdapManager, LdapValidator {
response.setLastname(user.getLastname());
response.setEmail(user.getEmail());
response.setPrincipal(user.getPrincipal());
+ response.setDomain(user.getDomain());
return response;
}
@@ -164,6 +160,7 @@ public class LdapManagerImpl implements LdapManager, LdapValidator {
cmdList.add(LdapDeleteConfigurationCmd.class);
cmdList.add(LdapListConfigurationCmd.class);
cmdList.add(LdapCreateAccountCmd.class);
+ cmdList.add(LdapImportUsersCmd.class);
return cmdList;
}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/9300d4a3/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUser.java
----------------------------------------------------------------------
diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUser.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUser.java
index 18ad7d9..592459e 100644
--- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUser.java
+++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUser.java
@@ -22,15 +22,15 @@ public class LdapUser implements Comparable<LdapUser> {
private final String firstname;
private final String lastname;
private final String username;
+ private final String domain;
- public LdapUser(final String username, final String email,
- final String firstname, final String lastname,
- final String principal) {
+ public LdapUser(final String username, final String email, final String firstname, final String lastname, final String principal, String domain) {
this.username = username;
this.email = email;
this.firstname = firstname;
this.lastname = lastname;
this.principal = principal;
+ this.domain = domain;
}
@Override
@@ -70,7 +70,11 @@ public class LdapUser implements Comparable<LdapUser> {
return username;
}
- @Override
+ public String getDomain() {
+ return domain;
+ }
+
+ @Override
public int hashCode() {
return getUsername().hashCode();
}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/9300d4a3/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUserManager.java
----------------------------------------------------------------------
diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUserManager.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUserManager.java
index 7494346..47697c9 100644
--- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUserManager.java
+++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUserManager.java
@@ -30,111 +30,100 @@ import javax.naming.directory.SearchResult;
public class LdapUserManager {
- @Inject
- private LdapConfiguration _ldapConfiguration;
-
- public LdapUserManager() {
+ @Inject
+ private LdapConfiguration _ldapConfiguration;
+
+ public LdapUserManager() {
+ }
+
+ public LdapUserManager(final LdapConfiguration ldapConfiguration) {
+ _ldapConfiguration = ldapConfiguration;
+ }
+
+ private LdapUser createUser(final SearchResult result) throws NamingException {
+ final Attributes attributes = result.getAttributes();
+
+ final String username = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getUsernameAttribute());
+ final String email = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getEmailAttribute());
+ final String firstname = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getFirstnameAttribute());
+ final String lastname = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getLastnameAttribute());
+ final String principal = result.getNameInNamespace();
+
+ String domain = principal.replace("cn="+LdapUtils.getAttributeValue(attributes,_ldapConfiguration.getCommonNameAttribute())+",", "");
+ domain = domain.replace(","+_ldapConfiguration.getBaseDn(), "");
+ domain = domain.replace("ou=","");
+
+ return new LdapUser(username, email, firstname, lastname, principal, domain);
+ }
+
+ private String generateSearchFilter(final String username) {
+ final StringBuilder userObjectFilter = new StringBuilder();
+ userObjectFilter.append("(objectClass=");
+ userObjectFilter.append(_ldapConfiguration.getUserObject());
+ userObjectFilter.append(")");
+
+ final StringBuilder usernameFilter = new StringBuilder();
+ usernameFilter.append("(");
+ usernameFilter.append(_ldapConfiguration.getUsernameAttribute());
+ usernameFilter.append("=");
+ usernameFilter.append((username == null ? "*" : username));
+ usernameFilter.append(")");
+
+ final StringBuilder memberOfFilter = new StringBuilder();
+ if (_ldapConfiguration.getSearchGroupPrinciple() != null) {
+ memberOfFilter.append("(memberof=");
+ memberOfFilter.append(_ldapConfiguration.getSearchGroupPrinciple());
+ memberOfFilter.append(")");
}
- public LdapUserManager(final LdapConfiguration ldapConfiguration) {
- _ldapConfiguration = ldapConfiguration;
+ final StringBuilder result = new StringBuilder();
+ result.append("(&");
+ result.append(userObjectFilter);
+ result.append(usernameFilter);
+ result.append(memberOfFilter);
+ result.append(")");
+
+ return result.toString();
+ }
+
+ public LdapUser getUser(final String username, final DirContext context) throws NamingException {
+ final NamingEnumeration<SearchResult> result = searchUsers(username, context);
+ if (result.hasMoreElements()) {
+ return createUser(result.nextElement());
+ } else {
+ throw new NamingException("No user found for username " + username);
}
+ }
- private LdapUser createUser(final SearchResult result)
- throws NamingException {
- final Attributes attributes = result.getAttributes();
-
- final String username = LdapUtils.getAttributeValue(attributes,
- _ldapConfiguration.getUsernameAttribute());
- final String email = LdapUtils.getAttributeValue(attributes,
- _ldapConfiguration.getEmailAttribute());
- final String firstname = LdapUtils.getAttributeValue(attributes,
- _ldapConfiguration.getFirstnameAttribute());
- final String lastname = LdapUtils.getAttributeValue(attributes,
- _ldapConfiguration.getLastnameAttribute());
- final String principal = result.getName() + ","
- + _ldapConfiguration.getBaseDn();
-
- return new LdapUser(username, email, firstname, lastname, principal);
- }
+ public List<LdapUser> getUsers(final DirContext context) throws NamingException {
+ return getUsers(null, context);
+ }
- private String generateSearchFilter(final String username) {
- final StringBuilder userObjectFilter = new StringBuilder();
- userObjectFilter.append("(objectClass=");
- userObjectFilter.append(_ldapConfiguration.getUserObject());
- userObjectFilter.append(")");
-
- final StringBuilder usernameFilter = new StringBuilder();
- usernameFilter.append("(");
- usernameFilter.append(_ldapConfiguration.getUsernameAttribute());
- usernameFilter.append("=");
- usernameFilter.append((username == null ? "*" : username));
- usernameFilter.append(")");
-
- final StringBuilder memberOfFilter = new StringBuilder();
- if (_ldapConfiguration.getSearchGroupPrinciple() != null) {
- memberOfFilter.append("(memberof=");
- memberOfFilter.append(_ldapConfiguration.getSearchGroupPrinciple());
- memberOfFilter.append(")");
- }
-
- final StringBuilder result = new StringBuilder();
- result.append("(&");
- result.append(userObjectFilter);
- result.append(usernameFilter);
- result.append(memberOfFilter);
- result.append(")");
-
- return result.toString();
- }
+ public List<LdapUser> getUsers(final String username, final DirContext context) throws NamingException {
+ final NamingEnumeration<SearchResult> results = searchUsers(username, context);
- public LdapUser getUser(final String username, final DirContext context)
- throws NamingException {
- final NamingEnumeration<SearchResult> result = searchUsers(username,
- context);
- if (result.hasMoreElements()) {
- return createUser(result.nextElement());
- } else {
- throw new NamingException("No user found for username " + username);
- }
- }
+ final List<LdapUser> users = new ArrayList<LdapUser>();
- public List<LdapUser> getUsers(final DirContext context)
- throws NamingException {
- return getUsers(null, context);
+ while (results.hasMoreElements()) {
+ final SearchResult result = results.nextElement();
+ users.add(createUser(result));
}
- public List<LdapUser> getUsers(final String username,
- final DirContext context) throws NamingException {
- final NamingEnumeration<SearchResult> results = searchUsers(username,
- context);
-
- final List<LdapUser> users = new ArrayList<LdapUser>();
+ Collections.sort(users);
- while (results.hasMoreElements()) {
- final SearchResult result = results.nextElement();
- users.add(createUser(result));
- }
+ return users;
+ }
- Collections.sort(users);
+ public NamingEnumeration<SearchResult> searchUsers(final DirContext context) throws NamingException {
+ return searchUsers(null, context);
+ }
- return users;
- }
-
- public NamingEnumeration<SearchResult> searchUsers(final DirContext context)
- throws NamingException {
- return searchUsers(null, context);
- }
+ public NamingEnumeration<SearchResult> searchUsers(final String username, final DirContext context) throws NamingException {
+ final SearchControls controls = new SearchControls();
- public NamingEnumeration<SearchResult> searchUsers(final String username,
- final DirContext context) throws NamingException {
- final SearchControls controls = new SearchControls();
+ controls.setSearchScope(_ldapConfiguration.getScope());
+ controls.setReturningAttributes(_ldapConfiguration.getReturnAttributes());
- controls.setSearchScope(_ldapConfiguration.getScope());
- controls.setReturningAttributes(_ldapConfiguration
- .getReturnAttributes());
-
- return context.search(_ldapConfiguration.getBaseDn(),
- generateSearchFilter(username), controls);
- }
+ return context.search(_ldapConfiguration.getBaseDn(), generateSearchFilter(username), controls);
+ }
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/9300d4a3/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationSpec.groovy
----------------------------------------------------------------------
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationSpec.groovy
index c593959..66b4673 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationSpec.groovy
@@ -120,8 +120,8 @@ class LdapConfigurationSpec extends spock.lang.Specification {
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get return attributes is called"
String[] returnAttributes = ldapConfiguration.getReturnAttributes()
- then: "An array containing uid, mail, givenname and sn is returned"
- returnAttributes == ["uid", "mail", "givenname", "sn"]
+ then: "An array containing uid, mail, givenname, sn and cn is returned"
+ returnAttributes == ["uid", "mail", "givenname", "sn", "cn"]
}
def "Test that getScope returns SearchControls.SUBTREE_SCOPE"() {
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/9300d4a3/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapImportUsersCmdSpec.groovy
----------------------------------------------------------------------
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapImportUsersCmdSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapImportUsersCmdSpec.groovy
new file mode 100644
index 0000000..d04b094
--- /dev/null
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapImportUsersCmdSpec.groovy
@@ -0,0 +1,71 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you 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 groovy.org.apache.cloudstack.ldap
+
+import com.cloud.domain.Domain
+import com.cloud.domain.DomainVO
+import com.cloud.user.AccountService
+import com.cloud.user.DomainService
+import com.cloud.user.UserAccount
+import com.cloud.user.UserAccountVO
+import org.apache.cloudstack.api.command.LdapImportUsersCmd
+import org.apache.cloudstack.api.response.LdapUserResponse
+import org.apache.cloudstack.ldap.LdapManager
+import org.apache.cloudstack.ldap.LdapUser
+
+class LdapImportUsersCmdSpec extends spock.lang.Specification {
+
+
+ def "Test successful return of getCommandName"() {
+ given: "We have an LdapManager, DomainService and a LdapImportUsersCmd"
+ def ldapManager = Mock(LdapManager)
+ def domainService = Mock(DomainService)
+ def ldapImportUsersCmd = new LdapImportUsersCmd(ldapManager, domainService)
+ when: "Get command name is called"
+ String commandName = ldapImportUsersCmd.getCommandName()
+ then: "ldapuserresponse is returned"
+ commandName == "ldapuserresponse"
+ }
+
+ def "Test successful response from execute"() {
+ given: "We have an LdapManager, DomainService, one user and a LdapImportUsersCmd"
+ def ldapManager = Mock(LdapManager)
+ def domainService = Mock(DomainService)
+ def accountService = Mock(AccountService)
+
+ List<LdapUser> users = new ArrayList()
+ users.add(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,ou=engineering,dc=cloudstack,dc=org", "engineering"))
+ users.add(new LdapUser("bob", "bob@test.com", "Robert", "Young", "cn=bob,ou=engineering,dc=cloudstack,dc=org", "engineering"))
+ ldapManager.getUsers() >> users
+ LdapUserResponse response1 = new LdapUserResponse("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,ou=engineering,dc=cloudstack,dc=org", "engineering")
+ LdapUserResponse response2 = new LdapUserResponse("bob", "bob@test.com", "Robert", "Young", "cn=bob,ou=engineering,dc=cloudstack,dc=org", "engineering")
+ ldapManager.createLdapUserResponse(_) >>>[response1, response2]
+
+
+ Domain domain = new DomainVO("engineering", 1L, 1L, "engineering", UUID.randomUUID().toString())
+ domainService.getDomainByName("engineering", 1L) >>> [null, domain]
+ 1 * domainService.createDomain("engineering", 1L, "engineering", _) >> domain
+
+ def ldapImportUsersCmd = new LdapImportUsersCmd(ldapManager, domainService, accountService)
+ ldapImportUsersCmd.accountType = 2;
+
+ when: "LdapListUsersCmd is executed"
+ ldapImportUsersCmd.execute()
+ then: "a list of size 2 is returned"
+ ldapImportUsersCmd.responseObject.getResponses().size() == 2
+ }
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/9300d4a3/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapListUsersCmdSpec.groovy
----------------------------------------------------------------------
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapListUsersCmdSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapListUsersCmdSpec.groovy
index 5039443..4b32eb1 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapListUsersCmdSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapListUsersCmdSpec.groovy
@@ -17,8 +17,6 @@
package groovy.org.apache.cloudstack.ldap
import org.apache.cloudstack.api.command.LdapListUsersCmd
-import org.apache.cloudstack.api.ServerApiException
-import org.apache.cloudstack.api.command.admin.user.ListUsersCmd
import org.apache.cloudstack.api.response.LdapUserResponse
import org.apache.cloudstack.api.response.ListResponse
import org.apache.cloudstack.api.response.UserResponse
@@ -55,9 +53,9 @@ class LdapListUsersCmdSpec extends spock.lang.Specification {
given: "We have an LdapManager, one user, QueryService and a LdapListUsersCmd"
def ldapManager = Mock(LdapManager)
List<LdapUser> users = new ArrayList()
- users.add(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org"))
+ users.add(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org", null))
ldapManager.getUsers() >> users
- LdapUserResponse response = new LdapUserResponse("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org")
+ LdapUserResponse response = new LdapUserResponse("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org", null)
ldapManager.createLdapUserResponse(_) >> response
def queryService = Mock(QueryService)
def ldapListUsersCmd = new LdapListUsersCmd(ldapManager, queryService)
@@ -94,7 +92,7 @@ class LdapListUsersCmdSpec extends spock.lang.Specification {
queryService.searchForUsers(_) >> queryServiceResponse
- def ldapUser = new LdapUser("rmurphy", "rmurphy@cloudstack.org", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org")
+ def ldapUser = new LdapUser("rmurphy", "rmurphy@cloudstack.org", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org", null)
def ldapListUsersCmd = new LdapListUsersCmd(ldapManager,queryService)
when: "isACloudstackUser is executed"
@@ -111,7 +109,7 @@ class LdapListUsersCmdSpec extends spock.lang.Specification {
queryService.searchForUsers(_) >> new ListResponse<UserResponse>()
- def ldapUser = new LdapUser("rmurphy", "rmurphy@cloudstack.org", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org")
+ def ldapUser = new LdapUser("rmurphy", "rmurphy@cloudstack.org", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org", null)
def ldapListUsersCmd = new LdapListUsersCmd(ldapManager,queryService)
when: "isACloudstackUser is executed"
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/9300d4a3/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapManagerImplSpec.groovy
----------------------------------------------------------------------
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapManagerImplSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapManagerImplSpec.groovy
index d681eac..321e1af 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapManagerImplSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapManagerImplSpec.groovy
@@ -16,6 +16,13 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
+import org.apache.cloudstack.api.command.LdapAddConfigurationCmd
+import org.apache.cloudstack.api.command.LdapCreateAccountCmd
+import org.apache.cloudstack.api.command.LdapDeleteConfigurationCmd
+import org.apache.cloudstack.api.command.LdapImportUsersCmd
+import org.apache.cloudstack.api.command.LdapListUsersCmd
+import org.apache.cloudstack.api.command.LdapUserSearchCmd
+
import javax.naming.NamingException
import javax.naming.ldap.InitialLdapContext
@@ -86,13 +93,15 @@ class LdapManagerImplSpec extends spock.lang.Specification {
def ldapUserManager = Mock(LdapUserManager)
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "A ldap user response is generated"
- def result = ldapManager.createLdapUserResponse(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org"))
+ def result = ldapManager.createLdapUserResponse(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,ou=engineering,dc=cloudstack,dc=org",
+ "engineering"))
then: "The result of the response should match the given ldap user"
result.username == "rmurphy"
result.email == "rmurphy@test.com"
result.firstname == "Ryan"
result.lastname == "Murphy"
- result.principal == "cn=rmurphy,dc=cloudstack,dc=org"
+ result.principal == "cn=rmurphy,ou=engineering,dc=cloudstack,dc=org"
+ result.domain == "engineering"
}
def "Test success getUsers"() {
@@ -102,7 +111,7 @@ class LdapManagerImplSpec extends spock.lang.Specification {
def ldapUserManager = Mock(LdapUserManager)
ldapContextFactory.createBindContext() >> null
List<LdapUser> users = new ArrayList<>();
- users.add(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org"))
+ users.add(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org", null))
ldapUserManager.getUsers(_) >> users;
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "We search for a group of users"
@@ -117,7 +126,7 @@ class LdapManagerImplSpec extends spock.lang.Specification {
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapContextFactory.createBindContext() >> null
- ldapUserManager.getUser(_, _) >> new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org")
+ ldapUserManager.getUser(_, _) >> new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org", null)
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "We search for a user"
def result = ldapManager.getUser("rmurphy")
@@ -149,7 +158,7 @@ class LdapManagerImplSpec extends spock.lang.Specification {
ldapContextFactory.createUserContext(_, _) >> { throw new NamingException() }
def ldapUserManager = Mock(LdapUserManager)
def ldapManager = Spy(LdapManagerImpl, constructorArgs: [ldapConfigurationDao, ldapContextFactory, ldapUserManager])
- ldapManager.getUser(_) >> { new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org") }
+ ldapManager.getUser(_) >> { new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org", null) }
when: "The user attempts to authenticate with a bad password"
def result = ldapManager.canAuthenticate("rmurphy", "password")
then: "The authentication fails"
@@ -203,7 +212,7 @@ class LdapManagerImplSpec extends spock.lang.Specification {
ldapContextFactory.createUserContext(_, _) >> null
def ldapUserManager = Mock(LdapUserManager)
def ldapManager = Spy(LdapManagerImpl, constructorArgs: [ldapConfigurationDao, ldapContextFactory, ldapUserManager])
- ldapManager.getUser(_) >> { new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org") }
+ ldapManager.getUser(_) >> { new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org", null) }
when: "A user authenticates"
def result = ldapManager.canAuthenticate("rmurphy", "password")
then: "The result is true"
@@ -237,7 +246,7 @@ class LdapManagerImplSpec extends spock.lang.Specification {
ldapContextFactory.createBindContext() >> null;
List<LdapUser> users = new ArrayList<LdapUser>();
- users.add(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org"))
+ users.add(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,ou=engineering,dc=cloudstack,dc=org", "engineering"))
ldapUserManager.getUsers(_, _) >> users;
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
@@ -288,16 +297,30 @@ class LdapManagerImplSpec extends spock.lang.Specification {
thrown InvalidParameterValueException
}
+ def supportedLdapCommands() {
+ List<Class<?>> cmdList = new ArrayList<Class<?>>();
+ cmdList.add(LdapUserSearchCmd.class);
+ cmdList.add(LdapListUsersCmd.class);
+ cmdList.add(LdapAddConfigurationCmd.class);
+ cmdList.add(LdapDeleteConfigurationCmd.class);
+ cmdList.add(LdapListConfigurationCmd.class);
+ cmdList.add(LdapCreateAccountCmd.class);
+ cmdList.add(LdapImportUsersCmd.class);
+ return cmdList
+ }
+
def "Test that getCommands isn't empty"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
+ final List<Class<?>> cmdList = supportedLdapCommands()
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "Get commands is called"
def result = ldapManager.getCommands()
- then: "it must have atleast 1 command"
+ then: "it must return all the commands"
result.size() > 0
+ result == cmdList
}
def "Testing of listConfigurations"() {
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/9300d4a3/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapSearchUserCmdSpec.groovy
----------------------------------------------------------------------
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapSearchUserCmdSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapSearchUserCmdSpec.groovy
index fce299d..1411c29 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapSearchUserCmdSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapSearchUserCmdSpec.groovy
@@ -16,7 +16,6 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
-import org.apache.cloudstack.api.ServerApiException
import org.apache.cloudstack.api.command.LdapUserSearchCmd
import org.apache.cloudstack.api.response.LdapUserResponse
import org.apache.cloudstack.ldap.LdapManager
@@ -49,9 +48,9 @@ class LdapSearchUserCmdSpec extends spock.lang.Specification {
given: "We have an Ldap manager and ldap user search cmd"
def ldapManager = Mock(LdapManager)
List<LdapUser> users = new ArrayList()
- users.add(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org"))
+ users.add(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org", null))
ldapManager.searchUsers(_) >> users
- LdapUserResponse response = new LdapUserResponse("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org")
+ LdapUserResponse response = new LdapUserResponse("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org", null)
ldapManager.createLdapUserResponse(_) >> response
def ldapUserSearchCmd = new LdapUserSearchCmd(ldapManager)
when: "The command is executed"
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/9300d4a3/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserResponseSpec.groovy
----------------------------------------------------------------------
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserResponseSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserResponseSpec.groovy
index f1978fa..9a64539 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserResponseSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserResponseSpec.groovy
@@ -64,4 +64,22 @@ class LdapUserResponseSpec extends spock.lang.Specification {
then: "Get username returns the set value."
response.getUsername() == "rmurphy"
}
+
+ def "Testing successful setting of LdapUserResponse domain"() {
+ given: "We have an LdapUserResponse"
+ LdapUserResponse response = new LdapUserResponse()
+ when: "A domain is set"
+ response.setDomain("engineering")
+ then: "Get domain returns the set value."
+ response.getDomain() == "engineering"
+ }
+
+ def "Testing setting of LdapUserResponse domain to null"() {
+ given: "We have an LdapUserResponse"
+ LdapUserResponse response = new LdapUserResponse()
+ when: "A domain is set"
+ response.setDomain(null)
+ then: "Get domain returns the set value."
+ response.getDomain() == null
+ }
}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/9300d4a3/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserSpec.groovy
----------------------------------------------------------------------
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserSpec.groovy
index 8fd1ccc..6df947b 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserSpec.groovy
@@ -22,7 +22,7 @@ class LdapUserSpec extends spock.lang.Specification {
def "Testing LdapUsers hashCode generation"() {
given:
- def userA = new LdapUser(usernameA, "", "", "", "")
+ def userA = new LdapUser(usernameA, "", "", "", "", "")
expect:
userA.hashCode() == usernameA.hashCode()
where:
@@ -31,8 +31,8 @@ class LdapUserSpec extends spock.lang.Specification {
def "Testing that LdapUser successfully gives the correct result for a compare to"() {
given: "You have created two LDAP user objects"
- def userA = new LdapUser(usernameA, "", "", "", "")
- def userB = new LdapUser(usernameB, "", "", "", "")
+ def userA = new LdapUser(usernameA, "", "", "", "", "")
+ def userB = new LdapUser(usernameB, "", "", "", "", "")
expect: "That when compared the result is less than or equal to 0"
userA.compareTo(userB) <= 0
where: "The following values are used"
@@ -43,8 +43,8 @@ class LdapUserSpec extends spock.lang.Specification {
def "Testing that LdapUsers equality"() {
given:
- def userA = new LdapUser(usernameA, "", "", "", "")
- def userB = new LdapUser(usernameB, "", "", "", "")
+ def userA = new LdapUser(usernameA, "", "", "", "", "")
+ def userB = new LdapUser(usernameB, "", "", "", "", "")
expect:
userA.equals(userA) == true
userA.equals(new Object()) == false
@@ -56,7 +56,7 @@ class LdapUserSpec extends spock.lang.Specification {
def "Testing that the username is correctly set with the ldap object"() {
given: "You have created a LDAP user object with a username"
- def user = new LdapUser(username, "", "", "","")
+ def user = new LdapUser(username, "", "", "", "", "")
expect: "The username is equal to the given data source"
user.getUsername() == username
where: "The username is set to "
@@ -65,7 +65,7 @@ class LdapUserSpec extends spock.lang.Specification {
def "Testing the email is correctly set with the ldap object"() {
given: "You have created a LDAP user object with a email"
- def user = new LdapUser("", email, "", "","")
+ def user = new LdapUser("", email, "", "", "", "")
expect: "The email is equal to the given data source"
user.getEmail() == email
where: "The email is set to "
@@ -74,7 +74,7 @@ class LdapUserSpec extends spock.lang.Specification {
def "Testing the firstname is correctly set with the ldap object"() {
given: "You have created a LDAP user object with a firstname"
- def user = new LdapUser("", "", firstname, "", "")
+ def user = new LdapUser("", "", firstname, "", "", "")
expect: "The firstname is equal to the given data source"
user.getFirstname() == firstname
where: "The firstname is set to "
@@ -83,7 +83,7 @@ class LdapUserSpec extends spock.lang.Specification {
def "Testing the lastname is correctly set with the ldap object"() {
given: "You have created a LDAP user object with a lastname"
- def user = new LdapUser("", "", "", lastname, "")
+ def user = new LdapUser("", "", "", lastname, "", "")
expect: "The lastname is equal to the given data source"
user.getLastname() == lastname
where: "The lastname is set to "
@@ -92,10 +92,19 @@ class LdapUserSpec extends spock.lang.Specification {
def "Testing the principal is correctly set with the ldap object"() {
given: "You have created a LDAP user object with a principal"
- def user = new LdapUser("", "", "", "", principal)
+ def user = new LdapUser("", "", "", "", principal, "")
expect: "The principal is equal to the given data source"
user.getPrincipal() == principal
- where: "The username is set to "
+ where: "The principal is set to "
principal << ["", null, "cn=rmurphy,dc=cloudstack,dc=org"]
}
+
+ def "Testing the domain is correctly set with the ldap object"() {
+ given: "You have created a LDAP user object with a principal"
+ def user = new LdapUser("", "", "", "", "", domain)
+ expect: "The principal is equal to the given data source"
+ user.getDomain() == domain
+ where: "The username is set to "
+ domain << ["", null, "engineering"]
+ }
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/9300d4a3/server/src/com/cloud/user/DomainManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/user/DomainManagerImpl.java b/server/src/com/cloud/user/DomainManagerImpl.java
index 2942148..8416645 100644
--- a/server/src/com/cloud/user/DomainManagerImpl.java
+++ b/server/src/com/cloud/user/DomainManagerImpl.java
@@ -115,6 +115,15 @@ public class DomainManagerImpl extends ManagerBase implements DomainManager, Dom
}
@Override
+ public Domain getDomainByName(String name, long parentId) {
+ SearchCriteria<DomainVO> sc = _domainDao.createSearchCriteria();
+ sc.addAnd("name", SearchCriteria.Op.EQ, name);
+ sc.addAnd("parent", SearchCriteria.Op.EQ, parentId);
+ Domain domain = _domainDao.findOneBy(sc);
+ return domain;
+ }
+
+ @Override
public Set<Long> getDomainChildrenIds(String parentDomainPath) {
Set<Long> childDomains = new HashSet<Long>();
SearchCriteria<DomainVO> sc = _domainDao.createSearchCriteria();
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/9300d4a3/server/test/com/cloud/user/MockDomainManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/user/MockDomainManagerImpl.java b/server/test/com/cloud/user/MockDomainManagerImpl.java
index 616e12d..aab8001 100644
--- a/server/test/com/cloud/user/MockDomainManagerImpl.java
+++ b/server/test/com/cloud/user/MockDomainManagerImpl.java
@@ -52,6 +52,12 @@ public class MockDomainManagerImpl extends ManagerBase implements DomainManager,
}
@Override
+ public Domain getDomainByName(String name, long parentId) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
public boolean isChildDomain(Long parentId, Long childId) {
// TODO Auto-generated method stub
return false;