From 4dafbb1ad011fa3204a4bb838cd89b6614deefb8 Mon Sep 17 00:00:00 2001 From: Santhoshkumar Sellavel Date: Thu, 15 Jul 2021 07:54:44 +0530 Subject: [PATCH] Restrict self requests & naming convention changes --- src/libs/OptionsListUtils.js | 40 ++++++++++++++++++- .../IOUParticipantsRequest.js | 4 +- .../IOUParticipantsSplit.js | 4 +- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/libs/OptionsListUtils.js b/src/libs/OptionsListUtils.js index 48c2918c6133..8c7f07c8e0f1 100644 --- a/src/libs/OptionsListUtils.js +++ b/src/libs/OptionsListUtils.js @@ -23,6 +23,12 @@ Onyx.connect({ callback: val => currentUserLogin = val && val.email, }); +let currentUser; +Onyx.connect({ + key: ONYXKEYS.USER, + callback: val => currentUser = val, +}); + let countryCodeByIP; Onyx.connect({ key: ONYXKEYS.COUNTRY_CODE, @@ -217,7 +223,8 @@ function createOption(personalDetailList, report, draftComments, { // It doesn't make sense to provide a login in the case of a report with multiple participants since // there isn't any one single login to refer to for a report. - login: !hasMultipleParticipants ? personalDetail.login : null, + // If single login is a mobile number, appending SMS domain + login: !hasMultipleParticipants ? addSMSDomainIfPhoneNumber(personalDetail.login) : null, reportID: report ? report.reportID : null, isUnread: report ? report.unreadActionCount > 0 : null, hasDraftComment: _.size(reportDraftComment) > 0, @@ -673,6 +680,36 @@ function getReportIcons(report, personalDetails) { .map(item => item.avatar); } +/** + * Returns the given userDetails is currentUser or not. + * @param {Object} userDetails + * @returns {Bool} + */ + +function isCurrentUser(userDetails) { + if (!userDetails) { + // If userDetails is null or undefined + return false; + } + + // If user login is mobile number, append sms domain if not appended already just a fail safe. + const userDetailsLogin = addSMSDomainIfPhoneNumber(userDetails.login); + + // Initial check with currentUserLogin + let result = currentUserLogin.toLowerCase() === userDetailsLogin.toLowerCase(); + const {loginList} = currentUser; + let index = 0; + + // Checking userDetailsLogin against to current user login options. + while (index < loginList.length && !result) { + if (loginList[index].partnerUserID.toLowerCase() === userDetailsLogin.toLowerCase()) { + result = true; + } + index++; + } + return result; +} + export { getSearchOptions, getNewChatOptions, @@ -685,4 +722,5 @@ export { getIOUConfirmationOptionsFromParticipants, getDefaultAvatar, getReportIcons, + isCurrentUser, }; diff --git a/src/pages/iou/steps/IOUParticipantsPage/IOUParticipantsRequest.js b/src/pages/iou/steps/IOUParticipantsPage/IOUParticipantsRequest.js index 2b671c1f0325..6b3ee1b552bd 100755 --- a/src/pages/iou/steps/IOUParticipantsPage/IOUParticipantsRequest.js +++ b/src/pages/iou/steps/IOUParticipantsPage/IOUParticipantsRequest.js @@ -2,7 +2,7 @@ import React, {Component} from 'react'; import PropTypes from 'prop-types'; import {withOnyx} from 'react-native-onyx'; import _ from 'underscore'; -import {getNewChatOptions} from '../../../../libs/OptionsListUtils'; +import {getNewChatOptions, isCurrentUser} from '../../../../libs/OptionsListUtils'; import OptionsSelector from '../../../../components/OptionsSelector'; import ONYXKEYS from '../../../../ONYXKEYS'; import withLocalize, {withLocalizePropTypes} from '../../../../components/withLocalize'; @@ -90,7 +90,7 @@ class IOUParticipantsRequest extends Component { indexOffset: 0, }); - if (this.state.userToInvite) { + if (this.state.userToInvite && !isCurrentUser(this.state.userToInvite)) { sections.push({ undefined, data: [this.state.userToInvite], diff --git a/src/pages/iou/steps/IOUParticipantsPage/IOUParticipantsSplit.js b/src/pages/iou/steps/IOUParticipantsPage/IOUParticipantsSplit.js index b2a312a7a000..6b9c6e75dc46 100755 --- a/src/pages/iou/steps/IOUParticipantsPage/IOUParticipantsSplit.js +++ b/src/pages/iou/steps/IOUParticipantsPage/IOUParticipantsSplit.js @@ -6,7 +6,7 @@ import {withOnyx} from 'react-native-onyx'; import ONYXKEYS from '../../../../ONYXKEYS'; import styles from '../../../../styles/styles'; import OptionsSelector from '../../../../components/OptionsSelector'; -import {getNewGroupOptions} from '../../../../libs/OptionsListUtils'; +import {getNewGroupOptions, isCurrentUser} from '../../../../libs/OptionsListUtils'; import CONST from '../../../../CONST'; import withLocalize, {withLocalizePropTypes} from '../../../../components/withLocalize'; import compose from '../../../../libs/compose'; @@ -133,7 +133,7 @@ class IOUParticipantsSplit extends Component { indexOffset: sections.reduce((prev, {data}) => prev + data.length, 0), }); - if (this.state.userToInvite) { + if (this.state.userToInvite && !isCurrentUser(this.state.userToInvite)) { sections.push(({ undefined, data: [this.state.userToInvite],