Skip to content

Commit

Permalink
bug fictadvisor#248: made permissions global for FE and BE
Browse files Browse the repository at this point in the history
- permissions enum is now in a shared utils package
- changed FE and BE dockerfiles to build shared packages
- changed docker container execution context in github workflows
- fixed the bug with group page not displaying
- fixed test user's permissions on dev

closes fictadvisor#248
  • Loading branch information
ShavuhaS authored and sifes committed Apr 2, 2024
1 parent 91ee01d commit 6cc1fbd
Show file tree
Hide file tree
Showing 47 changed files with 153 additions and 187 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/node_modules

.idea/
.turbo/
3 changes: 2 additions & 1 deletion .github/workflows/build-back.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ jobs:
- name: Build and push app Docker image
uses: docker/build-push-action@v2
with:
context: ./fictadvisor-api
context: .
file: ./fictadvisor-api/Dockerfile
tags: fictadvisor/fictadvisor-api
3 changes: 2 additions & 1 deletion .github/workflows/build-front.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ jobs:
- name: Build and push app Docker image
uses: docker/build-push-action@v2
with:
context: ./fictadvisor-web
context: .
file: ./fictadvisor-web/Dockerfile
build-args:
NODE_ENV=${{ github.ref_name == 'master' && 'production' || 'development' }}
tags: fictadvisor/fictadvisor-web
3 changes: 2 additions & 1 deletion .github/workflows/deploy-back.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jobs:
- name: Build and push app Docker image
uses: docker/build-push-action@v2
with:
context: ./fictadvisor-api
context: .
file: ./fictadvisor-api/Dockerfile
push: true
tags: fictadvisor/fictadvisor-api:${{ github.ref_name }}
3 changes: 2 additions & 1 deletion .github/workflows/deploy-front.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ jobs:
- name: Build and push app Docker image
uses: docker/build-push-action@v2
with:
context: ./fictadvisor-web
context: .
file: ./fictadvisor-web/Dockerfile
push: true
build-args:
NODE_ENV=${{ github.ref_name == 'master' && 'production' || 'development' }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.turbo
/env/postgres-dev.env
/env/postgres.env
./out
43 changes: 29 additions & 14 deletions fictadvisor-api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,37 @@
# BUILD FOR PRODUCTION
###################

FROM node:18-alpine3.17 as build
FROM node:18-alpine3.17 as pruner

WORKDIR /app

COPY . ./
COPY . .

RUN npm i -g turbo && \
turbo prune api --docker


FROM node:18-alpine3.17 as builder

WORKDIR /app

COPY --from=pruner ./app/out/full/ ./
COPY --from=pruner ./app/out/yarn.lock ./

RUN npm install && \
npx prisma generate --schema ./fictadvisor-api/prisma/schema.prisma && \
npx turbo build --filter api && \
npm prune --prod && \
rm -rf ./fictadvisor-api/src && \
rm -rf ./fictadvisor-api/prisma && \
rm -rf ./utils/src && \
mv ./fictadvisor-api/dist/* ./fictadvisor-api && \
rm -rf dist && \
mv -T fictadvisor-api/src fictadvisor-api/dist && \
mv ./fictadvisor-api/email . && \
mkdir ./static && \
mkdir ./private

RUN yarn install --prod && \
yarn add @vercel/ncc && \
yarn ncc build src/main.ts -o dist && \
mv dist/client/* dist && \
mkdir dist/static && \
mkdir dist/private && \
mkdir dist/swagger && \
cp node_modules/swagger-ui-dist/swagger-ui* dist/swagger && \
mkdir -p dist/email/templates && \
cp email/templates/template.hbs dist/email/templates

###################
# PRODUCTION
Expand All @@ -32,7 +47,7 @@ RUN apk --no-cache add -U \
nodejs~18 \
dumb-init

COPY --from=build /app/dist/ ./
COPY --from=builder /app ./

CMD [ "dumb-init", "node", "index.js" ]
CMD [ "dumb-init", "node", "./fictadvisor-api/dist/main.js" ]

2 changes: 2 additions & 0 deletions fictadvisor-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"author": "",
"private": true,
"license": "UNLICENSED",
"packageManager": "[email protected]",
"prisma": {
"seed": "ts-node prisma/seed.ts"
},
Expand Down Expand Up @@ -33,6 +34,7 @@
"migrate:dev": "dotenv -e .development.env -- npx prisma migrate dev"
},
"dependencies": {
"@fictadvisor/utils": "*",
"@nestjs-modules/mailer": "^1.8.1",
"@nestjs/common": "^9.2.1",
"@nestjs/config": "^2.2.0",
Expand Down
10 changes: 2 additions & 8 deletions fictadvisor-api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { HttpExceptionFilter, validationExceptionFactory } from './v2/security/e
import { NestExpressApplication } from '@nestjs/platform-express';
import { applyStaticMiddleware } from './v2/utils/StaticUtil';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { join } from 'path';
import { join, resolve } from 'path';

(BigInt.prototype as any).toJSON = function () {
const int = Number.parseInt(this.toString());
Expand Down Expand Up @@ -46,13 +46,7 @@ async function bootstrap () {
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);

if (process.env.NODE_ENV === 'production') {
app.useStaticAssets(join(__dirname, '/swagger'), {
prefix: '/api',
});
}

app.useStaticAssets(join(__dirname, '/static/'));
app.useStaticAssets(join(resolve(), '/static/'));

await app.listen(port);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@nestjs/swagger';
import { ApiEndpoint } from '../../utils/documentation/decorators';
import { Body, Controller, Delete, Param, Patch, Post, Get, Query } from '@nestjs/common';
import { PERMISSION } from '../../security/PERMISSION';
import { PERMISSION } from '@fictadvisor/utils/security';
import { CathedraService } from '../services/CathedraService';
import { CathedraMapper } from '../../mappers/CathedraMapper';
import { CathedraByIdPipe } from '../pipes/CathedraByIdPipe';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DisciplineService } from '../services/DisciplineService';
import { CreateDisciplineDTO } from '../dtos/CreateDisciplineDTO';
import { GroupByDisciplineGuard } from '../../security/group-guard/GroupByDisciplineGuard';
import { Access } from 'src/v2/security/Access';
import { PERMISSION } from '../../security/PERMISSION';
import { PERMISSION } from '@fictadvisor/utils/security';
import {
ApiBadRequestResponse,
ApiBearerAuth,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DisciplineTeacherService } from '../services/DisciplineTeacherService';
import { CreateAnswersDTO, CreateAnswersWithUserIdDTO } from '../dtos/CreateAnswersDTO';
import { GroupByDisciplineTeacherGuard } from 'src/v2/security/group-guard/GroupByDisciplineTeacherGuard';
import { Access } from 'src/v2/security/Access';
import { PERMISSION } from '../../security/PERMISSION';
import { PERMISSION } from '@fictadvisor/utils/security';
import { DisciplineTeacherByIdPipe } from '../pipes/DisciplineTeacherByIdPipe';
import { TelegramGuard } from '../../security/TelegramGuard';
import { ResponseDTO } from '../dtos/ResponseDTO';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CreateContractDTO } from '../dtos/CreateContractDTO';
import { Actions, EntrantService } from '../services/EntrantService';
import { EntrantMapper } from '../../mappers/EntrantMapper';
import { Access } from '../../security/Access';
import { PERMISSION } from '../../security/PERMISSION';
import { PERMISSION } from '@fictadvisor/utils/security';
import {
ApiBadRequestResponse,
ApiBearerAuth,
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion fictadvisor-api/src/v2/api/controllers/GroupController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ApproveDTO } from '../dtos/ApproveDTO';
import { RoleDTO } from '../dtos/RoleDTO';
import { UserByIdPipe } from '../pipes/UserByIdPipe';
import { UpdateGroupDTO } from '../dtos/UpdateGroupDTO';
import { PERMISSION } from '../../security/PERMISSION';
import { PERMISSION } from '@fictadvisor/utils/security';
import { StudentMapper } from '../../mappers/StudentMapper';
import { AbsenceOfCaptainException } from '../../utils/exceptions/AbsenceOfCaptainException';
import { GroupMapper } from '../../mappers/GroupMapper';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Body, Controller, Get, Patch, Post, Query } from '@nestjs/common';
import { PERMISSION } from '../../security/PERMISSION';
import { PERMISSION } from '@fictadvisor/utils/security';
import {
ApiBadRequestResponse,
ApiBearerAuth,
Expand Down
2 changes: 1 addition & 1 deletion fictadvisor-api/src/v2/api/controllers/PollController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Body, Controller, Delete, Get, Patch, Param, Post, Query } from '@nestjs/common';
import { PollService } from '../services/PollService';
import { PERMISSION } from '../../security/PERMISSION';
import { PERMISSION } from '@fictadvisor/utils/security';
import { QuestionByIdPipe } from '../pipes/QuestionByIdPipe';
import { QuestionByRoleAndIdPipe } from '../pipes/QuestionByRoleAndIdPipe';
import { UserByIdPipe } from '../pipes/UserByIdPipe';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ResourceService } from '../services/ResourceService';
import { ResourceByIdPipe } from '../pipes/ResourceByIdPipe';
import { CreateResourceDTO } from '../dtos/CreateResourceDTO';
import { UpdateResourceDTO } from '../dtos/UpdateResourceDTO';
import { PERMISSION } from '../../security/PERMISSION';
import { PERMISSION } from '@fictadvisor/utils/security';
import {
ApiBadRequestResponse,
ApiBearerAuth,
Expand Down
2 changes: 1 addition & 1 deletion fictadvisor-api/src/v2/api/controllers/RoleController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { RoleService } from '../services/RoleService';
import { GrantMapper } from '../../mappers/GrantMapper';
import { RoleMapper } from '../../mappers/RoleMapper';
import { PERMISSION } from '../../security/PERMISSION';
import { PERMISSION } from '@fictadvisor/utils/security';
import { ApiEndpoint } from '../../utils/documentation/decorators';
import { BaseRoleResponse, RoleResponse } from '../responses/RoleResponse';
import { RolesResponse } from '../responses/RolesResponse';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ScheduleService } from '../services/ScheduleService';
import { GroupByIdPipe } from '../pipes/GroupByIdPipe';
import { ScheduleMapper } from '../../mappers/ScheduleMapper';
import { Access } from '../../security/Access';
import { PERMISSION } from '../../security/PERMISSION';
import { PERMISSION } from '@fictadvisor/utils/security';
import {
ApiBadRequestResponse,
ApiBearerAuth,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { StudentMapper } from '../../mappers/StudentMapper';
import { StudentService } from '../services/StudentService';
import { AllStudentsPipe } from '../pipes/AllStudentsPipe';
import { ApiEndpoint } from '../../utils/documentation/decorators';
import { PERMISSION } from '../../security/PERMISSION';
import { PERMISSION } from '@fictadvisor/utils/security';
import {
FullStudentResponse,
SimpleStudentResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SubjectService } from '../services/SubjectService';
import { SubjectByIdPipe } from '../pipes/SubjectByIdPipe';
import { QueryAllSubjectDTO } from '../dtos/QueryAllSubjectDTO';
import { Access } from 'src/v2/security/Access';
import { PERMISSION } from '../../security/PERMISSION';
import { PERMISSION } from '@fictadvisor/utils/security';
import { SubjectMapper } from '../../mappers/SubjectMapper';
import { CreateSubjectDTO } from '../dtos/CreateSubjectDTO';
import { UpdateSubjectDTO } from '../dtos/UpdateSubjectDTO';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { UpdateTeacherDTO } from '../dtos/UpdateTeacherDTO';
import { CreateContactDTO } from '../dtos/CreateContactDTO';
import { UpdateContactDTO } from '../dtos/UpdateContactDTO';
import { Access } from 'src/v2/security/Access';
import { PERMISSION } from '../../security/PERMISSION';
import { PERMISSION } from '@fictadvisor/utils/security';
import { TeacherByIdPipe } from '../pipes/TeacherByIdPipe';
import { ContactByNamePipe } from '../pipes/ContactByNamePipe';
import { SubjectByIdPipe } from '../pipes/SubjectByIdPipe';
Expand Down
2 changes: 1 addition & 1 deletion fictadvisor-api/src/v2/api/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { UpdateUserDTO } from '../dtos/UpdateUserDTO';
import { UpdateStudentDTO } from '../dtos/UpdateStudentDTO';
import { ContactByUserIdPipe } from '../pipes/ContactByUserIdPipe';
import { GroupRequestDTO } from '../dtos/GroupRequestDTO';
import { PERMISSION } from '../../security/PERMISSION';
import { PERMISSION } from '@fictadvisor/utils/security';
import { TelegramDTO } from '../dtos/TelegramDTO';
import { UserMapper } from '../../mappers/UserMapper';
import { AvatarValidationPipe } from '../pipes/AvatarValidationPipe';
Expand Down
2 changes: 1 addition & 1 deletion fictadvisor-api/src/v2/api/dtos/CheckPermissionsDTO.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IsArray, IsEnum, IsObject, IsOptional } from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { PERMISSION } from '../../security/PERMISSION';
import { PERMISSION } from '@fictadvisor/utils/security';

export class CheckPermissionsDTO {
@ApiProperty({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MapperModule } from '../../modules/MapperModule';
import { State } from '@prisma/client';
import { PrismaService } from '../../database/PrismaService';
import { PermissionService } from './PermissionService';
import { PERMISSION } from '../../security/PERMISSION';
import { PERMISSION } from '@fictadvisor/utils/security';
import { DataNotFoundException } from '../../utils/exceptions/DataNotFoundException';

describe('PermissionService', () => {
Expand Down
2 changes: 1 addition & 1 deletion fictadvisor-api/src/v2/utils/documentation/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { applyDecorators, UseGuards } from '@nestjs/common';
import { ApiOperation } from '@nestjs/swagger';
import { PERMISSION } from '../../security/PERMISSION';
import { PERMISSION } from '@fictadvisor/utils/security';
import { JwtGuard } from '../../security/JwtGuard';
import { PermissionGuard } from '../../security/permission-guard/PermissionGuard';
import { Permissions } from '../../security/permission-guard/Permissions';
Expand Down
14 changes: 7 additions & 7 deletions fictadvisor-api/src/v2/utils/files/FileService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { Express } from 'express';
import { createHash } from 'crypto';
import { join, extname } from 'path';
import { join, extname, resolve as pathResolve } from 'path';
import { resolve } from 'url';
import PizZip from 'pizzip';
import Docxtemplater from 'docxtemplater';
Expand All @@ -16,7 +16,7 @@ import { MINUTE } from '../date/DateService';
export class FileService {
async saveByHash (file: Express.Multer.File, directory: string) {
const fileName = createHash('md5').update(file.buffer).digest('hex');
const filePath = join(__dirname, 'static', directory, fileName + extname(file.originalname));
const filePath = join(pathResolve(), 'static', directory, fileName + extname(file.originalname));

await fs.promises.writeFile(filePath, file.buffer);

Expand All @@ -29,22 +29,22 @@ export class FileService {
}

checkFileExist (path: string, isPrivate = true): boolean {
const filePath = join(__dirname, isPrivate ? 'private' : 'static', path);
const filePath = join(pathResolve(), isPrivate ? 'private' : 'static', path);
return fs.existsSync(filePath);
}

async deleteFile (path: string, isPrivate = true) {
const filePath = join(__dirname, isPrivate ? 'private' : 'static', path);
const filePath = join(pathResolve(), isPrivate ? 'private' : 'static', path);
await fs.promises.unlink(filePath);
}

getFileContent (path: string, isPrivate = true) {
const filePath = join(__dirname, isPrivate ? 'private' : 'static', path);
const filePath = join(pathResolve(), isPrivate ? 'private' : 'static', path);
return fs.readFileSync(filePath, 'utf-8');
}

fillTemplate (fileName: string, data: object) {
const path = join(__dirname, 'private/templates', fileName);
const path = join(pathResolve(), 'private/templates', fileName);
const zip = new PizZip(fs.readFileSync(path, 'binary'));

const doc = new Docxtemplater(zip, {
Expand All @@ -62,7 +62,7 @@ export class FileService {

generateGroupList (students: StudentWithContactsData[]) {
const fileName = `${v4()}.csv`;
const path = join(__dirname, 'static', 'lists', fileName);
const path = join(pathResolve(), 'static', 'lists', fileName);

let result = 'lastName,firstName,middleName,email,telegram,github,instagram,facebook,twitter,discord,youtube,mail';
for (const student of students) {
Expand Down
Loading

0 comments on commit 6cc1fbd

Please sign in to comment.