Search in sources :

Example 16 with MongoDataException

use of org.craftercms.commons.mongo.MongoDataException in project profile by craftercms.

the class ProfileRepositoryImpl method findByIds.

@Override
public Iterable<Profile> findByIds(List<String> ids, String sortBy, SortOrder sortOrder, String... attributesToReturn) throws MongoDataException {
    List<ObjectId> objectIds = new ArrayList<>(ids.size());
    for (String id : ids) {
        try {
            objectIds.add(new ObjectId(id));
        } catch (IllegalArgumentException ex) {
            String msg = "Given id '" + id + "' can't be converted to an ObjectId";
            logger.error(msg, ex);
            throw new MongoDataException(msg, ex);
        }
    }
    try {
        String query = getQueryFor(KEY_FIND_BY_IDS_QUERY);
        Find find = getCollection().find(query, objectIds);
        addSort(find, sortBy, sortOrder);
        addProjection(find, attributesToReturn);
        return find.as(Profile.class);
    } catch (MongoException ex) {
        String msg = "Unable to find profiles for ids " + ids;
        logger.error(msg, ex);
        throw new MongoDataException(msg, ex);
    }
}
Also used : MongoException(com.mongodb.MongoException) ObjectId(org.bson.types.ObjectId) ArrayList(java.util.ArrayList) Find(org.jongo.Find) MongoDataException(org.craftercms.commons.mongo.MongoDataException)

Example 17 with MongoDataException

use of org.craftercms.commons.mongo.MongoDataException in project profile by craftercms.

the class AuthenticationServiceImpl method invalidateTicket.

@Override
public void invalidateTicket(String ticketId) throws ProfileException {
    try {
        Ticket ticket = ticketRepository.findByStringId(ticketId);
        if (ticket != null) {
            checkIfManageTicketsIsAllowed(ticket.getTenant());
            ticketRepository.removeByStringId(ticketId);
            logger.debug(LOG_KEY_TICKET_INVALIDATED, ticketId);
        }
    } catch (MongoDataException e) {
        throw new I10nProfileException(ERROR_KEY_DELETE_TICKET_ERROR, ticketId);
    }
}
Also used : Ticket(org.craftercms.profile.api.Ticket) I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) MongoDataException(org.craftercms.commons.mongo.MongoDataException)

Example 18 with MongoDataException

use of org.craftercms.commons.mongo.MongoDataException in project profile by craftercms.

the class AuthenticationServiceImpl method deletePersistentLogin.

@Override
public void deletePersistentLogin(String loginId) throws ProfileException {
    try {
        PersistentLogin login = persistentLoginRepository.findByStringId(loginId);
        if (login != null) {
            checkIfManageTicketsIsAllowed(login.getTenant());
            persistentLoginRepository.removeByStringId(loginId);
            logger.debug(LOG_KEY_PERSISTENT_LOGIN_DELETED, loginId);
        }
    } catch (MongoDataException e) {
        throw new I10nProfileException(ERROR_KEY_DELETE_PERSISTENT_LOGIN_ERROR, loginId);
    }
}
Also used : I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) MongoDataException(org.craftercms.commons.mongo.MongoDataException) PersistentLogin(org.craftercms.profile.api.PersistentLogin)

Example 19 with MongoDataException

use of org.craftercms.commons.mongo.MongoDataException in project profile by craftercms.

the class AuthenticationServiceImpl method authenticate.

