Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@nestjs/typeorm": "^11.0.0",
"bcrypt": "^5.1.1",
"cache-manager": "^6.4.2",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"handlebars": "^4.7.8",
"lodash-es": "^4.17.21",
Expand Down
6 changes: 4 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions src/api-key/api-key.entity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Base } from 'src/common/base.entity';
import { User } from 'src/user/user.entity';
import { UserRole } from 'src/user-role/user-role.entity';
import {
Column,
Entity,
Expand All @@ -20,8 +19,4 @@ export class APIKey extends Base {
@ManyToOne(() => User, (user) => user.apiKey)
@JoinColumn({ name: 'user_id' })
user: User;

@ManyToOne(() => UserRole, (userRole) => userRole.apiKey)
@JoinColumn({ name: 'role' })
role: UserRole;
}
6 changes: 4 additions & 2 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import { TasksModule } from 'src/tasks/tasks.module';
import { WizardModule } from 'src/wizard/wizard.module';
import { APIKeyModule } from 'src/api-key/api-key.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { UserRoleModule } from 'src/user-role/user-role.module';
import { ResourcesModule } from 'src/resources/resources.module';
import { SnakeCaseInterceptor } from 'src/interceptor/snake-case';
import { NamespacesModule } from 'src/namespaces/namespaces.module';
import { PermissionsModule } from 'src/permissions/permissions.module';
import { GroupsModule } from 'src/groups/groups.module';

@Module({
controllers: [AppController],
Expand All @@ -32,11 +33,12 @@ import { NamespacesModule } from 'src/namespaces/namespaces.module';
AuthModule,
UserModule,
APIKeyModule,
UserRoleModule,
NamespacesModule,
ResourcesModule,
TasksModule,
WizardModule,
GroupsModule,
PermissionsModule,
// CacheModule.registerAsync({
// imports: [ConfigModule],
// inject: [ConfigService],
Expand Down
9 changes: 9 additions & 0 deletions src/groups/dto/add-group-user.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Expose } from 'class-transformer';
import { IsNotEmpty, IsString } from 'class-validator';

export class AddGroupUserDto {
@IsString()
@IsNotEmpty()
@Expose({ name: 'user_id' })
userId: string;
}
7 changes: 7 additions & 0 deletions src/groups/dto/create-group.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IsNotEmpty, IsString } from 'class-validator';

export class CreateGroupDto {
@IsString()
@IsNotEmpty()
title: string;
}
16 changes: 16 additions & 0 deletions src/groups/dto/group-user.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { IsNotEmpty, IsString } from 'class-validator';
import { Expose } from 'class-transformer';

@Expose()
export class GroupUserDto {
@IsString()
@IsNotEmpty()
id: string;

@IsString()
username: string;

@IsString()
@IsNotEmpty()
email: string;
}
17 changes: 17 additions & 0 deletions src/groups/dto/group.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IsNotEmpty, IsString } from 'class-validator';
import { Expose } from 'class-transformer';

@Expose()
export class GroupDto {
@IsString()
@IsNotEmpty()
id: string;

@IsString()
@IsNotEmpty()
@Expose({ name: 'namespace_id' })
namespaceId: string;

@IsString()
title: string;
}
7 changes: 7 additions & 0 deletions src/groups/dto/update-group.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IsNotEmpty, IsString } from 'class-validator';

export class UpdateGroupDto {
@IsString()
@IsNotEmpty()
title: string;
}
34 changes: 34 additions & 0 deletions src/groups/entities/group-user.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Base } from 'src/common/base.entity';
import { User } from 'src/user/user.entity';
import { Group } from './group.entity';
import {
Entity,
PrimaryGeneratedColumn,
ManyToOne,
JoinColumn,
Index,
Column,
} from 'typeorm';
import { Namespace } from 'src/namespaces/entities/namespace.entity';

@Entity('group_users')
@Index(['namespace', 'group', 'user'], {
unique: true,
where: 'deleted_at IS NULL',
})
export class GroupUser extends Base {
@PrimaryGeneratedColumn()
id: number;

@ManyToOne(() => Namespace, { nullable: false })
@JoinColumn({ name: 'namespace_id' })
namespace: Namespace;

@ManyToOne(() => Group, { nullable: false })
@JoinColumn({ name: 'group_id' })
group: Group;

@ManyToOne(() => User, { nullable: false })
@JoinColumn({ name: 'user_id' })
user: User;
}
31 changes: 31 additions & 0 deletions src/groups/entities/group.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Base } from 'src/common/base.entity';
import { Namespace } from 'src/namespaces/entities/namespace.entity';
import generateId from 'src/utils/generate_id';
import {
Entity,
ManyToOne,
JoinColumn,
Column,
PrimaryColumn,
BeforeInsert,
} from 'typeorm';

