Skip to content

Commit b82a5c1

Browse files
author
Daan Hoogland
committed
unlink a domain from ldap
1 parent 0d16c05 commit b82a5c1

File tree

3 files changed

+91
-3
lines changed

3 files changed

+91
-3
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.cloudstack.api.command;
20+
21+
import com.cloud.exception.ConcurrentOperationException;
22+
import com.cloud.exception.InsufficientCapacityException;
23+
import com.cloud.exception.NetworkRuleConflictException;
24+
import com.cloud.exception.ResourceAllocationException;
25+
import com.cloud.exception.ResourceUnavailableException;
26+
import com.cloud.user.Account;
27+
import org.apache.cloudstack.api.APICommand;
28+
import org.apache.cloudstack.api.ApiConstants;
29+
import org.apache.cloudstack.api.BaseCmd;
30+
import org.apache.cloudstack.api.Parameter;
31+
import org.apache.cloudstack.api.ServerApiException;
32+
import org.apache.cloudstack.api.response.SuccessResponse;
33+
import org.apache.cloudstack.api.response.DomainResponse;
34+
import org.apache.cloudstack.ldap.LdapManager;
35+
36+
import javax.inject.Inject;
37+
38+
@APICommand(name = "unlinkDomainFromLdap", description = "remove the linkage of a cloudstack domain to group or OU in ldap",
39+
responseObject = SuccessResponse.class, since = "4.23.0", requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
40+
public class UnlinkDomainFromLdapCmd extends BaseCmd {
41+
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, required = true, entityType = DomainResponse.class,
42+
description = "The id of the domain which has to be linked to LDAP.")
43+
private Long domainId;
44+
45+
@Inject
46+
private LdapManager _ldapManager;
47+
48+
public Long getDomainId() {
49+
return domainId;
50+
}
51+
52+
@Override
53+
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
54+
boolean rc = _ldapManager.unlinkDomainFromLdap(this);
55+
SuccessResponse response = new SuccessResponse();
56+
if (rc) {
57+
response.setDisplayText("Domain unlinked from LDAP successfully");
58+
response.setSuccess(true);
59+
} else {
60+
response.setDisplayText("Failed to unlink domain from LDAP");
61+
response.setSuccess(false);
62+
}
63+
setResponseObject(response);
64+
}
65+
66+
@Override
67+
public long getEntityOwnerId() {
68+
return Account.ACCOUNT_ID_SYSTEM;
69+
}
70+
}

plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/ldap/LdapManager.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.cloudstack.api.command.LdapListConfigurationCmd;
2424
import org.apache.cloudstack.api.command.LinkAccountToLdapCmd;
2525
import org.apache.cloudstack.api.command.LinkDomainToLdapCmd;
26+
import org.apache.cloudstack.api.command.UnlinkDomainFromLdapCmd;
2627
import org.apache.cloudstack.api.response.LdapConfigurationResponse;
2728
import org.apache.cloudstack.api.response.LdapUserResponse;
2829

@@ -34,7 +35,7 @@
3435

3536
public interface LdapManager extends PluggableService {
3637

37-
enum LinkType { GROUP, OU;}
38+
enum LinkType { GROUP, OU}
3839

3940
LdapConfigurationResponse addConfiguration(final LdapAddConfigurationCmd cmd) throws InvalidParameterValueException;
4041

@@ -69,6 +70,8 @@ enum LinkType { GROUP, OU;}
6970

7071
LinkDomainToLdapResponse linkDomainToLdap(LinkDomainToLdapCmd cmd);
7172

73+
boolean unlinkDomainFromLdap(UnlinkDomainFromLdapCmd cmd);
74+
7275
LdapTrustMapVO getDomainLinkedToLdap(long domainId);
7376

7477
List<LdapTrustMapVO> getDomainLinkage(long domainId);

plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/ldap/LdapManagerImpl.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.apache.cloudstack.api.command.LdapUserSearchCmd;
4444
import org.apache.cloudstack.api.command.LinkAccountToLdapCmd;
4545
import org.apache.cloudstack.api.command.LinkDomainToLdapCmd;
46+
import org.apache.cloudstack.api.command.UnlinkDomainFromLdapCmd;
4647
import org.apache.cloudstack.api.response.LdapConfigurationResponse;
4748
import org.apache.cloudstack.api.response.LdapUserResponse;
4849
import org.apache.cloudstack.api.response.LinkAccountToLdapResponse;
@@ -292,7 +293,7 @@ private LdapConfigurationResponse deleteConfigurationInternal(final String hostn
292293

293294
@Override
294295
public List<Class<?>> getCommands() {
295-
final List<Class<?>> cmdList = new ArrayList<Class<?>>();
296+
final List<Class<?>> cmdList = new ArrayList<>();
296297
cmdList.add(LdapUserSearchCmd.class);
297298
cmdList.add(LdapListUsersCmd.class);
298299
cmdList.add(LdapAddConfigurationCmd.class);
@@ -393,7 +394,7 @@ public Pair<List<? extends LdapConfigurationVO>, Integer> listConfigurations(fin
393394
final boolean listAll = cmd.listAll();
394395
final Long id = cmd.getId();
395396
final Pair<List<LdapConfigurationVO>, Integer> result = _ldapConfigurationDao.searchConfigurations(id, hostname, port, domainId, listAll);
396-
return new Pair<List<? extends LdapConfigurationVO>, Integer>(result.first(), result.second());
397+
return new Pair<>(result.first(), result.second());
397398
}
398399

399400
@Override
@@ -423,6 +424,11 @@ public LinkDomainToLdapResponse linkDomainToLdap(LinkDomainToLdapCmd cmd) {
423424
return linkDomainToLdap(cmd.getDomainId(),cmd.getType(), ldapDomain,cmd.getAccountType());
424425
}
425426

427+
@Override
428+
public boolean unlinkDomainFromLdap(UnlinkDomainFromLdapCmd cmd) {
429+
return unlinkDomainFromLdap(cmd.getDomainId());
430+
}
431+
426432
private LinkDomainToLdapResponse linkDomainToLdap(Long domainId, String type, String name, Account.Type accountType) {
427433
Validate.notNull(type, "type cannot be null. It should either be GROUP or OU");
428434
Validate.notNull(domainId, "domainId cannot be null.");
@@ -442,6 +448,15 @@ private LinkDomainToLdapResponse linkDomainToLdap(Long domainId, String type, St
442448
return response;
443449
}
444450

451+
private boolean unlinkDomainFromLdap(Long domainId) {
452+
LdapTrustMapVO vo = _ldapTrustMapDao.findByDomainId(domainId);
453+
if (vo != null) {
454+
removeTrustmap(vo);
455+
return true;
456+
}
457+
return false;
458+
}
459+
445460
@Override
446461
public LdapTrustMapVO getDomainLinkedToLdap(long domainId){
447462
return _ldapTrustMapDao.findByDomainId(domainId);

0 commit comments

Comments
 (0)