szurubooru/client/js/controllers/users_controller.js

205 lines
7.1 KiB
JavaScript
Raw Normal View History

'use strict';
const page = require('page');
const api = require('../api.js');
const config = require('../config.js');
2016-04-07 19:03:49 +02:00
const events = require('../events.js');
const misc = require('../util/misc.js');
const views = require('../util/views.js');
const topNavController = require('../controllers/top_nav_controller.js');
const RegistrationView = require('../views/registration_view.js');
const UserView = require('../views/user_view.js');
class UsersController {
constructor() {
this.registrationView = new RegistrationView();
this.userView = new UserView();
}
registerRoutes() {
page('/register', () => { this.createUserRoute(); });
page('/users', () => { this.listUsersRoute(); });
page(
'/user/:name',
2016-04-06 22:34:21 +02:00
(ctx, next) => { this.loadUserRoute(ctx, next); },
(ctx, next) => { this.showUserRoute(ctx, next); });
page(
'/user/:name/edit',
2016-04-06 22:34:21 +02:00
(ctx, next) => { this.loadUserRoute(ctx, next); },
(ctx, next) => { this.editUserRoute(ctx, next); });
2016-04-09 09:52:00 +02:00
page(
'/user/:name/delete',
(ctx, next) => { this.loadUserRoute(ctx, next); },
(ctx, next) => { this.deleteUserRoute(ctx, next); });
2016-04-06 22:34:21 +02:00
page.exit('/user/', (ctx, next) => { this.user = null; });
}
listUsersRoute() {
topNavController.activate('users');
}
createUserRoute() {
topNavController.activate('register');
this.registrationView.render({
register: (...args) => {
return this._register(...args);
}});
}
loadUserRoute(ctx, next) {
if (ctx.state.user) {
next();
} else if (this.user && this.user.name == ctx.params.name) {
ctx.state.user = this.user;
next();
} else {
api.get('/user/' + ctx.params.name).then(response => {
ctx.state.user = response.user;
ctx.save();
this.user = response.user;
next();
}).catch(response => {
views.emptyView(document.getElementById('content-holder'));
events.notify(events.Error, response.description);
});
}
}
2016-04-09 09:52:00 +02:00
showUserRoute(ctx, next) {
this._show(ctx.state.user, 'summary');
}
editUserRoute(ctx, next) {
this._show(ctx.state.user, 'edit');
}
deleteUserRoute(ctx, next) {
this._show(ctx.state.user, 'delete');
}
_register(name, password, email) {
const data = {
name: name,
password: password,
email: email
};
return new Promise((resolve, reject) => {
api.post('/users/', data).then(() => {
api.forget();
api.login(name, password, false).then(() => {
resolve();
page('/');
2016-04-07 19:03:49 +02:00
events.notify(events.Success, 'Welcome aboard!');
}).catch(errorMessage => {
reject();
events.notify(events.Error, errorMessage);
});
}).catch(response => {
reject();
events.notify(events.Error, response.description);
});
});
}
_edit(user, newName, newPassword, newEmail, newRank) {
const data = {};
if (newName) { data.name = newName; }
if (newPassword) { data.password = newPassword; }
if (newEmail) { data.email = newEmail; }
if (newRank) { data.rank = newRank; }
/* TODO: avatar */
const isLoggedIn = api.isLoggedIn() && api.user.id == user.id;
return new Promise((resolve, reject) => {
api.put('/user/' + user.name, data)
.then(response => {
const next = () => {
resolve();
page('/user/' + newName + '/edit');
events.notify(events.Success, 'Settings updated');
};
if (isLoggedIn) {
api.login(
newName,
newPassword || api.userPassword,
false)
.then(next)
.catch(errorMessage => {
reject();
events.notify(events.Error, errorMessage);
});
} else {
next();
}
}).catch(response => {
reject();
events.notify(events.Error, response.description);
});
});
2016-04-06 22:34:21 +02:00
}
2016-04-09 09:52:00 +02:00
_delete(user) {
const isLoggedIn = api.isLoggedIn() && api.user.id == user.id;
return new Promise((resolve, reject) => {
api.delete('/user/' + user.name)
.then(response => {
if (isLoggedIn) {
api.forget();
2016-04-09 09:52:00 +02:00
api.logout();
}
resolve();
if (api.hasPrivilege('users:list')) {
page('/users');
} else {
page('/');
}
events.notify(events.Success, 'Account deleted');
}).catch(response => {
reject();
events.notify(events.Error, response.description);
});
});
}
2016-04-06 22:34:21 +02:00
_show(user, section) {
const isLoggedIn = api.isLoggedIn() && api.user.id == user.id;
const infix = isLoggedIn ? 'self' : 'any';
const myRankIdx = api.user ? config.ranks.indexOf(api.user.rank) : 0;
const rankNames = Object.values(config.rankNames);
let ranks = {};
for (let rankIdx of misc.range(config.ranks.length)) {
const rankIdentifier = config.ranks[rankIdx];
if (rankIdentifier === 'anonymous') {
continue;
}
if (rankIdx > myRankIdx) {
continue;
}
ranks[rankIdentifier] = rankNames[rankIdx];
}
if (isLoggedIn) {
topNavController.activate('account');
} else {
topNavController.activate('users');
}
2016-04-06 22:34:21 +02:00
this.userView.render({
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),
2016-04-09 09:52:00 +02:00
canDelete: api.hasPrivilege('users:delete:' + infix),
ranks: ranks,
edit: (...args) => { return this._edit(user, ...args); },
2016-04-09 09:52:00 +02:00
delete: (...args) => { return this._delete(user, ...args); },
});
}
}
module.exports = new UsersController();