Skip to content

Commit 2825c07

Browse files
committed
CLOUDSTACK-8647 support for assigning and admin to linked ldap domain
if an admin username is given to the linkDomainToLdap, added support to import this user User will be imported only if the user is available in the group/ou in ldap and an account with the name doesnt exist in cloudstack. on successful import, accountid will be returned in response.
1 parent 5929186 commit 2825c07

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LinkDomainToLdapCmd.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import javax.inject.Inject;
2222

2323
import com.cloud.exception.InvalidParameterValueException;
24+
import com.cloud.user.AccountService;
25+
import com.cloud.user.User;
26+
import com.cloud.user.UserAccount;
2427
import org.apache.cloudstack.api.APICommand;
2528
import org.apache.cloudstack.api.ApiConstants;
2629
import org.apache.cloudstack.api.ApiErrorCode;
@@ -30,10 +33,14 @@
3033
import org.apache.cloudstack.api.response.DomainResponse;
3134
import org.apache.cloudstack.api.response.LinkDomainToLdapResponse;
3235
import org.apache.cloudstack.ldap.LdapManager;
36+
import org.apache.cloudstack.ldap.LdapUser;
37+
import org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException;
3338
import org.apache.log4j.Logger;
3439

3540
import com.cloud.user.Account;
3641

42+
import java.util.UUID;
43+
3744
@APICommand(name = "linkDomainToLdap", description = "link an existing cloudstack domain to group or OU in ldap", responseObject = LinkDomainToLdapResponse.class, since = "4.6.0",
3845
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
3946
public class LinkDomainToLdapCmd extends BaseCmd {
@@ -59,10 +66,34 @@ public class LinkDomainToLdapCmd extends BaseCmd {
5966
@Inject
6067
private LdapManager _ldapManager;
6168

69+
@Inject
70+
public AccountService _accountService;
71+
6272
@Override
6373
public void execute() throws ServerApiException {
6474
try {
6575
LinkDomainToLdapResponse response = _ldapManager.linkDomainToLdap(domainId, type, name, accountType);
76+
if(admin!=null) {
77+
try {
78+
LdapUser ldapUser = _ldapManager.getUser(admin, type, name);
79+
if(!ldapUser.isDisabled()) {
80+
Account account = _accountService.getActiveAccountByName(admin, domainId);
81+
if (account == null) {
82+
UserAccount userAccount =
83+
_accountService.createUserAccount(admin, "", ldapUser.getFirstname(), ldapUser.getLastname(), ldapUser.getEmail(), null, admin, Account.ACCOUNT_TYPE_DOMAIN_ADMIN, domainId, admin, null, UUID.randomUUID().toString(),
84+
UUID.randomUUID().toString(), User.Source.LDAP);
85+
response.setAdminId(String.valueOf(userAccount.getAccountId()));
86+
s_logger.info("created an account with name " + admin + " in the given domain " + domainId);
87+
} else {
88+
s_logger.debug("an account with name " + admin + " already exists in the domain " + domainId);
89+
}
90+
} else {
91+
s_logger.debug("ldap user with username "+admin+" is disabled in the given group/ou");
92+
}
93+
} catch (NoLdapUserMatchingQueryException e) {
94+
s_logger.debug("no ldap user matching username " + admin + " in the given group/ou");
95+
}
96+
}
6697
response.setObjectName("LinkDomainToLdap");
6798
response.setResponseName(getCommandName());
6899
setResponseObject(response);

plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/response/LinkDomainToLdapResponse.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public class LinkDomainToLdapResponse extends BaseResponse {
4141
@Param(description = "Type of the account to auto import")
4242
private short accountType;
4343

44+
@SerializedName(ApiConstants.ACCOUNT_ID)
45+
@Param(description = "Domain Admin accountId that is created")
46+
private String adminId;
47+
4448
public LinkDomainToLdapResponse(long domainId, String type, String name, short accountType) {
4549
this.domainId = domainId;
4650
this.name = name;
@@ -63,4 +67,12 @@ public String getType() {
6367
public short getAccountType() {
6468
return accountType;
6569
}
70+
71+
public String getAdminId() {
72+
return adminId;
73+
}
74+
75+
public void setAdminId(String adminId) {
76+
this.adminId = adminId;
77+
}
6678
}

0 commit comments

Comments
 (0)