From 9cf3a63f9cf36e95fdc9f19e34045990c33904ad Mon Sep 17 00:00:00 2001 From: Ramin Date: Mon, 4 Apr 2022 06:43:37 +0430 Subject: [PATCH] F #2586 add sign-in check before all ipfs service usages --- src/components/views/CreateBounty.jsx | 14 ++- src/components/views/CreateExpense.jsx | 6 ++ src/components/views/CreateMilestone.jsx | 6 ++ src/components/views/CreatePayment.jsx | 8 +- src/components/views/EditBounty.jsx | 6 ++ src/components/views/EditCampaign.jsx | 105 +++++++++++----------- src/components/views/EditCommunity.jsx | 109 ++++++++++++----------- src/components/views/EditExpense.jsx | 6 ++ src/components/views/EditMilestone.jsx | 6 ++ src/components/views/EditPayment.jsx | 6 ++ src/components/views/EditTraceOld.jsx | 5 ++ src/lib/ErrorHandler.jsx | 1 + 12 files changed, 168 insertions(+), 110 deletions(-) diff --git a/src/components/views/CreateBounty.jsx b/src/components/views/CreateBounty.jsx index 46b6c1bf5..589e5e76c 100644 --- a/src/components/views/CreateBounty.jsx +++ b/src/components/views/CreateBounty.jsx @@ -44,7 +44,17 @@ function CreateBounty(props) { const [loading, setLoading] = useState(false); const [userIsCampaignOwner, setUserIsOwner] = useState(false); + function goBack() { + history.goBack(); + } + useEffect(() => { + if (currentUser.address) { + authenticateUser(currentUser, false, web3).then(auth => { + if (!auth) history.goBack(); + }); + } + setUserIsOwner( campaign && currentUser.address && @@ -67,10 +77,6 @@ function CreateBounty(props) { }); } - function goBack() { - history.goBack(); - } - const submit = async () => { const authenticated = await authenticateUser(currentUser, false, web3); diff --git a/src/components/views/CreateExpense.jsx b/src/components/views/CreateExpense.jsx index 4dc718624..a12bebc11 100644 --- a/src/components/views/CreateExpense.jsx +++ b/src/components/views/CreateExpense.jsx @@ -77,6 +77,12 @@ function CreateExpense(props) { const itemAmountMap = useRef({}); useEffect(() => { + if (currentUser.address) { + authenticateUser(currentUser, false, web3).then(auth => { + if (!auth) history.goBack(); + }); + } + if (currentUser.address && !expenseForm.recipientAddress) { setExpenseForm({ ...expenseForm, diff --git a/src/components/views/CreateMilestone.jsx b/src/components/views/CreateMilestone.jsx index 80275f5c5..49d2fd0be 100644 --- a/src/components/views/CreateMilestone.jsx +++ b/src/components/views/CreateMilestone.jsx @@ -47,6 +47,12 @@ function CreateMilestone(props) { const [userIsCampaignOwner, setUserIsOwner] = useState(false); useEffect(() => { + if (currentUser.address) { + authenticateUser(currentUser, false, web3).then(auth => { + if (!auth) history.goBack(); + }); + } + setUserIsOwner( campaign && currentUser.address && diff --git a/src/components/views/CreatePayment.jsx b/src/components/views/CreatePayment.jsx index 004cd0bb3..e405f00ba 100644 --- a/src/components/views/CreatePayment.jsx +++ b/src/components/views/CreatePayment.jsx @@ -21,7 +21,7 @@ import { Context as UserContext } from '../../contextProviders/UserProvider'; import { Context as ConversionRateContext } from '../../contextProviders/ConversionRateProvider'; import { Context as Web3Context } from '../../contextProviders/Web3Provider'; import { Context as NotificationContext } from '../../contextProviders/NotificationModalProvider'; -import { convertEthHelper, getStartOfDayUTC, ZERO_ADDRESS } from '../../lib/helpers'; +import { convertEthHelper, getStartOfDayUTC, ZERO_ADDRESS, history } from '../../lib/helpers'; import ErrorHandler from '../../lib/ErrorHandler'; import { authenticateUser } from '../../lib/middleware'; import BridgedTrace from '../../models/BridgedTrace'; @@ -108,6 +108,12 @@ function CreatePayment(props) { }, []); useEffect(() => { + if (currentUser.address) { + authenticateUser(currentUser, false, web3).then(auth => { + if (!auth) history.goBack(); + }); + } + if (currentUser.address && !payment.recipientAddress) { setPayment({ ...payment, diff --git a/src/components/views/EditBounty.jsx b/src/components/views/EditBounty.jsx index 42fee781b..d6c83658d 100644 --- a/src/components/views/EditBounty.jsx +++ b/src/components/views/EditBounty.jsx @@ -66,6 +66,12 @@ function EditBounty(props) { }; useEffect(() => { + if (currentUser.address) { + authenticateUser(currentUser, false, web3).then(auth => { + if (!auth) history.goBack(); + }); + } + if (trace) { setUserIsOwner( [campaign.ownerAddress, campaign.coownerAddress].includes(currentUser.address), diff --git a/src/components/views/EditCampaign.jsx b/src/components/views/EditCampaign.jsx index caa4bf01e..826cb3107 100644 --- a/src/components/views/EditCampaign.jsx +++ b/src/components/views/EditCampaign.jsx @@ -68,63 +68,64 @@ const EditCampaign = () => { }, []); useEffect(() => { - if (userIsLoading || whitelistIsLoading || !currentUser.address) return () => {}; - - if (isNew) { - if (!currentUser.isProjectOwner && projectOwnersWhitelistEnabled) { - const modal = Modal.error({ - title: 'Permission Denied', - content: 'You are not allowed to create a campaign', - closable: false, - centered: true, - onOk: () => history.replace('/'), - }); - - return () => { - modal.destroy(); - }; + const checks = async () => { + const authenticated = await authenticateUser(currentUser, false, web3); + if (!authenticated) { + goBack(); + return; } - - checkProfile(currentUser).then(() => { - setCampaign({ - owner: currentUser, - ownerAddress: currentUser.address, - }); - setIsLoading(false); - }); - } else { - CampaignService.get(campaignId) - .then(camp => { - if (isOwner(camp.ownerAddress, currentUser)) { - const imageIpfsPath = camp.image.match(/\/ipfs\/.*/); + if (isNew) { + if (!currentUser.isProjectOwner && projectOwnersWhitelistEnabled) { + Modal.error({ + title: 'Permission Denied', + content: 'You are not allowed to create a campaign', + closable: false, + centered: true, + onOk: () => history.replace('/'), + }); + } else + checkProfile(currentUser).then(() => { setCampaign({ - title: camp.title, - description: camp.description, - communityUrl: camp.communityUrl, - reviewerAddress: camp.reviewerAddress, - picture: imageIpfsPath ? imageIpfsPath[0] : camp.image, + owner: currentUser, + ownerAddress: currentUser.address, }); - campaignObject.current = camp; setIsLoading(false); - } else { - ErrorHandler({}, 'You are not allowed to edit this Campaign.'); - goBack(); - } - }) - .catch(err => { - if (err.status === 404) { - history.push('/notfound'); - } else { - setIsLoading(false); - ErrorHandler( - err, - 'There has been a problem loading the Campaign. Please refresh the page and try again.', - ); - } - }); - } + }); + } else { + CampaignService.get(campaignId) + .then(camp => { + if (isOwner(camp.ownerAddress, currentUser)) { + const imageIpfsPath = camp.image.match(/\/ipfs\/.*/); + setCampaign({ + title: camp.title, + description: camp.description, + communityUrl: camp.communityUrl, + reviewerAddress: camp.reviewerAddress, + picture: imageIpfsPath ? imageIpfsPath[0] : camp.image, + }); + campaignObject.current = camp; + setIsLoading(false); + } else { + ErrorHandler({}, 'You are not allowed to edit this Campaign.'); + goBack(); + } + }) + .catch(err => { + if (err.status === 404) { + history.push('/notfound'); + } else { + setIsLoading(false); + ErrorHandler( + err, + 'There has been a problem loading the Campaign. Please refresh the page and try again.', + ); + } + }); + } + }; - return () => {}; + if (userIsLoading || whitelistIsLoading || !currentUser.address) return; + checks().then(); }, [userIsLoading, currentUser, whitelistIsLoading]); // TODO: Check if user Changes (in Class components checked in didUpdate) diff --git a/src/components/views/EditCommunity.jsx b/src/components/views/EditCommunity.jsx index 01a7b63dd..582e1404a 100644 --- a/src/components/views/EditCommunity.jsx +++ b/src/components/views/EditCommunity.jsx @@ -69,65 +69,68 @@ const EditCommunity = ({ isNew, match }) => { }, []); useEffect(() => { - if (userIsLoading || whitelistIsLoading || !currentUser.address) return () => {}; - - if (isNew) { - if (!currentUser.isDelegator && projectOwnersWhitelistEnabled) { - const modal = Modal.error({ - title: 'Permission Denied', - content: 'You are not allowed to create a Community', - closable: false, - centered: true, - onOk: () => history.replace('/'), - }); - - return () => { - modal.destroy(); - }; + const checks = async () => { + const authenticated = await authenticateUser(currentUser, false, web3); + if (!authenticated) { + goBack(); + return; } - checkProfile(currentUser).then(() => { - setCommunity({ - owner: currentUser, - ownerAddress: currentUser.address, - }); - setIsLoading(false); - }); - } else { - CommunityService.get(match.params.id) - .then(communityItem => { - if (isOwner(communityItem.ownerAddress, currentUser)) { + if (isNew) { + if (!currentUser.isDelegator && projectOwnersWhitelistEnabled) { + Modal.error({ + title: 'Permission Denied', + content: 'You are not allowed to create a Community', + closable: false, + centered: true, + onOk: () => history.replace('/'), + }); + } else { + checkProfile(currentUser).then(() => { setCommunity({ - title: communityItem.title, - description: communityItem.description, - communityUrl: communityItem.communityUrl, - id: match.params.id, - slug: communityItem.slug, - reviewerAddress: communityItem.reviewerAddress, - ownerAddress: communityItem.ownerAddress, - picture: communityItem.image.match(/\/ipfs\/.*/)[0], + owner: currentUser, + ownerAddress: currentUser.address, }); - communityObject.current = communityItem; - setIsLoading(false); - } else { - ErrorHandler({}, 'You are not allowed to edit this Community.'); - goBack(); - } - }) - .catch(err => { - if (err.status === 404) { - history.push('/notfound'); - } else { setIsLoading(false); - ErrorHandler( - err, - 'There has been a problem loading the Community. Please refresh the page and try again.', - ); - } - }); - } + }); + } + } else { + CommunityService.get(match.params.id) + .then(communityItem => { + if (isOwner(communityItem.ownerAddress, currentUser)) { + setCommunity({ + title: communityItem.title, + description: communityItem.description, + communityUrl: communityItem.communityUrl, + id: match.params.id, + slug: communityItem.slug, + reviewerAddress: communityItem.reviewerAddress, + ownerAddress: communityItem.ownerAddress, + picture: communityItem.image.match(/\/ipfs\/.*/)[0], + }); + communityObject.current = communityItem; + setIsLoading(false); + } else { + ErrorHandler({}, 'You are not allowed to edit this Community.'); + goBack(); + } + }) + .catch(err => { + if (err.status === 404) { + history.push('/notfound'); + } else { + setIsLoading(false); + ErrorHandler( + err, + 'There has been a problem loading the Community. Please refresh the page and try again.', + ); + } + }); + } + }; - return () => {}; + if (userIsLoading || whitelistIsLoading || !currentUser.address) return; + checks().then(); }, [userIsLoading, currentUser, whitelistIsLoading]); // TODO: Check if user Changes (in Class components checked in didUpdate) diff --git a/src/components/views/EditExpense.jsx b/src/components/views/EditExpense.jsx index a536006f6..302f85c6b 100644 --- a/src/components/views/EditExpense.jsx +++ b/src/components/views/EditExpense.jsx @@ -160,6 +160,12 @@ function EditExpense(props) { }; useEffect(() => { + if (currentUser.address) { + authenticateUser(currentUser, false, web3).then(auth => { + if (!auth) history.goBack(); + }); + } + if (trace) { setUserIsOwner( [campaign.ownerAddress, campaign.coownerAddress].includes(currentUser.address), diff --git a/src/components/views/EditMilestone.jsx b/src/components/views/EditMilestone.jsx index 8c312f2bf..7bbdc37f3 100644 --- a/src/components/views/EditMilestone.jsx +++ b/src/components/views/EditMilestone.jsx @@ -72,6 +72,12 @@ function EditMilestone(props) { }; useEffect(() => { + if (currentUser.address) { + authenticateUser(currentUser, false, web3).then(auth => { + if (!auth) history.goBack(); + }); + } + if (trace) { setUserIsOwner( [campaign.ownerAddress, campaign.coownerAddress].includes(currentUser.address), diff --git a/src/components/views/EditPayment.jsx b/src/components/views/EditPayment.jsx index ad4c03eba..857821b99 100644 --- a/src/components/views/EditPayment.jsx +++ b/src/components/views/EditPayment.jsx @@ -111,6 +111,12 @@ function EditPayment(props) { }; useEffect(() => { + if (currentUser.address) { + authenticateUser(currentUser, false, web3).then(auth => { + if (!auth) history.goBack(); + }); + } + if (trace) { setUserIsOwner( [campaign.ownerAddress, campaign.coownerAddress].includes(currentUser.address), diff --git a/src/components/views/EditTraceOld.jsx b/src/components/views/EditTraceOld.jsx index 8fded7f76..245bc4bf8 100644 --- a/src/components/views/EditTraceOld.jsx +++ b/src/components/views/EditTraceOld.jsx @@ -251,6 +251,11 @@ function EditTraceOld(props) { }, [editedForm.token, editedForm.fiatAmount, editedForm.date, editedForm.currency]); useEffect(() => { + if (currentUser.address) { + authenticateUser(currentUser, false, web3).then(auth => { + if (!auth) history.goBack(); + }); + } if (trace) { setUserIsOwner( [campaign.ownerAddress, campaign.coownerAddress].includes(currentUser.address), diff --git a/src/lib/ErrorHandler.jsx b/src/lib/ErrorHandler.jsx index 28c3a13dc..9405e8266 100644 --- a/src/lib/ErrorHandler.jsx +++ b/src/lib/ErrorHandler.jsx @@ -2,6 +2,7 @@ import { notification } from 'antd'; import ErrorPopup from '../components/ErrorPopup'; export default (err, message, forcePopup = false, onCancel = () => {}, onError = () => {}) => { + console.error(err); let _message = ''; let _description = '';