@Override
public Ticket authenticate(String tenantName, String username, String password) throws ProfileException {
    checkIfManageTicketsIsAllowed(tenantName);
    Profile profile = profileService.getProfileByUsername(tenantName, username, ProfileConstants.NO_ATTRIBUTE);
    if (profile == null) {
        // Invalid username
        throw new BadCredentialsException();
    }
    if (!profile.isEnabled()) {
        throw new DisabledProfileException(profile.getId().toString(), tenantName);
    }
    if (isProfileInTimeOut(profile)) {
        throw new ProfileLockedException();
    }
    try {
        if (!CryptoUtils.matchPassword(profile.getPassword(), password)) {
            // Invalid password
            countAsFail(profile);
            throw new BadCredentialsException();
        }
        clearAllLoginAttempts(profile);
        Ticket ticket = new Ticket();
        ticket.setId(UUID.randomUUID().toString());
        ticket.setTenant(tenantName);
        ticket.setProfileId(profile.getId().toString());
        ticket.setLastRequestTime(new Date());
        ticketRepository.insert(ticket);
        logger.debug(LOG_KEY_AUTHENTICATION_SUCCESSFUL, profile.getId(), ticket);
        return ticket;
    } catch (MongoDataException e) {
        throw new I10nProfileException(ERROR_KEY_CREATE_TICKET_ERROR, profile.getId());
    }
}
Also used : Ticket(org.craftercms.profile.api.Ticket) ProfileLockedException(org.craftercms.profile.exceptions.ProfileLockedException) I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) DisabledProfileException(org.craftercms.profile.exceptions.DisabledProfileException) MongoDataException(org.craftercms.commons.mongo.MongoDataException) BadCredentialsException(org.craftercms.profile.exceptions.BadCredentialsException) Profile(org.craftercms.profile.api.Profile) Date(java.util.Date)

Example 20 with MongoDataException

use of org.craftercms.commons.mongo.MongoDataException in project profile by craftercms.

the class AuthenticationServiceImpl method createPersistentLogin.

@Override
public PersistentLogin createPersistentLogin(String profileId) throws ProfileException {
    Profile profile = profileService.getProfile(profileId, ProfileConstants.NO_ATTRIBUTE);
    if (profile != null) {
        String tenantName = profile.getTenant();
        checkIfManageTicketsIsAllowed(tenantName);
        if (!profile.isEnabled()) {
            throw new DisabledProfileException(profile.getId().toString(), tenantName);
        }
        try {
            PersistentLogin login = new PersistentLogin();
            login.setId(UUID.randomUUID().toString());
            login.setTenant(tenantName);
            login.setProfileId(profileId);
            login.setToken(UUID.randomUUID().toString());
            login.setTimestamp(new Date());
            persistentLoginRepository.insert(login);
            logger.debug(LOG_KEY_PERSISTENT_LOGIN_CREATED, profile.getId(), login);
            return login;
        } catch (MongoDataException e) {
            throw new I10nProfileException(ERROR_KEY_CREATE_PERSISTENT_LOGIN_ERROR, profile.getId());
        }
    } else {
        throw new NoSuchProfileException.ById(profileId);
    }
}
Also used : I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) DisabledProfileException(org.craftercms.profile.exceptions.DisabledProfileException) MongoDataException(org.craftercms.commons.mongo.MongoDataException) PersistentLogin(org.craftercms.profile.api.PersistentLogin) Profile(org.craftercms.profile.api.Profile) Date(java.util.Date)

Aggregations

MongoDataException (org.craftercms.commons.mongo.MongoDataException)39 I10nProfileException (org.craftercms.profile.api.exceptions.I10nProfileException)27 Profile (org.craftercms.profile.api.Profile)15 MongoException (com.mongodb.MongoException)12 Date (java.util.Date)7 Find (org.jongo.Find)6 Tenant (org.craftercms.profile.api.Tenant)5 Ticket (org.craftercms.profile.api.Ticket)4 ObjectId (org.bson.types.ObjectId)3 DuplicateKeyException (org.craftercms.commons.mongo.DuplicateKeyException)3 PersistentLogin (org.craftercms.profile.api.PersistentLogin)3 DisabledProfileException (org.craftercms.profile.exceptions.DisabledProfileException)3 FindOne (org.jongo.FindOne)3 Update (org.jongo.Update)3 UpdateHelper (org.craftercms.commons.mongo.UpdateHelper)2 VerificationToken (org.craftercms.profile.api.VerificationToken)2 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 FileExistsException (org.apache.commons.io.FileExistsException)1 FileInfo (org.craftercms.commons.mongo.FileInfo)1