1
0
mirror of https://github.com/sismics/docs.git synced 2025-12-14 02:06:25 +00:00

#13: ACL system

This commit is contained in:
jendib
2015-05-09 14:44:19 +02:00
parent 6ff639baac
commit fc1bb22d8d
30 changed files with 1197 additions and 231 deletions

View File

@@ -0,0 +1,23 @@
package com.sismics.docs.core.constant;
/**
* ACL target types.
*
* @author bgamard
*/
public enum AclTargetType {
/**
* An user.
*/
USER,
/**
* A group.
*/
GROUP,
/**
* A share.
*/
SHARE
}

View File

@@ -0,0 +1,18 @@
package com.sismics.docs.core.constant;
/**
* Permissions.
*
* @author bgamard
*/
public enum PermType {
/**
* Read document.
*/
READ,
/**
* Write document.
*/
WRITE
}

View File

@@ -0,0 +1,133 @@
package com.sismics.docs.core.dao.jpa;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import com.sismics.docs.core.constant.AclTargetType;
import com.sismics.docs.core.constant.PermType;
import com.sismics.docs.core.dao.jpa.dto.AclDto;
import com.sismics.docs.core.model.jpa.Acl;
import com.sismics.util.context.ThreadLocalContext;
/**
* ACL DAO.
*
* @author bgamard
*/
public class AclDao {
/**
* Creates a new ACL.
*
* @param acl ACL
* @return New ID
* @throws Exception
*/
public String create(Acl acl) {
// Create the UUID
acl.setId(UUID.randomUUID().toString());
// Create the ACL
EntityManager em = ThreadLocalContext.get().getEntityManager();
em.persist(acl);
return acl.getId();
}
/**
* Search ACLs by target ID.
*
* @param targetId Target ID
* @return ACL list
*/
@SuppressWarnings("unchecked")
public List<Acl> getByTargetId(String targetId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select a from Acl a where a.targetId = :targetId and a.deleteDate is null");
q.setParameter("targetId", targetId);
return q.getResultList();
}
/**
* Search ACLs by source ID.
*
* @param sourceId Source ID
* @return ACL DTO list
*/
@SuppressWarnings("unchecked")
public List<AclDto> getBySourceId(String sourceId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
StringBuilder sb = new StringBuilder("select a.ACL_ID_C, a.ACL_PERM_C, a.ACL_TARGETID_C, u.USE_USERNAME_C, s.SHA_NAME_C");
sb.append(" from T_ACL a ");
sb.append(" left join T_USER u on u.USE_ID_C = a.ACL_TARGETID_C ");
sb.append(" left join T_SHARE s on s.SHA_ID_C = a.ACL_TARGETID_C ");
sb.append(" where a.ACL_DELETEDATE_D is null and a.ACL_SOURCEID_C = :sourceId ");
// Perform the query
Query q = em.createNativeQuery(sb.toString());
q.setParameter("sourceId", sourceId);
List<Object[]> l = q.getResultList();
// Assemble results
List<AclDto> aclDtoList = new ArrayList<AclDto>();
for (Object[] o : l) {
int i = 0;
AclDto aclDto = new AclDto();
aclDto.setId((String) o[i++]);
aclDto.setPerm(PermType.valueOf((String) o[i++]));
aclDto.setTargetId((String) o[i++]);
String userName = (String) o[i++];
String shareName = (String) o[i++];
aclDto.setTargetName(userName == null ? shareName : userName);
aclDto.setTargetType(userName == null ?
AclTargetType.SHARE.name() : AclTargetType.USER.name());
aclDtoList.add(aclDto);
}
return aclDtoList;
}
/**
* Check if a source is accessible to a target.
*
* @param sourceId ACL source entity ID
* @parm perm Necessary permission
* @param targetId ACL target entity ID
* @return True if the document is accessible
*/
public boolean checkPermission(String sourceId, PermType perm, String targetId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select a from Acl a where a.sourceId = :sourceId and a.perm = :perm and a.targetId = :targetId and a.deleteDate is null");
q.setParameter("sourceId", sourceId);
q.setParameter("perm", perm);
q.setParameter("targetId", targetId);
// We have a matching permission
if (q.getResultList().size() > 0) {
return true;
}
return false;
}
/**
* Delete an ACL.
*
* @param sourceId Source ID
* @param perm Permission
* @param targetId Target ID
*/
public void delete(String sourceId, PermType perm, String targetId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("update Acl a set a.deleteDate = :dateNow where a.sourceId = :sourceId and a.perm = :perm and a.targetId = :targetId");
q.setParameter("sourceId", sourceId);
q.setParameter("perm", perm);
q.setParameter("targetId", targetId);
q.setParameter("dateNow", new Date());
q.executeUpdate();
}
}

