Skip to content

Commit a9df597

Browse files
Daan HooglandDaanHoogland
authored andcommitted
small refactors on account manager
1 parent 5bf869c commit a9df597

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

@@ -176,6 +175,7 @@
176175
import com.cloud.utils.ConstantTimeComparator;
177176
import com.cloud.utils.NumbersUtil;
178177
import com.cloud.utils.Pair;
178+
import com.cloud.utils.StringUtils;
179179
import com.cloud.utils.Ternary;
180180
import com.cloud.utils.UuidUtils;
181181
import com.cloud.utils.component.ComponentContext;
@@ -591,10 +591,9 @@ public boolean isAdmin(Long accountId) {
591591
}
592592
if ((isRootAdmin(accountId)) || (isDomainAdmin(accountId)) || (isResourceDomainAdmin(accountId))) {
593593
return true;
594-
} else if (acct.getType() == Account.Type.READ_ONLY_ADMIN) {
595-
return true;
594+
} else {
595+
return acct.getType() == Account.Type.READ_ONLY_ADMIN;
596596
}
597-
598597
}
599598
return false;
600599
}
@@ -648,10 +647,7 @@ public boolean isDomainAdmin(Long accountId) {
648647
@Override
649648
public boolean isNormalUser(long accountId) {
650649
AccountVO acct = _accountDao.findById(accountId);
651-
if (acct != null && acct.getType() == Account.Type.NORMAL) {
652-
return true;
653-
}
654-
return false;
650+
return acct != null && acct.getType() == Account.Type.NORMAL;
655651
}
656652

657653
@Override
@@ -682,10 +678,7 @@ public boolean isInternalAccount(long accountId) {
682678
if (account == null) {
683679
return false; //account is deleted or does not exist
684680
}
685-
if (isRootAdmin(accountId) || (account.getType() == Account.Type.ADMIN)) {
686-
return true;
687-
}
688-
return false;
681+
return isRootAdmin(accountId) || (account.getType() == Account.Type.ADMIN);
689682
}
690683

691684
@Override
@@ -735,12 +728,7 @@ public void checkAccess(Account caller, AccessType accessType, boolean sameOwner
735728
HashMap<Long, List<ControlledEntity>> domains = new HashMap<>();
736729

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

793781
}
794782

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

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

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

0 commit comments

Comments
 (0)