Added getUser API to get user details using API key. Services like S3 can user this API to authenticate. API is admin only.

This commit is contained in:
kishan 2012-07-18 14:20:04 -07:00
parent 2b2e491f27
commit f2bbf62d9d
6 changed files with 102 additions and 0 deletions

View File

@ -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");
}
}
}

View File

@ -196,6 +196,8 @@ public interface AccountService {
List<? extends UserAccount> 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;

View File

@ -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

View File

@ -2225,4 +2225,9 @@ public class AccountManagerImpl implements AccountManager, AccountService, Manag
}
}
@Override
public UserAccount getUserByApiKey(String apiKey) {
return _userAccountDao.getUserByApiKey(apiKey);
}
}

View File

@ -23,4 +23,5 @@ import com.cloud.utils.db.GenericDao;
public interface UserAccountDao extends GenericDao<UserAccountVO, Long> {
UserAccount getUserAccount(String username, Long domainId);
boolean validateUsernameInDomain(String username, Long domainId);
UserAccount getUserByApiKey(String apiKey);
}

View File

@ -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<UserAccountVO, Long> implements UserAccountDao {
protected final SearchBuilder<UserAccountVO> 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<UserAccountVO, Long> impl
}
return false;
}
@Override
public UserAccount getUserByApiKey(String apiKey) {
SearchCriteria<UserAccountVO> sc = userAccountSearch.create();
sc.setParameters("apiKey",apiKey);
return findOneBy(sc);
}
}