Skip to content

Commit 2e881d1

Browse files
authored
fix: allow non authors to view brief (#2966)
1 parent dcfb901 commit 2e881d1

File tree

3 files changed

+5
-87
lines changed

3 files changed

+5
-87
lines changed

__tests__/posts.ts

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
ArticlePost,
1616
Bookmark,
1717
BookmarkList,
18-
BRIEFING_SOURCE,
1918
clearPostTranslations,
2019
Comment,
2120
Feed,
@@ -104,7 +103,6 @@ import { Product, ProductType } from '../src/entity/Product';
104103
import { BriefingModel, BriefingType } from '../src/integrations/feed';
105104
import { UserBriefingRequest } from '@dailydotdev/schema';
106105
import { addDays } from 'date-fns';
107-
import { BriefPost } from '../src/entity/posts/BriefPost';
108106

109107
jest.mock('../src/common/pubsub', () => ({
110108
...(jest.requireActual('../src/common/pubsub') as Record<string, unknown>),
@@ -1245,54 +1243,6 @@ describe('query post', () => {
12451243
);
12461244
});
12471245

1248-
it('should throw when brief post is from other user', async () => {
1249-
loggedUser = '1';
1250-
1251-
await saveFixtures(con, BriefPost, [
1252-
{
1253-
id: 'pbriefanotherauthor',
1254-
shortId: 'pbfaa',
1255-
title: 'pbriefanotherauthor',
1256-
score: 0,
1257-
sourceId: BRIEFING_SOURCE,
1258-
createdAt: new Date('2021-09-22T07:15:51.247Z'),
1259-
private: true,
1260-
visible: true,
1261-
authorId: '2',
1262-
},
1263-
]);
1264-
1265-
return testQueryErrorCode(
1266-
client,
1267-
{ query: QUERY('pbriefanotherauthor') },
1268-
'FORBIDDEN',
1269-
);
1270-
});
1271-
1272-
it('should throw for anonymous user accessing brief', async () => {
1273-
loggedUser = null;
1274-
1275-
await saveFixtures(con, BriefPost, [
1276-
{
1277-
id: 'pbriefanotherauthor',
1278-
shortId: 'pbfaa',
1279-
title: 'pbriefanotherauthor',
1280-
score: 0,
1281-
sourceId: BRIEFING_SOURCE,
1282-
createdAt: new Date('2021-09-22T07:15:51.247Z'),
1283-
private: true,
1284-
visible: true,
1285-
authorId: '2',
1286-
},
1287-
]);
1288-
1289-
return testQueryErrorCode(
1290-
client,
1291-
{ query: QUERY('pbriefanotherauthor') },
1292-
'FORBIDDEN',
1293-
);
1294-
});
1295-
12961246
describe('clickbaitTitleDetected', () => {
12971247
const LOCAL_QUERY = /* GraphQL */ `
12981248
query Post($id: ID!) {

src/schema/posts.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,13 +1780,7 @@ export const resolvers: IResolvers<unknown, BaseContext> = traceResolvers<
17801780
sourceTypesWithMembers.includes(postSource.type)
17811781
) {
17821782
try {
1783-
await ensureSourcePermissions(
1784-
ctx,
1785-
partialPost.sourceId,
1786-
undefined,
1787-
undefined,
1788-
partialPost,
1789-
);
1783+
await ensureSourcePermissions(ctx, partialPost.sourceId);
17901784
} catch (permissionError) {
17911785
if (permissionError instanceof ForbiddenError) {
17921786
const forbiddenError = permissionError as ForbiddenError;
@@ -1950,13 +1944,7 @@ export const resolvers: IResolvers<unknown, BaseContext> = traceResolvers<
19501944
const post = await ctx.con
19511945
.getRepository(Post)
19521946
.findOneByOrFail([{ id: args.id }, { slug: args.id }]);
1953-
await ensureSourcePermissions(
1954-
ctx,
1955-
post.sourceId,
1956-
undefined,
1957-
undefined,
1958-
post,
1959-
);
1947+
await ensureSourcePermissions(ctx, post.sourceId);
19601948

19611949
return queryPaginatedByDate(
19621950
ctx,
@@ -3043,13 +3031,7 @@ export const resolvers: IResolvers<unknown, BaseContext> = traceResolvers<
30433031
ctx: AuthContext,
30443032
): Promise<GQLEmptyResponse> => {
30453033
const post = await ctx.con.getRepository(Post).findOneByOrFail({ id });
3046-
await ensureSourcePermissions(
3047-
ctx,
3048-
post.sourceId,
3049-
undefined,
3050-
undefined,
3051-
post,
3052-
);
3034+
await ensureSourcePermissions(ctx, post.sourceId);
30533035
if (post.type !== PostType.Article) {
30543036
await notifyView(
30553037
ctx.log,

src/schema/sources.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { IResolvers } from '@graphql-tools/utils';
33
import { ConnectionArguments } from 'graphql-relay';
44
import { AuthContext, BaseContext, Context } from '../Context';
55
import {
6+
BRIEFING_SOURCE,
67
createSharePost,
78
NotificationPreferenceSource,
8-
PostType,
99
REPUTATION_THRESHOLD,
1010
Source,
1111
SourceFeed,
@@ -14,9 +14,8 @@ import {
1414
SourceMemberFlagsPublic,
1515
SquadSource,
1616
User,
17-
type Post,
1817
} from '../entity';
19-
import { BRIEFING_SOURCE, SourceType, SourceUser } from '../entity/Source';
18+
import { SourceType, SourceUser } from '../entity/Source';
2019
import {
2120
SourceMemberRoles,
2221
sourceRoleRank,
@@ -1188,7 +1187,6 @@ export const ensureSourcePermissions = async (
11881187
sourceId: string | undefined,
11891188
permission: SourcePermissions = SourcePermissions.View,
11901189
validateRankAgainstId?: string,
1191-
post?: Pick<Post, 'type' | 'authorId' | 'private' | 'sourceId'>,
11921190
): Promise<Source> => {
11931191
if (sourceId) {
11941192
const source = await ctx.con
@@ -1202,18 +1200,6 @@ export const ensureSourcePermissions = async (
12021200
return source;
12031201
}
12041202

1205-
if (
1206-
permission == SourcePermissions.View &&
1207-
source.id === BRIEFING_SOURCE &&
1208-
post?.type === PostType.Brief
1209-
) {
1210-
if (!ctx.userId || post.authorId !== ctx.userId) {
1211-
throw new ForbiddenError('Access denied!');
1212-
}
1213-
1214-
return source;
1215-
}
1216-
12171203
const sourceMember = ctx.userId
12181204
? await ctx.con
12191205
.getRepository(SourceMember)

0 commit comments

Comments
 (0)