Skip to content

Commit

Permalink
fix: make sure campaign creation is not exposed accidentally (#865)
Browse files Browse the repository at this point in the history
* fix: make sure campaign creation is not exposed accidentally

* fix: need userId

* fix: equality check

Co-authored-by: Graham Tackley <gtackley@brave.com>

* fix: change funtion signature

---------

Co-authored-by: Graham Tackley <gtackley@brave.com>
  • Loading branch information
IanKrieger and tackley authored Aug 17, 2023
1 parent c186788 commit d4de0a5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/components/Campaigns/CloneCampaign.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { AdvertiserCampaignsDocument } from "graphql/advertiser.generated";
import { createCampaignFromFragment } from "form/fragmentUtil";
import { useAdvertiser } from "auth/hooks/queries/useAdvertiser";
import ContentCopyIcon from "@mui/icons-material/ContentCopy";
import { useUser } from "auth/hooks/queries/useUser";

interface Props {
campaignFragment?: CampaignFragment | null;
Expand All @@ -27,6 +28,7 @@ interface Props {

export function CloneCampaign({ campaignFragment, useChip }: Props) {
const { advertiser } = useAdvertiser();
const { userId } = useUser();
const history = useHistory();
const [open, setOpen] = useState(false);

Expand Down Expand Up @@ -94,7 +96,7 @@ export function CloneCampaign({ campaignFragment, useChip }: Props) {
if (campaignFragment) {
copyCampaign({
variables: {
input: createCampaignFromFragment(campaignFragment),
input: createCampaignFromFragment(campaignFragment, userId),
},
});
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/form/fragmentUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { AdSetFragment } from "graphql/ad-set.generated";

export function createCampaignFromFragment(
data: CampaignFragment,
userId?: string,
): CreateCampaignInput {
const adSets: CreateAdSetInput[] = data.adSets.map((adSet) =>
createAdSetFromFragment(adSet),
);

const two = moment().utc().add(3, "days");
return {
userId,
adSets: adSets && adSets.length > 0 ? adSets : undefined,
advertiserId: data.advertiser.id,
budget: data.budget,
Expand Down
9 changes: 9 additions & 0 deletions src/user/User.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Navbar } from "components/Navigation/Navbar";
import { CampaignView } from "user/views/user/CampaignView";
import { CampaignReportView } from "user/views/user/CampaignReportView";
import { Profile } from "user/views/user/Profile";
import { IAdvertiser } from "auth/context/auth.interface";

const buildApolloClient = () => {
const httpLink = createHttpLink({
Expand Down Expand Up @@ -50,11 +51,13 @@ export function User() {
<ProtectedRoute
path="/user/main/adsmanager/advanced/new/:draftId"
authedComponent={NewCampaign}
validateAdvertiserProperty={(a) => a.selfServiceCreate}
/>

<ProtectedRoute
path="/user/main/adsmanager/advanced/:campaignId"
authedComponent={EditCampaign}
validateAdvertiserProperty={(a) => a.selfServiceEdit}
/>

<ProtectedRoute
Expand Down Expand Up @@ -92,19 +95,25 @@ interface ProtectedProps {
authedComponent?: ComponentType;
unauthedComponent?: ComponentType;
path?: string;
validateAdvertiserProperty?: (a: IAdvertiser) => boolean;
}

const ProtectedRoute = ({
authedComponent,
unauthedComponent,
path,
validateAdvertiserProperty = () => true,
}: ProtectedProps) => {
const { advertiser } = useAdvertiser();

if (!advertiser.agreed && unauthedComponent === undefined) {
return <Redirect to="/user/main" />;
}

if (!validateAdvertiserProperty(advertiser)) {
return <Redirect to="/user/main" />;
}

return (
<Route
path={path}
Expand Down
1 change: 0 additions & 1 deletion src/user/campaignList/CampaignList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ interface CheckBoxProps {
onCampaignSelect: (c: string, insert: boolean) => void;
}
const CampaignCheckBox = (props: CheckBoxProps) => {
console.log(props.selectedCampaigns);
const campaignSelected = props.selectedCampaigns.some(
(c) => c === props.campaign.id,
);
Expand Down

0 comments on commit d4de0a5

Please sign in to comment.