Skip to content

Commit 7718dd8

Browse files
Daan HooglandDaanHoogland
authored andcommitted
small refactors on account manager
1 parent a7178ee commit 7718dd8

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
@@ -86,7 +86,6 @@
8686
import org.apache.commons.codec.binary.Base64;
8787
import org.apache.commons.collections.CollectionUtils;
8888
import org.apache.commons.lang3.BooleanUtils;
89-
import org.apache.commons.lang3.StringUtils;
9089
import org.jetbrains.annotations.NotNull;
9190
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
9291

@@ -177,6 +176,7 @@
177176
import com.cloud.utils.ConstantTimeComparator;
178177
import com.cloud.utils.NumbersUtil;
179178
import com.cloud.utils.Pair;
179+
import com.cloud.utils.StringUtils;
180180
import com.cloud.utils.Ternary;
181181
import com.cloud.utils.UuidUtils;
182182
import com.cloud.utils.component.ComponentContext;
@@ -592,10 +592,9 @@ public boolean isAdmin(Long accountId) {
592592
}
593593
if ((isRootAdmin(accountId)) || (isDomainAdmin(accountId)) || (isResourceDomainAdmin(accountId))) {
594594
return true;
595-
} else if (acct.getType() == Account.Type.READ_ONLY_ADMIN) {
596-
return true;
595+
} else {
596+
return acct.getType() == Account.Type.READ_ONLY_ADMIN;
597597
}
598-
599598
}
600599
return false;
601600
}
@@ -649,10 +648,7 @@ public boolean isDomainAdmin(Long accountId) {
649648
@Override
650649
public boolean isNormalUser(long accountId) {
651650
AccountVO acct = _accountDao.findById(accountId);
652-
if (acct != null && acct.getType() == Account.Type.NORMAL) {
653-
return true;
654-
}
655-
return false;
651+
return acct != null && acct.getType() == Account.Type.NORMAL;
656652
}
657653

658654
@Override
@@ -683,10 +679,7 @@ public boolean isInternalAccount(long accountId) {
683679
if (account == null) {
684680
return false; //account is deleted or does not exist
685681
}
686-
if (isRootAdmin(accountId) || (account.getType() == Account.Type.ADMIN)) {
687-
return true;
688-
}
689-
return false;
682+
return isRootAdmin(accountId) || (account.getType() == Account.Type.ADMIN);
690683
}
691684

692685
@Override
@@ -736,12 +729,7 @@ public void checkAccess(Account caller, AccessType accessType, boolean sameOwner
736729
HashMap<Long, List<ControlledEntity>> domains = new HashMap<>();
737730

738731
for (ControlledEntity entity : entities) {
739-
long domainId = entity.getDomainId();
740-
if (entity.getAccountId() != -1 && domainId == -1) { // If account exists domainId should too so calculate
741-
// it. This condition might be hit for templates or entities which miss domainId in their tables
742-
Account account = ApiDBUtils.findAccountById(entity.getAccountId());
743-
domainId = account != null ? account.getDomainId() : -1;
744-
}
732+
long domainId = getDomainIdFor(entity);
745733
if (entity.getAccountId() != -1 && domainId != -1 && !(entity instanceof VirtualMachineTemplate)
746734
&& !(entity instanceof Network && accessType != null && (accessType == AccessType.UseEntry || accessType == AccessType.OperateEntry))
747735
&& !(entity instanceof AffinityGroup) && !(entity instanceof VirtualRouter)) {
@@ -793,6 +781,17 @@ public void checkAccess(Account caller, AccessType accessType, boolean sameOwner
793781

794782
}
795783

784+
private static long getDomainIdFor(ControlledEntity entity) {
785+
long domainId = entity.getDomainId();
786+
if (entity.getAccountId() != -1 && domainId == -1) {
787+
// If account exists domainId should too so calculate it.
788+
// This condition might be hit for templates or entities which miss domainId in their tables
789+
Account account = ApiDBUtils.findAccountById(entity.getAccountId());
790+
domainId = account != null ? account.getDomainId() : -1;
791+
}
792+
return domainId;
793+
}
794+
796795
@Override
797796
public void validateAccountHasAccessToResource(Account account, AccessType accessType, Object resource) {
798797
Class<?> resourceClass = resource.getClass();
@@ -2830,11 +2829,11 @@ public UserAccount authenticateUser(final String username, final String password
28302829
final Boolean ApiSourceCidrChecksEnabled = ApiServiceConfiguration.ApiSourceCidrChecksEnabled.value();
28312830

28322831
if (ApiSourceCidrChecksEnabled) {
2833-
logger.debug("CIDRs from which account '" + account.toString() + "' is allowed to perform API calls: " + accessAllowedCidrs);
2832+
logger.debug("CIDRs from which account '{}' is allowed to perform API calls: {}", account, accessAllowedCidrs);
28342833

28352834
// Block when is not in the list of allowed IPs
28362835
if (!NetUtils.isIpInCidrList(loginIpAddress, accessAllowedCidrs.split(","))) {
2837-
logger.warn("Request by account '" + account.toString() + "' was denied since " + loginIpAddress.toString().replace("/", "") + " does not match " + accessAllowedCidrs);
2836+
logger.warn("Request by account '{}' was denied since {} does not match {}", account , loginIpAddress.toString().replace("/", ""), accessAllowedCidrs);
28382837
throw new CloudAuthenticationException("Failed to authenticate user '" + username + "' in domain '" + domain.getPath() + "' from ip "
28392838
+ loginIpAddress.toString().replace("/", "") + "; please provide valid credentials");
28402839
}
@@ -3007,7 +3006,7 @@ private UserAccount getUserAccountForSSO(String username, Long domainId, Map<Str
30073006
if (unsignedRequestBuffer.length() != 0) {
30083007
unsignedRequestBuffer.append("&");
30093008
}
3010-
unsignedRequestBuffer.append(paramName).append("=").append(URLEncoder.encode(paramValue, "UTF-8"));
3009+
unsignedRequestBuffer.append(paramName).append("=").append(URLEncoder.encode(paramValue, StringUtils.getPreferredCharset()));
30113010
}
30123011
}
30133012

0 commit comments

Comments
 (0)