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);
}
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);
}
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());
}
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);
}
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);
}
Aggregations