Search in sources :

Example 6 with User

use of com.pratilipi.data.type.User in project pratilipi by Pratilipi.

the class UserDataUtil method updateUserPassword.

public static void updateUserPassword(String email, String verificationToken, String newPassword) throws InvalidArgumentException, InsufficientAccessException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    User user = dataAccessor.getUserByEmail(email.toLowerCase());
    JsonObject errorMessages = new JsonObject();
    if (user == null || user.getState() == UserState.REFERRAL || user.getState() == UserState.DELETED) {
        errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_NOT_REGISTERED);
        throw new InvalidArgumentException(errorMessages);
    } else if (user.getState() == UserState.BLOCKED) {
        throw new InvalidArgumentException(GenericRequest.ERR_ACCOUNT_BLOCKED);
    }
    if (!_verifyToken(user, verificationToken))
        throw new InvalidArgumentException(GenericRequest.ERR_VERIFICATION_TOKEN_INVALID_OR_EXPIRED);
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_UPDATE, user);
    user.setPassword(PasswordUtil.getSaltedHash(newPassword));
    // Note: Do not clear VerificationToken
    user = dataAccessor.createOrUpdateUser(user, auditLog);
}
Also used : User(com.pratilipi.data.type.User) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) JsonObject(com.google.gson.JsonObject) AuditLog(com.pratilipi.data.type.AuditLog)

Example 7 with User

use of com.pratilipi.data.type.User in project pratilipi by Pratilipi.

the class UserDataUtil method _validateUserDataForSave.

private static void _validateUserDataForSave(UserData userData) throws InvalidArgumentException {
    boolean isNew = userData.getId() == null;
    JsonObject errorMessages = new JsonObject();
    // New user profile must have name.
    if (isNew && userData.getFirstName() == null)
        errorMessages.addProperty("name", GenericRequest.ERR_NAME_REQUIRED);
    // New user profile must have email.
    if (isNew && (!userData.hasEmail() || userData.getEmail() == null))
        errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REQUIRED);
    else // Email can not be un-set or set to null.
    if (!isNew && userData.hasEmail() && userData.getEmail() == null)
        errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REQUIRED);
    // For new user, user email should be not registered already.
    if (isNew && DataAccessorFactory.getDataAccessor().getUserByEmail(userData.getEmail()) != null)
        errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REGISTERED_ALREADY);
    else // Email, if provided, must not be registered with some other user.
    if (!isNew && userData.hasEmail() && userData.getEmail() != null) {
        User user = DataAccessorFactory.getDataAccessor().getUserByEmail(userData.getEmail());
        if (user != null && !user.getId().equals(userData.getId()))
            errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REGISTERED_ALREADY);
    }
    if (errorMessages.entrySet().size() > 0)
        throw new InvalidArgumentException(errorMessages);
}
Also used : User(com.pratilipi.data.type.User) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) JsonObject(com.google.gson.JsonObject)

Example 8 with User

use of com.pratilipi.data.type.User in project pratilipi by Pratilipi.

the class UserAuthorDataUtil method getUserFollowList.

public static DataListCursorTuple<AuthorData> getUserFollowList(Long userId, String cursor, Integer offset, Integer resultCount) {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    User user = dataAccessor.getUser(userId);
    if (user.getFollowCount() == 0L)
        return new DataListCursorTuple<>(new ArrayList<AuthorData>(0), null, 0L);
    DataListCursorTuple<Long> authorIdListCursorTuple = dataAccessor.getUserAuthorFollowList(userId, null, cursor, offset, resultCount);
    List<Long> authorIdList = authorIdListCursorTuple.getDataList();
    List<AuthorData> authorDataList = AuthorDataUtil.createAuthorDataList(authorIdList, true);
    // Setting AuthorData.isFollowing flag
    if (userId.equals(AccessTokenFilter.getAccessToken().getUserId())) {
        for (AuthorData authorData : authorDataList) authorData.setFollowing(true);
    } else {
        List<UserAuthor> userAuthorList = dataAccessor.getUserAuthorList(AccessTokenFilter.getAccessToken().getUserId(), authorIdList);
        if (userAuthorList.size() != 0)
            for (int i = 0; i < authorIdList.size(); i++) if (userAuthorList.get(i) != null && userAuthorList.get(i).getFollowState() == UserFollowState.FOLLOWING)
                authorDataList.get(i).setFollowing(true);
    }
    // Setting UserData.isFollowing flag
    Author author = dataAccessor.getAuthorByUserId(AccessTokenFilter.getAccessToken().getUserId());
    if (author != null) {
        List<Long> userIdList = new ArrayList<>(authorDataList.size());
        List<UserData> userDataList = new ArrayList<>(authorDataList.size());
        for (AuthorData authorData : authorDataList) {
            if (authorData.getUser().getId() != null) {
                userIdList.add(authorData.getUser().getId());
                userDataList.add(authorData.getUser());
            }
        }
        List<UserAuthor> userAuthorList = dataAccessor.getUserAuthorList(userIdList, author.getId());
        if (userAuthorList != null) {
            for (UserAuthor userAuthor : userAuthorList) if (userAuthor != null && userAuthor.getFollowState() == UserFollowState.FOLLOWING)
                userDataList.get(userIdList.indexOf(userAuthor.getUserId())).setFollowing(true);
        }
    }
    return new DataListCursorTuple<>(authorDataList, authorIdListCursorTuple.getCursor(), user.getFollowCount());
}
Also used : User(com.pratilipi.data.type.User) UserData(com.pratilipi.data.client.UserData) DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) DataListCursorTuple(com.pratilipi.data.DataListCursorTuple) UserAuthorData(com.pratilipi.data.client.UserAuthorData) AuthorData(com.pratilipi.data.client.AuthorData) UserAuthor(com.pratilipi.data.type.UserAuthor) UserAuthor(com.pratilipi.data.type.UserAuthor) Author(com.pratilipi.data.type.Author)

