client/auth: show errors early in controllers

In other words, verify the privileges client-side before issuing an
request to the server. This commit focuses on routing (e.g. clicking a
link while not logged in), rather than DOM element visibility that
should be already taken care of.
This commit is contained in:
rr- 2016-08-23 21:18:03 +02:00
parent 803a1350fa
commit 08c6c2c145
11 changed files with 81 additions and 3 deletions

View File

@ -6,11 +6,19 @@ const PostList = require('../models/post_list.js');
const topNavigation = require('../models/top_navigation.js'); const topNavigation = require('../models/top_navigation.js');
const PageController = require('../controllers/page_controller.js'); const PageController = require('../controllers/page_controller.js');
const CommentsPageView = require('../views/comments_page_view.js'); const CommentsPageView = require('../views/comments_page_view.js');
const EmptyView = require('../views/empty_view.js');
const fields = ['id', 'comments', 'commentCount', 'thumbnailUrl']; const fields = ['id', 'comments', 'commentCount', 'thumbnailUrl'];
class CommentsController { class CommentsController {
constructor(ctx) { constructor(ctx) {
if (!api.hasPrivilege('comments:list')) {
this._view = new EmptyView();
this._view.showError(
'You don\'t have privileges to view comments.');
return;
}
topNavigation.activate('comments'); topNavigation.activate('comments');
topNavigation.setTitle('Listing comments'); topNavigation.setTitle('Listing comments');

View File

@ -13,6 +13,12 @@ const EmptyView = require('../views/empty_view.js');
class PostController { class PostController {
constructor(id, editMode, ctx) { constructor(id, editMode, ctx) {
if (!api.hasPrivilege('posts:view')) {
this._view = new EmptyView();
this._view.showError('You don\'t have privileges to view posts.');
return;
}
topNavigation.activate('posts'); topNavigation.activate('posts');
topNavigation.setTitle('Post #' + id.toString()); topNavigation.setTitle('Post #' + id.toString());

View File

@ -8,6 +8,7 @@ const topNavigation = require('../models/top_navigation.js');
const PageController = require('../controllers/page_controller.js'); const PageController = require('../controllers/page_controller.js');
const PostsHeaderView = require('../views/posts_header_view.js'); const PostsHeaderView = require('../views/posts_header_view.js');
const PostsPageView = require('../views/posts_page_view.js'); const PostsPageView = require('../views/posts_page_view.js');
const EmptyView = require('../views/empty_view.js');
const fields = [ const fields = [
'id', 'thumbnailUrl', 'type', 'id', 'thumbnailUrl', 'type',
@ -15,6 +16,12 @@ const fields = [
class PostListController { class PostListController {
constructor(ctx) { constructor(ctx) {
if (!api.hasPrivilege('posts:list')) {
this._view = new EmptyView();
this._view.showError('You don\'t have privileges to view posts.');
return;
}
topNavigation.activate('posts'); topNavigation.activate('posts');
topNavigation.setTitle('Listing posts'); topNavigation.setTitle('Listing posts');

View File

@ -1,13 +1,21 @@
'use strict'; 'use strict';
const api = require('../api.js');
const router = require('../router.js'); const router = require('../router.js');
const misc = require('../util/misc.js'); const misc = require('../util/misc.js');
const topNavigation = require('../models/top_navigation.js'); const topNavigation = require('../models/top_navigation.js');
const Post = require('../models/post.js'); const Post = require('../models/post.js');
const PostUploadView = require('../views/post_upload_view.js'); const PostUploadView = require('../views/post_upload_view.js');
const EmptyView = require('../views/empty_view.js');
class PostUploadController { class PostUploadController {
constructor() { constructor() {
if (!api.hasPrivilege('posts:create')) {
this._view = new EmptyView();
this._view.showError('You don\'t have privileges to upload posts.');
return;
}
topNavigation.activate('upload'); topNavigation.activate('upload');
topNavigation.setTitle('Upload'); topNavigation.setTitle('Upload');
this._view = new PostUploadView(); this._view = new PostUploadView();

View File

@ -6,9 +6,16 @@ const SnapshotList = require('../models/snapshot_list.js');
const PageController = require('../controllers/page_controller.js'); const PageController = require('../controllers/page_controller.js');
const topNavigation = require('../models/top_navigation.js'); const topNavigation = require('../models/top_navigation.js');
const SnapshotsPageView = require('../views/snapshots_page_view.js'); const SnapshotsPageView = require('../views/snapshots_page_view.js');
const EmptyView = require('../views/empty_view.js');
class SnapshotsController { class SnapshotsController {
constructor(ctx) { constructor(ctx) {
if (!api.hasPrivilege('snapshots:list')) {
this._view = new EmptyView();
this._view.showError('You don\'t have privileges to view history.');
return;
}
topNavigation.activate(''); topNavigation.activate('');
topNavigation.setTitle('History'); topNavigation.setTitle('History');

View File

@ -9,6 +9,13 @@ const EmptyView = require('../views/empty_view.js');
class TagCategoriesController { class TagCategoriesController {
constructor() { constructor() {
if (!api.hasPrivilege('tagCategories:list')) {
this._view = new EmptyView();
this._view.showError(
'You don\'t have privileges to view tag categories.');
return;
}
topNavigation.activate('tags'); topNavigation.activate('tags');
topNavigation.setTitle('Listing tags'); topNavigation.setTitle('Listing tags');
TagCategoryList.get().then(response => { TagCategoryList.get().then(response => {

View File

@ -11,6 +11,12 @@ const EmptyView = require('../views/empty_view.js');
class TagController { class TagController {
constructor(ctx, section) { constructor(ctx, section) {
if (!api.hasPrivilege('tags:view')) {
this._view = new EmptyView();
this._view.showError('You don\'t have privileges to view tags.');
return;
}
Tag.get(ctx.parameters.name).then(tag => { Tag.get(ctx.parameters.name).then(tag => {
topNavigation.activate('tags'); topNavigation.activate('tags');
topNavigation.setTitle('Tag #' + tag.names[0]); topNavigation.setTitle('Tag #' + tag.names[0]);

View File

@ -7,12 +7,19 @@ const topNavigation = require('../models/top_navigation.js');
const PageController = require('../controllers/page_controller.js'); const PageController = require('../controllers/page_controller.js');
const TagsHeaderView = require('../views/tags_header_view.js'); const TagsHeaderView = require('../views/tags_header_view.js');
const TagsPageView = require('../views/tags_page_view.js'); const TagsPageView = require('../views/tags_page_view.js');
const EmptyView = require('../views/empty_view.js');
const fields = [ const fields = [
'names', 'suggestions', 'implications', 'lastEditTime', 'usages']; 'names', 'suggestions', 'implications', 'lastEditTime', 'usages'];
class TagListController { class TagListController {
constructor(ctx) { constructor(ctx) {
if (!api.hasPrivilege('tags:list')) {
this._view = new EmptyView();
this._view.showError('You don\'t have privileges to view tags.');
return;
}
topNavigation.activate('tags'); topNavigation.activate('tags');
topNavigation.setTitle('Listing tags'); topNavigation.setTitle('Listing tags');

View File

@ -12,12 +12,20 @@ const EmptyView = require('../views/empty_view.js');
class UserController { class UserController {
constructor(ctx, section) { constructor(ctx, section) {
topNavigation.setTitle('User ' + ctx.parameters.name); const userName = ctx.parameters.name;
User.get(ctx.parameters.name).then(user => { if (!api.hasPrivilege('users:view') &&
!api.isLoggedIn({name: userName})) {
this._view = new EmptyView();
this._view.showError('You don\'t have privileges to view users.');
return;
}
topNavigation.setTitle('User ' + userName);
User.get(userName).then(user => {
const isLoggedIn = api.isLoggedIn(user); const isLoggedIn = api.isLoggedIn(user);
const infix = isLoggedIn ? 'self' : 'any'; const infix = isLoggedIn ? 'self' : 'any';
this._name = ctx.parameters.name; this._name = userName;
user.addEventListener('change', e => this._evtSaved(e)); user.addEventListener('change', e => this._evtSaved(e));
const myRankIndex = api.user ? const myRankIndex = api.user ?

View File

@ -7,9 +7,16 @@ const topNavigation = require('../models/top_navigation.js');
const PageController = require('../controllers/page_controller.js'); const PageController = require('../controllers/page_controller.js');
const UsersHeaderView = require('../views/users_header_view.js'); const UsersHeaderView = require('../views/users_header_view.js');
const UsersPageView = require('../views/users_page_view.js'); const UsersPageView = require('../views/users_page_view.js');
const EmptyView = require('../views/empty_view.js');
class UserListController { class UserListController {
constructor(ctx) { constructor(ctx) {
if (!api.hasPrivilege('users:list')) {
this._view = new EmptyView();
this._view.showError('You don\'t have privileges to view users.');
return;
}
topNavigation.activate('users'); topNavigation.activate('users');
topNavigation.setTitle('Listing users'); topNavigation.setTitle('Listing users');

View File

@ -5,9 +5,16 @@ const api = require('../api.js');
const User = require('../models/user.js'); const User = require('../models/user.js');
const topNavigation = require('../models/top_navigation.js'); const topNavigation = require('../models/top_navigation.js');
const RegistrationView = require('../views/registration_view.js'); const RegistrationView = require('../views/registration_view.js');
const EmptyView = require('../views/empty_view.js');
class UserRegistrationController { class UserRegistrationController {
constructor() { constructor() {
if (!api.hasPrivilege('users:create')) {
this._view = new EmptyView();
this._view.showError('Registration is closed.');
return;
}
topNavigation.activate('register'); topNavigation.activate('register');
topNavigation.setTitle('Registration'); topNavigation.setTitle('Registration');
this._view = new RegistrationView(); this._view = new RegistrationView();