View File

@@ -15,6 +15,7 @@ import javax.persistence.Query;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.sismics.docs.core.constant.PermType;
import com.sismics.docs.core.dao.jpa.criteria.DocumentCriteria;
import com.sismics.docs.core.dao.jpa.dto.DocumentDto;
import com.sismics.docs.core.dao.lucene.LuceneDao;
@@ -78,13 +79,17 @@ public class DocumentDao {
* Returns an active document.
*
* @param id Document ID
* @param perm Permission needed
* @param userId User ID
* @return Document
*/
public Document getDocument(String id, String userId) {
public Document getDocument(String id, PermType perm, String userId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select d from Document d where d.id = :id and d.userId = :userId and d.deleteDate is null");
Query q = em.createNativeQuery("select d.* from T_DOCUMENT d "
+ " join T_ACL a on a.ACL_SOURCEID_C = d.DOC_ID_C and a.ACL_TARGETID_C = :userId and a.ACL_PERM_C = :perm and a.ACL_DELETEDATE_D is null "
+ " where d.DOC_ID_C = :id and d.DOC_DELETEDATE_D is null", Document.class);
q.setParameter("id", id);
q.setParameter("perm", perm.name());
q.setParameter("userId", userId);
return (Document) q.getSingleResult();
}
@@ -112,7 +117,13 @@ public class DocumentDao {
q.setParameter("dateNow", dateNow);
q.executeUpdate();
q = em.createQuery("update Share s set s.deleteDate = :dateNow where s.documentId = :documentId and s.deleteDate is null");
// TODO Delete share from deleted ACLs
// q = em.createQuery("update Share s set s.deleteDate = :dateNow where s.documentId = :documentId and s.deleteDate is null");
// q.setParameter("documentId", id);
// q.setParameter("dateNow", dateNow);
// q.executeUpdate();
q = em.createQuery("update Acl a set a.deleteDate = :dateNow where a.sourceId = :documentId");
q.setParameter("documentId", id);
q.setParameter("dateNow", dateNow);
q.executeUpdate();
@@ -145,19 +156,20 @@ public class DocumentDao {
Map<String, Object> parameterMap = new HashMap<String, Object>();
List<String> criteriaList = new ArrayList<String>();
StringBuilder sb = new StringBuilder("select distinct d.DOC_ID_C c0, d.DOC_TITLE_C c1, d.DOC_DESCRIPTION_C c2, d.DOC_CREATEDATE_D c3, d.DOC_LANGUAGE_C c4, s.SHA_ID_C is not null c5, ");
StringBuilder sb = new StringBuilder("select distinct d.DOC_ID_C c0, d.DOC_TITLE_C c1, d.DOC_DESCRIPTION_C c2, d.DOC_CREATEDATE_D c3, d.DOC_LANGUAGE_C c4, ");
sb.append(" (select count(s.SHA_ID_C) from T_SHARE s, T_ACL ac where ac.ACL_SOURCEID_C = d.DOC_ID_C and ac.ACL_TARGETID_C = s.SHA_ID_C and ac.ACL_DELETEDATE_D is null and s.SHA_DELETEDATE_D is null) c5, ");
sb.append(" (select count(f.FIL_ID_C) from T_FILE f where f.FIL_DELETEDATE_D is null and f.FIL_IDDOC_C = d.DOC_ID_C) c6 ");
sb.append(" from T_DOCUMENT d ");
sb.append(" left join T_SHARE s on s.SHA_IDDOCUMENT_C = d.DOC_ID_C and s.SHA_DELETEDATE_D is null ");
// Adds search criteria
if (criteria.getUserId() != null) {
criteriaList.add("d.DOC_IDUSER_C = :userId");
// Read permission is enough for searching
sb.append(" join T_ACL a on a.ACL_SOURCEID_C = d.DOC_ID_C and a.ACL_TARGETID_C = :userId and a.ACL_PERM_C = 'READ' and a.ACL_DELETEDATE_D is null ");
parameterMap.put("userId", criteria.getUserId());
}
if (!Strings.isNullOrEmpty(criteria.getSearch()) || !Strings.isNullOrEmpty(criteria.getFullSearch())) {
LuceneDao luceneDao = new LuceneDao();
Set<String> documentIdList = luceneDao.search(criteria.getUserId(), criteria.getSearch(), criteria.getFullSearch());
Set<String> documentIdList = luceneDao.search(criteria.getSearch(), criteria.getFullSearch());
if (documentIdList.size() == 0) {
// If the search doesn't find any document, the request should return nothing
documentIdList.add(UUID.randomUUID().toString());
@@ -183,7 +195,7 @@ public class DocumentDao {
}
}
if (criteria.getShared() != null && criteria.getShared()) {
criteriaList.add("s.SHA_ID_C is not null");
criteriaList.add("(select count(s.SHA_ID_C) from T_SHARE s, T_ACL ac where ac.ACL_SOURCEID_C = d.DOC_ID_C and ac.ACL_TARGETID_C = s.SHA_ID_C and ac.ACL_DELETEDATE_D is null and s.SHA_DELETEDATE_D is null) > 0");
}
if (criteria.getLanguage() != null) {
criteriaList.add("d.DOC_LANGUAGE_C = :language");
@@ -211,7 +223,7 @@ public class DocumentDao {
documentDto.setDescription((String) o[i++]);
documentDto.setCreateTimestamp(((Timestamp) o[i++]).getTime());
documentDto.setLanguage((String) o[i++]);
documentDto.setShared((Boolean) o[i++]);
documentDto.setShared(((Number) o[i++]).intValue() > 0);
documentDto.setFileCount(((Number) o[i++]).intValue());
documentDtoList.add(documentDto);
}

View File

@@ -1,14 +1,11 @@
package com.sismics.docs.core.dao.jpa;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import com.sismics.docs.core.model.jpa.Document;
import com.sismics.docs.core.model.jpa.Share;
import com.sismics.util.context.ThreadLocalContext;
@@ -37,23 +34,6 @@ public class ShareDao {
return share.getId();
}
/**
* Returns an active share.
*
* @param id Share ID
* @return Document
*/
public Share getShare(String id) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select s from Share s where s.id = :id and s.deleteDate is null");
q.setParameter("id", id);
try {
return (Share) q.getSingleResult();
} catch (NoResultException e) {
return null;
}
}
/**
* Deletes a share.
*
@@ -70,44 +50,11 @@ public class ShareDao {
// Delete the share
Date dateNow = new Date();
shareDb.setDeleteDate(dateNow);
}
/**
* Get shares by document ID.
*
* @param documentId Document ID
* @return List of shares
*/
@SuppressWarnings("unchecked")
public List<Share> getByDocumentId(String documentId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select s from Share s where s.documentId = :documentId and s.deleteDate is null");
q.setParameter("documentId", documentId);
return q.getResultList();
}
/**
* Check if a document is visible.
*
* @param document Document to check for visibility
* @param userId Optional user trying to access the document
* @param shareId Optional share to access the document
* @return True if the document is visible
*/
public boolean checkVisibility(Document document, String userId, String shareId) {
// The user owns the document
if (document.getUserId().equals(userId)) {
return true;
}
// The share is linked to the document
if (shareId != null) {
Share share = getShare(shareId);
if (share != null && share.getDocumentId().equals(document.getId())) {
return true;
}
}
return false;
// Delete the linked ACL
q = em.createQuery("update Acl a set a.deleteDate = :dateNow where a.targetId = :targetId");
q.setParameter("targetId", id);
q.setParameter("dateNow", dateNow);
q.executeUpdate();
}
}

View File

@@ -1,7 +1,22 @@
package com.sismics.docs.core.dao.jpa;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import org.mindrot.jbcrypt.BCrypt;
import com.google.common.base.Joiner;
import com.sismics.docs.core.constant.Constants;
import com.sismics.docs.core.dao.jpa.criteria.UserCriteria;
import com.sismics.docs.core.dao.jpa.dto.UserDto;
import com.sismics.docs.core.model.jpa.User;
import com.sismics.docs.core.util.jpa.PaginatedList;
@@ -9,13 +24,6 @@ import com.sismics.docs.core.util.jpa.PaginatedLists;
import com.sismics.docs.core.util.jpa.QueryParam;
import com.sismics.docs.core.util.jpa.SortCriteria;
import com.sismics.util.context.ThreadLocalContext;
import org.mindrot.jbcrypt.BCrypt;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import java.sql.Timestamp;
import java.util.*;
/**
* User DAO.
@@ -204,13 +212,19 @@ public class UserDao {
* @param paginatedList List of users (updated by side effects)
* @param sortCriteria Sort criteria
*/
public void findAll(PaginatedList<UserDto> paginatedList, SortCriteria sortCriteria) {
public void findByCriteria(PaginatedList<UserDto> paginatedList, UserCriteria criteria, SortCriteria sortCriteria) {
Map<String, Object> parameterMap = new HashMap<String, Object>();
List<String> criteriaList = new ArrayList<String>();
StringBuilder sb = new StringBuilder("select u.USE_ID_C as c0, u.USE_USERNAME_C as c1, u.USE_EMAIL_C as c2, u.USE_CREATEDATE_D as c3, u.USE_IDLOCALE_C as c4");
sb.append(" from T_USER u ");
// Add search criterias
List<String> criteriaList = new ArrayList<String>();
if (criteria.getSearch() != null) {
criteriaList.add("lower(u.USE_USERNAME_C) like lower(:search)");
parameterMap.put("search", "%" + criteria.getSearch() + "%");
}
criteriaList.add("u.USE_DELETEDATE_D is null");
if (!criteriaList.isEmpty()) {

View File

@@ -0,0 +1,34 @@
package com.sismics.docs.core.dao.jpa.criteria;
/**
* User criteria.
*
* @author bgamard
*/
public class UserCriteria {
/**
* Search query.
*/
private String search;
/**
* Getter of search.
*
* @return the search
*/
public String getSearch() {
return search;
}
/**
* Setter of search.
*
* @param search search
*/
public UserCriteria setSearch(String search) {
this.search = search;
return this;
}
}

View File

@@ -0,0 +1,91 @@
package com.sismics.docs.core.dao.jpa.dto;
import javax.persistence.Id;
import com.sismics.docs.core.constant.PermType;
/**
* Acl DTO.
*
* @author bgamard
*/
public class AclDto {
/**
* Acl ID.
*/
@Id
private String id;
/**
* Target name.
*/
private String targetName;
/**
* Permission.
*/
private PermType perm;
/**
* Source ID.
*/
private String sourceId;
/**
* Target ID.
*/
private String targetId;
/**
* Target type.
*/
private String targetType;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTargetName() {
return targetName;
}
public void setTargetName(String targetName) {
this.targetName = targetName;
}
public PermType getPerm() {
return perm;
}
public void setPerm(PermType perm) {
this.perm = perm;
}
public String getSourceId() {
return sourceId;
}
public void setSourceId(String sourceId) {
this.sourceId = sourceId;
}
public String getTargetId() {
return targetId;
}
public void setTargetId(String targetId) {
this.targetId = targetId;
}
public String getTargetType() {
return targetType;
}
public void setTargetType(String targetType) {
this.targetType = targetType;
}
}

View File

@@ -1,6 +1,5 @@
package com.sismics.docs.core.dao.lucene;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -13,7 +12,6 @@ import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queries.TermsFilter;
import org.apache.lucene.queryparser.flexible.standard.QueryParserUtil;
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser;
import org.apache.lucene.search.BooleanClause.Occur;
@@ -143,13 +141,12 @@ public class LuceneDao {
/**
* Search files.
*
* @param userId User ID to filter on
* @param searchQuery Search query on title and description
* @param fullSearchQuery Search query on all fields
* @return List of document IDs
* @throws Exception
*/
public Set<String> search(String userId, String searchQuery, String fullSearchQuery) throws Exception {
public Set<String> search(String searchQuery, String fullSearchQuery) throws Exception {
// Escape query and add quotes so QueryParser generate a PhraseQuery
searchQuery = "\"" + QueryParserUtil.escape(searchQuery + " " + fullSearchQuery) + "\"";
fullSearchQuery = "\"" + QueryParserUtil.escape(fullSearchQuery) + "\"";
@@ -164,13 +161,6 @@ public class LuceneDao {
query.add(qpHelper.parse(searchQuery, "description"), Occur.SHOULD);
query.add(qpHelper.parse(fullSearchQuery, "content"), Occur.SHOULD);
// Filter on provided user ID
List<Term> terms = new ArrayList<Term>();
if (userId != null) {
terms.add(new Term("user_id", userId));
}
TermsFilter userFilter = new TermsFilter(terms);
// Search
DirectoryReader directoryReader = AppContext.getInstance().getIndexingService().getDirectoryReader();
Set<String> documentIdList = new HashSet<String>();
@@ -179,7 +169,7 @@ public class LuceneDao {
return documentIdList;
}
IndexSearcher searcher = new IndexSearcher(directoryReader);
TopDocs topDocs = searcher.search(query, userFilter, Integer.MAX_VALUE);
TopDocs topDocs = searcher.search(query, Integer.MAX_VALUE);
ScoreDoc[] docs = topDocs.scoreDocs;
// Extract document IDs
@@ -207,7 +197,6 @@ public class LuceneDao {
private org.apache.lucene.document.Document getDocumentFromDocument(Document document) {
org.apache.lucene.document.Document luceneDocument = new org.apache.lucene.document.Document();
luceneDocument.add(new StringField("id", document.getId(), Field.Store.YES));
luceneDocument.add(new StringField("user_id", document.getUserId(), Field.Store.YES));
luceneDocument.add(new StringField("type", "document", Field.Store.YES));
if (document.getTitle() != null) {
luceneDocument.add(new TextField("title", document.getTitle(), Field.Store.NO));
@@ -229,7 +218,6 @@ public class LuceneDao {
private org.apache.lucene.document.Document getDocumentFromFile(File file, Document document) {
org.apache.lucene.document.Document luceneDocument = new org.apache.lucene.document.Document();
luceneDocument.add(new StringField("id", file.getId(), Field.Store.YES));
luceneDocument.add(new StringField("user_id", document.getUserId(), Field.Store.YES));
luceneDocument.add(new StringField("type", "file", Field.Store.YES));
luceneDocument.add(new StringField("document_id", file.getDocumentId(), Field.Store.YES));
if (file.getContent() != null) {

View File

@@ -0,0 +1,104 @@
package com.sismics.docs.core.model.jpa;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.Table;
import com.google.common.base.Objects;
import com.sismics.docs.core.constant.PermType;
/**
* ACL entity.
*
* @author bgamard
*/
@Entity
@Table(name = "T_ACL")
public class Acl {
/**
* ACL ID.
*/
@Id
@Column(name = "ACL_ID_C", length = 36)
private String id;
/**
* ACL permission.
*/
@Column(name = "ACL_PERM_C", length = 30)
@Enumerated(EnumType.STRING)
private PermType perm;
/**
* ACL source ID.
*/
@Column(name = "ACL_SOURCEID_C", length = 36)
private String sourceId;
/**
* ACL target ID.
*/
@Column(name = "ACL_TARGETID_C", length = 36)
private String targetId;
/**
* Deletion date.
*/
@Column(name = "ACL_DELETEDATE_D")
private Date deleteDate;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public PermType getPerm() {
return perm;
}
public void setPerm(PermType perm) {
this.perm = perm;
}
public String getSourceId() {
return sourceId;
}
public void setSourceId(String sourceId) {
this.sourceId = sourceId;
}
public String getTargetId() {
return targetId;
}
public void setTargetId(String targetId) {
this.targetId = targetId;
}
public Date getDeleteDate() {
return deleteDate;
}
public void setDeleteDate(Date deleteDate) {
this.deleteDate = deleteDate;
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("id", id)
.add("perm", perm)
.add("sourceId", sourceId)
.add("targetId", targetId)
.toString();
}
}

View File

@@ -9,7 +9,8 @@ import javax.persistence.Table;
import java.util.Date;
/**
* File share.
* ACL target used to share a document.
* Can only be used on a single ACL
*
* @author bgamard
*/
@@ -26,12 +27,6 @@ public class Share {
@Column(name = "SHA_NAME_C", length = 36)
private String name;
/**
* Document ID.
*/
@Column(name = "SHA_IDDOCUMENT_C", nullable = false, length = 36)
private String documentId;
/**
* Creation date.
*/
@@ -80,24 +75,6 @@ public class Share {
this.name = name;
}
/**
* Getter of documentId.
*
* @return the documentId
*/
public String getDocumentId() {
return documentId;
}
/**
* Setter of documentId.
*
* @param documentId documentId
*/
public void setDocumentId(String documentId) {
this.documentId = documentId;
}
/**
* Getter of createDate.
*
@@ -138,7 +115,6 @@ public class Share {
public String toString() {
return Objects.toStringHelper(this)
.add("id", id)
.add("tagId", documentId)
.toString();
}
}

View File

@@ -16,5 +16,6 @@
<class>com.sismics.docs.core.model.jpa.Tag</class>
<class>com.sismics.docs.core.model.jpa.DocumentTag</class>
<class>com.sismics.docs.core.model.jpa.Share</class>
<class>com.sismics.docs.core.model.jpa.Acl</class>
</persistence-unit>
</persistence>

View File

@@ -1 +1 @@
db.version=7
db.version=8

View File

@@ -0,0 +1,4 @@
create cached table T_ACL ( ACL_ID_C varchar(36) not null, ACL_PERM_C varchar(30) not null, ACL_SOURCEID_C varchar(36) not null, ACL_TARGETID_C varchar(36) not null, ACL_DELETEDATE_D datetime, primary key (ACL_ID_C) );
drop table T_SHARE;
create cached table T_SHARE ( SHA_ID_C varchar(36) not null, SHA_NAME_C varchar(36), SHA_CREATEDATE_D datetime, SHA_DELETEDATE_D datetime, primary key (SHA_ID_C) );
update T_CONFIG set CFG_VALUE_C='8' where CFG_ID_C='DB_VERSION';