wekan/client/components/settings/peopleBody.js

224 lines
6.5 KiB
JavaScript
Raw Normal View History

const usersPerPage = 25;
BlazeComponent.extendComponent({
mixins() {
return [Mixins.InfiniteScrolling];
},
onCreated() {
this.error = new ReactiveVar('');
this.loading = new ReactiveVar(false);
this.people = new ReactiveVar(true);
2019-04-19 14:57:52 +02:00
this.findUsersOptions = new ReactiveVar({});
2019-04-19 16:17:17 +02:00
this.number = new ReactiveVar(0);
this.page = new ReactiveVar(1);
this.loadNextPageLocked = false;
this.callFirstWith(null, 'resetNextPeak');
this.autorun(() => {
const limit = this.page.get() * usersPerPage;
this.subscribe('people', limit, () => {
this.loadNextPageLocked = false;
const nextPeakBefore = this.callFirstWith(null, 'getNextPeak');
this.calculateNextPeak();
const nextPeakAfter = this.callFirstWith(null, 'getNextPeak');
if (nextPeakBefore === nextPeakAfter) {
this.callFirstWith(null, 'resetNextPeak');
}
});
});
},
2019-04-19 14:57:52 +02:00
events() {
return [{
2019-04-19 16:39:14 +02:00
'click #searchButton'() {
this.filterPeople();
2019-04-19 16:17:17 +02:00
},
'keydown #searchInput'(event) {
if (event.keyCode === 13 && !event.shiftKey) {
2019-04-19 16:39:14 +02:00
this.filterPeople();
2019-04-19 14:57:52 +02:00
}
2019-04-19 16:39:14 +02:00
},
2019-04-19 14:57:52 +02:00
}];
},
2019-04-19 16:39:14 +02:00
filterPeople() {
2019-04-19 16:17:17 +02:00
const value = $('#searchInput').first().val();
if (value === '') {
this.findUsersOptions.set({});
} else {
const regex = new RegExp(value, 'i');
this.findUsersOptions.set({
$or: [
{ username: regex },
{ 'profile.fullname': regex },
{ 'emails.address': regex },
2019-04-19 16:39:14 +02:00
],
2019-04-19 16:17:17 +02:00
});
}
},
loadNextPage() {
if (this.loadNextPageLocked === false) {
this.page.set(this.page.get() + 1);
this.loadNextPageLocked = true;
}
},
calculateNextPeak() {
const element = this.find('.main-body');
if (element) {
const altitude = element.scrollHeight;
this.callFirstWith(this, 'setNextPeak', altitude);
}
},
reachNextPeak() {
this.loadNextPage();
},
setError(error) {
this.error.set(error);
},
setLoading(w) {
this.loading.set(w);
},
peopleList() {
2019-04-19 16:17:17 +02:00
const users = Users.find(this.findUsersOptions.get(), {
2017-11-07 22:26:21 +07:00
fields: {_id: true},
});
2019-04-19 16:17:17 +02:00
this.number.set(users.count());
return users;
},
2019-04-19 16:17:17 +02:00
peopleNumber() {
return this.number.get();
2019-04-19 16:39:14 +02:00
},
}).register('people');
2017-11-07 22:26:21 +07:00
Template.peopleRow.helpers({
userData() {
const userCollection = this.esSearch ? ESSearchResults : Users;
return userCollection.findOne(this.userId);
},
});
Template.editUserPopup.onCreated(function() {
this.authenticationMethods = new ReactiveVar([]);
Meteor.call('getAuthenticationsEnabled', (_, result) => {
if (result) {
2018-10-09 21:16:47 +03:00
// TODO : add a management of different languages
// (ex {value: ldap, text: TAPi18n.__('ldap', {}, T9n.getLanguage() || 'en')})
this.authenticationMethods.set([
{value: 'password'},
// Gets only the authentication methods availables
2018-10-09 21:16:47 +03:00
...Object.entries(result).filter((e) => e[1]).map((e) => ({value: e[0]})),
]);
}
});
});
2017-11-07 22:26:21 +07:00
Template.editUserPopup.helpers({
user() {
return Users.findOne(this.userId);
},
authentications() {
return Template.instance().authenticationMethods.get();
},
isSelected(match) {
const userId = Template.instance().data.userId;
const selected = Users.findOne(userId).authenticationMethod;
return selected === match;
},
isLdap() {
const userId = Template.instance().data.userId;
const selected = Users.findOne(userId).authenticationMethod;
return selected === 'ldap';
2018-10-09 21:16:47 +03:00
},
2017-11-07 22:26:21 +07:00
});
BlazeComponent.extendComponent({
onCreated() {
},
user() {
return Users.findOne(this.userId);
},
events() {
return [{
'click a.edit-user': Popup.open('editUser'),
}];
},
}).register('peopleRow');
Template.editUserPopup.events({
submit(evt, tpl) {
evt.preventDefault();
const user = Users.findOne(this.userId);
const fullname = tpl.find('.js-profile-fullname').value.trim();
const username = tpl.find('.js-profile-username').value.trim();
const password = tpl.find('.js-profile-password').value;
2017-11-07 22:26:21 +07:00
const isAdmin = tpl.find('.js-profile-isadmin').value.trim();
const isActive = tpl.find('.js-profile-isactive').value.trim();
2017-11-07 22:26:21 +07:00
const email = tpl.find('.js-profile-email').value.trim();
const authentication = tpl.find('.js-authenticationMethod').value.trim();
const isChangePassword = password.length > 0;
const isChangeUserName = username !== user.username;
const isChangeEmail = email.toLowerCase() !== user.emails[0].address.toLowerCase();
2017-11-07 22:26:21 +07:00
Users.update(this.userId, {
$set: {
'profile.fullname': fullname,
'isAdmin': isAdmin === 'true',
'loginDisabled': isActive === 'true',
2018-10-09 21:16:47 +03:00
'authenticationMethod': authentication,
2017-11-07 22:26:21 +07:00
},
});
if(isChangePassword){
Meteor.call('setPassword', password, this.userId);
}
2017-11-07 22:26:21 +07:00
if (isChangeUserName && isChangeEmail) {
Meteor.call('setUsernameAndEmail', username, email.toLowerCase(), this.userId, function (error) {
2017-11-07 22:26:21 +07:00
const usernameMessageElement = tpl.$('.username-taken');
const emailMessageElement = tpl.$('.email-taken');
if (error) {
const errorElement = error.error;
if (errorElement === 'username-already-taken') {
usernameMessageElement.show();
emailMessageElement.hide();
} else if (errorElement === 'email-already-taken') {
usernameMessageElement.hide();
emailMessageElement.show();
}
} else {
usernameMessageElement.hide();
emailMessageElement.hide();
Popup.close();
2017-11-07 22:26:21 +07:00
}
});
} else if (isChangeUserName) {
Meteor.call('setUsername', username, this.userId, function (error) {
const usernameMessageElement = tpl.$('.username-taken');
2017-11-07 22:26:21 +07:00
if (error) {
const errorElement = error.error;
if (errorElement === 'username-already-taken') {
usernameMessageElement.show();
}
2017-11-07 22:26:21 +07:00
} else {
usernameMessageElement.hide();
Popup.close();
2017-11-07 22:26:21 +07:00
}
});
} else if (isChangeEmail) {
Meteor.call('setEmail', email.toLowerCase(), this.userId, function (error) {
const emailMessageElement = tpl.$('.email-taken');
2017-11-07 22:26:21 +07:00
if (error) {
const errorElement = error.error;
if (errorElement === 'email-already-taken') {
emailMessageElement.show();
}
2017-11-07 22:26:21 +07:00
} else {
emailMessageElement.hide();
Popup.close();
2017-11-07 22:26:21 +07:00
}
});
} else Popup.close();
2017-11-07 22:26:21 +07:00
},
});