Skip to content

Commit

Permalink
Unify collections for setting and other permissions again into one
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimpson committed Nov 27, 2017
1 parent 8f59f1c commit 8d923c2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
6 changes: 3 additions & 3 deletions packages/rocketchat-authorization/client/hasPermission.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* globals ChatPermissions, SettingPermissions */
/* globals ChatPermissions */

function atLeastOne(permissions = [], scope) {
return permissions.some((permissionId) => {
const permission = ChatPermissions.findOne(permissionId) || SettingPermissions.findOne(permissionId);
const permission = ChatPermissions.findOne(permissionId);
const roles = (permission && permission.roles) || [];

return roles.some((roleName) => {
Expand All @@ -17,7 +17,7 @@ function atLeastOne(permissions = [], scope) {

function all(permissions = [], scope) {
return permissions.every((permissionId) => {
const permission = ChatPermissions.findOne(permissionId) || SettingPermissions.findOne(permissionId);
const permission = ChatPermissions.findOne(permissionId);
const roles = (permission && permission.roles) || [];

return roles.some((roleName) => {
Expand Down
25 changes: 16 additions & 9 deletions packages/rocketchat-authorization/client/views/permissions.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
/* globals ChatPermissions, SettingPermissions */
/* globals ChatPermissions */
import {permissionLevel} from '../../lib/rocketchat';

const whereNotSetting = {
$where: function() {
return this.level !== permissionLevel.SETTING;
}.toString()
};

Template.permissions.helpers({
roles() {
return Template.instance().roles.get();
},

permissions() {
return ChatPermissions.find({}, {
sort: {
_id: 1
}
});
return ChatPermissions.find(whereNotSetting,
{
sort: {
_id: 1
}
}).fetch().filter((setting)=>!setting.level);
},

settingPermissions() {
return SettingPermissions.find();
return ChatPermissions.find({level: permissionLevel.SETTING});
},

hasPermission() {
Expand Down Expand Up @@ -110,11 +117,11 @@ Template.permissionsTable.onCreated(function() {
}
};
if (this.data.collection === 'Chat') {
ChatPermissions.find().observeChanges(observer);
ChatPermissions.find(whereNotSetting).observeChanges(observer);
}

if (this.data.collection === 'Setting') {
SettingPermissions.find().observeChanges(observer);
ChatPermissions.find({level: permissionLevel.SETTING}).observeChanges(observer);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Meteor.methods({
'permissions/get'(updatedAt) {
this.unblock();

const records = RocketChat.models.Permissions.find({level: {$ne: permissionLevel.SETTING}}).fetch();
const records = RocketChat.models.Permissions.find().fetch();

if (updatedAt instanceof Date) {
return {
Expand Down Expand Up @@ -48,19 +48,15 @@ Meteor.methods({
}
});

function notifyPermissionSettingsChanged(type, permission) {
// 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.Notifications.notifyLoggedInThisInstance('setting-permissions-changed', type, permission);
}

RocketChat.models.Permissions.on('changed', (type, permission) => {
RocketChat.Notifications.notifyLoggedInThisInstance('permissions-changed', type, permission);

if (permission.level && permission.level === permissionLevel.SETTING) {
notifyPermissionSettingsChanged(type, permission);
} else {
RocketChat.Notifications.notifyLoggedInThisInstance('permissions-changed', type, permission);
// 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);
}
});

0 comments on commit 8d923c2

Please sign in to comment.