Example 9 with User

use of com.pratilipi.data.type.User in project pratilipi by Pratilipi.

the class AuditLogProcessApi method _updateEmailTable.

private void _updateEmailTable(List<Email> emailList) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Set<Long> userIds = new HashSet<>(emailList.size());
    for (Email email : emailList) userIds.add(email.getUserId());
    Map<Long, UserPreferenceRtdb> userPreferenceMap = DataAccessorFactory.getRtdbAccessor().getUserPreferences(userIds);
    Map<Long, User> users = dataAccessor.getUsers(userIds);
    List<Email> emailsToUpdate = new ArrayList<>(emailList.size());
    for (Email email : emailList) {
        UserPreferenceRtdb preference = userPreferenceMap.get(email.getUserId());
        User user = users.get(email.getUserId());
        if (user.getEmail() == null)
            continue;
        if (preference.getEmailFrequency() == EmailFrequency.NEVER)
            continue;
        email.setScheduledDate(preference.getEmailFrequency().getNextSchedule(user.getLastEmailedDate()));
        emailsToUpdate.add(email);
    }
    emailsToUpdate = dataAccessor.createOrUpdateEmailList(emailsToUpdate);
}
Also used : Email(com.pratilipi.data.type.Email) User(com.pratilipi.data.type.User) DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) UserPreferenceRtdb(com.pratilipi.data.type.UserPreferenceRtdb) HashSet(java.util.HashSet)

Example 10 with User

use of com.pratilipi.data.type.User in project pratilipi by Pratilipi.

the class UserDataUtil method loginUser.

public static UserData loginUser(String email, String password) throws InvalidArgumentException, InsufficientAccessException {
    // Do nothing if a user is already logged in
    if (!AccessTokenFilter.getAccessToken().getUserId().equals(0L))
        return getCurrentUser();
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    User user = dataAccessor.getUserByEmail(email.toLowerCase());
    if (user == null || user.getState() == UserState.REFERRAL || user.getState() == UserState.DELETED) {
        JsonObject errorMessages = new JsonObject();
        errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_NOT_REGISTERED);
        throw new InvalidArgumentException(errorMessages);
    } else if (user.getState() == UserState.BLOCKED) {
        throw new InsufficientAccessException(GenericRequest.ERR_ACCOUNT_BLOCKED);
    }
    if (user.getPassword() == null && user.getFacebookId() != null)
        throw new InvalidArgumentException(GenericRequest.ERR_EMAIL_REGISTERED_WITH_FACEBOOK);
    if (user.getPassword() == null && user.getGoogleId() != null)
        throw new InvalidArgumentException(GenericRequest.ERR_EMAIL_REGISTERED_WITH_GOOGLE);
    if (PasswordUtil.check(password, user.getPassword())) {
        _loginUser(AccessTokenFilter.getAccessToken(), user);
        return createUserData(user);
    }
    if (_verifyToken(user, password)) {
        AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_ADD, user);
        user.setVerificationToken(null);
        dataAccessor.createOrUpdateUser(user, auditLog);
        _loginUser(AccessTokenFilter.getAccessToken(), user);
        return createUserData(user);
    }
    throw new InvalidArgumentException(GenericRequest.ERR_INVALID_CREDENTIALS);
}
Also used : User(com.pratilipi.data.type.User) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) JsonObject(com.google.gson.JsonObject) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog)

Aggregations

User (com.pratilipi.data.type.User)30 DataAccessor (com.pratilipi.data.DataAccessor)22 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)9 AuditLog (com.pratilipi.data.type.AuditLog)8 UserData (com.pratilipi.data.client.UserData)7 UserEntity (com.pratilipi.data.type.gae.UserEntity)7 Date (java.util.Date)7 Author (com.pratilipi.data.type.Author)6 ConversationUser (com.pratilipi.data.type.ConversationUser)5 ArrayList (java.util.ArrayList)5 JsonObject (com.google.gson.JsonObject)4 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)4 ConversationUserEntity (com.pratilipi.data.type.gae.ConversationUserEntity)4 GenericResponse (com.pratilipi.api.shared.GenericResponse)3 DateFormat (java.text.DateFormat)3 SimpleDateFormat (java.text.SimpleDateFormat)3 HashMap (java.util.HashMap)3 Get (com.pratilipi.api.annotation.Get)2 BlobAccessor (com.pratilipi.data.BlobAccessor)2 DocAccessor (com.pratilipi.data.DocAccessor)2