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:
parent
803a1350fa
commit
08c6c2c145
11 changed files with 81 additions and 3 deletions
|
@ -6,11 +6,19 @@ const PostList = require('../models/post_list.js');
|
|||
const topNavigation = require('../models/top_navigation.js');
|
||||
const PageController = require('../controllers/page_controller.js');
|
||||
const CommentsPageView = require('../views/comments_page_view.js');
|
||||
const EmptyView = require('../views/empty_view.js');
|
||||
|
||||
const fields = ['id', 'comments', 'commentCount', 'thumbnailUrl'];
|
||||
|
||||
class CommentsController {
|
||||
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.setTitle('Listing comments');
|
||||
|
||||
|
|
|
@ -13,6 +13,12 @@ const EmptyView = require('../views/empty_view.js');
|
|||
|
||||
class PostController {
|
||||
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.setTitle('Post #' + id.toString());
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ const topNavigation = require('../models/top_navigation.js');
|
|||
const PageController = require('../controllers/page_controller.js');
|
||||
const PostsHeaderView = require('../views/posts_header_view.js');
|
||||
const PostsPageView = require('../views/posts_page_view.js');
|
||||
const EmptyView = require('../views/empty_view.js');
|
||||
|
||||
const fields = [
|
||||
'id', 'thumbnailUrl', 'type',
|
||||
|
@ -15,6 +16,12 @@ const fields = [
|
|||
|
||||
class PostListController {
|
||||
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.setTitle('Listing posts');
|
||||
|
||||
|
|
|
@ -1,13 +1,21 @@
|
|||
'use strict';
|
||||
|
||||
const api = require('../api.js');
|
||||
const router = require('../router.js');
|
||||
const misc = require('../util/misc.js');
|
||||
const topNavigation = require('../models/top_navigation.js');
|
||||
const Post = require('../models/post.js');
|
||||
const PostUploadView = require('../views/post_upload_view.js');
|
||||
const EmptyView = require('../views/empty_view.js');
|
||||
|
||||
class PostUploadController {
|
||||
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.setTitle('Upload');
|
||||
this._view = new PostUploadView();
|
||||
|
|
|
@ -6,9 +6,16 @@ const SnapshotList = require('../models/snapshot_list.js');
|
|||
const PageController = require('../controllers/page_controller.js');
|
||||
const topNavigation = require('../models/top_navigation.js');
|
||||
const SnapshotsPageView = require('../views/snapshots_page_view.js');
|
||||
const EmptyView = require('../views/empty_view.js');
|
||||
|
||||
class SnapshotsController {
|
||||
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.setTitle('History');
|
||||
|
||||
|
|
|
@ -9,6 +9,13 @@ const EmptyView = require('../views/empty_view.js');
|
|||
|
||||
class TagCategoriesController {
|
||||
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.setTitle('Listing tags');
|
||||
TagCategoryList.get().then(response => {
|
||||
|
|
|
@ -11,6 +11,12 @@ const EmptyView = require('../views/empty_view.js');
|
|||
|
||||
class TagController {
|
||||
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 => {
|
||||
topNavigation.activate('tags');
|
||||
topNavigation.setTitle('Tag #' + tag.names[0]);
|
||||
|
|
|
@ -7,12 +7,19 @@ const topNavigation = require('../models/top_navigation.js');
|
|||
const PageController = require('../controllers/page_controller.js');
|
||||
const TagsHeaderView = require('../views/tags_header_view.js');
|
||||
const TagsPageView = require('../views/tags_page_view.js');
|
||||
const EmptyView = require('../views/empty_view.js');
|
||||
|
||||
const fields = [
|
||||
'names', 'suggestions', 'implications', 'lastEditTime', 'usages'];
|
||||
|
||||
class TagListController {
|
||||
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.setTitle('Listing tags');
|
||||
|
||||
|
|
|
@ -12,12 +12,20 @@ const EmptyView = require('../views/empty_view.js');
|
|||
|
||||
class UserController {
|
||||
constructor(ctx, section) {
|
||||
topNavigation.setTitle('User ' + ctx.parameters.name);
|
||||
User.get(ctx.parameters.name).then(user => {
|
||||
const userName = ctx.parameters.name;
|
||||
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 infix = isLoggedIn ? 'self' : 'any';
|
||||
|
||||
this._name = ctx.parameters.name;
|
||||
this._name = userName;
|
||||
user.addEventListener('change', e => this._evtSaved(e));
|
||||
|
||||
const myRankIndex = api.user ?
|
||||
|
|
|
@ -7,9 +7,16 @@ const topNavigation = require('../models/top_navigation.js');
|
|||
const PageController = require('../controllers/page_controller.js');
|
||||
const UsersHeaderView = require('../views/users_header_view.js');
|
||||
const UsersPageView = require('../views/users_page_view.js');
|
||||
const EmptyView = require('../views/empty_view.js');
|
||||
|
||||
class UserListController {
|
||||
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.setTitle('Listing users');
|
||||
|
||||
|
|
|
@ -5,9 +5,16 @@ const api = require('../api.js');
|
|||
const User = require('../models/user.js');
|
||||
const topNavigation = require('../models/top_navigation.js');
|
||||
const RegistrationView = require('../views/registration_view.js');
|
||||
const EmptyView = require('../views/empty_view.js');
|
||||
|
||||
class UserRegistrationController {
|
||||
constructor() {
|
||||
if (!api.hasPrivilege('users:create')) {
|
||||
this._view = new EmptyView();
|
||||
this._view.showError('Registration is closed.');
|
||||
return;
|
||||
}
|
||||
|
||||
topNavigation.activate('register');
|
||||
topNavigation.setTitle('Registration');
|
||||
this._view = new RegistrationView();
|
||||
|
|
Loading…
Reference in a new issue