use of org.jbei.ice.storage.model.ApiKey in project ice by JBEI.
the class ApiKeyDAO method getApiKeysCount.
public long getApiKeysCount(String userId) {
try {
CriteriaQuery<Long> query = getBuilder().createQuery(Long.class);
Root<ApiKey> from = query.from(ApiKey.class);
query.select(getBuilder().countDistinct(from.get("id")));
if (userId != null)
query.where(getBuilder().equal(from.get("ownerEmail"), userId)).distinct(true);
return currentSession().createQuery(query).uniqueResult();
} catch (HibernateException he) {
Logger.error(he);
throw new DAOException(he);
}
}
use of org.jbei.ice.storage.model.ApiKey in project ice by JBEI.
the class UserApiKeys method getKeys.
/**
* Retrieves either list of available keys for current user or all keys.
* If requesting all keys then user must be an administrator
*
* @param limit maximum number of keys to retrieve
* @param offset paging parameter start
* @param sortField field to sort on
* @param asc whether the retrieve order is in ascending order
* @param getAvailable whether to retrieve all available keys or restrict by current user
* @return wrapper around list of retrieved keys including number available
* @throws PermissionException if <code>getAvailable</code> is true but user making the request does not have
* administrative privileges
*/
public Results<AccessKey> getKeys(int limit, int offset, String sortField, boolean asc, boolean getAvailable) {
Results<AccessKey> accessKeyResults = new Results<>();
List<ApiKey> results;
AccountController accountController = new AccountController();
boolean isAdmin = accountController.isAdministrator(this.userId);
if (getAvailable) {
if (!isAdmin)
throw new PermissionException("Cannot retrieve all api keys without admin privileges");
results = apiKeyDAO.getAllApiKeys(sortField, limit, offset, asc);
} else {
results = apiKeyDAO.getApiKeysForUser(userId, sortField, limit, offset, asc);
}
for (ApiKey key : results) {
AccessKey accessKey = key.toDataTransferObject();
Account account = accountController.getByEmail(key.getOwnerEmail());
accessKey.setAccount(account.toDataTransferObject());
accessKeyResults.getData().add(accessKey);
}
// get count
String user = getAvailable ? null : this.userId;
long count = apiKeyDAO.getApiKeysCount(user);
accessKeyResults.setResultCount(count);
return accessKeyResults;
}
use of org.jbei.ice.storage.model.ApiKey in project ice by JBEI.
the class UserApiKeys method deleteKey.
/**
* Deletes an api key
*
* @param id unique database identifier for the key
* @param secret unique key secret
* @return true if the key is successfully deleted. false otherwise, including if the key cannot be found
* @throws PermissionException if the key being deleted does not belong to user and user does not have
* administrative privileges
*/
public boolean deleteKey(long id, String secret) {
ApiKey key = apiKeyDAO.get(id);
if (key == null)
return false;
if (!key.getSecret().equalsIgnoreCase(secret))
return false;
if (!this.userId.equalsIgnoreCase(key.getOwnerEmail())) {
AccountController accountController = new AccountController();
boolean isAdmin = accountController.isAdministrator(this.userId);
if (!isAdmin)
throw new PermissionException("Cannot delete key you did not create without administrative privileges");
}
apiKeyDAO.delete(key);
return true;
}
use of org.jbei.ice.storage.model.ApiKey in project ice by JBEI.
the class ApiKeyDAO method getByClientId.
/**
* Retrieves an api key by client id.
* If one exists it is expected that the client id + the creator id uniquely retrieves it. In other words,
* the same user cannot have two api keys for the same client
*
* @param clientId client identifier for api key
* @return container that may or may not contain the found key
* @throws DAOException if more that one api key is found or there is a problem accessing the database
*/
public Optional<ApiKey> getByClientId(String clientId) {
try {
CriteriaQuery<ApiKey> query = getBuilder().createQuery(ApiKey.class);
Root<ApiKey> from = query.from(ApiKey.class);
query.where(getBuilder().equal(from.get("clientId"), clientId));
return currentSession().createQuery(query).uniqueResultOptional();
} catch (HibernateException he) {
Logger.error(he);
throw new DAOException(he);
}
}
use of org.jbei.ice.storage.model.ApiKey in project ice by JBEI.
the class ApiKeyDAO method getAllApiKeys.
public List<ApiKey> getAllApiKeys(String sort, int limit, int start, boolean asc) {
try {
CriteriaQuery<ApiKey> query = getBuilder().createQuery(ApiKey.class);
Root<ApiKey> from = query.from(ApiKey.class);
query.orderBy(asc ? getBuilder().asc(from.get(sort)) : getBuilder().desc(from.get(sort)));
return currentSession().createQuery(query).setFirstResult(start).setMaxResults(limit).list();
} catch (HibernateException he) {
Logger.error(he);
throw new DAOException(he);
}
}
Aggregations