From f2bbf62d9d5d87149b5db729b902e6ba6364718a Mon Sep 17 00:00:00 2001 From: kishan Date: Wed, 18 Jul 2012 14:20:04 -0700 Subject: [PATCH] Added getUser API to get user details using API key. Services like S3 can user this API to authenticate. API is admin only. --- .../com/cloud/api/commands/GetUserCmd.java | 76 +++++++++++++++++++ api/src/com/cloud/user/AccountService.java | 2 + client/tomcatconf/commands.properties.in | 1 + .../com/cloud/user/AccountManagerImpl.java | 5 ++ .../com/cloud/user/dao/UserAccountDao.java | 1 + .../cloud/user/dao/UserAccountDaoImpl.java | 17 +++++ 6 files changed, 102 insertions(+) create mode 100644 api/src/com/cloud/api/commands/GetUserCmd.java diff --git a/api/src/com/cloud/api/commands/GetUserCmd.java b/api/src/com/cloud/api/commands/GetUserCmd.java new file mode 100644 index 00000000000..465e440a0f3 --- /dev/null +++ b/api/src/com/cloud/api/commands/GetUserCmd.java @@ -0,0 +1,76 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.api.commands; + +import org.apache.log4j.Logger; + +import com.cloud.api.ApiConstants; +import com.cloud.api.BaseCmd; +import com.cloud.api.Implementation; +import com.cloud.api.Parameter; +import com.cloud.api.response.UserResponse; +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.user.UserAccount; + +@Implementation(description="Find user account by API key", responseObject=UserResponse.class) +public class GetUserCmd extends BaseCmd { + public static final Logger s_logger = Logger.getLogger(GetUserCmd.class.getName()); + + private static final String s_name = "getuserresponse"; + + ///////////////////////////////////////////////////// + //////////////// API parameters ///////////////////// + ///////////////////////////////////////////////////// + + @Parameter(name=ApiConstants.API_KEY, type=CommandType.STRING, required=true, description="API key of the user") + private String apiKey; + + ///////////////////////////////////////////////////// + /////////////////// Accessors /////////////////////// + ///////////////////////////////////////////////////// + + public String getApiKey() { + return apiKey; + } + + ///////////////////////////////////////////////////// + /////////////// API Implementation/////////////////// + ///////////////////////////////////////////////////// + + @Override + public String getCommandName() { + return s_name; + } + + @Override + public long getEntityOwnerId() { + return 0; + } + + @Override + public void execute(){ + UserAccount result = _accountService.getUserByApiKey(getApiKey()); + if(result != null){ + UserResponse response = _responseGenerator.createUserResponse(result); + response.setResponseName(getCommandName()); + response.setResponseName(getCommandName()); + this.setResponseObject(response); + } else { + throw new InvalidParameterValueException("User with specified API key does not exist"); + } + } +} diff --git a/api/src/com/cloud/user/AccountService.java b/api/src/com/cloud/user/AccountService.java index 02e9b27f0f5..53383d3c7c3 100755 --- a/api/src/com/cloud/user/AccountService.java +++ b/api/src/com/cloud/user/AccountService.java @@ -196,6 +196,8 @@ public interface AccountService { List searchForUsers(ListUsersCmd cmd) throws PermissionDeniedException; + UserAccount getUserByApiKey(String apiKey); + void checkAccess(Account account, Domain domain) throws PermissionDeniedException; void checkAccess(Account account, AccessType accessType, boolean sameOwner, ControlledEntity... entities) throws PermissionDeniedException; diff --git a/client/tomcatconf/commands.properties.in b/client/tomcatconf/commands.properties.in index 08c175bbc52..28beadeef85 100755 --- a/client/tomcatconf/commands.properties.in +++ b/client/tomcatconf/commands.properties.in @@ -19,6 +19,7 @@ listUsers=com.cloud.api.commands.ListUsersCmd;7 ####lockUser=com.cloud.api.commands.LockUserCmd;7 disableUser=com.cloud.api.commands.DisableUserCmd;7 enableUser=com.cloud.api.commands.EnableUserCmd;7 +getUser=com.cloud.api.commands.GetUserCmd;1 #### Domain commands createDomain=com.cloud.api.commands.CreateDomainCmd;1 diff --git a/server/src/com/cloud/user/AccountManagerImpl.java b/server/src/com/cloud/user/AccountManagerImpl.java index e66b886839d..0a11dc4d884 100755 --- a/server/src/com/cloud/user/AccountManagerImpl.java +++ b/server/src/com/cloud/user/AccountManagerImpl.java @@ -2225,4 +2225,9 @@ public class AccountManagerImpl implements AccountManager, AccountService, Manag } } + + @Override + public UserAccount getUserByApiKey(String apiKey) { + return _userAccountDao.getUserByApiKey(apiKey); + } } diff --git a/server/src/com/cloud/user/dao/UserAccountDao.java b/server/src/com/cloud/user/dao/UserAccountDao.java index f0907192c46..eb4e0cdf517 100644 --- a/server/src/com/cloud/user/dao/UserAccountDao.java +++ b/server/src/com/cloud/user/dao/UserAccountDao.java @@ -23,4 +23,5 @@ import com.cloud.utils.db.GenericDao; public interface UserAccountDao extends GenericDao { UserAccount getUserAccount(String username, Long domainId); boolean validateUsernameInDomain(String username, Long domainId); + UserAccount getUserByApiKey(String apiKey); } diff --git a/server/src/com/cloud/user/dao/UserAccountDaoImpl.java b/server/src/com/cloud/user/dao/UserAccountDaoImpl.java index 5cc7434c886..663e58fba4f 100644 --- a/server/src/com/cloud/user/dao/UserAccountDaoImpl.java +++ b/server/src/com/cloud/user/dao/UserAccountDaoImpl.java @@ -21,10 +21,20 @@ import javax.ejb.Local; import com.cloud.user.UserAccount; import com.cloud.user.UserAccountVO; import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; @Local(value={UserAccountDao.class}) public class UserAccountDaoImpl extends GenericDaoBase implements UserAccountDao { + + protected final SearchBuilder userAccountSearch; + + protected UserAccountDaoImpl() { + userAccountSearch = createSearchBuilder(); + userAccountSearch.and("apiKey", userAccountSearch.entity().getApiKey(), SearchCriteria.Op.EQ); + userAccountSearch.done(); + } + @Override public UserAccount getUserAccount(String username, Long domainId) { if ((username == null) || (domainId == null)) { @@ -45,4 +55,11 @@ public class UserAccountDaoImpl extends GenericDaoBase impl } return false; } + + @Override + public UserAccount getUserByApiKey(String apiKey) { + SearchCriteria sc = userAccountSearch.create(); + sc.setParameters("apiKey",apiKey); + return findOneBy(sc); + } }