@Entity('groups')
export class Group extends Base {
@PrimaryColumn('varchar', {
length: 6,
})
id: string;

@BeforeInsert()
generateId?() {
this.id = generateId(6);
}

@Column()
title: string;

@ManyToOne(() => Namespace, { nullable: false })
@JoinColumn({ name: 'namespace_id' })
namespace: Namespace;
}
129 changes: 129 additions & 0 deletions src/groups/groups.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
Req,
UnauthorizedException,
} from '@nestjs/common';
import { GroupsService } from './groups.service';
import { CreateGroupDto } from './dto/create-group.dto';
import { plainToInstance } from 'class-transformer';
import { GroupDto } from './dto/group.dto';
import { UpdateGroupDto } from './dto/update-group.dto';
import { GroupUserDto } from './dto/group-user.dto';
import { AddGroupUserDto } from './dto/add-group-user.dto';

@Controller('api/v1/namespaces/:namespaceId/groups')
export class GroupController {
constructor(private readonly groupsService: GroupsService) {}

@Get()
async list(@Req() req, @Param('namespaceId') namespaceId: string) {
const groups = await this.groupsService.listGroupByUser(
namespaceId,
req.user.id,
);
return plainToInstance(GroupDto, groups);
}

@Post()
async create(
@Req() req,
@Param('namespaceId') namespaceId: string,
@Body() createGroupDto: CreateGroupDto,
) {
const group = await this.groupsService.createGroup(
namespaceId,
req.user.id,
createGroupDto,
);
return plainToInstance(GroupDto, group);
}

@Patch(':groupId')
async update(
@Req() req,
@Param('namespaceId') namespaceId: string,
@Param('groupId') groupId: string,
@Body() updateGroupDto: UpdateGroupDto,
) {
if (
!(await this.groupsService.userInGroup(namespaceId, groupId, req.user.id))
) {
throw new UnauthorizedException('current user is not in this group');
}
const group = await this.groupsService.updateGroup(
namespaceId,
groupId,
updateGroupDto,
);
return plainToInstance(GroupDto, group);
}

@Delete(':groupId')
async delete(
@Req() req,
@Param('namespaceId') namespaceId: string,
@Param('groupId') groupId: string,
) {
if (
!(await this.groupsService.userInGroup(namespaceId, groupId, req.user.id))
) {
throw new UnauthorizedException('current user is not in this group');
}
await this.groupsService.deleteGroup(namespaceId, groupId);
}

@Get(':groupId/users')
async listGroupUsers(
@Req() req,
@Param('namespaceId') namespaceId: string,
@Param('groupId') groupId: string,
) {
if (
!(await this.groupsService.userInGroup(namespaceId, groupId, req.user.id))
) {
throw new UnauthorizedException('current user is not in this group');
}
const users = await this.groupsService.listGroupUsers(namespaceId, groupId);
return plainToInstance(GroupUserDto, users);
}

@Post(':groupId/users')
async addGroupUser(
@Req() req,
@Param('namespaceId') namespaceId: string,
@Param('groupId') groupId: string,
@Body() addGroupUserDto: AddGroupUserDto,
) {
if (
!(await this.groupsService.userInGroup(namespaceId, groupId, req.user.id))
) {
throw new UnauthorizedException('current user is not in this group');
}
await this.groupsService.addGroupUser(
namespaceId,
groupId,
addGroupUserDto.userId,
);
}

@Delete(':groupId/users/:userId')
async deleteGroupUser(
@Req() req,
@Param('namespaceId') namespaceId: string,
@Param('groupId') groupId: string,
@Param('userId') userId: string,
) {
if (
!(await this.groupsService.userInGroup(namespaceId, groupId, req.user.id))
) {
throw new UnauthorizedException('current user is not in this group');
}
await this.groupsService.deleteGroupUser(namespaceId, groupId, userId);
}
}
16 changes: 16 additions & 0 deletions src/groups/groups.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Module } from '@nestjs/common';
import { GroupsService } from './groups.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Group } from './entities/group.entity';
import { GroupUser } from './entities/group-user.entity';

@Module({
providers: [GroupsService],
exports: [GroupsService],
controllers: [],
imports: [
TypeOrmModule.forFeature([Group]),
TypeOrmModule.forFeature([GroupUser]),
],
})
export class GroupsModule {}
Loading