Skip to content

Commit 9dc2f0d

Browse files
author
Daan Hoogland
committed
small refactors on account manager
1 parent 36cfd76 commit 9dc2f0d

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

server/src/main/java/com/cloud/user/AccountManagerImpl.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
import org.apache.commons.codec.binary.Base64;
8686
import org.apache.commons.collections.CollectionUtils;
8787
import org.apache.commons.lang3.BooleanUtils;
88-
import org.apache.commons.lang3.StringUtils;
8988
import org.jetbrains.annotations.NotNull;
9089
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
9190

@@ -175,6 +174,7 @@
175174
import com.cloud.utils.ConstantTimeComparator;
176175
import com.cloud.utils.NumbersUtil;
177176
import com.cloud.utils.Pair;
177+
import com.cloud.utils.StringUtils;
178178
import com.cloud.utils.Ternary;
179179
import com.cloud.utils.component.ComponentContext;
180180
import com.cloud.utils.component.Manager;
@@ -587,10 +587,9 @@ public boolean isAdmin(Long accountId) {
587587
}
588588
if ((isRootAdmin(accountId)) || (isDomainAdmin(accountId)) || (isResourceDomainAdmin(accountId))) {
589589
return true;
590-
} else if (acct.getType() == Account.Type.READ_ONLY_ADMIN) {
591-
return true;
590+
} else {
591+
return acct.getType() == Account.Type.READ_ONLY_ADMIN;
592592
}
593-
594593
}
595594
return false;
596595
}
@@ -644,10 +643,7 @@ public boolean isDomainAdmin(Long accountId) {
644643
@Override
645644
public boolean isNormalUser(long accountId) {
646645
AccountVO acct = _accountDao.findById(accountId);
647-
if (acct != null && acct.getType() == Account.Type.NORMAL) {
648-
return true;
649-
}
650-
return false;
646+
return acct != null && acct.getType() == Account.Type.NORMAL;
651647
}
652648

653649
@Override
@@ -678,10 +674,7 @@ public boolean isInternalAccount(long accountId) {
678674
if (account == null) {
679675
return false; //account is deleted or does not exist
680676
}
681-
if (isRootAdmin(accountId) || (account.getType() == Account.Type.ADMIN)) {
682-
return true;
683-
}
684-
return false;
677+
return isRootAdmin(accountId) || (account.getType() == Account.Type.ADMIN);
685678
}
686679

687680
@Override
@@ -731,12 +724,7 @@ public void checkAccess(Account caller, AccessType accessType, boolean sameOwner
731724
HashMap<Long, List<ControlledEntity>> domains = new HashMap<>();
732725

733726
for (ControlledEntity entity : entities) {
734-
long domainId = entity.getDomainId();
735-
if (entity.getAccountId() != -1 && domainId == -1) { // If account exists domainId should too so calculate
736-
// it. This condition might be hit for templates or entities which miss domainId in their tables
737-
Account account = ApiDBUtils.findAccountById(entity.getAccountId());
738-
domainId = account != null ? account.getDomainId() : -1;
739-
}
727+
long domainId = getDomainIdFor(entity);
740728
if (entity.getAccountId() != -1 && domainId != -1 && !(entity instanceof VirtualMachineTemplate)
741729
&& !(entity instanceof Network && accessType != null && (accessType == AccessType.UseEntry || accessType == AccessType.OperateEntry))
742730
&& !(entity instanceof AffinityGroup) && !(entity instanceof VirtualRouter)) {
@@ -788,6 +776,17 @@ public void checkAccess(Account caller, AccessType accessType, boolean sameOwner
788776

789777
}
790778

779+
private static long getDomainIdFor(ControlledEntity entity) {
780+
long domainId = entity.getDomainId();
781+
if (entity.getAccountId() != -1 && domainId == -1) {
782+
// If account exists domainId should too so calculate it.
783+
// This condition might be hit for templates or entities which miss domainId in their tables
784+
Account account = ApiDBUtils.findAccountById(entity.getAccountId());
785+
domainId = account != null ? account.getDomainId() : -1;
786+
}
787+
return domainId;
788+
}
789+
791790
@Override
792791
public void validateAccountHasAccessToResource(Account account, AccessType accessType, Object resource) {
793792
Class<?> resourceClass = resource.getClass();
@@ -2813,11 +2812,11 @@ public UserAccount authenticateUser(final String username, final String password
28132812
final Boolean ApiSourceCidrChecksEnabled = ApiServiceConfiguration.ApiSourceCidrChecksEnabled.value();
28142813

28152814
if (ApiSourceCidrChecksEnabled) {
2816-
logger.debug("CIDRs from which account '" + account.toString() + "' is allowed to perform API calls: " + accessAllowedCidrs);
2815+
logger.debug("CIDRs from which account '{}' is allowed to perform API calls: {}", account, accessAllowedCidrs);
28172816

28182817
// Block when is not in the list of allowed IPs
28192818
if (!NetUtils.isIpInCidrList(loginIpAddress, accessAllowedCidrs.split(","))) {
2820-
logger.warn("Request by account '" + account.toString() + "' was denied since " + loginIpAddress.toString().replace("/", "") + " does not match " + accessAllowedCidrs);
2819+
logger.warn("Request by account '{}' was denied since {} does not match {}", account , loginIpAddress.toString().replace("/", ""), accessAllowedCidrs);
28212820
throw new CloudAuthenticationException("Failed to authenticate user '" + username + "' in domain '" + domain.getPath() + "' from ip "
28222821
+ loginIpAddress.toString().replace("/", "") + "; please provide valid credentials");
28232822
}
@@ -2990,7 +2989,7 @@ private UserAccount getUserAccountForSSO(String username, Long domainId, Map<Str
29902989
if (unsignedRequestBuffer.length() != 0) {
29912990
unsignedRequestBuffer.append("&");
29922991
}
2993-
unsignedRequestBuffer.append(paramName).append("=").append(URLEncoder.encode(paramValue, "UTF-8"));
2992+
unsignedRequestBuffer.append(paramName).append("=").append(URLEncoder.encode(paramValue, StringUtils.getPreferredCharset()));
29942993
}
29952994
}
29962995

0 commit comments

Comments
 (0)