Skip to content

Commit

Permalink
Merge pull request apache#231 from Microsoft/xiongyf/webportal-dev
Browse files Browse the repository at this point in the history
[Webportal] Protect user management
  • Loading branch information
abuccts authored Jan 22, 2018
2 parents 8187c77 + eaabd3f commit c04ca84
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 8 deletions.
5 changes: 3 additions & 2 deletions rest-server/src/controllers/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ const get = (req, res) => {
});
}
return res.status(200).json({
token,
user: username
user: username,
token: token,
admin: admin
});
});
}
Expand Down
4 changes: 4 additions & 0 deletions webportal/config/webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ const config = {
jQuery: 'jquery',
'window.jQuery': 'jquery'
}),
new webpack.ProvidePlugin({
cookies: 'js-cookie',
'window.cookies': 'js-cookie'
}),
new HtmlWebpackPlugin({
title: 'Platform for AI',
filename: 'index.html',
Expand Down
6 changes: 6 additions & 0 deletions webportal/src/app/layout/layout.component.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
<aside class="main-sidebar">
<section class="sidebar">
<ul class="sidebar-menu" data-widget="tree">
<li id="sidebar-menu--user-management">
<a href="/register.html">
<i class="fa fa-user"></i>
<span>User Management</span>
</a>
</li>
<li id="sidebar-menu--submit-job">
<a href="/submit.html">
<i class="fa fa-plus-circle"></i>
Expand Down
8 changes: 5 additions & 3 deletions webportal/src/app/layout/layout.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ require('bootstrap/dist/css/bootstrap.min.css');
require('admin-lte/dist/css/AdminLTE.min.css');
require('admin-lte/dist/css/skins/_all-skins.min.css');
require('font-awesome/css/font-awesome.min.css');
const cookies = require('js-cookie');
const userAuthComponent = require('../user/user-auth/user-auth.component.js');
const userLogoutComponent = require('../user/user-logout/user-logout.component.js');
const userLoginNavComponent = require('../user/user-login/user-login-nav.component.ejs');


const userLoginNavHtml = userLoginNavComponent({ cookies });

window.cookies = cookies;
window.userLogout = userLogoutComponent.userLogout;

$('#navbar').html(userLoginNavHtml);
$('#navbar').html(userLoginNavHtml);
if (!userAuthComponent.checkAdmin()) {
$('#sidebar-menu--user-management').hide();
}
6 changes: 5 additions & 1 deletion webportal/src/app/user/user-auth/user-auth.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ const checkToken = (callback) => {
}
};

module.exports = { checkToken };
const checkAdmin = () => {
return cookies.get('admin') === 'true';
};

module.exports = { checkToken, checkAdmin };
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<span class="hidden-xs">Welcome, <%= cookies.get("user") %></span>
</a>
<ul class="dropdown-menu">
<li><a href="#" onclick="window.location.replace('/register.html')">New user</a></li>
<li><a href="#" onclick="userLogout()">Logout</a></li>
</ul>
</li>
Expand Down
3 changes: 2 additions & 1 deletion webportal/src/app/user/user-login/user-login.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ $(document).ready(() => {
} else {
cookies.set('user', data.user, { expires: expiration });
cookies.set('token', data.token, { expires: expiration });
window.location.replace("/view.html");
cookies.set('admin', data.admin, { expires: expiration });
window.location.replace('/view.html');
}
},
error: (xhr, textStatus, error) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
const userLogout = () => {
cookies.remove('user');
cookies.remove('token');
cookies.remove('admin');
window.location.replace('/login.html');
};

Expand Down

0 comments on commit c04ca84

Please sign in to comment.