mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
ApiDiscovery: Use UserContext getCallerUser, do a lazy eval to get account service
Impl. and use UserContext to get User. CloudStack's @Inject is horrible, it may sometimes fail to inject account service during startup. Do a lazy injection using ComponentLocator when needed. Signed-off-by: Rohit Yadav <bhaisaab@apache.org>
This commit is contained in:
parent
4f889bd426
commit
f6a8b45de5
@ -50,6 +50,10 @@ public class UserContext {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public User getCallerUser() {
|
||||
return _accountMgr.getActiveUser(userId);
|
||||
}
|
||||
|
||||
public void setCallerUserId(long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
@ -16,11 +16,8 @@
|
||||
// under the License.
|
||||
package org.apache.cloudstack.api.command.user.discovery;
|
||||
|
||||
import com.cloud.user.AccountService;
|
||||
import com.cloud.user.User;
|
||||
import com.cloud.user.UserContext;
|
||||
import com.cloud.utils.component.Inject;
|
||||
import org.apache.cloudstack.acl.RoleType;
|
||||
import org.apache.cloudstack.api.APICommand;
|
||||
import org.apache.cloudstack.api.ApiConstants;
|
||||
import org.apache.cloudstack.api.BaseCmd;
|
||||
@ -42,16 +39,13 @@ public class ListApisCmd extends BaseCmd {
|
||||
@PlugService
|
||||
ApiDiscoveryService _apiDiscoveryService;
|
||||
|
||||
@Inject
|
||||
private AccountService _accountService;
|
||||
|
||||
@Parameter(name=ApiConstants.NAME, type=CommandType.STRING, description="API name")
|
||||
private String name;
|
||||
|
||||
@Override
|
||||
public void execute() throws ServerApiException {
|
||||
if (_apiDiscoveryService != null) {
|
||||
User user = _accountService.getActiveUser(UserContext.current().getCallerUserId());
|
||||
User user = UserContext.current().getCallerUser();
|
||||
ListResponse<ApiDiscoveryResponse> response = (ListResponse<ApiDiscoveryResponse>) _apiDiscoveryService.listApis(user, name);
|
||||
if (response == null) {
|
||||
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Api Discovery plugin was unable to find an api by that name or process any apis");
|
||||
|
||||
@ -17,17 +17,13 @@
|
||||
package org.apache.cloudstack.discovery;
|
||||
|
||||
import com.cloud.serializer.Param;
|
||||
import com.cloud.server.ManagementServer;
|
||||
import com.cloud.user.User;
|
||||
import com.cloud.utils.ReflectUtil;
|
||||
import com.cloud.utils.StringUtils;
|
||||
import com.cloud.utils.component.Adapters;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
import com.cloud.utils.component.Inject;
|
||||
import com.cloud.utils.component.PluggableService;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import org.apache.cloudstack.acl.APIChecker;
|
||||
import org.apache.cloudstack.acl.RoleType;
|
||||
import org.apache.cloudstack.api.APICommand;
|
||||
import org.apache.cloudstack.api.BaseCmd;
|
||||
import org.apache.cloudstack.api.BaseAsyncCmd;
|
||||
@ -54,16 +50,14 @@ import java.util.Set;
|
||||
public class ApiDiscoveryServiceImpl implements ApiDiscoveryService {
|
||||
private static final Logger s_logger = Logger.getLogger(ApiDiscoveryServiceImpl.class);
|
||||
|
||||
@Inject(adapter = APIChecker.class)
|
||||
protected Adapters<APIChecker> _apiAccessCheckers;
|
||||
|
||||
private static Map<String, ApiDiscoveryResponse> _apiNameDiscoveryResponseMap = null;
|
||||
protected static Adapters<APIChecker> s_apiAccessCheckers = null;
|
||||
private static Map<String, ApiDiscoveryResponse> s_apiNameDiscoveryResponseMap = null;
|
||||
|
||||
protected ApiDiscoveryServiceImpl() {
|
||||
super();
|
||||
if (_apiNameDiscoveryResponseMap == null) {
|
||||
if (s_apiNameDiscoveryResponseMap == null) {
|
||||
long startTime = System.nanoTime();
|
||||
_apiNameDiscoveryResponseMap = new HashMap<String, ApiDiscoveryResponse>();
|
||||
s_apiNameDiscoveryResponseMap = new HashMap<String, ApiDiscoveryResponse>();
|
||||
cacheResponseMap();
|
||||
long endTime = System.nanoTime();
|
||||
s_logger.info("Api Discovery Service: Annotation, docstrings, api relation graph processed in " + (endTime - startTime) / 1000000.0 + " ms");
|
||||
@ -142,11 +136,11 @@ public class ApiDiscoveryServiceImpl implements ApiDiscoveryService {
|
||||
}
|
||||
}
|
||||
response.setObjectName("api");
|
||||
_apiNameDiscoveryResponseMap.put(apiName, response);
|
||||
s_apiNameDiscoveryResponseMap.put(apiName, response);
|
||||
}
|
||||
|
||||
for (String apiName : _apiNameDiscoveryResponseMap.keySet()) {
|
||||
ApiDiscoveryResponse response = _apiNameDiscoveryResponseMap.get(apiName);
|
||||
for (String apiName : s_apiNameDiscoveryResponseMap.keySet()) {
|
||||
ApiDiscoveryResponse response = s_apiNameDiscoveryResponseMap.get(apiName);
|
||||
Set<ApiParameterResponse> processedParams = new HashSet<ApiParameterResponse>();
|
||||
for (ApiParameterResponse param : response.getParams()) {
|
||||
if (responseApiNameListMap.containsKey(param.getRelated())) {
|
||||
@ -166,7 +160,7 @@ public class ApiDiscoveryServiceImpl implements ApiDiscoveryService {
|
||||
} else {
|
||||
response.setRelated(null);
|
||||
}
|
||||
_apiNameDiscoveryResponseMap.put(apiName, response);
|
||||
s_apiNameDiscoveryResponseMap.put(apiName, response);
|
||||
}
|
||||
}
|
||||
|
||||
@ -175,34 +169,39 @@ public class ApiDiscoveryServiceImpl implements ApiDiscoveryService {
|
||||
ListResponse<ApiDiscoveryResponse> response = new ListResponse<ApiDiscoveryResponse>();
|
||||
List<ApiDiscoveryResponse> responseList = new ArrayList<ApiDiscoveryResponse>();
|
||||
|
||||
if (s_apiAccessCheckers == null) {
|
||||
ComponentLocator locator = ComponentLocator.getCurrentLocator();
|
||||
s_apiAccessCheckers = locator.getAdapters(APIChecker.class);
|
||||
}
|
||||
|
||||
if (user == null)
|
||||
return null;
|
||||
|
||||
if (name != null) {
|
||||
if (!_apiNameDiscoveryResponseMap.containsKey(name))
|
||||
if (!s_apiNameDiscoveryResponseMap.containsKey(name))
|
||||
return null;
|
||||
|
||||
for (APIChecker apiChecker : _apiAccessCheckers) {
|
||||
for (APIChecker apiChecker : s_apiAccessCheckers) {
|
||||
try {
|
||||
apiChecker.checkAccess(user, name);
|
||||
} catch (Exception ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
responseList.add(_apiNameDiscoveryResponseMap.get(name));
|
||||
responseList.add(s_apiNameDiscoveryResponseMap.get(name));
|
||||
|
||||
} else {
|
||||
for (String apiName : _apiNameDiscoveryResponseMap.keySet()) {
|
||||
for (String apiName : s_apiNameDiscoveryResponseMap.keySet()) {
|
||||
boolean isAllowed = true;
|
||||
for (APIChecker apiChecker : _apiAccessCheckers) {
|
||||
for (APIChecker apiChecker : s_apiAccessCheckers) {
|
||||
try {
|
||||
apiChecker.checkAccess(user, name);
|
||||
apiChecker.checkAccess(user, apiName);
|
||||
} catch (Exception ex) {
|
||||
isAllowed = false;
|
||||
}
|
||||
}
|
||||
if (isAllowed)
|
||||
responseList.add(_apiNameDiscoveryResponseMap.get(apiName));
|
||||
responseList.add(s_apiNameDiscoveryResponseMap.get(apiName));
|
||||
}
|
||||
}
|
||||
response.setResponses(responseList);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user