diff --git a/app.js b/app.js index 2fb42d8..6ae2129 100644 --- a/app.js +++ b/app.js @@ -15,6 +15,7 @@ var api = require('./routes/api'); var kthpeople = require('./routes/kthpeople'); var login = require('./routes/login'); var groups = require('./routes/groups'); +var helpers = require('./routes/helpers'); var pug = require("pug"); var babel = require("jade-babel"); @@ -47,7 +48,7 @@ app.use('/users', users); app.use('/roles', roles); app.use('/mandates', mandates); app.use('/api', api); -app.use('/kthpeople', kthpeople); +app.use('/kthpeople', helpers.requiresearch, kthpeople); app.use('/login', login); app.use('/groups', groups); diff --git a/public_scripts/search-components.js b/public_scripts/search-components.js index 05578c2..494adf4 100644 --- a/public_scripts/search-components.js +++ b/public_scripts/search-components.js @@ -155,7 +155,7 @@ var SearchBar = React.createClass({ render: function() { return (
- +
); @@ -184,6 +184,7 @@ var Result = React.createClass({ return (
+

{this.props.fullname}

diff --git a/routes/helpers.js b/routes/helpers.js index c8d7e49..fe056a8 100644 --- a/routes/helpers.js +++ b/routes/helpers.js @@ -21,7 +21,23 @@ var isadmin = function(user) { }); }; -exports.isadmin = isadmin; +exports.isadmin = isadmin; + +var issearch = function(user) { + var plsurl = "https://pls.datasektionen.se/api/user/" + user + "/dfunkt/search"; + return new Promise(function (resolve) { + request({uri: plsurl, method: 'GET'}, function (error, response, body) { + if(error) console.error(error); + if (body === "true") { + resolve(true); + } else { + resolve(false); + } + }); + }); +}; + +exports.issearch = issearch; exports.requirelogin = function(req, res, next) { if(req.user) { @@ -43,6 +59,18 @@ exports.requireadmin = function(req, res, next) { }); }; +exports.requiresearch = function(req, res, next) { + issearch(req.user).then(function(search) { + if(search) { + next(); + } else { + denied(res); + } + }).catch(function(e) { + console.error(e); + }); +}; + var roleAtt = ['title', 'description', 'identifier', 'email', 'active', 'id']; var userAtt = ['first_name', 'last_name', 'email', 'kthid', 'ugkthid']; var groupAtt = ['name', 'identifier']; diff --git a/routes/index.js b/routes/index.js index 6eb39ce..a153cfb 100644 --- a/routes/index.js +++ b/routes/index.js @@ -10,14 +10,17 @@ router.get('/', function(req, res) { Promise.all([ helpers.rolesFindAllCurrent(), helpers.isadmin(req.user), + helpers.issearch(req.user), ]).then(function(results) { var rolemandates = results[0]; console.log(rolemandates); var isadmin = results[1]; + var issearch = results[2]; res.render('index', { user: req.user, - isadmin: isadmin, - rolemandates: rolemandates, + isadmin, + issearch, + rolemandates, }); }).catch(function(e) { console.log(e); @@ -36,14 +39,17 @@ router.get('/user/:kthid', function(req, res) { order: 'start DESC' }), helpers.isadmin(req.user), + helpers.issearch(req.user), ]).then(function(results) { var mandates = results[0]; var isadmin = results[1]; + var issearch = results[2]; res.render('user', { user: req.user, userobj: user, - isadmin: isadmin, - mandates: mandates, + isadmin, + mandates, + issearch, }); }).catch(function(e) { console.log(e); @@ -92,11 +98,12 @@ function respondPositionWithRole(role, req, res) { order: 'start DESC' }); - return Promise.all([ mandatesWithRoleIdP, helpers.isadmin(req.user), models.Group.findAll({}) ]) - .spread(function (mandates, isadmin, groups) { + return Promise.all([ mandatesWithRoleIdP, helpers.isadmin(req.user), helpers.issearch(req.user), models.Group.findAll({}) ]) + .spread(function (mandates, isadmin, issearch, groups) { res.render( 'position', { user: req.user, isadmin, + issearch, roleobj: role, mandates, groups, @@ -121,6 +128,7 @@ router.get('/admin', helpers.requireadmin, function(req, res) { models.Mandate.findAll({include: [{model: models.User, as: "User"}, {model: models.Role, as: "Role"}]}), helpers.isadmin(req.user), + helpers.issearch(req.user), models.Group.findAll({}), models.User.findAll({ order: 'last_name', @@ -131,16 +139,18 @@ router.get('/admin', helpers.requireadmin, function(req, res) { var roles = results[1]; var mandates = results[2]; var isadmin = results[3]; - var groups = results[4]; - var admins = results[5]; + var issearch = results[4]; + var groups = results[5]; + var admins = results[6]; res.render('admin', { user: req.user, - isadmin: isadmin, - users: users, - roles: roles, - mandates: mandates, - groups: groups, - admins: admins, + isadmin, + issearch, + users, + roles, + mandates, + groups, + admins, }); }); }); diff --git a/routes/kthpeople.js b/routes/kthpeople.js index 696df81..60497d8 100644 --- a/routes/kthpeople.js +++ b/routes/kthpeople.js @@ -9,11 +9,14 @@ var router = express.Router(); router.get('/', function(req, res) { Promise.all([ helpers.isadmin(req.user), + helpers.issearch(req.user), ]).then(function(results) { var isadmin = results[0]; + var issearch = results[1]; res.render('kthsearch', { user: req.user, - isadmin: isadmin, + isadmin, + issearch, }); }); }); diff --git a/views/sidebar.pug b/views/sidebar.pug index c897d79..3d292d7 100644 --- a/views/sidebar.pug +++ b/views/sidebar.pug @@ -1,7 +1,7 @@ div(class='col-sm-4 col-md-3') div(id='secondary-nav') h3 - #user + #user if user p Logged in as #{user} else @@ -9,10 +9,9 @@ div(class='col-sm-4 col-md-3') ul li a(href='/') Hem - li - a(href='/kthpeople') Användar-lookup - #isadmin - if isadmin - ul + if issearch + li + a(href='/kthpeople') Användarsökningsfunktion + if isadmin li a(href='/admin') Adminstrativt