Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No QA] Improved permission checking #3321

Merged
merged 2 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/libs/OptionsListUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ function isSearchStringMatch(searchValue, searchText, participantNames) {
* @private
*/
function getOptions(reports, personalDetails, draftComments, activeReportID, {
betas = [],
selectedOptions = [],
maxRecentReportsToShow = 0,
excludeConcierge = false,
Expand Down Expand Up @@ -335,7 +336,7 @@ function getOptions(reports, personalDetails, draftComments, activeReportID, {
&& personalDetailsOptions.length === 0
&& _.every(selectedOptions, option => option.login !== searchValue)
&& ((Str.isValidEmail(searchValue) && !Str.isDomainEmail(searchValue)) || Str.isValidPhone(searchValue))
&& (searchValue !== CONST.EMAIL.CHRONOS || Permissions.canUseChronos())
&& (searchValue !== CONST.EMAIL.CHRONOS || Permissions.canUseChronos(betas))
) {
// If the phone number doesn't have an international code then let's prefix it with the
// current users international code based on their IP address.
Expand All @@ -362,14 +363,17 @@ function getOptions(reports, personalDetails, draftComments, activeReportID, {
* @param {Object} reports
* @param {Object} personalDetails
* @param {String} searchValue
* @param {Array<String>} betas
* @returns {Object}
*/
function getSearchOptions(
reports,
personalDetails,
searchValue = '',
betas,
) {
return getOptions(reports, personalDetails, {}, 0, {
betas,
searchValue,
includeRecentReports: true,
includeMultipleParticipantReports: true,
Expand All @@ -389,15 +393,18 @@ function getSearchOptions(
* @param {Object} personalDetails
* @param {String} searchValue
* @param {Boolean} excludeConcierge
* @param {Array<String>} betas
* @returns {Object}
*/
function getNewChatOptions(
reports,
personalDetails,
searchValue = '',
excludeConcierge,
betas,
) {
return getOptions(reports, personalDetails, {}, 0, {
betas,
searchValue,
includePersonalDetails: true,
includeRecentReports: true,
Expand Down Expand Up @@ -445,6 +452,7 @@ function getIOUConfirmationOptionsFromParticipants(
* @param {String} searchValue
* @param {Array} selectedOptions
* @param {Boolean} excludeConcierge
* @param {Array<String>} betas
* @returns {Object}
*/
function getNewGroupOptions(
Expand All @@ -453,8 +461,10 @@ function getNewGroupOptions(
searchValue = '',
selectedOptions = [],
excludeConcierge,
betas,
) {
return getOptions(reports, personalDetails, {}, 0, {
betas,
searchValue,
selectedOptions,
includeRecentReports: true,
Expand Down
21 changes: 8 additions & 13 deletions src/libs/Permissions.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
import _ from 'underscore';
import Onyx from 'react-native-onyx';
import {isDevelopment} from './Environment/Environment';
import CONST from '../CONST';
import ONYXKEYS from '../ONYXKEYS';

let betas;
Onyx.connect({
key: ONYXKEYS.BETAS,
callback: val => betas = val || [],
});

/**
* @private
* @param {Array<String>} betas
* @returns {Boolean}
*/
function canUseAllBetas() {
function canUseAllBetas(betas) {
return isDevelopment() || _.contains(betas, CONST.BETAS.ALL);
}

/**
* @param {Array<String>} betas
* @returns {Boolean}
*/
function canUseChronos() {
return _.contains(betas, CONST.BETAS.CHRONOS_IN_CASH) || canUseAllBetas();
function canUseChronos(betas) {
return _.contains(betas, CONST.BETAS.CHRONOS_IN_CASH) || canUseAllBetas(betas);
}

/**
* @param {Array<String>} betas
* @returns {Boolean}
*/
function canUseIOU() {
return _.contains(betas, CONST.BETAS.IOU) || canUseAllBetas();
function canUseIOU(betas) {
return _.contains(betas, CONST.BETAS.IOU) || canUseAllBetas(betas);
}

export default {
Expand Down
8 changes: 8 additions & 0 deletions src/pages/NewChatPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const personalDetailsPropTypes = PropTypes.shape({
});

const propTypes = {
/** Beta features list */
betas: PropTypes.arrayOf(PropTypes.string).isRequired,

/** All of the personal details for everyone */
personalDetails: PropTypes.objectOf(personalDetailsPropTypes).isRequired,

Expand Down Expand Up @@ -62,6 +65,7 @@ class NewChatPage extends Component {
props.reports,
props.personalDetails,
'',
props.betas,
);

this.state = {
Expand Down Expand Up @@ -149,6 +153,7 @@ class NewChatPage extends Component {
this.props.reports,
this.props.personalDetails,
searchValue,
this.props.betas,
);
this.setState({
searchValue,
Expand Down Expand Up @@ -187,5 +192,8 @@ export default compose(
session: {
key: ONYXKEYS.SESSION,
},
betas: {
key: ONYXKEYS.BETAS,
},
}),
)(NewChatPage);
12 changes: 12 additions & 0 deletions src/pages/NewGroupPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const personalDetailsPropTypes = PropTypes.shape({
});

const propTypes = {
/** Beta features list */
betas: PropTypes.arrayOf(PropTypes.string).isRequired,

/** All of the personal details for everyone */
personalDetails: PropTypes.objectOf(personalDetailsPropTypes).isRequired,

Expand Down Expand Up @@ -66,6 +69,8 @@ class NewGroupPage extends Component {
props.personalDetails,
'',
[],
false,
props.betas,
);

this.state = {
Expand Down Expand Up @@ -163,6 +168,8 @@ class NewGroupPage extends Component {
this.props.personalDetails,
isOptionInList ? prevState.searchValue : '',
newSelectedOptions,
false,
this.props.betas,
);

return {
Expand Down Expand Up @@ -212,6 +219,8 @@ class NewGroupPage extends Component {
this.props.personalDetails,
searchValue,
[],
false,
this.props.betas,
);
this.setState({
searchValue,
Expand Down Expand Up @@ -262,5 +271,8 @@ export default compose(
session: {
key: ONYXKEYS.SESSION,
},
betas: {
key: ONYXKEYS.BETAS,
},
}),
)(NewGroupPage);
8 changes: 8 additions & 0 deletions src/pages/SearchPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const personalDetailsPropTypes = PropTypes.shape({
const propTypes = {
/* Onyx Props */

/** Beta features list */
betas: PropTypes.arrayOf(PropTypes.string).isRequired,

/** All of the personal details for everyone */
personalDetails: PropTypes.objectOf(personalDetailsPropTypes).isRequired,

Expand Down Expand Up @@ -69,6 +72,7 @@ class SearchPage extends Component {
props.reports,
props.personalDetails,
'',
props.betas,
);

this.state = {
Expand Down Expand Up @@ -163,6 +167,7 @@ class SearchPage extends Component {
this.props.reports,
this.props.personalDetails,
searchValue,
this.props.betas,
);
this.setState({
searchValue,
Expand Down Expand Up @@ -202,5 +207,8 @@ export default compose(
session: {
key: ONYXKEYS.SESSION,
},
betas: {
key: ONYXKEYS.BETAS,
},
}),
)(SearchPage);
51 changes: 31 additions & 20 deletions src/pages/home/report/ReportActionCompose.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ import ReportActionPropTypes from './ReportActionPropTypes';
import {canEditReportAction} from '../../../libs/reportUtils';

const propTypes = {
/** Beta features list */
betas: PropTypes.arrayOf(PropTypes.string).isRequired,

/** A method to call when the form is submitted */
onSubmit: PropTypes.func.isRequired,

Expand Down Expand Up @@ -397,27 +400,32 @@ class ReportActionCompose extends React.Component {
animationIn="fadeInUp"
animationOut="fadeOutDown"
menuItems={[
...(!hasConciergeParticipant && Permissions.canUseIOU() ? [
hasMultipleParticipants
? {
icon: Receipt,
text: this.props.translate('iou.splitBill'),
onSelected: () => {
Navigation.navigate(
ROUTES.getIouSplitRoute(this.props.reportID),
);
},
}
: {
icon: MoneyCircle,
text: this.props.translate('iou.requestMoney'),
onSelected: () => {
Navigation.navigate(
ROUTES.getIouRequestRoute(this.props.reportID),
);
...(!hasConciergeParticipant
&& Permissions.canUseIOU(this.props.betas) ? [
hasMultipleParticipants
? {
icon: Receipt,
text: this.props.translate('iou.splitBill'),
onSelected: () => {
Navigation.navigate(
ROUTES.getIouSplitRoute(
this.props.reportID,
),
);
},
}
: {
icon: MoneyCircle,
text: this.props.translate('iou.requestMoney'),
onSelected: () => {
Navigation.navigate(
ROUTES.getIouRequestRoute(
this.props.reportID,
),
);
},
},
},
] : []),
] : []),
{
icon: Paperclip,
text: this.props.translate('reportActionCompose.addAttachment'),
Expand Down Expand Up @@ -553,6 +561,9 @@ export default compose(
withNavigationFocus,
withLocalize,
withOnyx({
betas: {
key: ONYXKEYS.BETAS,
},
comment: {
key: ({reportID}) => `${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`,
},
Expand Down
15 changes: 13 additions & 2 deletions src/pages/home/sidebar/SidebarScreen.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, {Component} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
import styles from '../../../styles/styles';
import SidebarLinks from './SidebarLinks';
import CreateMenu from '../../../components/CreateMenu';
Expand All @@ -19,8 +21,12 @@ import {
Receipt,
} from '../../../components/Icon/Expensicons';
import Permissions from '../../../libs/Permissions';
import ONYXKEYS from '../../../ONYXKEYS';

const propTypes = {
/** Beta features list */
betas: PropTypes.arrayOf(PropTypes.string).isRequired,

...windowDimensionsPropTypes,

...withLocalizePropTypes,
Expand Down Expand Up @@ -107,7 +113,7 @@ class SidebarScreen extends Component {
text: this.props.translate('sidebarScreen.newChat'),
onSelected: () => Navigation.navigate(ROUTES.NEW_CHAT),
},
...(Permissions.canUseIOU() ? [
...(Permissions.canUseIOU(this.props.betas) ? [
{
icon: MoneyCircle,
text: this.props.translate('iou.requestMoney'),
Expand All @@ -119,7 +125,7 @@ class SidebarScreen extends Component {
text: this.props.translate('sidebarScreen.newGroup'),
onSelected: () => Navigation.navigate(ROUTES.NEW_GROUP),
},
...(Permissions.canUseIOU() ? [
...(Permissions.canUseIOU(this.props.betas) ? [
{
icon: Receipt,
text: this.props.translate('iou.splitBill'),
Expand All @@ -139,4 +145,9 @@ SidebarScreen.propTypes = propTypes;
export default compose(
withLocalize,
withWindowDimensions,
withOnyx({
betas: {
key: ONYXKEYS.BETAS,
},
}),
)(SidebarScreen);