Skip to content

Commit 7179b27

Browse files
committed
add authorization tables (role, permissions) + app configs
1 parent 0262753 commit 7179b27

11 files changed

+180
-4
lines changed

migrations/1697109279164-CreateNewAdminUser.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { hash } from 'bcrypt';
2-
import { getRandomValues } from 'crypto';
32
import { MigrationInterface, QueryRunner } from "typeorm"
43
import { v4 } from 'uuid';
54

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { MigrationInterface, QueryRunner, Table, DatabaseType } from "typeorm"
2+
3+
export class CreateAppConfigsTable1697207258635 implements MigrationInterface {
4+
5+
public async up(queryRunner: QueryRunner): Promise<void> {
6+
await queryRunner.createTable(new Table({
7+
name: "app_configs",
8+
columns: [
9+
{ name: "id", type: "int", isPrimary: true, isGenerated: true, generationStrategy: "increment" },
10+
{ name: "key", type: "varchar" },
11+
{ name: "value", type: "text" },
12+
{ name: "group", type: "varchar", isNullable: true }
13+
],
14+
indices: [
15+
{ columnNames: ['key', 'group'] },
16+
{ columnNames: ['key'] },
17+
]
18+
}));
19+
}
20+
21+
public async down(queryRunner: QueryRunner): Promise<void> {
22+
await queryRunner.dropTable("app_configs");
23+
}
24+
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { MigrationInterface, QueryRunner, Table } from "typeorm"
2+
3+
export class CreateRolesTable1697207571111 implements MigrationInterface {
4+
5+
public async up(queryRunner: QueryRunner): Promise<void> {
6+
await queryRunner.createTable(new Table({
7+
name: "roles",
8+
columns: [
9+
{ name: "id", type: "int8", isPrimary: true, isGenerated: true, generationStrategy: 'increment' },
10+
{ name: "name", type: "varchar", isUnique: true, isNullable: false },
11+
{ name: "key", type: "varchar", isUnique: true, isNullable: false },
12+
{ name: "description", type: "varchar", isNullable: true },
13+
{ name: "created_at", type: "timestamp", default: "now()" },
14+
{ name: "updated_at", type: "timestamp", default: "now()" },
15+
],
16+
indices: [
17+
{ columnNames: ['id'] },
18+
{ columnNames: ['key'] },
19+
]
20+
}))
21+
}
22+
23+
public async down(queryRunner: QueryRunner): Promise<void> {
24+
queryRunner.dropTable('roles')
25+
}
26+
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { MigrationInterface, QueryRunner, Table } from "typeorm"
2+
3+
export class CreatePermissionsTable1697207588388 implements MigrationInterface {
4+
5+
public async up(queryRunner: QueryRunner): Promise<void> {
6+
await queryRunner.createTable(new Table({
7+
name: "permissions",
8+
columns: [
9+
{ name: "id", type: "int8", isPrimary: true, isGenerated: true, generationStrategy: 'increment' },
10+
{ name: "name", type: "varchar", isNullable: false },
11+
{ name: "path", type: "varchar", isNullable: true },
12+
{ name: "description", type: "varchar", isNullable: true,},
13+
],
14+
indices: [
15+
{ columnNames: ['id'] },
16+
]
17+
}));
18+
}
19+
20+
public async down(queryRunner: QueryRunner): Promise<void> {
21+
queryRunner.dropTable('permissions');
22+
}
23+
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { MigrationInterface, QueryRunner, Table } from "typeorm"
2+
3+
export class CreateRoleToPermissionsTable1697208335927 implements MigrationInterface {
4+
5+
public async up(queryRunner: QueryRunner): Promise<void> {
6+
await queryRunner.createTable(new Table({
7+
name: "role_to_permissions",
8+
columns: [
9+
{ name: "role_id", type: "int8" },
10+
{ name: "permission_id", type: "int8" },
11+
],
12+
uniques: [
13+
{ columnNames: ['role_id', 'permission_id'] },
14+
],
15+
indices: [
16+
{ columnNames: ['role_id'] },
17+
{ columnNames: ['role_id', 'permission_id'] },
18+
]
19+
}))
20+
}
21+
22+
public async down(queryRunner: QueryRunner): Promise<void> {
23+
await queryRunner.dropTable('role_to_permissions');
24+
}
25+
26+
}

src/apps/auth/auth.controller.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { AuthService } from './auth.service';
66
import { LoginDTO } from './dto/login.dto';
77
import { RegisterDTO } from './dto/register.dto';
88
import { I18n, I18nContext } from 'nestjs-i18n';
9-
import { ApiBody, ApiHeader, ApiSecurity, ApiTags } from '@nestjs/swagger';
9+
import { ApiBearerAuth, ApiBody, ApiHeader, ApiSecurity, ApiTags } from '@nestjs/swagger';
1010

1111
@Controller('auth')
1212
@ApiTags("Auth")
@@ -34,6 +34,7 @@ export class AuthController {
3434
}
3535

3636
@UseJwtRefreshGuard()
37+
@ApiBearerAuth()
3738
@Post('/refresh')
3839
@HttpCode(HttpStatus.OK)
3940
async refresh(
@@ -50,6 +51,7 @@ export class AuthController {
5051
}
5152

5253
@Post('/register')
54+
@ApiBearerAuth('RefreshToken')
5355
@HttpCode(HttpStatus.CREATED)
5456
@ApiBody({type: () => RegisterDTO})
5557
async register(

src/entities/app-config.entity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class AppConfig {
99
id: number;
1010

1111
@Column()
12-
name: string;
12+
key: string;
1313

1414
@Column()
1515
value: string;

src/entities/permission.entity.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from "typeorm";
2+
import { RoleToPermission } from "./role-to-permission.entity";
3+
4+
@Entity({ name: "permissions" })
5+
export class Permission {
6+
7+
@PrimaryGeneratedColumn({ type: "bigint" })
8+
id: number;
9+
10+
@Column({ type: "varchar", nullable: false })
11+
name: string;
12+
13+
@Column({ type: "varchar", nullable: true })
14+
path: string;
15+
16+
@Column({ type: "varchar", nullable: true })
17+
description: string;
18+
19+
@OneToMany(type => RoleToPermission, roleToPermission => roleToPermission.permission)
20+
roles: RoleToPermission[];
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Entity, Index, ManyToOne, JoinColumn, PrimaryColumn } from "typeorm";
2+
import { Role } from "./role.entity";
3+
import { Permission } from "./permission.entity";
4+
5+
@Entity({ name: "role_to_permissions" })
6+
@Index("role_permission_unique", ["role_id", "permission_id"], { unique: true })
7+
export class RoleToPermission {
8+
9+
@PrimaryColumn({ type: "bigint" })
10+
role_id: number;
11+
12+
@PrimaryColumn({ type: "bigint" })
13+
permission_id: number;
14+
15+
@ManyToOne(type => Role, role => role.permissions)
16+
@JoinColumn({ name: "role_id" })
17+
role: Role;
18+
19+
@ManyToOne(type => Permission, permission => permission.roles)
20+
@JoinColumn({ name: "permission_id" })
21+
permission: Permission;
22+
}

src/entities/role.entity.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, Unique, OneToMany } from "typeorm";
2+
import { RoleToPermission } from "./role-to-permission.entity";
3+
4+
@Entity({ name: "roles" })
5+
@Unique(["name", "key"])
6+
export class Role {
7+
8+
@PrimaryGeneratedColumn({ type: "bigint" })
9+
id: number;
10+
11+
@Column({ type: "varchar", unique: true, nullable: false })
12+
name: string;
13+
14+
@Column({ type: "varchar", unique: true, nullable: false })
15+
key: string;
16+
17+
@Column({ type: "varchar", nullable: true })
18+
description: string;
19+
20+
@CreateDateColumn({ type: "timestamp", default: () => "CURRENT_TIMESTAMP" })
21+
created_at: Date;
22+
23+
@UpdateDateColumn({ type: "timestamp", default: () => "CURRENT_TIMESTAMP" })
24+
updated_at: Date;
25+
26+
@OneToMany(type => RoleToPermission, roleToPermission => roleToPermission.role)
27+
permissions: RoleToPermission[];
28+
}
29+

0 commit comments

Comments
 (0)