summaryrefslogtreecommitdiff
path: root/client/js/controllers/user_controller.js
diff options
context:
space:
mode:
Diffstat (limited to 'client/js/controllers/user_controller.js')
-rw-r--r--client/js/controllers/user_controller.js357
1 files changed, 217 insertions, 140 deletions
diff --git a/client/js/controllers/user_controller.js b/client/js/controllers/user_controller.js
index 48989af..326736b 100644
--- a/client/js/controllers/user_controller.js
+++ b/client/js/controllers/user_controller.js
@@ -1,23 +1,25 @@
-'use strict';
+"use strict";
-const router = require('../router.js');
-const api = require('../api.js');
-const uri = require('../util/uri.js');
-const misc = require('../util/misc.js');
-const views = require('../util/views.js');
-const User = require('../models/user.js');
-const UserToken = require('../models/user_token.js');
-const topNavigation = require('../models/top_navigation.js');
-const UserView = require('../views/user_view.js');
-const EmptyView = require('../views/empty_view.js');
+const router = require("../router.js");
+const api = require("../api.js");
+const uri = require("../util/uri.js");
+const misc = require("../util/misc.js");
+const views = require("../util/views.js");
+const User = require("../models/user.js");
+const UserToken = require("../models/user_token.js");
+const topNavigation = require("../models/top_navigation.js");
+const UserView = require("../views/user_view.js");
+const EmptyView = require("../views/empty_view.js");
class UserController {
constructor(ctx, section) {
const userName = ctx.parameters.name;
- if (!api.hasPrivilege('users:view') &&
- !api.isLoggedIn({name: userName})) {
+ if (
+ !api.hasPrivilege("users:view") &&
+ !api.isLoggedIn({ name: userName })
+ ) {
this._view = new EmptyView();
- this._view.showError('You don\'t have privileges to view users.');
+ this._view.showError("You don't have privileges to view users.");
return;
}
@@ -25,100 +27,129 @@ class UserController {
this._errorMessages = [];
let userTokenPromise = Promise.resolve([]);
- if (section === 'list-tokens') {
- userTokenPromise = UserToken.get(userName)
- .then(userTokens => {
- return userTokens.map(token => {
- token.isCurrentAuthToken = api.isCurrentAuthToken(token);
+ if (section === "list-tokens") {
+ userTokenPromise = UserToken.get(userName).then(
+ (userTokens) => {
+ return userTokens.map((token) => {
+ token.isCurrentAuthToken = api.isCurrentAuthToken(
+ token
+ );
return token;
});
- }, error => {
+ },
+ (error) => {
return [];
- });
+ }
+ );
}
- topNavigation.setTitle('User ' + userName);
- Promise.all([
- userTokenPromise,
- User.get(userName)
- ]).then(responses => {
- const [userTokens, user] = responses;
- const isLoggedIn = api.isLoggedIn(user);
- const infix = isLoggedIn ? 'self' : 'any';
+ topNavigation.setTitle("User " + userName);
+ Promise.all([userTokenPromise, User.get(userName)]).then(
+ (responses) => {
+ const [userTokens, user] = responses;
+ const isLoggedIn = api.isLoggedIn(user);
+ const infix = isLoggedIn ? "self" : "any";
- this._name = userName;
- user.addEventListener('change', e => this._evtSaved(e, section));
+ this._name = userName;
+ user.addEventListener("change", (e) =>
+ this._evtSaved(e, section)
+ );
- const myRankIndex = api.user ?
- api.allRanks.indexOf(api.user.rank) :
- 0;
- let ranks = {};
- for (let [rankIdx, rankIdentifier] of api.allRanks.entries()) {
- if (rankIdentifier === 'anonymous') {
- continue;
+ const myRankIndex = api.user
+ ? api.allRanks.indexOf(api.user.rank)
+ : 0;
+ let ranks = {};
+ for (let [rankIdx, rankIdentifier] of api.allRanks.entries()) {
+ if (rankIdentifier === "anonymous") {
+ continue;
+ }
+ if (rankIdx > myRankIndex) {
+ continue;
+ }
+ ranks[rankIdentifier] = api.rankNames.get(rankIdentifier);
}
- if (rankIdx > myRankIndex) {
- continue;
- }
- ranks[rankIdentifier] = api.rankNames.get(rankIdentifier);
- }
- if (isLoggedIn) {
- topNavigation.activate('account');
- } else {
- topNavigation.activate('users');
- }
+ if (isLoggedIn) {
+ topNavigation.activate("account");
+ } else {
+ topNavigation.activate("users");
+ }
- this._view = new UserView({
- user: user,
- section: section,
- isLoggedIn: isLoggedIn,
- canEditName: api.hasPrivilege(`users:edit:${infix}:name`),
- canEditPassword: api.hasPrivilege(`users:edit:${infix}:pass`),
- canEditEmail: api.hasPrivilege(`users:edit:${infix}:email`),
- canEditRank: api.hasPrivilege(`users:edit:${infix}:rank`),
- canEditAvatar: api.hasPrivilege(`users:edit:${infix}:avatar`),
- canEditAnything: api.hasPrivilege(`users:edit:${infix}`),
- canListTokens: api.hasPrivilege(`userTokens:list:${infix}`),
- canCreateToken: api.hasPrivilege(`userTokens:create:${infix}`),
- canEditToken: api.hasPrivilege(`userTokens:edit:${infix}`),
- canDeleteToken: api.hasPrivilege(`userTokens:delete:${infix}`),
- canDelete: api.hasPrivilege(`users:delete:${infix}`),
- ranks: ranks,
- tokens: userTokens,
- });
- this._view.addEventListener('change', e => this._evtChange(e));
- this._view.addEventListener('submit', e => this._evtUpdate(e));
- this._view.addEventListener('delete', e => this._evtDelete(e));
- this._view.addEventListener('create-token', e => this._evtCreateToken(e));
- this._view.addEventListener('delete-token', e => this._evtDeleteToken(e));
- this._view.addEventListener('update-token', e => this._evtUpdateToken(e));
+ this._view = new UserView({
+ user: user,
+ section: section,
+ isLoggedIn: isLoggedIn,
+ canEditName: api.hasPrivilege(`users:edit:${infix}:name`),
+ canEditPassword: api.hasPrivilege(
+ `users:edit:${infix}:pass`
+ ),
+ canEditEmail: api.hasPrivilege(
+ `users:edit:${infix}:email`
+ ),
+ canEditRank: api.hasPrivilege(`users:edit:${infix}:rank`),
+ canEditAvatar: api.hasPrivilege(
+ `users:edit:${infix}:avatar`
+ ),
+ canEditAnything: api.hasPrivilege(`users:edit:${infix}`),
+ canListTokens: api.hasPrivilege(
+ `userTokens:list:${infix}`
+ ),
+ canCreateToken: api.hasPrivilege(
+ `userTokens:create:${infix}`
+ ),
+ canEditToken: api.hasPrivilege(`userTokens:edit:${infix}`),
+ canDeleteToken: api.hasPrivilege(
+ `userTokens:delete:${infix}`
+ ),
+ canDelete: api.hasPrivilege(`users:delete:${infix}`),
+ ranks: ranks,
+ tokens: userTokens,
+ });
+ this._view.addEventListener("change", (e) =>
+ this._evtChange(e)
+ );
+ this._view.addEventListener("submit", (e) =>
+ this._evtUpdate(e)
+ );
+ this._view.addEventListener("delete", (e) =>
+ this._evtDelete(e)
+ );
+ this._view.addEventListener("create-token", (e) =>
+ this._evtCreateToken(e)
+ );
+ this._view.addEventListener("delete-token", (e) =>
+ this._evtDeleteToken(e)
+ );
+ this._view.addEventListener("update-token", (e) =>
+ this._evtUpdateToken(e)
+ );
- for (let message of this._successMessages) {
- this.showSuccess(message);
- }
+ for (let message of this._successMessages) {
+ this.showSuccess(message);
+ }
- for (let message of this._errorMessages) {
- this.showError(message);
+ for (let message of this._errorMessages) {
+ this.showError(message);
+ }
+ },
+ (error) => {
+ this._view = new EmptyView();
+ this._view.showError(error.message);
}
-
- }, error => {
- this._view = new EmptyView();
- this._view.showError(error.message);
- });
+ );
}
showSuccess(message) {
- if (typeof this._view === 'undefined') {
- this._successMessages.push(message)
+ if (typeof this._view === "undefined") {
+ this._successMessages.push(message);
} else {
this._view.showSuccess(message);
}
}
showError(message) {
- if (typeof this._view === 'undefined') {
- this._errorMessages.push(message)
+ if (typeof this._view === "undefined") {
+ this._errorMessages.push(message);
} else {
this._view.showError(message);
}
@@ -132,8 +163,10 @@ class UserController {
misc.disableExitConfirmation();
if (this._name !== e.detail.user.name) {
router.replace(
- uri.formatClientLink('user', e.detail.user.name, section),
- null, false);
+ uri.formatClientLink("user", e.detail.user.name, section),
+ null,
+ false
+ );
}
}
@@ -141,7 +174,7 @@ class UserController {
this._view.clearMessages();
this._view.disableForm();
const isLoggedIn = api.isLoggedIn(e.detail.user);
- const infix = isLoggedIn ? 'self' : 'any';
+ const infix = isLoggedIn ? "self" : "any";
if (e.detail.name !== undefined) {
e.detail.user.name = e.detail.name;
@@ -164,72 +197,105 @@ class UserController {
}
}
- e.detail.user.save().then(() => {
- return isLoggedIn ?
- api.login(
- e.detail.name || api.userName,
- e.detail.password || api.userPassword,
- false) :
- Promise.resolve();
- }).then(() => {
- this._view.showSuccess('Settings updated.');
- this._view.enableForm();
- }, error => {
- this._view.showError(error.message);
- this._view.enableForm();
- });
+ e.detail.user
+ .save()
+ .then(() => {
+ return isLoggedIn
+ ? api.login(
+ e.detail.name || api.userName,
+ e.detail.password || api.userPassword,
+ false
+ )
+ : Promise.resolve();
+ })
+ .then(
+ () => {
+ this._view.showSuccess("Settings updated.");
+ this._view.enableForm();
+ },
+ (error) => {
+ this._view.showError(error.message);
+ this._view.enableForm();
+ }
+ );
}
_evtDelete(e) {
this._view.clearMessages();
this._view.disableForm();
const isLoggedIn = api.isLoggedIn(e.detail.user);
- e.detail.user.delete()
- .then(() => {
+ e.detail.user.delete().then(
+ () => {
if (isLoggedIn) {
api.forget();
api.logout();
}
- if (api.hasPrivilege('users:list')) {
- const ctx = router.show(uri.formatClientLink('users'));
- ctx.controller.showSuccess('Account deleted.');
+ if (api.hasPrivilege("users:list")) {
+ const ctx = router.show(uri.formatClientLink("users"));
+ ctx.controller.showSuccess("Account deleted.");
} else {
const ctx = router.show(uri.formatClientLink());
- ctx.controller.showSuccess('Account deleted.');
+ ctx.controller.showSuccess("Account deleted.");
}
- }, error => {
+ },
+ (error) => {
this._view.showError(error.message);
this._view.enableForm();
- });
+ }
+ );
}
_evtCreateToken(e) {
this._view.clearMessages();
this._view.disableForm();
- UserToken.create(e.detail.user.name, e.detail.note, e.detail.expirationTime)
- .then(response => {
- const ctx = router.show(uri.formatClientLink('user', e.detail.user.name, 'list-tokens'));
- ctx.controller.showSuccess('Token ' + response.token + ' created.');
- }, error => {
+ UserToken.create(
+ e.detail.user.name,
+ e.detail.note,
+ e.detail.expirationTime
+ ).then(
+ (response) => {
+ const ctx = router.show(
+ uri.formatClientLink(
+ "user",
+ e.detail.user.name,
+ "list-tokens"
+ )
+ );
+ ctx.controller.showSuccess(
+ "Token " + response.token + " created."
+ );
+ },
+ (error) => {
this._view.showError(error.message);
this._view.enableForm();
- });
+ }
+ );
}
_evtDeleteToken(e) {
this._view.clearMessages();
this._view.disableForm();
if (api.isCurrentAuthToken(e.detail.userToken)) {
- router.show(uri.formatClientLink('logout'));
+ router.show(uri.formatClientLink("logout"));
} else {
- e.detail.userToken.delete(e.detail.user.name)
- .then(() => {
- const ctx = router.show(uri.formatClientLink('user', e.detail.user.name, 'list-tokens'));
- ctx.controller.showSuccess('Token ' + e.detail.userToken.token + ' deleted.');
- }, error => {
+ e.detail.userToken.delete(e.detail.user.name).then(
+ () => {
+ const ctx = router.show(
+ uri.formatClientLink(
+ "user",
+ e.detail.user.name,
+ "list-tokens"
+ )
+ );
+ ctx.controller.showSuccess(
+ "Token " + e.detail.userToken.token + " deleted."
+ );
+ },
+ (error) => {
this._view.showError(error.message);
this._view.enableForm();
- });
+ }
+ );
}
}
@@ -241,27 +307,38 @@ class UserController {
e.detail.userToken.note = e.detail.note;
}
- e.detail.userToken.save(e.detail.user.name).then(response => {
- const ctx = router.show(uri.formatClientLink('user', e.detail.user.name, 'list-tokens'));
- ctx.controller.showSuccess('Token ' + response.token + ' updated.');
- }, error => {
- this._view.showError(error.message);
- this._view.enableForm();
- });
+ e.detail.userToken.save(e.detail.user.name).then(
+ (response) => {
+ const ctx = router.show(
+ uri.formatClientLink(
+ "user",
+ e.detail.user.name,
+ "list-tokens"
+ )
+ );
+ ctx.controller.showSuccess(
+ "Token " + response.token + " updated."
+ );
+ },
+ (error) => {
+ this._view.showError(error.message);
+ this._view.enableForm();
+ }
+ );
}
}
-module.exports = router => {
- router.enter(['user', ':name'], (ctx, next) => {
- ctx.controller = new UserController(ctx, 'summary');
+module.exports = (router) => {
+ router.enter(["user", ":name"], (ctx, next) => {
+ ctx.controller = new UserController(ctx, "summary");
});
- router.enter(['user', ':name', 'edit'], (ctx, next) => {
- ctx.controller = new UserController(ctx, 'edit');
+ router.enter(["user", ":name", "edit"], (ctx, next) => {
+ ctx.controller = new UserController(ctx, "edit");
});
- router.enter(['user', ':name', 'list-tokens'], (ctx, next) => {
- ctx.controller = new UserController(ctx, 'list-tokens');
+ router.enter(["user", ":name", "list-tokens"], (ctx, next) => {
+ ctx.controller = new UserController(ctx, "list-tokens");
});
- router.enter(['user', ':name', 'delete'], (ctx, next) => {
- ctx.controller = new UserController(ctx, 'delete');
+ router.enter(["user", ":name", "delete"], (ctx, next) => {
+ ctx.controller = new UserController(ctx, "delete");
});
};