Skip to content

Commit

Permalink
refactor: move permissions to common module (freeCodeCamp#1190)
Browse files Browse the repository at this point in the history
* refactor: move permissions to common module

* fix: include common files

* fix: experimental externalDir

* fix: copy common files into containers

* fix: import from new permission module
  • Loading branch information
ojeytonwilliams authored Jul 8, 2022
1 parent c80e79d commit cd28afa
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,6 @@ cypress-coverage/
/server/prisma/generator/**/*.map
/server/tests/**/*.map
/server/reminders/**/*.map
/common/**/*.js
/common/**/*.map
tsconfig.tsbuildinfo
1 change: 1 addition & 0 deletions client.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ WORKDIR /usr/chapter/

# Copying source files
COPY client ./client
COPY common ./common
COPY package*.json ./

# Installing dependencies
Expand Down
3 changes: 3 additions & 0 deletions client/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ module.exports = {
// TODO: the client and server need their own eslint configs
ignoreDuringBuilds: true,
},
experimental: {
externalDir: true,
},
};
8 changes: 7 additions & 1 deletion client/src/hooks/useCheckPermission.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { useAuth } from '../modules/auth/store';
import {
InstancePermission,
ChapterPermission,
} from '../../../common/permissions';

export const useCheckPermission = (permission: string) => {
export const useCheckPermission = (
permission: InstancePermission | ChapterPermission,
) => {
const { user } = useAuth();

return user?.instance_role.instance_role_permissions.find(
Expand Down
8 changes: 4 additions & 4 deletions client/src/modules/dashboard/Chapters/pages/ChaptersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React from 'react';
import { useCheckPermission } from '../../../../hooks/useCheckPermission';
import { useChaptersQuery } from '../../../../generated/graphql';
import { Layout } from '../../shared/components/Layout';
import { Permission } from '../../../../../../common/permissions';

export const ChaptersPage: NextPage = () => {
const {
Expand All @@ -15,10 +16,9 @@ export const ChaptersPage: NextPage = () => {
data: chapterData,
} = useChaptersQuery();

// TODO: the permission names are stringly typed and should be refactored.
// This page, and the prisma factories, should both draw from a single source
// of truth (an enum, probably)
const hasPermissionToCreateChapter = useCheckPermission('chapter-create');
const hasPermissionToCreateChapter = useCheckPermission(
Permission.ChapterCreate,
);

return (
<Layout>
Expand Down
4 changes: 3 additions & 1 deletion client/src/modules/dashboard/shared/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { HStack } from '@chakra-ui/layout';
import { LinkButton } from 'chakra-next-link';
import { useRouter } from 'next/router';
import React from 'react';

import { Permission } from '../../../../../../common/permissions';
import { useCheckPermission } from 'hooks/useCheckPermission';

const links = [
Expand All @@ -11,7 +13,7 @@ const links = [
{
text: 'Sponsors',
link: '/dashboard/sponsors',
requiredPermission: 'sponsors-manage',
requiredPermission: Permission.SponsorsManage,
},
{ text: 'Users', link: '/dashboard/users' },
];
Expand Down
3 changes: 2 additions & 1 deletion client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
],
"include": [
"**/*.ts",
"**/*.tsx"
"**/*.tsx",
"../common/**/*.ts",
]
}
19 changes: 19 additions & 0 deletions common/permissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export enum ChapterPermission {
ChapterEdit = 'chapter-edit',
EventCreate = 'event-create',
EventEdit = 'event-edit',
Rsvp = 'rsvp',
RsvpDelete = 'rsvp-delete',
RsvpConfirm = 'rsvp-confirm',
}

export enum InstancePermission {
ChapterCreate = 'chapter-create',
ChangeInstanceRole = 'change-instance-role',
SponsorsManage = 'sponsors-manage',
ViewUsers = 'view-users',
}

// Ideally this would be a new enum, but TS does not (to my knowledge) support
// that yet.
export const Permission = { ...InstancePermission, ...ChapterPermission };
3 changes: 2 additions & 1 deletion server.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ ARG build
WORKDIR /usr/chapter/

# Bundle app source
COPY ./server ./server
COPY server ./server
COPY common ./common
COPY package*.json ./

# Install app dependencies
Expand Down
10 changes: 1 addition & 9 deletions server/prisma/generator/factories/chapterRoles.factory.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { prisma } from '../../../src/prisma';

export enum ChapterPermission {
ChapterEdit = 'chapter-edit',
EventCreate = 'event-create',
EventEdit = 'event-edit',
Rsvp = 'rsvp',
RsvpDelete = 'rsvp-delete',
RsvpConfirm = 'rsvp-confirm',
}
import { ChapterPermission } from '../../../../common/permissions';

const chapterPermissions = Object.values(ChapterPermission);

Expand Down
12 changes: 1 addition & 11 deletions server/prisma/generator/factories/instanceRoles.factory.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import { prisma } from '../../../src/prisma';

import { ChapterPermission } from './chapterRoles.factory';
import { Permission } from '../../../../common/permissions';

enum InstancePermission {
ChapterCreate = 'chapter-create',
ChangeInstanceRole = 'change-instance-role',
SponsorsManage = 'sponsors-manage',
ViewUsers = 'view-users',
}

// Ideally this would be a new enum, but TS does not (to my knowledge) support
// that yet.
export const Permission = { ...InstancePermission, ...ChapterPermission };
const allPermissions = Object.values(Permission);

interface InstanceRole {
Expand Down
2 changes: 1 addition & 1 deletion server/src/controllers/Chapter/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Ctx,
Authorized,
} from 'type-graphql';
import { Permission } from '../../../prisma/generator/factories/instanceRoles.factory';
import { Permission } from '../../../../common/permissions';

import { GQLCtx } from '../../common-types/gql';
import { Chapter, ChapterWithRelations } from '../../graphql-types';
Expand Down
2 changes: 1 addition & 1 deletion server/src/controllers/Events/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { isEqual, sub } from 'date-fns';
import ical from 'ical-generator';

import { Permission } from '../../../prisma/generator/factories/instanceRoles.factory';
import { Permission } from '../../../../common/permissions';
import { GQLCtx } from '../../common-types/gql';
import {
Event,
Expand Down
2 changes: 1 addition & 1 deletion server/src/controllers/Sponsors/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Prisma } from '@prisma/client';
import { Arg, Authorized, Int, Mutation, Query, Resolver } from 'type-graphql';

import { Permission } from '../../../prisma/generator/factories/instanceRoles.factory';
import { Permission } from '../../../../common/permissions';
import { Sponsor } from '../../graphql-types/Sponsor';
import { prisma } from '../../prisma';
import { CreateSponsorInputs, UpdateSponsorInputs } from './inputs';
Expand Down
2 changes: 1 addition & 1 deletion server/src/controllers/Users/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Arg, Authorized, Int, Mutation, Query, Resolver } from 'type-graphql';
import { prisma } from '../../prisma';

import { UserWithInstanceRole } from '../../graphql-types';
import { Permission } from '../../../prisma/generator/factories/instanceRoles.factory';
import { Permission } from '../../../../common/permissions';

@Resolver()
export class UsersResolver {
Expand Down
2 changes: 1 addition & 1 deletion server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
"typeRoots": ["src/common-types/index.d.ts", "../node_modules/@types"],
"incremental": true
},
"include": ["src/**/*", "prisma/**/*", "reminders/**/*", "tests/**/*"],
"include": ["src/**/*", "prisma/**/*", "reminders/**/*", "tests/**/*", "../common/**/*"],
"exclude": ["node_modules"]
}

0 comments on commit cd28afa

Please sign in to comment.