#4 - add support for sharing pages publicly, without login

This commit is contained in:
2021-03-04 11:26:14 -06:00
parent 2f3d94adf3
commit 82605bb697
8 changed files with 139 additions and 15 deletions

View File

@@ -106,7 +106,7 @@ class Page extends VersionedModel {
return visible
}
is_shared() {
is_shared() { // TODO: public user sharing...
return this.shared_users_view.length > 0 || this.shared_users_update.length > 0 || this.shared_users_manage.length > 0
}
@@ -215,6 +215,46 @@ class Page extends VersionedModel {
else return false
}
async share_public(current_user, level = 'view') {
const PublicUserPermission = this.models.get('auth:PublicUserPermission')
if ( !['view', 'update', 'manage'].includes(level) ) {
throw new Error(`Invalid share level: ${level}`)
}
const possible_grants = [':view', ':manage', ':update', ''].map(x => `page:${this.UUID}${x}`)
// Remove existing sharing info
await PublicUserPermission.deleteMany({
permission: {
$in: possible_grants,
},
})
// Create the new sharing level
const share = new PublicUserPermission({
associated_user_id: this.OrgUserId,
permission: `page:${this.UUID}:${level}`,
})
await this.version_save(`Shared publicly (${level} access)`, current_user.id)
await share.save()
}
async unshare_public(current_user) {
const PublicUserPermission = this.models.get('auth:PublicUserPermission')
const possible_grants = [':view', ':manage', ':update', ''].map(x => `page:${this.UUID}${x}`)
// Remove existing sharing info
await PublicUserPermission.deleteMany({
permission: {
$in: possible_grants,
},
})
await this.version_save(`Un-shared public access)`, current_user.id)
}
async share_with(user, level = 'view') {
if ( !['view', 'update', 'manage'].includes(level) ) {
throw new Error(`Invalid share level: ${level}`)