@Override public Attachment shareFile(Long attachmentId, long who, Long with) { if (log.isTraceEnabled()) { log.trace(">> shareFile(): attachmentId=" + attachmentId + ", who=" + who + ", with=" + with); } Attachment att = em.find(Attachment.class, attachmentId); if (att == null) { if (log.isTraceEnabled()) { log.trace("<< shareFile(): null - no such attachment"); } return null; } boolean canShare = false; try { // canShare |= um.isAdmin(who); User actor = em.find(User.class, who); if (actor != null && actor.getUserGroup() == 1) { canShare = true; } } catch (Exception ex) { } if (!canShare) { for (User u : att.getUser()) { if (u.getId() == who) { canShare = true; break; } } } if (!canShare) { if (log.isTraceEnabled()) { log.trace("<< shareFile(): null - operation is not permitted"); } return null; } User w = em.find(User.class, with); if (w == null) { if (log.isTraceEnabled()) { log.trace("<< shareFile(): cannot share with nobody, and owners list was not modified"); } return att; } att.getUser().add(w); em.persist(att); return att; }
@Override public Attachment renameAttachment(Long userId, long attachmentId, String name) { if (log.isDebugEnabled()) { log.debug(">> renameAttachment() : userId =" + userId + ", attachmentId=" + attachmentId); } if (userId == null) { if (log.isTraceEnabled()) { log.trace("<< renameAttachment() : null // operation is not permitted"); } return null; } User u = em.find(User.class, userId); if (u == null) { if (log.isTraceEnabled()) { log.trace("<< renameAttachment() : null // no such user! operation is not permitted"); } return null; } Attachment att = em.find(Attachment.class, attachmentId); if (att == null) { if (log.isTraceEnabled()) { log.trace("<< renameAttachment() : null // invalid attachmentId=" + attachmentId); } return null; } if (!isOwner(u, att) && u.getUserGroup() != User.ADMIN) { if (log.isTraceEnabled()) { log.trace("<< renameAttachment() : null // operation is not permitted"); } return null; } att.setName(name); if (log.isDebugEnabled()) { log.debug(">> renameAttachment() : userId =" + userId + ", attachmentId=" + attachmentId); } return em.merge(att); }
/** Checks if user has the right to download specified attachment */ @javax.ejb.TransactionAttribute(javax.ejb.TransactionAttributeType.SUPPORTS) private boolean checkDownloadRights(User u, Attachment a) { if (log.isTraceEnabled()) { log.trace(">> checkDownloadRights(): user="******"SELECT u FROM User u WHERE u.avatarId = :avatarId", User.class); q.setParameter("avatarId", a.getId()); List<User> lst = q.getResultList(); if (lst != null && !lst.isEmpty()) { if (log.isTraceEnabled()) { log.trace("<< checkDownloadRights(): true // this is an avatar"); } return true; } if (a == null) { if (log.isTraceEnabled()) { log.trace("<< checkDownloadRights(): false // attachment = null"); } return false; } // do check if it is a problem statement q = em.createQuery("SELECT o FROM Order o WHERE o.conditionId = :conditionId", Order.class); q.setParameter("conditionId", a.getId()); lst = q.getResultList(); if (lst != null && !lst.isEmpty()) { if (log.isTraceEnabled()) { log.trace("<< checkDownloadRights(): true // this is a problem statement"); } return true; } // do check if it is a solution to closed order q = em.createQuery( "SELECT o FROM Order o WHERE o.solutionId = :solutionId and o.status in :statuses", Order.class); q.setParameter("solutionId", a.getId()); q.setParameter( "statuses", new ArrayList() { { add(Order.CLOSED_OFFLINE_ORDER_STATUS); add(Order.FULL_PAYED_OFFLINE_ORDER_STATUS); add(Order.EXPIRED_OFFLINE_ORDER_STATUS); add(Order.EXPIRED_ONLINE_ORDER_STATUS); } }); lst = q.getResultList(); if (lst != null && !lst.isEmpty()) { if (log.isTraceEnabled()) { log.trace("<< checkDownloadRights(): true // this is a problem statement"); } return true; } if (u == null) { // guest tries to download file (not condition or avatar) if (log.isTraceEnabled()) { log.trace("<< checkDownloadRights(): false // user = null"); } return false; } // Admin ? if (u.getUserGroup() == 1) { return true; } try { // check whether the requestor is owner of the file if (log.isTraceEnabled()) { log.trace("checkDownloadRights(): file owners >> " + a.getUser().size()); } if (isOwner(u, a)) return true; } catch (Exception ex) { if (log.isTraceEnabled()) { log.trace("checkDownloadRights(): false // exception while processing owners list", ex); } return false; } // we might as well return false... if (log.isTraceEnabled()) { log.trace("<< checkDownloadRigths(): false"); } return false; }