Skip to content

Commit

Permalink
Fix: Apply changed setting permissions reactively
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimpson committed Nov 26, 2017
1 parent 9b71b62 commit 293ad73
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,21 @@ Meteor.methods({
}
});

function notifySettings(type, permission) {
if (permission.level === permissionLevel.SETTING) {
// if the permission changes, the effect on the visible settings depends on the role affected.
// The selected-settings-based consumers have to react accordingly and either add or remove the
// setting from the user's collection
const setting = RocketChat.models.Settings.findOneById(permission.settingId);
RocketChat.Notifications.notifyLoggedInThisInstance('private-settings-changed', 'auth', setting);
}
}

RocketChat.models.Permissions.on('changed', (type, permission) => {
RocketChat.Notifications.notifyLoggedInThisInstance('permissions-changed', type, permission);
if (permission.level === permissionLevel.SETTING) {
RocketChat.Notifications.notifyLoggedInThisInstance('selected-settings-changed', type, permission);
}
notifySettings(type, permission);
});

RocketChat.models.Permissions.on('removed', (type, permission) => {
notifySettings(type, permission);
});
31 changes: 31 additions & 0 deletions packages/rocketchat-ui-admin/client/SettingsCachedCollection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import _ from 'underscore';

export class PrivateSettingsCachedCollection extends RocketChat.CachedCollection {
constructor() {
super({
name: 'private-settings',
eventType: 'onLogged'
});
}

setupListener(eventType, eventName) {
super.setupListener(eventType, eventName);

// private settings also need to listen to a change of authorizationsfor the setting-based authorizations
RocketChat.Notifications[eventType || this.eventType](eventName || this.eventName, (t, record) => {
this.log('record received', t, record);
if (t === 'auth') {
if (! (RocketChat.authz.hasAllPermission([`change-setting-${ record._id }`, 'manage-selected-settings'])
|| RocketChat.authz.hasAllPermission('view-privileged-setting'))) {
this.collection.remove(record._id);
RoomManager.close(record.t + record.name);
} else {
delete record.$loki;
this.collection.upsert({_id: record._id}, _.omit(record, '_id'));
}

this.saveCache();
}
});
}
}
6 changes: 2 additions & 4 deletions packages/rocketchat-ui-admin/client/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import _ from 'underscore';
import s from 'underscore.string';
import toastr from 'toastr';
import {PrivateSettingsCachedCollection} from './SettingsCachedCollection';

const TempSettings = new Mongo.Collection(null);

Expand Down Expand Up @@ -45,10 +46,7 @@ const setFieldValue = function(settingId, value, type, editor) {

Template.admin.onCreated(function() {
if (RocketChat.settings.cachedCollectionPrivate == null) {
RocketChat.settings.cachedCollectionPrivate = new RocketChat.CachedCollection({
name: 'private-settings',
eventType: 'onLogged'
});
RocketChat.settings.cachedCollectionPrivate = new PrivateSettingsCachedCollection();
RocketChat.settings.collectionPrivate = RocketChat.settings.cachedCollectionPrivate.collection;
RocketChat.settings.cachedCollectionPrivate.init();
}
Expand Down
6 changes: 2 additions & 4 deletions packages/rocketchat-ui-admin/client/adminFlex.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import _ from 'underscore';
import s from 'underscore.string';
import {PrivateSettingsCachedCollection} from './SettingsCachedCollection';

Template.adminFlex.onCreated(function() {
this.settingsFilter = new ReactiveVar('');
if (RocketChat.settings.cachedCollectionPrivate == null) {
RocketChat.settings.cachedCollectionPrivate = new RocketChat.CachedCollection({
name: 'private-settings',
eventType: 'onLogged'
});
RocketChat.settings.cachedCollectionPrivate = new PrivateSettingsCachedCollection();
RocketChat.settings.collectionPrivate = RocketChat.settings.cachedCollectionPrivate.collection;
RocketChat.settings.cachedCollectionPrivate.init();
}
Expand Down

0 comments on commit 293ad73

Please sign in to comment.