Skip to content

Commit 24c1f1e

Browse files
committed
feat(db/access): improve access control with organizer or user case
1 parent a633f7a commit 24c1f1e

File tree

3 files changed

+91
-52
lines changed

3 files changed

+91
-52
lines changed

libs/cms/auth/strategies/linkedin.ts

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -50,42 +50,6 @@ export const linkedinOAuth = OAuth2Plugin({
5050
const { id, vanityName, imageUrl } =
5151
await fetchLinkedInProfileData(accessToken);
5252

53-
const existingUserByLinkedIn = await req.payload.find({
54-
collection: "users",
55-
where: {
56-
linkedinVanity: {
57-
equals: vanityName,
58-
},
59-
},
60-
limit: 1,
61-
});
62-
63-
if (existingUserByLinkedIn.docs.length > 0) {
64-
const existingUser = existingUserByLinkedIn.docs[0];
65-
66-
await req.payload.update({
67-
collection: "users",
68-
id: existingUser.id,
69-
data: {
70-
linkedinId: id,
71-
linkedinEmailVerified: user.email_verified,
72-
linkedinLocale: user.locale,
73-
},
74-
});
75-
76-
return {
77-
email: existingUser.email,
78-
firstName: existingUser.firstName,
79-
lastName: existingUser.lastName,
80-
preferredDisplayName: existingUser.preferredDisplayName,
81-
avatar: existingUser.avatar,
82-
linkedinId: id,
83-
linkedinVanity: vanityName,
84-
linkedinEmailVerified: user.email_verified,
85-
linkedinLocale: user.locale,
86-
};
87-
}
88-
8953
const existingUserByEmail = await req.payload.find({
9054
collection: "users",
9155
where: {
@@ -96,11 +60,24 @@ const existingUserByEmail = await req.payload.find({
9660
limit: 1,
9761
});
9862

99-
let avatarId = null;
63+
const hackathonResult = await req.payload.find({
64+
collection: "hackathons",
65+
where: { year: { equals: 2025 } },
66+
pagination: false,
67+
});
68+
69+
const hackathon = hackathonResult.docs[0] || null;
70+
71+
const groupResult = await req.payload.find({
72+
collection: "groups",
73+
where: { name: { equals: "Hacker" } },
74+
pagination: false,
75+
});
76+
77+
const group = groupResult.docs[0] || null;
10078

101-
if (existingUserByEmail.docs.length === 0 || existingUserByEmail.docs[0].avatar === null) {
10279
const filename = `${user.given_name.toLowerCase()}-${user.family_name.toLowerCase()}-avatar.png`;
103-
avatarId = imageUrl
80+
const avatarId = imageUrl
10481
? await getOrUploadMedia(
10582
req.payload,
10683
req,
@@ -109,18 +86,31 @@ if (existingUserByEmail.docs.length === 0 || existingUserByEmail.docs[0].avatar
10986
`${user.given_name} ${user.family_name}'s avatar`,
11087
)
11188
: null;
112-
}
11389

114-
if (existingUserByEmail.docs.length === 0) {
90+
if (existingUserByEmail.docs.length === 0 || existingUserByEmail.docs[0].avatar === null) {
11591
await req.payload.sendEmail({
11692
to: user.email,
11793
subject: "Welcome to cuHacking 2025",
11894
html: await generateEmail(),
11995
});
120-
}
12196

12297
return {
12398
email: user.email,
99+
hackathons: hackathon ? hackathon.id : undefined ,
100+
group: group ? group.id : undefined,
101+
firstName: user.given_name,
102+
lastName: user.family_name,
103+
preferredDisplayName: user.name,
104+
avatar: avatarId,
105+
linkedinId: id,
106+
linkedinVanity: vanityName,
107+
linkedinEmailVerified: user.email_verified,
108+
linkedinLocale: user.locale,
109+
};
110+
}
111+
112+
return {
113+
email: existingUserByEmail.docs[0].email || user.email,
124114
firstName: user.given_name,
125115
lastName: user.family_name,
126116
preferredDisplayName: user.name,

libs/db/access/index.ts

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ import type { AccessArgs, FieldHook } from 'payload'
33

44
type IsAuthenticated = (args: AccessArgs<User>) => boolean
55

6-
export const authenticated: IsAuthenticated = ({ req }) => Boolean(req.user)
6+
export const authenticated: IsAuthenticated = ({ req }) => {
7+
return Boolean(req.user)
8+
}
9+
10+
export const anyone: IsAuthenticated = ({ req: { user } }) => {
11+
if (!user) return false;
12+
return {
13+
id: { equals: user.id }
14+
};
15+
};
716

817
export const isSuperAdmin: IsAuthenticated = ({ req: { user } }) =>
918
user?.id === 1 || user?.organizerTeam?.name === "Co-Leads";
@@ -16,14 +25,48 @@ export const adminsAndUser: IsAuthenticated = ({ req: { user } }) => {
1625
return { id: user.id };
1726
};
1827

19-
export const isSameUser: IsAuthenticated = ({ req: { user } }) => {
28+
29+
export const isOrganizerOrUser = ({ req: {user} }) => {
2030
if (!user) return false;
31+
if (user){
32+
if (user.organizerTeam?.name){
33+
return true
34+
}
2135
return {
22-
id: { equals: user.id }
23-
};
36+
id: {
37+
equals: user.id
38+
}
39+
}
40+
};
41+
}
42+
43+
export const isSponsor = ({ req: {user} }) => {
44+
if (user){
45+
if (user.group.name === "Sponsor"){
46+
return true
47+
}
48+
}
49+
return false
2450
};
2551

26-
export const anyone: IsAuthenticated = () => true;
52+
export const isMentor = ({ req: {user} }) => {
53+
if (user){
54+
if (user.group.name === "Mentor"){
55+
return true
56+
}
57+
}
58+
return false
59+
};
60+
61+
62+
export const isJudge = ({ req: {user} }) => {
63+
if (user){
64+
if (user.group.name === "Mentor"){
65+
return true
66+
}
67+
}
68+
return false
69+
};
2770

2871
export const isOrganizer: IsAuthenticated = ({ req: { user } }) =>
2972
user?.group?.name === "Organizer";

libs/db/collections/models/Users.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import { Payload } from "payload";
55
import type { CollectionConfig } from "payload";
66
import {
77
admins,
8-
isSameUser,
8+
// isSameUser,
99
adminsAndUser,
1010
anyone,
1111
authenticated,
12+
isOrganizer,
1213
isSuperAdmin,
1314
// checkRole
1415
} from "@/db/access";
@@ -257,7 +258,6 @@ const SCHOOLS = ALL_SCHOOLS.map((school) => ({
257258

258259
export const Users: CollectionConfig = {
259260
slug: "users",
260-
// auth: true,
261261
auth: {
262262
cookies: {
263263
domain: process.env.NODE_ENV === 'development' ? 'localhost' : '.cuhacking.ca',
@@ -270,16 +270,22 @@ export const Users: CollectionConfig = {
270270
},
271271
},
272272
access: {
273-
admin: isSuperAdmin,
273+
admin: isOrganizer,
274274
read: adminsAndUser,
275275
create: authenticated,
276-
update: ({ req }) => isSuperAdmin({ req }) || isSameUser({ req }),
277-
delete: admins,
276+
update: authenticated,
277+
// delete: admins,
278278
},
279279
// hooks: {
280280
// afterChange: [loginAfterCreate],
281281
// },
282282
admin: {
283+
// hidden: (user) => {
284+
// if (user?.id === 1){
285+
// return false
286+
// }
287+
// return user?.group?.name === "Organizer" ? false : true
288+
// },
283289
livePreview: {
284290
url: `${process.env.CUHACKING_2025_PORTAL_LOCAL_URL}/profile`,
285291
breakpoints: [

0 commit comments

Comments
 (0)