use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class UserDataUtil method sendEmailVerificationMail.
public static void sendEmailVerificationMail(String emailId, Language language) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
Long userId = AccessTokenFilter.getAccessToken().getUserId();
if (userId.equals(0L))
throw new InsufficientAccessException();
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
User user = dataAccessor.getUserByEmail(emailId.trim().toLowerCase());
if (user == null || !user.getId().equals(userId))
throw new InvalidArgumentException(GenericRequest.ERR_EMAIL_NOT_REGISTERED);
_sendEmailVerificationMail(user, language);
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class UserPratilipiDataUtil method hasAccessToUpdateUserPratilipiData.
public static boolean hasAccessToUpdateUserPratilipiData(UserPratilipi userPratilipi, AccessType accessType) {
AccessToken accessToken = AccessTokenFilter.getAccessToken();
if (!userPratilipi.getUserId().equals(accessToken.getUserId()))
return false;
if (!UserAccessUtil.hasUserAccess(accessToken.getUserId(), null, accessType))
return false;
// Review can not be created for content pieces created by the user
if (accessType == AccessType.USER_PRATILIPI_REVIEW) {
if (userPratilipi.getReviewState() == UserReviewState.BLOCKED)
return false;
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Pratilipi pratilipi = dataAccessor.getPratilipi(userPratilipi.getPratilipiId());
Author author = pratilipi.getAuthorId() == null ? null : dataAccessor.getAuthor(pratilipi.getAuthorId());
if (author != null && userPratilipi.getUserId().equals(author.getUserId()))
return false;
}
return true;
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class UserPratilipiDataUtil method saveUserPratilipiReview.
public static UserPratilipiData saveUserPratilipiReview(Long userId, Long pratilipiId, Integer rating, String review, UserReviewState reviewState) throws InsufficientAccessException, UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
UserPratilipi userPratilipi = dataAccessor.getUserPratilipi(userId, pratilipiId);
if (userPratilipi == null) {
userPratilipi = dataAccessor.newUserPratilipi();
userPratilipi.setUserId(userId);
userPratilipi.setPratilipiId(pratilipiId);
}
if (!hasAccessToUpdateUserPratilipiData(userPratilipi, AccessType.USER_PRATILIPI_REVIEW))
throw new InsufficientAccessException();
AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_PRATILIPI_REVIEW, userPratilipi);
if (rating != null && !rating.equals(userPratilipi.getRating()) && !(rating == 0 && userPratilipi.getRating() == null)) {
userPratilipi.setRating(rating);
userPratilipi.setRatingDate(new Date());
}
if (review != null) {
userPratilipi.setReviewTitle(null);
userPratilipi.setReview(review);
if (userPratilipi.getReviewState() != UserReviewState.PUBLISHED) {
// NOTE: As review comment and likes are counted only if they
// were created after review date, do NOT update reviewDate if
// review is already published.
userPratilipi.setReviewState(UserReviewState.PUBLISHED);
userPratilipi.setReviewDate(new Date());
}
}
// review is already published.
if (reviewState != null && reviewState != userPratilipi.getReviewState()) {
userPratilipi.setReviewState(reviewState);
userPratilipi.setReviewDate(new Date());
}
userPratilipi = dataAccessor.createOrUpdateUserPratilipi(userPratilipi, auditLog);
return createUserPratilipiData(userPratilipi);
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class AccessTokenFilter method doFilter.
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
String requestUri = request.getRequestURI();
String userAgent = request.getHeader("user-agent");
String accessTokenId = request.getParameter(RequestParameter.ACCESS_TOKEN.getName());
accessTokenId = accessTokenId == null ? null : accessTokenId.trim();
AccessToken accessToken;
if (requestUri.equals("/remote_api") || (userAgent != null && userAgent.equals("Amazon CloudFront"))) {
accessToken = null;
} else if (autoGenerate) {
// Used by gamma, default & api modules.
String accessTokenCookie = getCookieValue(RequestCookie.ACCESS_TOKEN.getName(), request);
if ((accessTokenId == null || accessTokenId.isEmpty()) && (accessTokenCookie == null || accessTokenCookie.isEmpty())) {
accessToken = AccessTokenDataUtil.newUserAccessToken(request);
} else {
accessToken = accessTokenId != null && !accessTokenId.isEmpty() ? dataAccessor.getAccessToken(accessTokenId) : dataAccessor.getAccessToken(accessTokenCookie);
if (accessToken == null || accessToken.isExpired()) {
accessToken = AccessTokenDataUtil.newUserAccessToken(request);
} else if (accessToken.getExpiry().getTime() < new Date().getTime() + AccessTokenDataUtil.MIN_EXPIRY_MILLIS) {
accessToken.setExpiry(new Date(new Date().getTime() + AccessTokenDataUtil.MAX_EXPIRY_MILLIS));
accessToken = dataAccessor.createOrUpdateAccessToken(accessToken);
}
}
if (!accessToken.getId().equals(accessTokenCookie)) {
accessTokenId = accessToken.getId();
setCookieValue(RequestCookie.ACCESS_TOKEN.getName(), accessTokenId, 30, response);
}
} else if (isWorker) {
// Used by worker module.
accessTokenId = dataAccessor.getAppProperty(AppProperty.WORKER_ACCESS_TOKEN_ID).getValue();
accessToken = dataAccessor.getAccessToken(accessTokenId);
} else if (requestUri.equals("/user/accesstoken")) {
// Used by gamma-android & android module.
accessToken = AccessTokenDataUtil.newUserAccessToken(request);
dispatchResponse(response, new UserAccessTokenApi.Response(accessToken.getId(), accessToken.getExpiry()));
return;
} else {
// TODO: Consider only header. Ignore accessToken from request param.
if (accessTokenId == null || accessTokenId.isEmpty()) {
accessTokenId = request.getHeader(RequestHeader.ACCESS_TOKEN.getName());
accessTokenId = accessTokenId == null ? null : accessTokenId.trim();
}
if (accessTokenId == null || accessTokenId.isEmpty()) {
dispatchResponse(response, new InvalidArgumentException("Access Token is missing."));
return;
} else if ((accessToken = dataAccessor.getAccessToken(accessTokenId)) == null) {
dispatchResponse(response, new InvalidArgumentException("Access Token is invalid."));
return;
} else if (accessToken.isExpired()) {
dispatchResponse(response, new InsufficientAccessException("Access Token is expired."));
return;
} else if (accessToken.getExpiry().getTime() < new Date().getTime() + AccessTokenDataUtil.MIN_EXPIRY_MILLIS) {
accessToken.setExpiry(new Date(new Date().getTime() + AccessTokenDataUtil.MAX_EXPIRY_MILLIS));
accessToken = dataAccessor.createOrUpdateAccessToken(accessToken);
}
}
threadLocalAccessToken.set(accessToken);
chain.doFilter(request, response);
threadLocalAccessToken.remove();
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class UserDataUtil method createUserDataList.
public static Map<Long, UserData> createUserDataList(List<Long> userIdList, boolean includeAuthorData) {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
List<User> userList = dataAccessor.getUserList(userIdList);
Map<Long, UserData> userDataList = new HashMap<>(userIdList.size());
if (includeAuthorData) {
Map<Long, Author> authors = dataAccessor.getAuthorsByUserIds(userIdList);
for (User user : userList) userDataList.put(user.getId(), createUserData(user, authors.get(user.getId())));
} else {
for (User user : userList) userDataList.put(user.getId(), createUserData(user, null));
}
return userDataList;
}
Aggregations