Skip to content

Commit

Permalink
[FIX] UI was not disabling the actions when users has had no permissi…
Browse files Browse the repository at this point in the history
…ons to create channels or add users to rooms (#10564)

* hide plus icon when user doesn't have both permission for create-c and create-p

* add helper to checkout two permissions set initial value for the room type

* hide the plus icon in directory if user doesn't have both create-c and creat-p permissions

* get permissions for create channels and groups

* check if user can add channel hide and groups, hide button based upon correct state

* prevent add user button from being hidden when user has permission add user to joined room

* removed the if statement and use short hand if else syntax

* better code for disabling checkbox in create room feature if user doesn't have permission

* add missing simicolon

* put canShowAddUsersButton into seperate function call function in events and helpers

* move the canShowAddUsersButton function to define before it's called

* fix bug that prevents the viewing of the keyboard shortcuts button in groups and direct messages

* fix permissions
  • Loading branch information
chuckAtCataworx authored and rodrigok committed May 18, 2018
1 parent 9824687 commit 0f5cffa
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 48 deletions.
2 changes: 1 addition & 1 deletion packages/rocketchat-lib/client/defaultTabBars.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ RocketChat.TabBar.addButton({
});

RocketChat.TabBar.addButton({
groups: ['channel', 'privategroup', 'directmessage'],
groups: ['channel', 'group', 'direct'],
id: 'keyboard-shortcut-list',
i18nTitle: 'Keyboard_Shortcuts_Title',
icon: 'keyboard',
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-ui-flextab/client/flexTabBar.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h1 class="contextual-bar__header-title">{{_ label}}</h1>
<template name="RoomsActionTab">
<div class="rc-room-actions">
{{#each buttons}}
<div class="rc-room-actions__action tab-button {{active}} {{visible}} {{class}} js-action">
<div class="rc-room-actions__action tab-button {{active}} {{visible}} {{class}} js-action" data-id="{{id}}">
<button class="rc-tooltip rc-tooltip--down rc-room-actions__button" aria-label="{{title}}">
{{> icon block="tab-button-icon" icon=icon }}
</button>
Expand Down
81 changes: 44 additions & 37 deletions packages/rocketchat-ui-flextab/client/flexTabBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,51 @@ const commonHelpers = {
}
}
};

function canShowAddUsersButton(rid) {
const canAddToChannel = RocketChat.authz.hasAllPermission(
'add-user-to-any-c-room', rid
);
const canAddToGroup = RocketChat.authz.hasAllPermission(
'add-user-to-any-p-room', rid
);
const canAddToJoinedRoom = RocketChat.authz.hasAllPermission(
'add-user-to-joined-room', rid
);
if (
!canAddToJoinedRoom &&
!canAddToChannel &&
Template.instance().tabBar.currentGroup() === 'channel'
) {
return false;
}
if (
!canAddToJoinedRoom &&
!canAddToGroup &&
Template.instance().tabBar.currentGroup() === 'group'
) {
return false;
}
return true;
}
const filterButtons = (button, anonymous, rid) => {
if (!Meteor.userId() && !anonymous) {
return false;
}
if (button.groups.indexOf(Template.instance().tabBar.currentGroup()) === -1) {
return false;
}
if (button.id === 'addUsers' && !canShowAddUsersButton(rid)) {
return false;
}
return true;
};
Template.flexTabBar.helpers({
headerData() {
return Template.instance().tabBar.getData();
},
...commonHelpers,
buttons() {
return RocketChat.TabBar.getButtons().filter(button => {
if (!Meteor.userId() && !this.anonymous) {
return false;
}
if (button.groups.indexOf(Template.instance().tabBar.currentGroup()) === -1) {
return false;
}
return true;
});
return RocketChat.TabBar.getButtons().filter(button => filterButtons(button, this.anonymous, this.data.rid));
},
opened() {
return Template.instance().tabBar.getState();
Expand Down Expand Up @@ -131,15 +160,7 @@ Template.RoomsActionTab.events({
'click .js-more'(e, instance) {
$(e.currentTarget).blur();
e.preventDefault();
const buttons = RocketChat.TabBar.getButtons().filter(button => {
if (!Meteor.userId() && !this.anonymous) {
return false;
}
if (button.groups.indexOf(Template.instance().tabBar.currentGroup()) === -1) {
return false;
}
return true;
});
const buttons = RocketChat.TabBar.getButtons().filter(button => filterButtons(button, instance.anonymous, instance.data.rid));
const groups = [{items:(instance.small.get() ? buttons : buttons.slice(4)).map(item => {
item.name = TAPi18n.__(item.i18nTitle);
item.action = action;
Expand Down Expand Up @@ -188,31 +209,17 @@ Template.RoomsActionTab.helpers({
if (Template.instance().small.get()) {
return [];
}
const buttons = RocketChat.TabBar.getButtons().filter(button => {
if (!Meteor.userId() && !this.anonymous) {
return false;
}
if (button.groups.indexOf(Template.instance().tabBar.currentGroup()) === -1) {
return false;
}
return true;
});
const buttons = RocketChat.TabBar.getButtons().filter(button => filterButtons(button, this.anonymous, this.data.rid));
return buttons.length <= 5 ? buttons : buttons.slice(0, 4);
},

moreButtons() {
if (Template.instance().small.get()) {
return true;
}
const buttons = RocketChat.TabBar.getButtons().filter(button => {
if (!Meteor.userId() && !this.anonymous) {
return false;
}
if (button.groups.indexOf(Template.instance().tabBar.currentGroup()) === -1) {
return false;
}
return true;
});
const buttons = RocketChat.TabBar.getButtons().filter(button =>
filterButtons(button, this.anonymous, this.data.rid)
);
return buttons.length > 5;
}
});
6 changes: 3 additions & 3 deletions packages/rocketchat-ui/client/views/app/createChannel.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ <h1 class="create-channel__title">{{_ "Create_A_New_Channel"}}</h1>
<div class="create-channel__switches">
<div class="rc-switch">
<label class="rc-switch__label" tabindex="-1">
<input type="checkbox" class="rc-switch__input" name="type" value="p" checked>
<input type="checkbox" class="rc-switch__input" name="type" value="p" checked={{roomTypeIsP}} disabled="{{cantCreateBothTypes}}">
<span class="rc-switch__button">
<span class="rc-switch__button-inside"></span>
</span>
<span class="rc-switch__button-inside"></span>
</span>
<span class="rc-switch__text">
{{typeLabel}}
</span>
Expand Down
8 changes: 7 additions & 1 deletion packages/rocketchat-ui/client/views/app/createChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ Template.createChannel.helpers({
readOnlyDescription() {
return t(Template.instance().readOnly.get() ? t('Only_authorized_users_can_write_new_messages') : t('All_users_in_the_channel_can_write_new_messages'));
},
cantCreateBothTypes() {
return !RocketChat.authz.hasAllPermission(['create-c', 'create-p']);
},
roomTypeIsP() {
return Template.instance().type.get() === 'p';
},
createIsDisabled() {
const instance = Template.instance();
const invalid = instance.invalid.get();
Expand Down Expand Up @@ -258,7 +264,7 @@ Template.createChannel.onCreated(function() {
this.extensions_validations = {};
this.extensions_submits = {};
this.name = new ReactiveVar('');
this.type = new ReactiveVar('p');
this.type = new ReactiveVar(RocketChat.authz.hasAllPermission(['create-p']) ? 'p' : 'c');
this.readOnly = new ReactiveVar(false);
this.broadcast = new ReactiveVar(false);
this.inUse = new ReactiveVar(undefined);
Expand Down
4 changes: 3 additions & 1 deletion packages/rocketchat-ui/client/views/app/directory.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
</div>
</label>
</div>
<button class="rc-button rc-button--small rc-button--primary rc-directory-plus">{{> icon icon="plus" }}</button>
{{#if createChannelOrGroup}}
<button class="rc-button rc-button--small rc-button--primary rc-directory-plus">{{> icon icon="plus" }}</button>
{{/if}}
</div>
{{/header}}
<div class="rc-directory-content">
Expand Down
3 changes: 3 additions & 0 deletions packages/rocketchat-ui/client/views/app/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ Template.directory.helpers({
} = Template.instance();

return key === searchSortBy.get() && sortDirection.get() !== 'asc' ? 'sort-up' : 'sort-down';
},
createChannelOrGroup() {
return RocketChat.authz.hasAtLeastOnePermission(['create-c', 'create-p']);
}
});

Expand Down
33 changes: 33 additions & 0 deletions tests/end-to-end/ui/04-main-elements-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,19 @@ describe('[Main Elements Render]', function() {

describe('Files Tab:', () => {
before(()=> {
if (flexTab.filesTab.isVisible()) {
this.shouldClose = undefined;
return flexTab.filesTab.click();
}
this.shouldClose = true;
flexTab.moreActions.click();
flexTab.operateFlexTab('files', true);
});

after(()=> {
if (!this.shouldClose) {
return;
}
flexTab.moreActions.click();
flexTab.operateFlexTab('files', false);
});
Expand All @@ -292,12 +300,21 @@ describe('[Main Elements Render]', function() {
});

describe('Mentions Tab:', () => {

before(()=> {
if (flexTab.mentionsTab.isVisible()) {
this.shouldClose = undefined;
return flexTab.mentionsTab.click();
}
this.shouldClose = true;
flexTab.moreActions.click();
flexTab.operateFlexTab('mentions', true);
});

after(()=> {
if (!this.shouldClose) {
return;
}
flexTab.moreActions.click();
flexTab.operateFlexTab('mentions', false);
});
Expand All @@ -309,11 +326,19 @@ describe('[Main Elements Render]', function() {

describe('Starred Messages Tab:', () => {
before(()=> {
if (flexTab.starredTab.isVisible()) {
this.shouldClose = undefined;
return flexTab.starredTab.click();
}
this.shouldClose = true;
flexTab.moreActions.click();
flexTab.operateFlexTab('starred', true);
});

after(()=> {
if (!this.shouldClose) {
return;
}
flexTab.moreActions.click();
flexTab.operateFlexTab('starred', false);
});
Expand All @@ -325,11 +350,19 @@ describe('[Main Elements Render]', function() {

describe('Pinned Messages Tab:', () => {
before(()=> {
if (flexTab.pinnedTab.isVisible()) {
this.shouldClose = undefined;
return flexTab.pinnedTab.click();
}
this.shouldClose = true;
flexTab.moreActions.click();
flexTab.operateFlexTab('pinned', true);
});

after(()=> {
if (!this.shouldClose) {
return;
}
flexTab.moreActions.click();
flexTab.operateFlexTab('pinned', false);
});
Expand Down
8 changes: 4 additions & 4 deletions tests/pageobjects/flex-tab.page.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,23 @@ class FlexTab extends Page {
get notificationsSettings() { return browser.element('.push-notifications'); }

// Files Tab
get filesTab() { return browser.element('.rc-popover__item[data-id=uploaded-files-list]'); }
get filesTab() { return browser.element('.rc-popover__item[data-id=uploaded-files-list], .tab-button[data-id=uploaded-files-list]'); }
get fileItem() { return browser.element('.uploaded-files-list ul:first-child'); }
get filesTabContent() { return browser.element('.uploaded-files-list'); }
get fileDelete() { return browser.element('.uploaded-files-list ul:first-child .file-delete'); }
get fileDownload() { return browser.element('.uploaded-files-list ul:first-child .file-download'); }
get fileName() { return browser.element('.uploaded-files-list ul:first-child .room-file-item'); }

// Mentions Tab
get mentionsTab() { return browser.element('.rc-popover__item[data-id=mentions]'); }
get mentionsTab() { return browser.element('.rc-popover__item[data-id=mentions], .tab-button[data-id=mentions]'); }
get mentionsTabContent() { return browser.element('.mentioned-messages-list'); }

// Starred Tab
get starredTab() { return browser.element('.rc-popover__item[data-id=starred-messages]'); }
get starredTab() { return browser.element('.rc-popover__item[data-id=starred-messages], .tab-button[data-id=starred-messages]'); }
get starredTabContent() { return browser.element('.starred-messages-list'); }

// Pinned Tab
get pinnedTab() { return browser.element('.rc-popover__item[data-id=pinned-messages]'); }
get pinnedTab() { return browser.element('.rc-popover__item[data-id=pinned-messages], .tab-button[data-id=pinned-messages]'); }
get pinnedTabContent() { return browser.element('.pinned-messages-list'); }

get firstSetting() { return browser.element('.clearfix li:nth-child(1) .current-setting'); }
Expand Down

0 comments on commit 0f5cffa

Please sign in to comment.