Search in sources :

Example 16 with UserIndexKey

use of org.onebusaway.users.model.UserIndexKey in project onebusaway-application-modules by camsys.

the class UserServiceImplTest method createUserIndex.

private UserIndex createUserIndex(String indexType, String indexValue, User user) {
    UserIndex index = new UserIndex();
    index.setId(new UserIndexKey(indexType, indexValue));
    index.setUser(user);
    index.setCredentials("");
    user.getUserIndices().add(index);
    return index;
}
Also used : UserIndex(org.onebusaway.users.model.UserIndex) UserIndexKey(org.onebusaway.users.model.UserIndexKey)

Example 17 with UserIndexKey

use of org.onebusaway.users.model.UserIndexKey in project onebusaway-application-modules by camsys.

the class KeysResource method saveOrUpdateKey.

// Private methods
private void saveOrUpdateKey(String apiKey, Long minApiRequestInterval, String contactName, String contactCompany, String contactEmail, String contactDetails) throws Exception {
    UserIndexKey key = new UserIndexKey(UserIndexTypes.API_KEY, apiKey);
    UserIndex userIndexForId = _userService.getUserIndexForId(key);
    if (userIndexForId != null) {
        throw new Exception("API key " + apiKey + " already exists.");
    }
    UserIndex userIndex = _userService.getOrCreateUserForIndexKey(key, "", true);
    _userPropertiesService.authorizeApi(userIndex.getUser(), minApiRequestInterval);
    // Set the API Key contact info
    User user = userIndex.getUser();
    _userPropertiesService.updateApiKeyContactInfo(user, contactName, contactCompany, contactEmail, contactDetails);
    // Clear the cached value here
    _userService.getMinApiRequestIntervalForKey(apiKey, true);
}
Also used : UserIndex(org.onebusaway.users.model.UserIndex) UserIndexKey(org.onebusaway.users.model.UserIndexKey) User(org.onebusaway.users.model.User) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 18 with UserIndexKey

use of org.onebusaway.users.model.UserIndexKey in project onebusaway-application-modules by camsys.

the class KeysResource method listEmails.

@Path("/list-emails")
@GET
@Produces("application/json")
public Response listEmails() throws JsonGenerationException, JsonMappingException, IOException {
    log.info("Starting listEmails");
    try {
        validateSecurity();
        List<String> apiKeys = _userService.getUserIndexKeyValuesForKeyType(UserIndexTypes.API_KEY);
        List<String> emails = new ArrayList<String>();
        int count = 0;
        for (String apiKey : apiKeys) {
            UserIndexKey key = new UserIndexKey(UserIndexTypes.API_KEY, apiKey);
            UserIndex userIndex = _userService.getUserIndexForId(key);
            count++;
            if (userIndex != null) {
                if (userIndex.getUser() != null) {
                    if (userIndex.getUser().getProperties() != null) {
                        Object o = userIndex.getUser().getProperties();
                        if (o instanceof UserPropertiesV3) {
                            UserPropertiesV3 v3 = (UserPropertiesV3) o;
                            if (!StringUtils.isBlank(v3.getContactEmail())) {
                                emails.add(v3.getContactEmail());
                            }
                        }
                    }
                }
            }
            if (count % 100 == 0)
                log.info("processed " + count + " users....");
        }
        log.info("processing complete of " + count + " users!");
        Response response = constructResponse(emails);
        log.info("Returning response from listEmails");
        return response;
    } catch (Exception e) {
        log.error(e.getMessage());
        throw new WebApplicationException(e, Response.serverError().build());
    }
}
Also used : Response(javax.ws.rs.core.Response) UserIndex(org.onebusaway.users.model.UserIndex) UserIndexKey(org.onebusaway.users.model.UserIndexKey) WebApplicationException(javax.ws.rs.WebApplicationException) ArrayList(java.util.ArrayList) UserPropertiesV3(org.onebusaway.users.model.properties.UserPropertiesV3) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 19 with UserIndexKey

