From efb879b8e716eba7a44b8895a8e02adbbacb7f27 Mon Sep 17 00:00:00 2001 From: midorikocak Date: Tue, 2 Feb 2021 19:23:46 +0100 Subject: [PATCH 1/2] public site --- server/routes/categories.js | 9 ++++++--- server/routes/playlists.js | 11 +++++++---- server/userAuth.js | 10 +++++++--- test/utils/bootstrap.js | 2 ++ 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/server/routes/categories.js b/server/routes/categories.js index 3a1b9380..082f7176 100644 --- a/server/routes/categories.js +++ b/server/routes/categories.js @@ -11,9 +11,12 @@ const {parseUrl} = require('../urlParser') router.get('*', handleCategory) module.exports = router +let isPublicUser = false + const categories = getTemplates('categories') async function handleCategory(req, res) { + isPublicUser = req.userInfo.userId === '0' log.info(`GET ${req.path}`) // FIXME: consider putting this in middleware and save on req const {meta, parent, data, root} = await parseUrl(req.path) @@ -40,7 +43,7 @@ async function handleCategory(req, res) { lastUpdatedBy: (meta.lastModifyingUser || {}).displayName, modifiedAt: meta.modifiedTime, createdAt: meta.createdTime, - editLink: meta.mimeType === 'text/html' ? meta.folder.webViewLink : meta.webViewLink, + editLink: isPublicUser ? false : (meta.mimeType === 'text/html' ? meta.folder.webViewLink : meta.webViewLink), id, template: stringTemplate, duplicates @@ -88,7 +91,7 @@ function prepareContextualData(data, url, breadcrumb, parent, slug) { return { url: `/${arr.slice(0, i + 1).join('/')}`, name: cleanName(breadcrumbInfo[i].name), - editLink: breadcrumbInfo[i].webViewLink + editLink: isPublicUser ? false : (breadcrumbInfo[i].webViewLink) } }) @@ -110,7 +113,7 @@ function createRelatedList(slugs, self, baseUrl) { return { sort, name: prettyName, - editLink: webViewLink, + editLink: isPublicUser ? false :webViewLink, resourceType, url, tags diff --git a/server/routes/playlists.js b/server/routes/playlists.js index 48ae0218..fce662b6 100644 --- a/server/routes/playlists.js +++ b/server/routes/playlists.js @@ -11,7 +11,10 @@ const {parseUrl} = require('../urlParser') router.get('*', handlePlaylist) module.exports = router +let isPublicUser = false + async function handlePlaylist(req, res) { + isPublicUser = req.userInfo.userId === '0' const {meta, parent, data} = await parseUrl(req.path) if (!meta || !data) return 'next' @@ -66,7 +69,7 @@ function preparePlaylistOverview(playlistMeta, values, breadcrumb) { modifiedAt: playlistMeta.modifiedTime, lastUpdatedBy: (playlistMeta.lastModifyingUser || {}).displayName, createdAt: playlistMeta.createdTime, - editLink: playlistMeta.mimeType === 'text/html' ? playlistMeta.folder.webViewLink : playlistMeta.webViewLink + editLink: isPublicUser ? false : (playlistMeta.mimeType === 'text/html' ? playlistMeta.folder.webViewLink : playlistMeta.webViewLink) }) return renderData @@ -97,7 +100,7 @@ async function preparePlaylistPage(data, url, parent) { return { url: `/${arr.slice(0, i + 1).join('/')}`, name: cleanName(breadcrumbInfo[i].name), - editLink: breadcrumbInfo[i].webViewLink + editLink: isPublicUser ? false : breadcrumbInfo[i].webViewLink } }) @@ -131,7 +134,7 @@ function prepareContextualData(playlistMeta, values, breadcrumb) { return { url: `/${arr.slice(0, i + 1).join('/')}`, name: cleanName(breadcrumbInfo[i].name), - editLink: breadcrumbInfo[i].webViewLink + editLink: isPublicUser ? false : breadcrumbInfo[i].webViewLink } }) @@ -141,7 +144,7 @@ function prepareContextualData(playlistMeta, values, breadcrumb) { sort: prettyName, name: prettyName, url: `${path}/${slug}`, - editLink: mimeType === 'text/html' ? folder.webViewLink : webViewLink, + editLink: isPublicUser ? false : mimeType === 'text/html' ? folder.webViewLink : webViewLink, resourceType: resourceType } }) diff --git a/server/userAuth.js b/server/userAuth.js index 9794372e..0c290d56 100644 --- a/server/userAuth.js +++ b/server/userAuth.js @@ -52,9 +52,10 @@ router.get('/auth/redirect', passport.authenticate('google'), (req, res) => { router.use((req, res, next) => { const isDev = process.env.NODE_ENV === 'development' + const isPublic = process.env.PUBLIC_SITE === true const passportUser = (req.session.passport || {}).user || {} - if (isDev || (req.isAuthenticated() && isAuthorized(passportUser))) { + if (isDev || isPublic || (req.isAuthenticated() && isAuthorized(passportUser))) { setUserInfo(req) return next() } @@ -81,14 +82,17 @@ function isAuthorized(user) { } function setUserInfo(req) { - if (process.env.NODE_ENV === 'development') { + const isPublic = process.env.PUBLIC_SITE === true + + if (process.env.NODE_ENV === 'development' || isPublic) { req.userInfo = { email: process.env.TEST_EMAIL || template('footer.defaultEmail'), - userId: '10', + userId: isPublic ? '0' : '10', analyticsUserId: md5('10library') } return } + req.userInfo = req.userInfo ? req.userInfo : { email: req.session.passport.user.emails[0].value, userId: req.session.passport.user.id, diff --git a/test/utils/bootstrap.js b/test/utils/bootstrap.js index bf780fb8..cc3fa1dc 100644 --- a/test/utils/bootstrap.js +++ b/test/utils/bootstrap.js @@ -13,6 +13,8 @@ process.env.GOOGLE_CLIENT_SECRET = 'abc123' process.env.SESSION_SECRET = 'abc123' process.env.APPROVED_DOMAINS = 'test.com, (.*)?ar.org, demo.user@demo.site.edu' process.env.GCP_PROJECT_ID = 'fake-project' +process.env.PUBLIC_SITE = true + const userInfo = { emails: [{value: 'test.user@test.com'}], email: 'test.user@test.com', From 86513a2f4675c0c1de02f994c5e0155c1851101c Mon Sep 17 00:00:00 2001 From: midorikocak Date: Tue, 2 Feb 2021 20:00:37 +0100 Subject: [PATCH 2/2] hide edits and userTools --- layouts/pages/index.ejs | 8 +++++--- layouts/partials/footer.ejs | 14 ++++++++------ layouts/partials/nav.ejs | 11 ++++++----- layouts/partials/userTools.ejs | 34 ++++++++++++++++++---------------- server/routes/categories.js | 9 ++++++--- server/routes/pages.js | 2 +- server/routes/playlists.js | 12 ++++++++---- server/userAuth.js | 4 +--- 8 files changed, 53 insertions(+), 41 deletions(-) diff --git a/layouts/pages/index.ejs b/layouts/pages/index.ejs index a502eb88..6a22bd03 100644 --- a/layouts/pages/index.ejs +++ b/layouts/pages/index.ejs @@ -5,7 +5,9 @@ @@ -18,11 +20,11 @@ <%- include('partials/search', {style: 'homepage', focus: 'autofocus', msgOnFocus: template('search.placeholder')}) %> - <% if (locals.editLink) { %> -
- Edit - -
+ <% if (!locals.isPublic) { %> + <% if (locals.editLink) { %> +
+ Edit +
+ <% } %> <% } %> diff --git a/layouts/partials/userTools.ejs b/layouts/partials/userTools.ejs index 2bd5819b..1eedf482 100644 --- a/layouts/partials/userTools.ejs +++ b/layouts/partials/userTools.ejs @@ -1,19 +1,21 @@ -
-
- - - +<% if (!locals.isPublic) { %> + -
- - +
+ + -
-
+
+
+<% } %> diff --git a/server/routes/categories.js b/server/routes/categories.js index 082f7176..114739de 100644 --- a/server/routes/categories.js +++ b/server/routes/categories.js @@ -43,7 +43,8 @@ async function handleCategory(req, res) { lastUpdatedBy: (meta.lastModifyingUser || {}).displayName, modifiedAt: meta.modifiedTime, createdAt: meta.createdTime, - editLink: isPublicUser ? false : (meta.mimeType === 'text/html' ? meta.folder.webViewLink : meta.webViewLink), + isPublic: isPublicUser, + editLink: meta.mimeType === 'text/html' ? meta.folder.webViewLink : meta.webViewLink, id, template: stringTemplate, duplicates @@ -91,7 +92,8 @@ function prepareContextualData(data, url, breadcrumb, parent, slug) { return { url: `/${arr.slice(0, i + 1).join('/')}`, name: cleanName(breadcrumbInfo[i].name), - editLink: isPublicUser ? false : (breadcrumbInfo[i].webViewLink) + isPublic: isPublicUser, + editLink: breadcrumbInfo[i].webViewLink } }) @@ -113,7 +115,8 @@ function createRelatedList(slugs, self, baseUrl) { return { sort, name: prettyName, - editLink: isPublicUser ? false :webViewLink, + isPublic: isPublicUser, + editLink: webViewLink, resourceType, url, tags diff --git a/server/routes/pages.js b/server/routes/pages.js index dec840ee..7192144c 100644 --- a/server/routes/pages.js +++ b/server/routes/pages.js @@ -47,7 +47,7 @@ async function handlePage(req, res) { if (page === 'categories' || page === 'index') { const tree = await getTree() const categories = buildDisplayCategories(tree) - res.render(template, {...categories, template: stringTemplate}) + res.render(template, {...categories, template: stringTemplate, isPublic: true}) return } diff --git a/server/routes/playlists.js b/server/routes/playlists.js index fce662b6..042cd14d 100644 --- a/server/routes/playlists.js +++ b/server/routes/playlists.js @@ -69,7 +69,8 @@ function preparePlaylistOverview(playlistMeta, values, breadcrumb) { modifiedAt: playlistMeta.modifiedTime, lastUpdatedBy: (playlistMeta.lastModifyingUser || {}).displayName, createdAt: playlistMeta.createdTime, - editLink: isPublicUser ? false : (playlistMeta.mimeType === 'text/html' ? playlistMeta.folder.webViewLink : playlistMeta.webViewLink) + isPublic: isPublicUser, + editLink: playlistMeta.mimeType === 'text/html' ? playlistMeta.folder.webViewLink : playlistMeta.webViewLink }) return renderData @@ -100,7 +101,8 @@ async function preparePlaylistPage(data, url, parent) { return { url: `/${arr.slice(0, i + 1).join('/')}`, name: cleanName(breadcrumbInfo[i].name), - editLink: isPublicUser ? false : breadcrumbInfo[i].webViewLink + isPublic: isPublicUser, + editLink: breadcrumbInfo[i].webViewLink } }) @@ -134,7 +136,8 @@ function prepareContextualData(playlistMeta, values, breadcrumb) { return { url: `/${arr.slice(0, i + 1).join('/')}`, name: cleanName(breadcrumbInfo[i].name), - editLink: isPublicUser ? false : breadcrumbInfo[i].webViewLink + isPublic: isPublicUser, + editLink: breadcrumbInfo[i].webViewLink } }) @@ -144,7 +147,8 @@ function prepareContextualData(playlistMeta, values, breadcrumb) { sort: prettyName, name: prettyName, url: `${path}/${slug}`, - editLink: isPublicUser ? false : mimeType === 'text/html' ? folder.webViewLink : webViewLink, + isPublic: isPublicUser, + editLink: mimeType === 'text/html' ? folder.webViewLink : webViewLink, resourceType: resourceType } }) diff --git a/server/userAuth.js b/server/userAuth.js index 0c290d56..54b91a7a 100644 --- a/server/userAuth.js +++ b/server/userAuth.js @@ -10,6 +10,7 @@ const {stringTemplate: template} = require('./utils') const router = require('express-promise-router')() const domains = new Set(process.env.APPROVED_DOMAINS.split(/,\s?/g)) +const isPublic = (process.env.TRUST_PROXY || '').toUpperCase() === 'TRUE' passport.use(new GoogleStrategy.Strategy({ clientID: process.env.GOOGLE_CLIENT_ID, @@ -52,7 +53,6 @@ router.get('/auth/redirect', passport.authenticate('google'), (req, res) => { router.use((req, res, next) => { const isDev = process.env.NODE_ENV === 'development' - const isPublic = process.env.PUBLIC_SITE === true const passportUser = (req.session.passport || {}).user || {} if (isDev || isPublic || (req.isAuthenticated() && isAuthorized(passportUser))) { @@ -82,8 +82,6 @@ function isAuthorized(user) { } function setUserInfo(req) { - const isPublic = process.env.PUBLIC_SITE === true - if (process.env.NODE_ENV === 'development' || isPublic) { req.userInfo = { email: process.env.TEST_EMAIL || template('footer.defaultEmail'),