Skip to content

Commit b1af392

Browse files
committed
Add ability limit write access to a subset of users through an additional configuration file
1 parent 52ecc0d commit b1af392

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,12 @@ Configuration options reference
385385

386386
If the endpoint doesn't provide the email address for the user, allow empty emails to authenticate anyway. Note that GitHub authentication usually requires this to be `true` (unless all wiki users have public email addresses on their GitHub accounts).
387387

388+
#### authorization.moderatorsFile (string: "")
389+
390+
Absolute path for your moderators YAML file. If used, this file must contain a list of `usernames` and `emails` for users who have write access to the wiki. A user who has a match in either the `usernames` or `emails` list will have right access.
391+
392+
If this field is left blank, all logged in users will have write access to the wiki.
393+
388394
#### pages.index (string: "Home")
389395

390396
Defines the page name for the index of the wiki

lib/app.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,23 @@ module.exports.initialize = function (config) {
175175
}
176176
}
177177

178-
app.all('/pages/*', requireAuthentication)
178+
function requireModerator (req, res, next) {
179+
requireAuthentication(req, res, function(){
180+
if (!res.locals.user.moderator) {
181+
res.locals.title = '403 - Permission denied'
182+
res.statusCode = 403
183+
res.render('403.pug')
184+
} else {
185+
next()
186+
}
187+
})
188+
}
189+
190+
if (app.locals.config.get('authorization').moderators) {
191+
app.all('/pages/*', requireModerator)
192+
} else {
193+
app.all('/pages/*', requireAuthentication)
194+
}
179195

180196
if (!app.locals.config.get('authorization').anonRead) {
181197
app.all('/wiki', requireAuthentication)

lib/config.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ module.exports = (function () {
3636
}
3737
}
3838

39+
// Load moderators from moderatorsFile
40+
if (config.authorization.moderatorsFile){
41+
config.authorization.moderators = yaml.load(fs.readFileSync(config.authorization.moderatorsFile).toString())
42+
}
43+
3944
return true
4045
},
4146

@@ -121,7 +126,8 @@ module.exports = (function () {
121126
anonRead: true,
122127
validMatches: '.+',
123128
// Breaking changes in Jingo 1.5 (when this parameter has been added): the default for new servers is to NOT allow empty emails to validate
124-
emptyEmailMatches: false
129+
emptyEmailMatches: false,
130+
moderatorsFile: ''
125131
},
126132

127133
// Defaults for the pages key are compatible with Jingo < 1 (which means

routes/auth.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,19 @@ passport.deserializeUser(function (user, done) {
184184
user.email = 'jingouser'
185185
}
186186

187+
// Check moderator status
188+
user.moderator = false
189+
var moderators = app.locals.config.get('authorization').moderators
190+
if (moderators){
191+
if (moderators.usernames.indexOf(user.displayName) > -1 ||
192+
moderators.emails.indexOf(user.email) > -1){
193+
user.moderator = true
194+
}
195+
} else {
196+
// If no moderators file supplied everyone is a 'moderator'
197+
user.moderator = true
198+
}
199+
187200
user.asGitAuthor = user.displayName + ' <' + user.email + '>'
188201
done(undefined, user)
189202
})

views/403.pug

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extends layout
2+
3+
block content
4+
#content
5+
.jumbotron
6+
h2 #{title}
7+
p You do not have permission to perform this action

0 commit comments

Comments
 (0)