use of org.onebusaway.users.model.UserIndexKey in project onebusaway-application-modules by camsys.

the class KeysResource method delete.

private void delete(String apiKey) throws Exception {
    UserIndexKey key = new UserIndexKey(UserIndexTypes.API_KEY, apiKey);
    UserIndex userIndex = _userService.getUserIndexForId(key);
    if (userIndex == null)
        throw new Exception("API key " + apiKey + " not found (userIndex null).");
    User user = userIndex.getUser();
    boolean found = false;
    for (UserIndex index : user.getUserIndices()) {
        if (index.getId().getValue().equalsIgnoreCase(key.getValue())) {
            userIndex = index;
            found = true;
            break;
        }
    }
    if (!found)
        throw new Exception("API key " + apiKey + " not found (no exact match).");
    _userService.removeUserIndexForUser(user, userIndex.getId());
    if (user.getUserIndices().isEmpty())
        _userService.deleteUser(user);
    // Clear the cached value here
    try {
        _userService.getMinApiRequestIntervalForKey(apiKey, true);
    } catch (Exception e) {
    // Ignore this
    }
}
Also used : UserIndex(org.onebusaway.users.model.UserIndex) UserIndexKey(org.onebusaway.users.model.UserIndexKey) User(org.onebusaway.users.model.User) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 20 with UserIndexKey

use of org.onebusaway.users.model.UserIndexKey in project onebusaway-application-modules by camsys.

the class KeysResource method updateKeyContactInfo.

private void updateKeyContactInfo(String apiKey, String contactName, String contactCompany, String contactEmail, String contactDetails, Long minApiReqIntervalLong) throws Exception {
    UserIndexKey key = new UserIndexKey(UserIndexTypes.API_KEY, apiKey);
    UserIndex userIndex = _userService.getUserIndexForId(key);
    if (userIndex == null)
        throw new Exception("API key " + apiKey + " not found (userIndex null).");
    User user = userIndex.getUser();
    UserBean bean = _userService.getUserAsBean(user);
    String keyContactName = bean.getContactName();
    String keyContactCompany = bean.getContactCompany();
    String keyContactEmail = bean.getContactEmail();
    String keyContactDetails = bean.getContactDetails();
    if (contactName != null) {
        keyContactName = contactName;
    }
    if (contactCompany != null) {
        keyContactCompany = contactCompany;
    }
    if (contactEmail != null) {
        keyContactEmail = contactEmail;
    }
    if (contactDetails != null) {
        keyContactDetails = contactDetails;
    }
    _userPropertiesService.authorizeApi(userIndex.getUser(), minApiReqIntervalLong);
    _userPropertiesService.updateApiKeyContactInfo(user, keyContactName, keyContactCompany, keyContactEmail, keyContactDetails);
}
Also used : UserIndex(org.onebusaway.users.model.UserIndex) UserIndexKey(org.onebusaway.users.model.UserIndexKey) User(org.onebusaway.users.model.User) UserBean(org.onebusaway.users.client.model.UserBean) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException) WebApplicationException(javax.ws.rs.WebApplicationException)

Aggregations

UserIndexKey (org.onebusaway.users.model.UserIndexKey)30 UserIndex (org.onebusaway.users.model.UserIndex)22 User (org.onebusaway.users.model.User)16 UserBean (org.onebusaway.users.client.model.UserBean)6 IOException (java.io.IOException)5 WebApplicationException (javax.ws.rs.WebApplicationException)5 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)5 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)5 Test (org.junit.Test)4 IndexedUserDetails (org.onebusaway.users.model.IndexedUserDetails)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 PostConstruct (javax.annotation.PostConstruct)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 Response (javax.ws.rs.core.Response)2 DefaultUserAuthenticationToken (org.onebusaway.users.impl.authentication.DefaultUserAuthenticationToken)2 UserRole (org.onebusaway.users.model.UserRole)2 UserPropertiesV2 (org.onebusaway.users.model.properties.UserPropertiesV2)2