Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public site #252

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions layouts/pages/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

<header id="masthead" class="masthead" role="banner">
<div class="container">
<%- include('partials/userTools') %>
<% if (!isPublic) { %>
<%- include('partials/userTools') %>
<% } %>
</div>
</header>

Expand All @@ -18,11 +20,11 @@
<%- include('partials/search', {style: 'homepage', focus: 'autofocus', msgOnFocus: template('search.placeholder')}) %>

<div class="featured-cat">
<% if (modules.length) {
<% if (modules.length) {
modules.forEach((module, i, all) => {
%>
<%- include('partials/landingModule', module) %>
<% })
<% })
} else { %>
<!-- Eventually we might display a warning here about why this space is empty. -->
<% } %>
Expand Down
14 changes: 8 additions & 6 deletions layouts/partials/footer.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
<a class="button btn-footer" href="<%- page.link %>"><%= i + 1 %></a>
<% }) %>
<% } %>
<% if (locals.editLink) { %>
<a class="button btn-footer" href="<%- editLink %>" id="edit-button" target="_blank"><%- template('footer.buttons.edit') %></a>
<% } %>
<% if (locals.parentId) { %>
<a class="button btn-footer" href="https://docs.google.com/document/u/0/create?usp=drive_web&folder=<%- parentId %>" %> target="_blank"><%- template('footer.buttons.create') %></a>
<a class="button btn-footer" href="https://drive.google.com/drive/u/0/folders/<%- parentId %>" target="_blank"><%- template('footer.buttons.viewFolder') %></a>
<% if (!locals.isPublic) { %>
<% if (locals.editLink) { %>
<a class="button btn-footer" href="<%- editLink %>" id="edit-button" target="_blank"><%- template('footer.buttons.edit') %></a>
<% } %>
<% if (locals.parentId) { %>
<a class="button btn-footer" href="https://docs.google.com/document/u/0/create?usp=drive_web&folder=<%- parentId %>" %> target="_blank"><%- template('footer.buttons.create') %></a>
<a class="button btn-footer" href="https://drive.google.com/drive/u/0/folders/<%- parentId %>" target="_blank"><%- template('footer.buttons.viewFolder') %></a>
<% } %>
<% } %>

<p class="help-text"><%- template('footer.helpText') %></p>
Expand Down
11 changes: 6 additions & 5 deletions layouts/partials/nav.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@
</p>
</div>

<% if (locals.editLink) { %>
<div class="tool">
<a href="<%- editLink %>" id="edit-button" target="_blank">Edit<i class="fa fa-pencil" aria-hidden="true"></i></a>

</div>
<% if (!locals.isPublic) { %>
<% if (locals.editLink) { %>
<div class="tool">
<a href="<%- editLink %>" id="edit-button" target="_blank">Edit<i class="fa fa-pencil" aria-hidden="true"></i></a>
</div>
<% } %>
<% } %>

</div>
Expand Down
34 changes: 18 additions & 16 deletions layouts/partials/userTools.ejs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<div class="user-tools">
<div id="user-profile">
<a class="btn-user-whole" href="#me">
<button class="button btn-user-initial"></button>
<span class="user-fullname"></span>
<% if (!locals.isPublic) { %>
<div class="user-tools">
<div id="user-profile">
<a class="btn-user-whole" href="#me">
<button class="button btn-user-initial"></button>
<span class="user-fullname"></span>

<i class="fa fa-angle-down" aria-hidden="true"></i>
</a>
</div>
<i class="fa fa-angle-down" aria-hidden="true"></i>
</a>
</div>

<div id="me" class="overlay">
<a class="cancel" href="#"></a>
<div class="popup">
<a class="close" href="#">&times;</a>
<i class="fa fa-spinner fa-pulse" aria-hidden="true"></i>
</div>
<div id="me" class="overlay">
<a class="cancel" href="#"></a>
<div class="popup">
<a class="close" href="#">&times;</a>
<i class="fa fa-spinner fa-pulse" aria-hidden="true"></i>
</div>

</div>
</div>
</div>
</div>
<% } %>
6 changes: 6 additions & 0 deletions server/routes/categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -40,6 +43,7 @@ async function handleCategory(req, res) {
lastUpdatedBy: (meta.lastModifyingUser || {}).displayName,
modifiedAt: meta.modifiedTime,
createdAt: meta.createdTime,
isPublic: isPublicUser,
editLink: meta.mimeType === 'text/html' ? meta.folder.webViewLink : meta.webViewLink,
id,
template: stringTemplate,
Expand Down Expand Up @@ -88,6 +92,7 @@ function prepareContextualData(data, url, breadcrumb, parent, slug) {
return {
url: `/${arr.slice(0, i + 1).join('/')}`,
name: cleanName(breadcrumbInfo[i].name),
isPublic: isPublicUser,
editLink: breadcrumbInfo[i].webViewLink
}
})
Expand All @@ -110,6 +115,7 @@ function createRelatedList(slugs, self, baseUrl) {
return {
sort,
name: prettyName,
isPublic: isPublicUser,
editLink: webViewLink,
resourceType,
url,
Expand Down
2 changes: 1 addition & 1 deletion server/routes/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 7 additions & 0 deletions server/routes/playlists.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -66,6 +69,7 @@ function preparePlaylistOverview(playlistMeta, values, breadcrumb) {
modifiedAt: playlistMeta.modifiedTime,
lastUpdatedBy: (playlistMeta.lastModifyingUser || {}).displayName,
createdAt: playlistMeta.createdTime,
isPublic: isPublicUser,
editLink: playlistMeta.mimeType === 'text/html' ? playlistMeta.folder.webViewLink : playlistMeta.webViewLink
})

Expand Down Expand Up @@ -97,6 +101,7 @@ async function preparePlaylistPage(data, url, parent) {
return {
url: `/${arr.slice(0, i + 1).join('/')}`,
name: cleanName(breadcrumbInfo[i].name),
isPublic: isPublicUser,
editLink: breadcrumbInfo[i].webViewLink
}
})
Expand Down Expand Up @@ -131,6 +136,7 @@ function prepareContextualData(playlistMeta, values, breadcrumb) {
return {
url: `/${arr.slice(0, i + 1).join('/')}`,
name: cleanName(breadcrumbInfo[i].name),
isPublic: isPublicUser,
editLink: breadcrumbInfo[i].webViewLink
}
})
Expand All @@ -141,6 +147,7 @@ function prepareContextualData(playlistMeta, values, breadcrumb) {
sort: prettyName,
name: prettyName,
url: `${path}/${slug}`,
isPublic: isPublicUser,
editLink: mimeType === 'text/html' ? folder.webViewLink : webViewLink,
resourceType: resourceType
}
Expand Down
10 changes: 7 additions & 3 deletions server/userAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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'

const authStrategies = ['google', 'Slack']
let authStrategy = process.env.OAUTH_STRATEGY
Expand Down Expand Up @@ -84,7 +85,8 @@ router.get('/auth/redirect', passport.authenticate(authStrategy, {failureRedirec
router.use((req, res, next) => {
const isDev = process.env.NODE_ENV === 'development'
const passportUser = (req.session.passport || {}).user || {}
if (isDev || (req.isAuthenticated() && isAuthorized(passportUser))) {

if (isDev || isPublic || (req.isAuthenticated() && isAuthorized(passportUser))) {
setUserInfo(req)
return next()
}
Expand All @@ -111,15 +113,17 @@ function isAuthorized(user) {
}

function setUserInfo(req) {
if (process.env.NODE_ENV === 'development') {
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
}

const email = isSlackOauth ? req.session.passport.user.email : req.session.passport.user.emails[0].value

req.userInfo = req.userInfo ? req.userInfo : {
userId: req.session.passport.user.id,
analyticsUserId: md5(req.session.passport.user.id + 'library'),
Expand Down
2 changes: 2 additions & 0 deletions test/utils/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down