Skip to content

Commit

Permalink
F #2586 add sign-in check before all ipfs service usages
Browse files Browse the repository at this point in the history
  • Loading branch information
RamRamez committed Apr 4, 2022
1 parent 2dfcc94 commit 9cf3a63
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 110 deletions.
14 changes: 10 additions & 4 deletions src/components/views/CreateBounty.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -67,10 +77,6 @@ function CreateBounty(props) {
});
}

function goBack() {
history.goBack();
}

const submit = async () => {
const authenticated = await authenticateUser(currentUser, false, web3);

Expand Down
6 changes: 6 additions & 0 deletions src/components/views/CreateExpense.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions src/components/views/CreateMilestone.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
8 changes: 7 additions & 1 deletion src/components/views/CreatePayment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions src/components/views/EditBounty.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
105 changes: 53 additions & 52 deletions src/components/views/EditCampaign.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
109 changes: 56 additions & 53 deletions src/components/views/EditCommunity.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions src/components/views/EditExpense.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 6 additions & 0 deletions src/components/views/EditMilestone.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 6 additions & 0 deletions src/components/views/EditPayment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
5 changes: 5 additions & 0 deletions src/components/views/EditTraceOld.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions src/lib/ErrorHandler.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';

Expand Down

0 comments on commit 9cf3a63

Please sign in to comment.