use of com.pratilipi.common.exception.InsufficientAccessException in project pratilipi by Pratilipi.
the class AccessTokenFilter method dispatchResponse.
// Ref: GenericApi.dispatchApiResponse
private void dispatchResponse(HttpServletResponse response, Throwable ex) throws IOException {
response.setCharacterEncoding("UTF-8");
PrintWriter writer = response.getWriter();
if (ex instanceof InvalidArgumentException)
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
else if (ex instanceof InsufficientAccessException)
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
else if (ex instanceof UnexpectedServerException)
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
else
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
writer.println(ex.getMessage());
writer.close();
}
use of com.pratilipi.common.exception.InsufficientAccessException 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.common.exception.InsufficientAccessException 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.common.exception.InsufficientAccessException 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.common.exception.InsufficientAccessException in project pratilipi by Pratilipi.
the class UserDataUtil method saveUserData.
public static UserData saveUserData(UserData userData) throws InvalidArgumentException, InsufficientAccessException {
_validateUserDataForSave(userData);
boolean isNew = userData.getId() == null;
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
User user = isNew ? dataAccessor.newUser() : dataAccessor.getUser(userData.getId());
if (isNew && !hasAccessToAddUserData(userData))
throw new InsufficientAccessException();
if (!isNew && !hasAccessToUpdateUserData(user, userData))
throw new InsufficientAccessException();
AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), isNew ? AccessType.USER_ADD : AccessType.USER_UPDATE, user);
if (userData.hasEmail() && !userData.getEmail().equals(user.getEmail())) {
user.setEmail(userData.getEmail());
if (user.getState() == UserState.ACTIVE)
user.setState(UserState.REGISTERED);
user.setVerificationToken(null);
}
if (userData.hasPhone())
user.setPhone(userData.getPhone());
if (userData.hasLanguage())
user.setLanguage(userData.getLanguage());
if (isNew) {
// Assuming only AEEs have USER_ADD access.
user.setState(UserState.REFERRAL);
user.setCampaign(UserCampaign.AEE_TEAM);
user.setReferrer(AccessTokenFilter.getAccessToken().getUserId().toString());
user.setSignUpDate(new Date());
user.setSignUpSource(getUserSignUpSource(false, false));
}
user.setLastUpdated(new Date());
user = dataAccessor.createOrUpdateUser(user, auditLog);
if (isNew) {
String firstName = userData.getFirstName();
String lastName = userData.getLastName();
userData = createUserData(user, null);
userData.setFirstName(firstName);
userData.setLastName(lastName);
} else {
userData = createUserData(user);
}
return userData;
}
Aggregations