Skip to content

Commit db7f290

Browse files
committed
Refactor domain handling in OAuth2AuthManagerImpl to use DomainService instead of DomainDao
1 parent ac4a622 commit db7f290

2 files changed

Lines changed: 34 additions & 27 deletions

File tree

plugins/user-authenticators/oauth2/src/main/java/org/apache/cloudstack/oauth2/OAuth2AuthManagerImpl.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
//
1919
package org.apache.cloudstack.oauth2;
2020

21-
import com.cloud.domain.DomainVO;
22-
import com.cloud.domain.dao.DomainDao;
21+
import com.cloud.domain.Domain;
22+
import com.cloud.user.DomainService;
2323
import com.cloud.user.dao.UserDao;
2424
import com.cloud.utils.component.Manager;
2525
import com.cloud.utils.component.ManagerBase;
@@ -56,7 +56,7 @@ public class OAuth2AuthManagerImpl extends ManagerBase implements OAuth2AuthMana
5656
protected OauthProviderDao _oauthProviderDao;
5757

5858
@Inject
59-
private DomainDao _domainDao;
59+
private DomainService _domainService;
6060

6161
protected static Map<String, UserOAuth2Authenticator> userOAuth2AuthenticationProvidersMap = new HashMap<>();
6262

@@ -247,22 +247,16 @@ public Long resolveDomainId(Map<String, Object[]> params) {
247247
if (GLOBAL_DOMAIN_FILTER.equals(domainUuid)) {
248248
return GLOBAL_DOMAIN_ID;
249249
}
250-
DomainVO domain = _domainDao.findByUuid(domainUuid);
250+
Domain domain = _domainService.getDomain(domainUuid);
251251
if (Objects.nonNull(domain)) {
252252
return domain.getId();
253253
}
254254
}
255255
final String[] domainArray = (String[])params.get(ApiConstants.DOMAIN);
256256
if (ArrayUtils.isNotEmpty(domainArray)) {
257-
String path = domainArray[0];
257+
String path = normalizeDomainPath(domainArray[0]);
258258
if (StringUtils.isNotEmpty(path)) {
259-
if (!path.startsWith("/")) {
260-
path = "/" + path;
261-
}
262-
if (!path.endsWith("/")) {
263-
path += "/";
264-
}
265-
DomainVO domain = _domainDao.findDomainByPath(path);
259+
Domain domain = _domainService.findDomainByIdOrPath(null, path);
266260
if (Objects.nonNull(domain)) {
267261
return domain.getId();
268262
}
@@ -271,6 +265,19 @@ public Long resolveDomainId(Map<String, Object[]> params) {
271265
return null;
272266
}
273267

268+
protected String normalizeDomainPath(String path) {
269+
if (StringUtils.isEmpty(path)) {
270+
return null;
271+
}
272+
if (!path.startsWith("/")) {
273+
path = "/" + path;
274+
}
275+
if (!path.endsWith("/")) {
276+
path += "/";
277+
}
278+
return path;
279+
}
280+
274281
@Override
275282
public String getConfigComponentName() {
276283
return "OAUTH2-PLUGIN";

plugins/user-authenticators/oauth2/src/test/java/org/apache/cloudstack/oauth2/OAuth2AuthManagerImplTest.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
package org.apache.cloudstack.oauth2;
2121

22-
import com.cloud.domain.DomainVO;
23-
import com.cloud.domain.dao.DomainDao;
22+
import com.cloud.domain.Domain;
23+
import com.cloud.user.DomainService;
2424
import com.cloud.utils.exception.CloudRuntimeException;
2525
import org.apache.cloudstack.api.ApiConstants;
2626
import org.apache.cloudstack.oauth2.api.command.DeleteOAuthProviderCmd;
@@ -61,7 +61,7 @@ public class OAuth2AuthManagerImplTest {
6161
OauthProviderDao _oauthProviderDao;
6262

6363
@Mock
64-
DomainDao _domainDao;
64+
DomainService _domainService;
6565

6666
AutoCloseable closeable;
6767
@Before
@@ -273,9 +273,9 @@ public void testResolveDomainIdFromDomainUuid() {
273273
Map<String, Object[]> params = new HashMap<>();
274274
params.put(ApiConstants.DOMAIN_ID, new String[]{"test-uuid-123"});
275275

276-
DomainVO domain = Mockito.mock(DomainVO.class);
276+
Domain domain = Mockito.mock(Domain.class);
277277
when(domain.getId()).thenReturn(10L);
278-
when(_domainDao.findByUuid("test-uuid-123")).thenReturn(domain);
278+
when(_domainService.getDomain("test-uuid-123")).thenReturn(domain);
279279

280280
Long result = _authManager.resolveDomainId(params);
281281
assertEquals(Long.valueOf(10L), result);
@@ -295,9 +295,9 @@ public void testResolveDomainIdFromDomainPath() {
295295
Map<String, Object[]> params = new HashMap<>();
296296
params.put(ApiConstants.DOMAIN, new String[]{"ROOT/child"});
297297

298-
DomainVO domain = Mockito.mock(DomainVO.class);
298+
Domain domain = Mockito.mock(Domain.class);
299299
when(domain.getId()).thenReturn(20L);
300-
when(_domainDao.findDomainByPath("/ROOT/child/")).thenReturn(domain);
300+
when(_domainService.findDomainByIdOrPath(null, "/ROOT/child/")).thenReturn(domain);
301301

302302
Long result = _authManager.resolveDomainId(params);
303303
assertEquals(Long.valueOf(20L), result);
@@ -308,9 +308,9 @@ public void testResolveDomainIdFromDomainPathWithSlashes() {
308308
Map<String, Object[]> params = new HashMap<>();
309309
params.put(ApiConstants.DOMAIN, new String[]{"/ROOT/child/"});
310310

311-
DomainVO domain = Mockito.mock(DomainVO.class);
311+
Domain domain = Mockito.mock(Domain.class);
312312
when(domain.getId()).thenReturn(20L);
313-
when(_domainDao.findDomainByPath("/ROOT/child/")).thenReturn(domain);
313+
when(_domainService.findDomainByIdOrPath(null, "/ROOT/child/")).thenReturn(domain);
314314

315315
Long result = _authManager.resolveDomainId(params);
316316
assertEquals(Long.valueOf(20L), result);
@@ -321,7 +321,7 @@ public void testResolveDomainIdReturnsNullWhenNotFound() {
321321
Map<String, Object[]> params = new HashMap<>();
322322
params.put(ApiConstants.DOMAIN_ID, new String[]{"nonexistent-uuid"});
323323

324-
when(_domainDao.findByUuid("nonexistent-uuid")).thenReturn(null);
324+
when(_domainService.getDomain("nonexistent-uuid")).thenReturn(null);
325325

326326
Long result = _authManager.resolveDomainId(params);
327327
assertNull(result);
@@ -340,9 +340,9 @@ public void testResolveDomainIdPrefersUuidOverPath() {
340340
params.put(ApiConstants.DOMAIN_ID, new String[]{"test-uuid"});
341341
params.put(ApiConstants.DOMAIN, new String[]{"/ROOT/child/"});
342342

343-
DomainVO domain = Mockito.mock(DomainVO.class);
343+
Domain domain = Mockito.mock(Domain.class);
344344
when(domain.getId()).thenReturn(10L);
345-
when(_domainDao.findByUuid("test-uuid")).thenReturn(domain);
345+
when(_domainService.getDomain("test-uuid")).thenReturn(domain);
346346

347347
Long result = _authManager.resolveDomainId(params);
348348
assertEquals(Long.valueOf(10L), result);
@@ -354,10 +354,10 @@ public void testResolveDomainIdFallsBackToPathWhenUuidNotFound() {
354354
params.put(ApiConstants.DOMAIN_ID, new String[]{"bad-uuid"});
355355
params.put(ApiConstants.DOMAIN, new String[]{"/ROOT/"});
356356

357-
when(_domainDao.findByUuid("bad-uuid")).thenReturn(null);
358-
DomainVO domain = Mockito.mock(DomainVO.class);
357+
when(_domainService.getDomain("bad-uuid")).thenReturn(null);
358+
Domain domain = Mockito.mock(Domain.class);
359359
when(domain.getId()).thenReturn(1L);
360-
when(_domainDao.findDomainByPath("/ROOT/")).thenReturn(domain);
360+
when(_domainService.findDomainByIdOrPath(null, "/ROOT/")).thenReturn(domain);
361361

362362
Long result = _authManager.resolveDomainId(params);
363363
assertEquals(Long.valueOf(1L), result);

0 commit comments

Comments
 (0)