Search in sources :

Example 1 with ApiKey

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);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) ApiKey(org.jbei.ice.storage.model.ApiKey) HibernateException(org.hibernate.HibernateException)

Example 2 with ApiKey

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;
}
Also used : PermissionException(org.jbei.ice.lib.access.PermissionException) Account(org.jbei.ice.storage.model.Account) ApiKey(org.jbei.ice.storage.model.ApiKey) Results(org.jbei.ice.lib.dto.common.Results) AccessKey(org.jbei.ice.lib.dto.access.AccessKey)

Example 3 with ApiKey

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;
}
Also used : PermissionException(org.jbei.ice.lib.access.PermissionException) ApiKey(org.jbei.ice.storage.model.ApiKey)

Example 4 with ApiKey

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);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) ApiKey(org.jbei.ice.storage.model.ApiKey) HibernateException(org.hibernate.HibernateException)

Example 5 with ApiKey

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);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) ApiKey(org.jbei.ice.storage.model.ApiKey) HibernateException(org.hibernate.HibernateException)

Aggregations

ApiKey (org.jbei.ice.storage.model.ApiKey)8 DAOException (org.jbei.ice.storage.DAOException)5 HibernateException (org.hibernate.HibernateException)4 PermissionException (org.jbei.ice.lib.access.PermissionException)2 AccessKey (org.jbei.ice.lib.dto.access.AccessKey)2 Account (org.jbei.ice.storage.model.Account)2 Date (java.util.Date)1 Results (org.jbei.ice.lib.dto.common.Results)1 AccountDAO (org.jbei.ice.storage.hibernate.dao.AccountDAO)1