Skip to content

Commit

Permalink
MM-12815: Stop using permissions-deprecated config settings in web app (
Browse files Browse the repository at this point in the history
mattermost#1970)

* Remove RestrictTeamInvite.

* Do EnableOnlyAdminIntegrations

* Do EnableTeamCreation.

* Do AllowEditPost.

* Cleanup SelectTeam component use of permissions.
  • Loading branch information
grundleborg committed Oct 31, 2018
1 parent bd3d52b commit 538f41f
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 94 deletions.
24 changes: 10 additions & 14 deletions actions/post_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import {batchActions} from 'redux-batched-actions';

import {PostTypes, SearchTypes} from 'mattermost-redux/action_types';
import {getMyChannelMember} from 'mattermost-redux/actions/channels';
import {getMyChannelMember as getMyChannelMemberSelector} from 'mattermost-redux/selectors/entities/channels';
import {getChannel, getMyChannelMember as getMyChannelMemberSelector} from 'mattermost-redux/selectors/entities/channels';
import * as PostActions from 'mattermost-redux/actions/posts';
import * as PostSelectors from 'mattermost-redux/selectors/entities/posts';
import {comparePosts} from 'mattermost-redux/utils/post_utils';
import {getCurrentUserId} from 'mattermost-redux/selectors/entities/users';
import {canEditPost, comparePosts} from 'mattermost-redux/utils/post_utils';

import {addRecentEmoji} from 'actions/emoji_actions';
import * as StorageActions from 'actions/storage';
Expand Down Expand Up @@ -260,20 +261,15 @@ export function setEditingPost(postId = '', commentCount = 0, refocusId = '', ti
return {data: false};
}

let canEditNow = true;
const config = state.entities.general.config;
const license = state.entities.general.license;
const userId = getCurrentUserId(state);
const channel = getChannel(state, post.channel_id);
const teamId = channel.team_id || '';

const canEditNow = canEditPost(state, config, license, teamId, post.channel_id, userId, post);

// Only show the modal if we can edit the post now, but allow it to be hidden at any time
if (postId && state.entities.general.license.IsLicensed === 'true') {
const config = state.entities.general.config;

if (config.AllowEditPost === Constants.ALLOW_EDIT_POST_NEVER) {
canEditNow = false;
} else if (config.AllowEditPost === Constants.ALLOW_EDIT_POST_TIME_LIMIT) {
if ((post.create_at + (config.PostEditTimeLimit * 1000)) < Date.now()) {
canEditNow = false;
}
}
}

if (canEditNow) {
doDispatch({
Expand Down
2 changes: 0 additions & 2 deletions components/integrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ function mapStateToProps(state) {
const enableOutgoingWebhooks = config.EnableOutgoingWebhooks === 'true';
const enableCommands = config.EnableCommands === 'true';
const enableOAuthServiceProvider = config.EnableOAuthServiceProvider === 'true';
const enableOnlyAdminIntegrations = config.EnableOnlyAdminIntegrations === 'true';

return {
siteName,
enableIncomingWebhooks,
enableOutgoingWebhooks,
enableCommands,
enableOAuthServiceProvider,
enableOnlyAdminIntegrations,
};
}

Expand Down
2 changes: 0 additions & 2 deletions components/integrations/installed_oauth_apps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ import InstalledOAuthApps from './installed_oauth_apps.jsx';
function mapStateToProps(state) {
const config = getConfig(state);
const enableOAuthServiceProvider = config.EnableOAuthServiceProvider === 'true';
const enableOnlyAdminIntegrations = config.EnableOnlyAdminIntegrations === 'true';

return {
canManageOauth: haveISystemPermission(state, {permission: Permissions.MANAGE_OAUTH}),
oauthApps: getOAuthApps(state),
regenOAuthAppSecretRequest: state.requests.integrations.updateOAuthApp,
enableOAuthServiceProvider,
enableOnlyAdminIntegrations,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ export default class InstalledOAuthApps extends React.PureComponent {
* Whether or not OAuth applications are enabled.
*/
enableOAuthServiceProvider: PropTypes.bool,

/**
* Whether or not integration configuration is restricted to admins.
*/
enableOnlyAdminIntegrations: PropTypes.bool,
}
};

constructor(props) {
super(props);
Expand Down
1 change: 0 additions & 1 deletion components/integrations/integrations.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export default class Integrations extends React.Component {
enableOutgoingWebhooks: PropTypes.bool,
enableCommands: PropTypes.bool,
enableOAuthServiceProvider: PropTypes.bool,
enableOnlyAdminIntegrations: PropTypes.bool,
};
}

Expand Down
7 changes: 4 additions & 3 deletions components/select_team/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {withRouter} from 'react-router-dom';
import {getTeams} from 'mattermost-redux/actions/teams';
import {loadRolesIfNeeded} from 'mattermost-redux/actions/roles';
import {getConfig} from 'mattermost-redux/selectors/entities/general';
import {getRoles} from 'mattermost-redux/selectors/entities/roles';
import {Permissions} from 'mattermost-redux/constants';
import {haveISystemPermission} from 'mattermost-redux/selectors/entities/roles';
import {getSortedJoinableTeams, getTeamMemberships} from 'mattermost-redux/selectors/entities/teams';
import {getCurrentUser} from 'mattermost-redux/selectors/entities/users';

Expand All @@ -22,11 +23,11 @@ function mapStateToProps(state) {
return {
currentUserRoles: currentUser.roles || '',
customDescriptionText: config.CustomDescriptionText,
roles: getRoles(state),
enableTeamCreation: config.EnableTeamCreation === 'true',
isMemberOfTeam: myTeamMemberships && myTeamMemberships.length > 0,
joinableTeams: getSortedJoinableTeams(state, currentUser.locale),
siteName: config.SiteName,
canCreateTeams: haveISystemPermission(state, {permission: Permissions.CREATE_TEAM}),
canManageSystem: haveISystemPermission(state, {permission: Permissions.MANAGE_SYSTEM}),
};
}

Expand Down
49 changes: 11 additions & 38 deletions components/select_team/select_team.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import React from 'react';
import {FormattedMessage} from 'react-intl';
import {Link} from 'react-router-dom';

import {General, Permissions} from 'mattermost-redux/constants';
import {Permissions} from 'mattermost-redux/constants';

import {emitUserLoggedOutEvent} from 'actions/global_actions.jsx';
import {addUserToTeamFromInvite} from 'actions/team_actions.jsx';

import {mappingValueFromRoles} from 'utils/policy_roles_adapter';
import * as UserAgent from 'utils/user_agent.jsx';
import * as Utils from 'utils/utils.jsx';

Expand All @@ -33,13 +32,14 @@ export default class SelectTeam extends React.Component {
customDescriptionText: PropTypes.string,
isMemberOfTeam: PropTypes.bool.isRequired,
joinableTeams: PropTypes.array,
roles: PropTypes.object.isRequired,
siteName: PropTypes.string,
canCreateTeams: PropTypes.bool.isRequired,
canManageSystem: PropTypes.bool.isRequired,
actions: PropTypes.shape({
getTeams: PropTypes.func.isRequired,
loadRolesIfNeeded: PropTypes.func.isRequired,
}).isRequired,
}
};

constructor(props) {
super(props);
Expand All @@ -57,35 +57,10 @@ export default class SelectTeam extends React.Component {
UNSAFE_componentWillMount() { // eslint-disable-line camelcase
const {
actions,
roles,
currentUserRoles,
} = this.props;

actions.loadRolesIfNeeded([General.SYSTEM_ADMIN_ROLE, General.SYSTEM_USER_ROLE]);

if (
roles.system_admin &&
roles.system_user
) {
this.loadPoliciesIntoState(roles);
}
}

UNSAFE_componentWillReceiveProps(nextProps) { // eslint-disable-line camelcase
if (
!this.state.loaded &&
nextProps.roles.system_admin &&
nextProps.roles.system_user
) {
this.loadPoliciesIntoState(nextProps.roles);
}
}

loadPoliciesIntoState = (roles) => {
// Purposely parsing boolean from string 'true' or 'false'
// because the string comes from the policy roles adapter mapping.
const enableTeamCreation = (mappingValueFromRoles('enableTeamCreation', roles) === 'true');

this.setState({enableTeamCreation, loaded: true});
actions.loadRolesIfNeeded(currentUserRoles.split(' '));
}

handleTeamClick = (team) => {
Expand All @@ -107,7 +82,7 @@ export default class SelectTeam extends React.Component {
handleLogoutClick = (e) => {
e.preventDefault();
emitUserLoggedOutEvent('/login');
}
};

clearError = (e) => {
e.preventDefault();
Expand All @@ -119,15 +94,13 @@ export default class SelectTeam extends React.Component {

render() {
const {
currentUserRoles,
canManageSystem,
customDescriptionText,
isMemberOfTeam,
joinableTeams,
siteName,
canCreateTeams,
} = this.props;
const {enableTeamCreation} = this.state;

const isSystemAdmin = Utils.isSystemAdmin(currentUserRoles);

let openContent;
if (this.state.loadingTeamId) {
Expand All @@ -153,7 +126,7 @@ export default class SelectTeam extends React.Component {
);
});

if (openTeamContents.length === 0 && (enableTeamCreation || isSystemAdmin)) {
if (openTeamContents.length === 0 && (canCreateTeams || canManageSystem)) {
openTeamContents = (
<div className='signup-team-dir-err'>
<div>
Expand Down Expand Up @@ -204,7 +177,7 @@ export default class SelectTeam extends React.Component {
}

let teamHelp = null;
if (isSystemAdmin && !enableTeamCreation) {
if (canManageSystem && !canCreateTeams) {
teamHelp = (
<div>
<FormattedMessage
Expand Down
6 changes: 0 additions & 6 deletions components/sidebar/header/dropdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@ function mapStateToProps(state) {
const enableCustomEmoji = config.EnableCustomEmoji === 'true';
const enableIncomingWebhooks = config.EnableIncomingWebhooks === 'true';
const enableOAuthServiceProvider = config.EnableOAuthServiceProvider === 'true';
const enableOnlyAdminIntegrations = config.EnableOnlyAdminIntegrations === 'true';
const enableOutgoingWebhooks = config.EnableOutgoingWebhooks === 'true';
const enableTeamCreation = config.EnableTeamCreation === 'true';
const enableUserCreation = config.EnableUserCreation === 'true';
const enableEmailInvitations = config.EnableEmailInvitations === 'true';
const experimentalPrimaryTeam = config.ExperimentalPrimaryTeam;
const helpLink = config.HelpLink;
const reportAProblemLink = config.ReportAProblemLink;
const restrictTeamInvite = config.RestrictTeamInvite;

let canCreateCustomEmoji = haveISystemPermission(state, {permission: Permissions.MANAGE_EMOJIS});
if (!canCreateCustomEmoji) {
Expand All @@ -54,15 +51,12 @@ function mapStateToProps(state) {
enableCustomEmoji,
enableIncomingWebhooks,
enableOAuthServiceProvider,
enableOnlyAdminIntegrations,
enableOutgoingWebhooks,
enableTeamCreation,
enableUserCreation,
enableEmailInvitations,
experimentalPrimaryTeam,
helpLink,
reportAProblemLink,
restrictTeamInvite,
pluginMenuItems: state.plugins.components.MainMenu,
canCreateCustomEmoji,
moreTeamsToJoin,
Expand Down
4 changes: 0 additions & 4 deletions components/sidebar_right_menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ function mapStateToProps(state) {

const isLicensed = license.IsLicensed === 'true';
const appDownloadLink = config.AppDownloadLink;
const enableTeamCreation = config.EnableTeamCreation === 'true';
const enableUserCreation = config.EnableUserCreation === 'true';
const experimentalPrimaryTeam = config.ExperimentalPrimaryTeam;
const helpLink = config.HelpLink;
const reportAProblemLink = config.ReportAProblemLink;
const restrictTeamInvite = config.RestrictTeamInvite;
const siteName = config.SiteName;

const joinableTeams = getJoinableTeamIds(state);
Expand All @@ -44,12 +42,10 @@ function mapStateToProps(state) {
showTutorialTip: enableTutorial && isMobile() && tutorialStep === TutorialSteps.MENU_POPOVER,
isLicensed,
appDownloadLink,
enableTeamCreation,
enableUserCreation,
experimentalPrimaryTeam,
helpLink,
reportAProblemLink,
restrictTeamInvite,
siteName,
moreTeamsToJoin,
pluginMenuItems: state.plugins.components.MainMenu,
Expand Down
8 changes: 7 additions & 1 deletion components/tutorial/index.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {connect} from 'react-redux';

import {Permissions} from 'mattermost-redux/constants';
import {getChannelsNameMapInCurrentTeam} from 'mattermost-redux/selectors/entities/channels';
import {getLicense, getConfig} from 'mattermost-redux/selectors/entities/general';
import {haveITeamPermission} from 'mattermost-redux/selectors/entities/roles';
import {getCurrentTeam} from 'mattermost-redux/selectors/entities/teams';

import Constants from 'utils/constants.jsx';

Expand All @@ -12,13 +16,15 @@ function mapStateToProps(state) {
const license = getLicense(state);
const config = getConfig(state);

const team = getCurrentTeam(state);

const teamChannels = getChannelsNameMapInCurrentTeam(state);
const townSquare = teamChannels[Constants.DEFAULT_CHANNEL];
const townSquareDisplayName = townSquare ? townSquare.display_name : Constants.DEFAULT_CHANNEL_UI_NAME;

const appDownloadLink = config.AppDownloadLink;
const isLicensed = license.IsLicensed === 'true';
const restrictTeamInvite = config.RestrictTeamInvite;
const restrictTeamInvite = !haveITeamPermission(state, {team: team.id, permission: Permissions.INVITE_USER});
const supportEmail = config.SupportEmail;

return {
Expand Down
2 changes: 0 additions & 2 deletions tests/components/edit_post_modal.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ jest.mock('actions/global_actions.jsx', () => ({
function createEditPost({ctrlSend, config, license, editingPost, actions} = {}) {
const ctrlSendProp = ctrlSend || false;
const configProp = config || {
AllowEditPost: 'always',
PostEditTimeLimit: 300,
EnableEmojiPicker: 'true',
};
Expand Down Expand Up @@ -63,7 +62,6 @@ describe('components/EditPostModal', () => {

it('should match without emoji picker', () => {
const config = {
AllowEditPost: 'always',
PostEditTimeLimit: 300,
EnableEmojiPicker: 'false',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ describe('components/integrations/InstalledOAuthApps', () => {
deleteOAuthApp: jest.fn(),
},
enableOAuthServiceProvider: true,
enableOnlyAdminIntegrations: true,
};

test('should match snapshot', () => {
Expand Down
10 changes: 4 additions & 6 deletions tests/components/select_team/select_team.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import React from 'react';
import {shallow} from 'enzyme';

import {General} from 'mattermost-redux/constants';

import SelectTeam from 'components/select_team/select_team.jsx';

import {emitUserLoggedOutEvent} from 'actions/global_actions.jsx';
Expand All @@ -26,14 +24,14 @@ jest.mock('utils/policy_roles_adapter', () => ({
describe('components/select_team/SelectTeam', () => {
const baseProps = {
currentUserRoles: 'system_admin',
enableTeamCreation: true,
isMemberOfTeam: true,
joinableTeams: [
{id: 'team_id_1', delete_at: 0, name: 'team-a', display_name: 'Team A'},
{id: 'team_id_2', delete_at: 0, name: 'b-team', display_name: 'B Team'},
],
roles: {system_admin: {name: 'system_admin'}, system_user: {name: 'system_user'}},
siteName: 'Mattermost',
canCreateTeams: false,
canManageSystem: true,
actions: {
getTeams: jest.fn(),
loadRolesIfNeeded: jest.fn(),
Expand All @@ -46,7 +44,7 @@ describe('components/select_team/SelectTeam', () => {
expect(wrapper).toMatchSnapshot();

// on componentWillMount
expect(props.actions.loadRolesIfNeeded).toHaveBeenCalledWith([General.SYSTEM_ADMIN_ROLE, General.SYSTEM_USER_ROLE]);
expect(props.actions.loadRolesIfNeeded).toHaveBeenCalledWith(baseProps.currentUserRoles.split(' '));

// on componentDidMount
expect(props.actions.getTeams).toHaveBeenCalledTimes(1);
Expand All @@ -72,7 +70,7 @@ describe('components/select_team/SelectTeam', () => {
});

test('should match snapshot, on no joinable team and is not system admin nor can create team', () => {
const props = {...baseProps, joinableTeams: [], currentUserRoles: '', enableTeamCreation: false};
const props = {...baseProps, joinableTeams: [], currentUserRoles: '', canManageSystem: false, canCreateTeams: false};
const wrapper = shallow(<SelectTeam {...props}/>);
expect(wrapper).toMatchSnapshot();
});
Expand Down
2 changes: 0 additions & 2 deletions tests/plugins/main_menu_action.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ describe('plugins/MainMenuActions', () => {
enableIncomingWebhooks: true,
enableOutgoingWebhooks: true,
enableOAuthServiceProvider: true,
enableOnlyAdminIntegrations: false,
enableTeamCreation: true,
enableUserCreation: true,
enableEmailInvitations: false,
showDropdown: true,
Expand Down
Loading

0 comments on commit 538f41f

Please sign in to comment.