salix/loopback/server/boot/salix.js

147 lines
4.5 KiB
JavaScript
Raw Normal View History

2017-10-13 14:23:00 +00:00
module.exports = function(app) {
2017-06-02 13:39:16 +00:00
let models = app.models;
let bootTimestamp = new Date().getTime();
2017-06-02 13:39:16 +00:00
2017-10-13 14:23:00 +00:00
app.get('/', function(req, res) {
2017-05-17 19:23:47 +00:00
let token = req.cookies.vnToken;
validateToken(token, function(isValid) {
if (!isValid) {
2017-05-17 19:23:47 +00:00
redirectToAuth(res, req.get('origin'));
return;
}
res.render('index.ejs', {
assets: app.getWpAssets('salix'),
version: bootTimestamp
});
2017-05-17 19:23:47 +00:00
});
2017-01-24 15:06:07 +00:00
});
2016-11-08 14:06:45 +00:00
2017-10-13 14:23:00 +00:00
app.get('/acl', function(req, res) {
2017-05-25 05:21:36 +00:00
let token = req.cookies.vnToken;
2017-06-01 11:31:42 +00:00
validateToken(token, function(isValid, token) {
2017-05-25 05:21:36 +00:00
if (isValid)
2017-06-01 11:31:42 +00:00
sendUserRole(res, token);
2017-05-25 05:21:36 +00:00
else
sendACL(res, {});
});
});
2017-10-13 14:23:00 +00:00
app.get('/login', function(req, res) {
2017-05-17 19:23:47 +00:00
let token = req.query.token;
let continueUrl = req.query.continue;
validateToken(token, function(isValid) {
if (isValid) {
2017-10-13 14:23:00 +00:00
res.cookie('vnToken', token /* , {httpOnly: true} */);
res.redirect(continueUrl ? continueUrl : '/');
2017-10-13 14:23:00 +00:00
} else
2017-05-17 19:23:47 +00:00
redirectToAuth(res);
});
2017-01-24 15:06:07 +00:00
});
2017-05-17 19:23:47 +00:00
2017-10-13 14:23:00 +00:00
app.get('/logout', function(req, res) {
2017-05-17 19:23:47 +00:00
let token = req.cookies.vnToken;
2017-06-02 13:39:16 +00:00
models.User.logout(token, function() {
2017-05-17 19:23:47 +00:00
redirectToAuth(res);
});
});
2017-11-27 14:08:18 +00:00
app.get('/validateToken', function(req, res) {
let token = req.headers.authorization;
validateToken(token, function(isValid) {
if (isValid)
2017-11-27 14:08:18 +00:00
res.json(null);
else {
2017-11-27 14:08:18 +00:00
res.status(401).json({
message: 'Invalid token'
});
}
});
});
2017-10-13 14:23:00 +00:00
function validateToken(tokenId, cb) {
2017-06-02 13:39:16 +00:00
models.AccessToken.findById(tokenId, function(err, token) {
2017-05-17 19:23:47 +00:00
if (token) {
token.validate(function(err, isValid) {
2017-06-01 11:31:42 +00:00
cb(isValid === true, token);
2017-05-17 19:23:47 +00:00
});
2017-10-13 14:23:00 +00:00
} else
2017-05-17 19:23:47 +00:00
cb(false);
});
}
2017-10-13 14:23:00 +00:00
function sendUserRole(res, token) {
if (token.userId) {
2017-05-25 05:21:36 +00:00
let query = {
2017-10-13 14:23:00 +00:00
where: {
principalId: token.userId,
principalType: 'USER'
},
include: [{
relation: 'role',
scope: {
fields: ['name']
}
}]
2017-06-01 11:31:42 +00:00
};
2017-10-13 14:23:00 +00:00
models.RoleMapping.find(query, function(_, roles) {
if (roles) {
let acl = {
userProfile: {},
roles: {}
};
2017-10-13 14:23:00 +00:00
Object.keys(roles).forEach(function(_, i) {
if (roles[i].roleId) {
2017-05-25 05:21:36 +00:00
let rol = roles[i].role();
acl.roles[rol.name] = true;
2017-05-25 05:21:36 +00:00
}
});
2017-10-13 14:23:00 +00:00
models.User.findById(token.userId, function(_, userProfile) {
// acl.userProfile = userProfile;
2018-02-20 13:30:02 +00:00
if (userProfile && userProfile.id) {
acl.userProfile.id = userProfile.id;
acl.userProfile.username = userProfile.username;
acl.userProfile.warehouseId = 1;
sendACL(res, acl);
} else
2018-02-20 13:30:02 +00:00
sendACL(res, {});
2017-06-01 11:31:42 +00:00
});
2017-10-13 14:23:00 +00:00
} else
2017-05-25 05:21:36 +00:00
sendACL(res, {});
});
2017-10-13 14:23:00 +00:00
} else
2017-05-25 05:21:36 +00:00
sendACL(res, {});
}
2017-10-13 14:23:00 +00:00
function redirectToAuth(res, continueUrl) {
2017-05-17 19:23:47 +00:00
let authUrl = app.get('url auth');
let params = {
apiKey: app.get('api key'),
continue: continueUrl
};
2017-10-13 14:23:00 +00:00
res.clearCookie('vnToken');
2017-05-17 19:23:47 +00:00
res.redirect(`${authUrl}/?${encodeUri(params)}`);
}
2017-05-25 05:21:36 +00:00
2017-10-13 14:23:00 +00:00
function sendACL(res, acl) {
2017-05-25 05:21:36 +00:00
let aclStr = JSON.stringify(acl);
res.header('Content-Type', 'application/javascript; charset=UTF-8');
2017-05-31 07:46:05 +00:00
res.send(`(function(window){window.salix = window.salix || {}; window.salix.acl = window.salix.acl || {}; window.salix.acl = ${aclStr}; })(window)`);
2017-05-25 05:21:36 +00:00
}
2016-11-08 14:06:45 +00:00
};
2017-05-17 19:23:47 +00:00
function encodeUri(object) {
let uri = '';
for (let key in object) {
2017-05-17 19:23:47 +00:00
if (object[key]) {
if (uri.length > 0)
uri += '&';
uri += encodeURIComponent(key) + '=';
uri += encodeURIComponent(object[key]);
2017-05-17 19:23:47 +00:00
}
}
2017-05-17 19:23:47 +00:00
return uri;
}