FIX: Disable poll options if user groups do not allow them (#8987)

This commit is contained in:
Dan Ungureanu
2020-02-18 12:13:19 +02:00
committed by GitHub
parent 5122826bde
commit 59db2811b0
2 changed files with 157 additions and 13 deletions

View File

@ -39,6 +39,20 @@ function _fetchVoters(data) {
});
}
function checkUserGroups(user, poll) {
const pollGroups =
poll && poll.groups && poll.groups.split(",").map(g => g.toLowerCase());
if (!pollGroups) {
return true;
}
const userGroups =
user && user.groups && user.groups.map(g => g.name.toLowerCase());
return userGroups && pollGroups.some(g => userGroups.includes(g));
}
createWidget("discourse-poll-option", {
tagName: "li",
@ -335,19 +349,7 @@ createWidget("discourse-poll-container", {
} else if (options) {
const contents = [];
const pollGroups =
poll.groups && poll.groups.split(",").map(g => g.toLowerCase());
const userGroups =
this.currentUser &&
this.currentUser.groups &&
this.currentUser.groups.map(g => g.name.toLowerCase());
if (
pollGroups &&
userGroups &&
!pollGroups.some(g => userGroups.includes(g))
) {
if (!checkUserGroups(this.currentUser, poll)) {
contents.push(
h(
"div.alert.alert-danger",
@ -1012,6 +1014,7 @@ export default createWidget("discourse-poll", {
if (this.isClosed()) return;
if (!this.currentUser) return this.showLogin();
if (!checkUserGroups(this.currentUser, this.attrs.poll)) return;
const { vote } = attrs;
if (!this.isMultiple()) {