Search in sources :

Example 21 with UserIndex

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

the class UserManagementServiceImplTest method testUpdatePassword.

@Test
public void testUpdatePassword() {
    String credentials = "encryptedPassword";
    Integer userId = 1;
    UserDetail userDetail = mock(UserDetail.class);
    UserIndex userIndex = mock(UserIndex.class);
    UserRole userRole = mock(UserRole.class);
    Set<UserIndex> userIndices = new HashSet<UserIndex>();
    userIndices.add(userIndex);
    Set<UserRole> userRoles = new HashSet<UserRole>();
    userRoles.add(userRole);
    when(userRole.getName()).thenReturn("ROLE_ADMINISTRATOR");
    buildUserDetail(userId, userDetail, "password");
    when(userService.getUserForId(userId)).thenReturn(user);
    when(passwordEncoder.encodePassword("password", "admin")).thenReturn(credentials);
    when(user.getUserIndices()).thenReturn(userIndices);
    when(user.getRoles()).thenReturn(userRoles);
    boolean success = service.updateUser(userDetail);
    assertTrue("User's password updated successfully", success);
    verify(passwordEncoder).encodePassword("password", "admin");
    verify(authoritiesService, times(0)).getAdministratorRole();
    verify(userDao).saveOrUpdateUser(user);
}
Also used : UserDetail(org.onebusaway.admin.model.ui.UserDetail) UserIndex(org.onebusaway.users.model.UserIndex) UserRole(org.onebusaway.users.model.UserRole) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 22 with UserIndex

use of org.onebusaway.users.model.UserIndex 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 23 with UserIndex

use of org.onebusaway.users.model.UserIndex 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 24 with UserIndex

use of org.onebusaway.users.model.UserIndex 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 25 with UserIndex

use of org.onebusaway.users.model.UserIndex 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

UserIndex (org.onebusaway.users.model.UserIndex)44 User (org.onebusaway.users.model.User)23 UserIndexKey (org.onebusaway.users.model.UserIndexKey)22 Test (org.junit.Test)9 UserBean (org.onebusaway.users.client.model.UserBean)8 UserRole (org.onebusaway.users.model.UserRole)8 HashSet (java.util.HashSet)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 UserDetail (org.onebusaway.admin.model.ui.UserDetail)5 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 PostConstruct (javax.annotation.PostConstruct)3 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 UserPropertiesV2 (org.onebusaway.users.model.properties.UserPropertiesV2)2