Skip to content

Commit

Permalink
Complete new user propagation
Browse files Browse the repository at this point in the history
OpenUserJS#1939 OpenUserJS#1938 OpenUserJS#1937 OpenUserJS#1446 OpenUserJS#1470 ... OpenUserJS#944

NOTE(s):
* Camelcaps not adhered to on some... may revisit.
* `userrole` isn't consistent with `roleName` however is consistent with other objects... may revisit.
* May not be the most efficient with `parseUser` but other option is to pull in `userRoles.json` and enumerate grab... may revisit.
* Will have to boot everyone off temporarily to propagate values to sessions.
  • Loading branch information
Martii committed Jan 15, 2022
1 parent 2dca71f commit 1dfdc4d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
24 changes: 21 additions & 3 deletions controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ var addSession = require('../libs/modifySessions').add;
var expandSession = require('../libs/modifySessions').expand;
var statusCodePage = require('../libs/templateHelpers').statusCodePage;

var modelParser = require('../libs/modelParser');

//--- Configuration inclusions
var allStrategies = require('./strategies.json');

Expand Down Expand Up @@ -69,6 +71,7 @@ exports.preauth = function (aReq, aRes, aNext) {
var authedUser = aReq.session.user;

var username = aReq.body.username;
var userauth = aReq.body.auth;
var SITEKEY = process.env.HCAPTCHA_SITE_KEY;

if (!authedUser) {
Expand All @@ -93,18 +96,29 @@ exports.preauth = function (aReq, aRes, aNext) {

User.findOne({ name: { $regex: new RegExp('^' + username + '$', 'i') } },
function (aErr, aUser) {
var user = null;

if (aErr) {
console.error('Authfail with no User found of', username, aErr);
aRes.redirect('/login?usernamefail');
return;
}

if (aUser) {
user = modelParser.parseUser(aUser);

// Ensure that casing is identical so we still have it, correctly, when they
// get back from authentication
aReq.body.username = aUser.name;
aReq.body.username = user.name;

if (userauth) {
aReq.body.userauth = userauth;
} else {
aReq.body.userauth = user.userStrategies[user.userStrategies.length - 1];
}
aReq.userrole = user.roleName;

if (!aUser._probationary) {
if (!user._probationary) {
// Skip captcha for well known individual
aReq.wellKnownUser = true;

Expand All @@ -122,6 +136,7 @@ exports.preauth = function (aReq, aRes, aNext) {
// Match cleansed name and this is the casing they have chosen
aReq.body.username = username;

aReq.body.userauth = userauth;
aReq.newUser = true;

// Validate captcha for unknown individual
Expand Down Expand Up @@ -209,8 +224,11 @@ exports.auth = function (aReq, aRes, aNext) {
}

// Save the known statuses of the user on the session and remove
aReq.session.userauth = aReq.body.userauth;
aReq.session.userrole = aReq.userrole;
aReq.session.wellKnownUser = aReq.wellKnownUser;
aReq.session.newUser = aReq.newUser;
delete aReq.userrole;
delete aReq.wellKnownUser;
delete aReq.newUser;

Expand Down Expand Up @@ -317,8 +335,8 @@ exports.auth = function (aReq, aRes, aNext) {
exports.callback = function (aReq, aRes, aNext) {
var strategy = aReq.params.strategy;
var username = aReq.session.username;
var newstrategy = aReq.session.newstrategy;
var wellKnownUser = aReq.session.wellKnownUser;
var newstrategy = aReq.session.newstrategy;
var captchaToken = aReq.session.captchaToken;
var captchaSuccess = aReq.session.captchaSuccess;

Expand Down
12 changes: 12 additions & 0 deletions libs/modifySessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,21 @@ exports.findSessionData = function (aQuery, aStore, aOptions, aCallback) {


data.passport.oujsOptions.username = data.username || findMeta(data.user, 'name');
data.passport.oujsOptions.userrole = data.passport.oujsOptions.username
? data.userrole
: '\u2026';
data.passport.oujsOptions.newUser = data.newUser;
data.passport.oujsOptions.sid = aSessionData._id;

// NOTE: These only shows up during authentication otherwise don't use
if (data.userauth) {
data.passport.oujsOptions.strategy = data.userauth;
}

if (data.useragent) {
data.passport.oujsOptions.userAgent = data.useragent;
}

// Very simple query filter search check to start.
// Currently only looking in `data.passport.oujsOptions.username`.
if (aQuery && aQuery.username) {
Expand Down
2 changes: 1 addition & 1 deletion views/includes/session.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<span>
{{#user.userPageUrl}}<a href="{{{user.userPageUrl}}}" class="username">{{name}}</a>{{/user.userPageUrl}}
{{^user.userPageUrl}}<span class="username">{{#passport.oujsOptions.newUser}}<em>{{/passport.oujsOptions.newUser}}{{#name}}{{name}}{{/name}}{{#passport.oujsOptions.newUser}}</em>{{/passport.oujsOptions.newUser}}{{^name}}<em>&hellip;</em>{{/name}}</span>{{/user.userPageUrl}}
<span class="label label-default">{{#user.roleName}}{{user.roleName}}{{/user.roleName}}{{^user.roleName}}<em>&hellip;</em>{{/user.roleName}}</span>
<span class="label label-default">{{#user.roleName}}{{user.roleName}}{{/user.roleName}}{{^user.roleName}}{{#passport.oujsOptions.userrole}}{{passport.oujsOptions.userrole}}{{/passport.oujsOptions.userrole}}{{^passport.oujsOptions.userrole}}<em>User</em>{{/passport.oujsOptions.userrole}}{{/user.roleName}}</span>
<span class="label label-{{#cookie.secure}}success{{/cookie.secure}}{{^cookie.secure}}warning{{/cookie.secure}}" title="secure">
<i class="fa fa-{{^cookie.secure}}un{{/cookie.secure}}lock"></i>
</span>
Expand Down

0 comments on commit 1dfdc4d

Please sign in to comment.