implemented option to allow guests to see wiki page
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -101,6 +101,7 @@ public class Constants {
|
||||
public static final String FULLTEXT = "fulltext";
|
||||
|
||||
public static final String GET = "GET";
|
||||
public static final String GUEST_ALLOWED = "guest_allowed";
|
||||
|
||||
public static final String HASH = "hash";
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ public class WikiPage implements Mappable {
|
||||
private final Map<Long,Member> members = new HashMap<>();
|
||||
private String content;
|
||||
private Set<String> dirtyFields = new HashSet<>();
|
||||
private boolean guestAllowed = false;
|
||||
|
||||
public WikiPage(long id, String title, int version, String content) {
|
||||
this.id = id;
|
||||
@@ -46,6 +47,15 @@ public class WikiPage implements Mappable {
|
||||
return Set.copyOf(dirtyFields);
|
||||
}
|
||||
|
||||
public boolean guestAllowed() {
|
||||
return guestAllowed;
|
||||
}
|
||||
|
||||
public void guestAllowed(boolean guestAllowed) {
|
||||
this.guestAllowed = guestAllowed;
|
||||
dirtyFields.add(GUEST_ALLOWED);
|
||||
}
|
||||
|
||||
public long id(){
|
||||
return id;
|
||||
}
|
||||
@@ -72,6 +82,10 @@ public class WikiPage implements Mappable {
|
||||
if (!(val instanceof String s)) throw invalidFieldException(CONTENT,"String");
|
||||
content(s);
|
||||
break;
|
||||
case GUEST_ALLOWED:
|
||||
if (!(val instanceof Boolean b)) throw invalidFieldException(GUEST_ALLOWED,"Boolean");
|
||||
guestAllowed(b);
|
||||
break;
|
||||
case MEMBERS:
|
||||
if (!(val instanceof JSONObject membersJson)) throw invalidFieldException(MEMBERS,"Json");
|
||||
for (var uid : membersJson.keySet()){
|
||||
@@ -131,6 +145,7 @@ public class WikiPage implements Mappable {
|
||||
return Map.of(
|
||||
ID,id,
|
||||
CONTENT,Map.of(SOURCE,content,RENDERED,markdown(content)),
|
||||
GUEST_ALLOWED,guestAllowed,
|
||||
MEMBERS,memberMap,
|
||||
TITLE,title,
|
||||
VERSION,version,
|
||||
|
||||
@@ -107,6 +107,12 @@
|
||||
return loadContent(res);
|
||||
}
|
||||
|
||||
async function patchGuestPermissions(ev){
|
||||
let data = {guest_allowed:page.guest_allowed};
|
||||
console.log(data);
|
||||
return patch(data);
|
||||
}
|
||||
|
||||
async function patchTitle(t){
|
||||
var result = await(patch({title:t}));
|
||||
router.navigate(`/wiki/${page.id}/view`);
|
||||
@@ -146,6 +152,10 @@
|
||||
{#if detail}
|
||||
{#if editable}
|
||||
<PermissionEditor members={page.members} {addMember} {dropMember} {getCandidates} {updatePermission} />
|
||||
<label>
|
||||
<input type="checkbox" bind:checked={page.guest_allowed} onchange={patchGuestPermissions} />
|
||||
{t('visible_to_guests')}
|
||||
</label>
|
||||
{:else}
|
||||
<table>
|
||||
<thead>
|
||||
|
||||
@@ -272,6 +272,7 @@
|
||||
|
||||
"version": "Version",
|
||||
"version_of": "Version {version} von {element}",
|
||||
"visible_to_guests": "Für Besucher sichtbar",
|
||||
|
||||
"year": "Jahr",
|
||||
"your_password_reset_token" : "Ihr Token zum Erstellen eines neuen Passworts",
|
||||
|
||||
@@ -278,11 +278,18 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
if (page.isDirty(CONTENT) || page.isDirty(ID) || page.isDirty(TITLE)) insertInto(TABLE_PAGES,ID,VERSION,TITLE,CONTENT)
|
||||
.values(page.id(),page.version(),page.title(),page.content()).execute(db).close();
|
||||
if (page.isDirty(MEMBERS)){
|
||||
Query.delete().from(TABLE_PAGES_USERS).where(PAGE_ID, equal(page.title())).where(USER_ID,Condition.notIn(page.members().keySet().toArray())).execute(db);
|
||||
Query.delete().from(TABLE_PAGES_USERS).where(PAGE_ID, equal(page.id())).where(USER_ID,Condition.notIn(page.members().keySet().toArray())).execute(db);
|
||||
var query = replaceInto(TABLE_PAGES_USERS,PAGE_ID,USER_ID,PERMISSIONS);
|
||||
for (var member : page.members().entrySet()) query.values(page.id(),member.getKey(),wikiPermissionCode(member.getValue().permission()));
|
||||
query.execute(db).close();
|
||||
}
|
||||
if (page.isDirty(GUEST_ALLOWED)){
|
||||
if (page.guestAllowed()) {
|
||||
insertInto(TABLE_PAGES_USERS,PAGE_ID,USER_ID,PERMISSIONS).values(page.id(),0, wikiPermissionCode(READ_ONLY)).execute(db).close();
|
||||
} else {
|
||||
Query.delete().from(TABLE_PAGES_USERS).where(PAGE_ID,equal(page.id())).where(USER_ID,equal(0)).execute(db);
|
||||
}
|
||||
}
|
||||
return page;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException("Failed to write wiki page \"{0}\" to database",page.title(),e);
|
||||
|
||||
@@ -9,6 +9,7 @@ import static de.srsoftware.umbrella.core.Paths.*;
|
||||
import static de.srsoftware.umbrella.core.Util.mapValues;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
import static de.srsoftware.umbrella.core.model.Permission.EDIT;
|
||||
import static de.srsoftware.umbrella.core.model.Permission.READ_ONLY;
|
||||
import static de.srsoftware.umbrella.wiki.Constants.*;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
@@ -148,8 +149,8 @@ public class WikiModule extends BaseHandler implements WikiService {
|
||||
}
|
||||
}
|
||||
var page = loadPage(id,version);
|
||||
var userId = user == null ? 0 : user.id();
|
||||
var permission = page.members().get(userId);
|
||||
if (page.guestAllowed()) return sendContent(ex,page);
|
||||
var permission = page.members().get(user.id());
|
||||
if (permission == null) throw forbidden("You are not allowed to access \"{0}\"!",id);
|
||||
return sendContent(ex, page);
|
||||
}
|
||||
@@ -166,6 +167,10 @@ public class WikiModule extends BaseHandler implements WikiService {
|
||||
for (var entry : members.entrySet()){
|
||||
var userId = entry.getKey();
|
||||
var permission = entry.getValue();
|
||||
if (userId == 0){
|
||||
if (permission == READ_ONLY) page.guestAllowed(true);
|
||||
continue;
|
||||
}
|
||||
var user = users.get(userId);
|
||||
pageMembers.put(userId,new Member(user,permission));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user