Skip to content

Commit

Permalink
Restrict self requests & naming convention changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Santhosh-Sellavel committed Jul 15, 2021
1 parent 57f557c commit 4dafbb1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
40 changes: 39 additions & 1 deletion src/libs/OptionsListUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -685,4 +722,5 @@ export {
getIOUConfirmationOptionsFromParticipants,
getDefaultAvatar,
getReportIcons,
isCurrentUser,
};
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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],
Expand Down

0 comments on commit 4dafbb1

Please sign in to comment.