Skip to content

Commit

Permalink
enhancements:
Browse files Browse the repository at this point in the history
#196 access rights to documents in CM module
#197 CM GUI error messages
#198 basic structure for www themes
  • Loading branch information
g.skorupa@gmail.com authored and gskorupa committed Jan 18, 2019
1 parent 28733b2 commit c87e21c
Show file tree
Hide file tree
Showing 26 changed files with 248 additions and 71 deletions.
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</target>
<!-- set global properties for this build -->
<property name="targetJava10" value="true"/>
<property name="mainVersion" value="1.2.54"/>
<property name="mainVersion" value="1.2.55"/>

<condition property="version" value="${mainVersion}">
<equals arg1="${targetJava10}" arg2="true"/>
Expand Down
4 changes: 2 additions & 2 deletions src/java/BasicService-help.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Usage:
java -jar cricketms-1.2.54.jar [options]
java -cp cricketms-1.2.54.jar org.cricketmsf.Runner [options]
java -jar cricketms-1.2.55.jar [options]
java -cp cricketms-1.2.55.jar org.cricketmsf.Runner [options]

Runs selected service class.

Expand Down
8 changes: 8 additions & 0 deletions src/java/cricket.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,20 @@
"password": ""
}
},
"cmsRuleEngine": {
"name": "cmsRuleEngine",
"interfaceName": "RuleEngineIface",
"classFullName": "org.cricketmsf.microsite.cms.DefaultRuleEngine",
"properties": {
}
},
"cmsAdapter": {
"name": "cmsAdapter",
"interfaceName": "CmsIface",
"classFullName": "org.cricketmsf.microsite.cms.CmsEmbededAdapter",
"properties": {
"helper-name": "cmsDB",
"rule-engine": "cmsRuleEngine",
"root-path": "./www/",
"file-path": "./work/files/",
"file-path-published": "./www/assets/",
Expand Down
4 changes: 2 additions & 2 deletions src/java/cricket.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Thu, 27 Dec 2018 00:52:41 +0100
version=1.2.54
#Sat, 19 Jan 2019 00:11:05 +0100
version=1.2.55
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,6 @@ private StandardResult getServiceConfig() {
* @param database
* @param userDB
* @param authDB
* @param thingsDB
* @param iotDataDB
* @param actuatorCommandsDB
*/
public void initDatabases(
KeyValueDBIface database,
Expand Down
61 changes: 43 additions & 18 deletions src/java/org/cricketmsf/microsite/cms/CmsEmbededAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,24 @@ public class CmsEmbededAdapter extends OutboundAdapter implements Adapter, CmsIf

String helperAdapterName = null;
KeyValueDBIface database = null;
String ruleEngineName = null;
RuleEngineIface ruleEngine = new DefaultRuleEngine();

int status = NOT_INITIALIZED;

private String wwwRoot = null; //www root path in the filesystem
private String fileRoot = null; //cms document files root path in the filesystem
private String publishedFilesRoot = null;
String indexFileName = "index.html";

private void initRuleEngine() {
if(ruleEngineName==null) return;
try {
ruleEngine = (RuleEngineIface) Kernel.getInstance().getAdaptersMap().get(ruleEngineName);
} catch (Exception e) {
}
}

private KeyValueDBIface getDatabase() throws KeyValueDBException {
if (database == null) {
try {
Expand Down Expand Up @@ -130,11 +141,11 @@ public void initDB() throws CmsException {

@Override
public Document getDocument(String uid, String language) throws CmsException {
return getDocument(uid, language, null);
return getDocument(uid, language, null, null);
}

@Override
public Document getDocument(String uid, String language, String status) throws CmsException {
public Document getDocument(String uid, String language, String status, List<String> roles) throws CmsException {
//TODO: getDocument(uid, null, status) should return list of documents
Document doc = null;
//System.out.println("LANGUAGE:[" + language + "]");
Expand Down Expand Up @@ -203,6 +214,7 @@ public Document getDocument(String uid, String language, String status) throws C
} else {
throw new CmsException(CmsException.UNSUPPORTED_STATUS, "unsupported status");
}
doc = ruleEngine.processDocument(doc, roles);
return doc;
}

Expand All @@ -211,10 +223,11 @@ private String resolveTableName(Document doc) {
}

@Override
public void addDocument(Document doc) throws CmsException {
public void addDocument(Document doc, List<String>roles) throws CmsException {
if (doc.getLanguage() == null || !supportedLanguages.contains(doc.getLanguage())) {
throw new CmsException(CmsException.UNSUPPORTED_LANGUAGE);
}
ruleEngine.checkDocument(doc, roles);
try {
if (getDatabase().containsKey(resolveTableName(doc), doc.getUid())) {
throw new CmsException(CmsException.ALREADY_EXISTS, "document already exists");
Expand All @@ -234,7 +247,7 @@ public void addDocument(Document doc) throws CmsException {
}

@Override
public void addDocument(Map parameters, String userID) throws CmsException {
public void addDocument(Map parameters, String userID, List<String>roles) throws CmsException {
Document doc = new Document();
try {
//System.out.println("SETTING UID:" + (String) parameters.get("uid"));
Expand Down Expand Up @@ -280,6 +293,7 @@ public void addDocument(Map parameters, String userID) throws CmsException {
if (getDatabase().containsKey(resolveTableName(doc), doc.getUid())) {
throw new CmsException(CmsException.ALREADY_EXISTS, "document already exists");
}
ruleEngine.checkDocument(doc, roles);
getDatabase().put("paths", doc.getPath(), doc.getPath());
getDatabase().put("tags", doc.getTags(), doc.getTags());
getDatabase().put(resolveTableName(doc), doc.getUid(), doc);
Expand All @@ -292,20 +306,21 @@ public void addDocument(Map parameters, String userID) throws CmsException {
}

@Override
public void updateDocument(Document doc) throws CmsException {
public void updateDocument(Document doc, List<String>roles) throws CmsException {

//TODO: when status changes from wip to published then doc should be removed from wip table
if (doc.getLanguage() == null || !supportedLanguages.contains(doc.getLanguage())) {
throw new CmsException(CmsException.UNSUPPORTED_LANGUAGE);
}
ruleEngine.checkDocument(doc, roles);
Document original = null;
original = getDocument(doc.getUid(), null, null);
doc.setCreated(original.getCreated());
doc.setModified(Instant.now().toString());
original = getDocument(doc.getUid(), null, null, null);
if (original == null) {
Kernel.getInstance().dispatchEvent(Event.logWarning(this.getClass().getSimpleName(), "original document uid=" + doc.getUid() + " not found"));
throw new CmsException(CmsException.NOT_FOUND, "original document not found");
}
doc.setCreated(original.getCreated());
doc.setModified(Instant.now().toString());
try {
doc.setMimeType(doc.getMimeType().trim());
if (!doc.getLanguage().equals(original.getLanguage()) || !doc.getStatus().equals(original.getStatus())) {
Expand All @@ -330,13 +345,14 @@ public void updateDocument(Document doc) throws CmsException {
}

@Override
public void updateDocument(String uid, String language, Map parameters) throws CmsException {
public void updateDocument(String uid, String language, Map parameters, List<String>roles) throws CmsException {
boolean statusChanged = false;
Document doc = getDocument(uid, (String) parameters.get("language"));
Document doc = getDocument(uid, language);
if (doc == null) {
Kernel.getInstance().dispatchEvent(Event.logWarning(this.getClass().getSimpleName(), "original document uid=" + uid + ", language=" + language + " not found"));
throw new CmsException(CmsException.NOT_FOUND, "original document not found");
}
ruleEngine.checkDocument(doc, roles);
try {
String actualLanguage = doc.getLanguage(); //its not possible to change document's language
String actualStatus = doc.getStatus();
Expand Down Expand Up @@ -378,6 +394,7 @@ public void updateDocument(String uid, String language, Map parameters) throws C
}
doc.setStatus(newStatus);
}

//if (doc.getType().equals(Document.FILE)) {
if (Document.FILE.equals(doc.getType())) {
String fileLocation = (String) parameters.getOrDefault("file", "");
Expand Down Expand Up @@ -429,9 +446,10 @@ public void updateDocument(String uid, String language, Map parameters) throws C
}

@Override
public void removeDocument(String uid) throws CmsException {
public void removeDocument(String uid, List<String>roles) throws CmsException {
boolean removed = false;
Document doc = getDocument(uid, null, null);
Document doc = getDocument(uid, null, null, null);
ruleEngine.checkDocument(doc, roles);
if (doc != null) {
if (Document.FILE.equals(doc.getType())) {
//TODO: delete file (
Expand All @@ -456,7 +474,8 @@ public void removeDocument(String uid) throws CmsException {
//TODO: remove path if thera are no more documents with this path
}
}
/*

/*
@Override
public List<Document> findByPath(String path, String language, String status) throws CmsException {
Document pattern = new Document();
Expand All @@ -478,9 +497,9 @@ public List<Document> findByPath(String path, String language, String status) th
}
return list;
}
*/
*/
@Override
public List<Document> findByPathAndTag(String path, String tag, String language, String status) throws CmsException {
public List<Document> findByPathAndTag(String path, String tag, String language, String status, List<String> roles) throws CmsException {
Document pattern = new Document();
if (!supportedLanguages.contains(language)) {
throw new CmsException(CmsException.UNSUPPORTED_LANGUAGE, language + " language is not supported");
Expand All @@ -499,6 +518,8 @@ public List<Document> findByPathAndTag(String path, String tag, String language,
} catch (KeyValueDBException ex) {
throw new CmsException(CmsException.HELPER_EXCEPTION, ex.getMessage());
}
//TODO: check access rules
list = (ArrayList)ruleEngine.processDocumentsList(list, roles);
return list;
}

Expand Down Expand Up @@ -528,9 +549,13 @@ public void destroy() {

@Override
public void loadProperties(HashMap<String, String> properties, String adapterName) {
super.loadProperties(properties, adapterName);
// no specific config is required
helperAdapterName = properties.get("helper-name");
Kernel.getInstance().getLogger().print("\thelper-name: " + helperAdapterName);
ruleEngineName = properties.get("rule-engine");
Kernel.getInstance().getLogger().print("\trule-engine-name: " + ruleEngineName);
initRuleEngine();
setWwwRoot(properties.get("root-path"));
Kernel.getInstance().getLogger().print("\troot-path: " + getWwwRoot());
setFileRoot(properties.get("file-path"));
Expand Down Expand Up @@ -561,7 +586,7 @@ public List getPaths() throws CmsException {
}
return list;
}

@Override
public List getTags() throws CmsException {
ArrayList<String> list = new ArrayList<>();
Expand Down Expand Up @@ -611,7 +636,7 @@ public void updateCache(RequestObject request, KeyValueDBIface cache, String tab
String filePath = getFilePath(request);
try {
fo = (FileObject) cache.get(tableName, filePath);
fo.content=fileContent.getBytes();
fo.content = fileContent.getBytes();
cache.put(tableName, filePath, cache);
} catch (KeyValueDBException e) {
e.printStackTrace();
Expand Down Expand Up @@ -680,7 +705,7 @@ public Result getFile(RequestObject request, KeyValueDBIface cache, String table
Document doc;

try {
doc = getDocument("/" + filePath, language, "published");
doc = getDocument("/" + filePath, language, "published", null);
if (doc != null && Document.FILE.equals(doc.getType())) {
File file = new File(doc.getContent());
content = readFile(file);
Expand Down
14 changes: 7 additions & 7 deletions src/java/org/cricketmsf/microsite/cms/CmsIface.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ public interface CmsIface {
public List getPaths() throws CmsException;
public List getTags() throws CmsException;
public Document getDocument(String uid, String language) throws CmsException;
public Document getDocument(String uid, String language, String status) throws CmsException;
public void addDocument(Document doc) throws CmsException;
public void addDocument(Map parameters, String userID) throws CmsException;
public void updateDocument(Document doc) throws CmsException;
public void updateDocument(String uid, String language, Map parameters) throws CmsException;
public void removeDocument(String uid) throws CmsException;
public Document getDocument(String uid, String language, String status, List<String>roles) throws CmsException;
public void addDocument(Document doc, List<String>roles) throws CmsException;
public void addDocument(Map parameters, String userID, List<String>roles) throws CmsException;
public void updateDocument(Document doc, List<String>roles) throws CmsException;
public void updateDocument(String uid, String language, Map parameters, List<String>roles) throws CmsException;
public void removeDocument(String uid, List<String>roles) throws CmsException;
//public List<Document> findByPath(String path, String language, String status) throws CmsException;
public List<Document> findByPathAndTag(String path, String tag, String language, String status) throws CmsException;
public List<Document> findByPathAndTag(String path, String tag, String language, String status, List<String>roles) throws CmsException;
public List<Comment> getComments(String uid) throws CmsException;
public void addComment(String documentUid, Comment comment) throws CmsException;
public void acceptComment(String documentUid, String commentUid) throws CmsException;
Expand Down
75 changes: 75 additions & 0 deletions src/java/org/cricketmsf/microsite/cms/DefaultRuleEngine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2017 Grzegorz Skorupa <g.skorupa at gmail.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.cricketmsf.microsite.cms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.cricketmsf.Adapter;
import org.cricketmsf.Kernel;
import org.cricketmsf.out.OutboundAdapter;

/**
*
* @author Grzegorz Skorupa <g.skorupa at gmail.com>
*/
public class DefaultRuleEngine extends OutboundAdapter implements Adapter, RuleEngineIface {

public DefaultRuleEngine() {
}

@Override
public void loadProperties(HashMap<String, String> properties, String adapterName) {
super.loadProperties(properties, adapterName);
Kernel.getInstance().getLogger().print("\tadapter-class: " + this.getClass().getName());
}

@Override
public Document processDocument(Document doc, List<String> roles) {
if (doc == null) {
return null;
}
if (roles == null) {
return doc.setRights(Document.READONLY);
}
if (roles.contains("redactor") || roles.contains("redactor." + doc.getLanguage())) {
return doc.setRights(Document.READWRITE);
} else {
return doc.setRights(Document.READONLY);
}
}

@Override
public ArrayList<Document> processDocumentsList(List<Document> documents, List<String> roles) {
ArrayList<Document> result = new ArrayList<>();
for (Document document : documents) {
if (roles.contains("redactor") || roles.contains("redactor." + document.getLanguage())) {
result.add(document.setRights(Document.READWRITE));
}else{
result.add(document.setRights(Document.READONLY));
}
}
return result;
}

@Override
public void checkDocument(Document doc, List<String> roles) throws CmsException {
if (!(roles.contains("redactor") || roles.contains("redactor." + doc.getLanguage()))) {
throw new CmsException(CmsException.HELPER_EXCEPTION, "not authorized");
}
}

}
Loading

0 comments on commit c87e21c

Please sign in to comment.