Search in sources :

Example 86 with DataAccessor

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);
}
Also used : User(com.pratilipi.data.type.User) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException)

Example 87 with DataAccessor

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;
}
Also used : AccessToken(com.pratilipi.data.type.AccessToken) DataAccessor(com.pratilipi.data.DataAccessor) Author(com.pratilipi.data.type.Author) Pratilipi(com.pratilipi.data.type.Pratilipi) UserPratilipi(com.pratilipi.data.type.UserPratilipi)

Example 88 with DataAccessor

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);
}
Also used : UserPratilipi(com.pratilipi.data.type.UserPratilipi) DataAccessor(com.pratilipi.data.DataAccessor) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 89 with DataAccessor

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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) AccessToken(com.pratilipi.data.type.AccessToken) HttpServletResponse(javax.servlet.http.HttpServletResponse) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) Date(java.util.Date)

Example 90 with DataAccessor

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;
}
Also used : User(com.pratilipi.data.type.User) HashMap(java.util.HashMap) UserData(com.pratilipi.data.client.UserData) DataAccessor(com.pratilipi.data.DataAccessor) Author(com.pratilipi.data.type.Author)

Aggregations

DataAccessor (com.pratilipi.data.DataAccessor)162 Date (java.util.Date)53 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)37 Pratilipi (com.pratilipi.data.type.Pratilipi)34 Author (com.pratilipi.data.type.Author)33 AuditLog (com.pratilipi.data.type.AuditLog)32 UserPratilipi (com.pratilipi.data.type.UserPratilipi)32 ArrayList (java.util.ArrayList)30 Page (com.pratilipi.data.type.Page)28 HashMap (java.util.HashMap)25 GenericResponse (com.pratilipi.api.shared.GenericResponse)24 DocAccessor (com.pratilipi.data.DocAccessor)22 User (com.pratilipi.data.type.User)22 Get (com.pratilipi.api.annotation.Get)20 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)19 PratilipiData (com.pratilipi.data.client.PratilipiData)15 UserData (com.pratilipi.data.client.UserData)14 BlobAccessor (com.pratilipi.data.BlobAccessor)13 Email (com.pratilipi.data.type.Email)13 JsonObject (com.google.gson.JsonObject)12