Skip to content

Commit ed046ea

Browse files
authored
Merge pull request #208 from import-ai/fix/bug
fix(bug): bug fix
2 parents aea6633 + 0ef9cc0 commit ed046ea

File tree

5 files changed

+78
-27
lines changed

5 files changed

+78
-27
lines changed

src/auth/google/google.service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { NamespacesService } from 'omniboxd/namespaces/namespaces.service';
77
import { CreateUserBindingDto } from 'omniboxd/user/dto/create-user-binding.dto';
88
import {
99
BadRequestException,
10+
ForbiddenException,
1011
Injectable,
1112
Logger,
1213
UnauthorizedException,
@@ -235,6 +236,10 @@ export class GoogleService extends SocialService {
235236
}
236237

237238
async unbind(userId: string) {
239+
const canDo = await this.canUnBinding(userId);
240+
if (!canDo) {
241+
throw new ForbiddenException('Unbinding is not allowed');
242+
}
238243
await this.userService.unbindByLoginType(userId, 'google');
239244
}
240245
}

src/auth/social.service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,13 @@ export class SocialService {
8282
'Unable to generate a valid username',
8383
);
8484
}
85+
86+
protected async canUnBinding(userId: string) {
87+
const user = await this.userService.find(userId);
88+
if (user.email) {
89+
return true;
90+
}
91+
const binding = await this.userService.listBinding(userId);
92+
return binding.length > 1;
93+
}
8594
}

src/auth/wechat/wechat.service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { CreateUserBindingDto } from 'omniboxd/user/dto/create-user-binding.dto'
88
import {
99
Logger,
1010
Injectable,
11+
ForbiddenException,
1112
BadRequestException,
1213
UnauthorizedException,
1314
} from '@nestjs/common';
@@ -271,6 +272,10 @@ export class WechatService extends SocialService {
271272
}
272273

273274
async unbind(userId: string) {
275+
const canDo = await this.canUnBinding(userId);
276+
if (!canDo) {
277+
throw new ForbiddenException('Unbinding is not allowed');
278+
}
274279
await this.userService.unbindByLoginType(userId, 'wechat');
275280
}
276281
}

src/namespace-resources/namespace-resources.controller.ts

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ import {
1313
Patch,
1414
Post,
1515
Query,
16-
Req,
1716
} from '@nestjs/common';
1817
import { UserId } from 'omniboxd/decorators/user-id.decorator';
19-
import { Request } from 'express';
2018
import { ResourceMetaDto } from 'omniboxd/resources/dto/resource-meta.dto';
2119
import { ChildrenMetaDto } from './dto/list-children-resp.dto';
2220

@@ -29,7 +27,7 @@ export class NamespaceResourcesController {
2927

3028
@Get()
3129
async findById(
32-
@Req() req: Request,
30+
@UserId() userId: string,
3331
@Param('namespaceId') namespaceId: string,
3432
@Query('id') id: string,
3533
) {
@@ -40,11 +38,23 @@ export class NamespaceResourcesController {
4038
if (ids.length <= 0) {
4139
return [];
4240
}
43-
return await this.namespaceResourcesService.findByIds(
41+
const resources = await this.namespaceResourcesService.findByIds(
4442
namespaceId,
45-
req.user!.id,
43+
userId,
4644
ids,
4745
);
46+
return Promise.all(
47+
resources.map((resource) =>
48+
this.namespaceResourcesService
49+
.hasChildren(userId, namespaceId, resource.id)
50+
.then((hasChildren) =>
51+
Promise.resolve({
52+
...resource,
53+
hasChildren,
54+
}),
55+
),
56+
),
57+
);
4858
}
4959

5060
@Post()
@@ -62,33 +72,33 @@ export class NamespaceResourcesController {
6272

6373
@Post(':resourceId/duplicate')
6474
async duplicate(
65-
@Req() req: Request,
75+
@UserId() userId: string,
6676
@Param('namespaceId') namespaceId: string,
6777
@Param('resourceId') resourceId: string,
6878
) {
6979
const newResource = await this.namespaceResourcesService.duplicate(
70-
req.user!.id,
80+
userId,
7181
namespaceId,
7282
resourceId,
7383
);
7484
return await this.namespaceResourcesService.getResource({
7585
namespaceId,
76-
userId: req.user!.id,
86+
userId,
7787
resourceId: newResource.id,
7888
});
7989
}
8090

8191
@Get('query')
8292
async query(
83-
@Req() req: Request,
93+
@UserId() userId: string,
8494
@Param('namespaceId') namespaceId: string,
8595
@Query('parentId') parentId: string,
8696
@Query('tags') tags: string,
8797
) {
8898
return await this.namespaceResourcesService.query(
8999
namespaceId,
90100
parentId,
91-
req.user!.id,
101+
userId,
92102
tags,
93103
);
94104
}
@@ -108,15 +118,15 @@ export class NamespaceResourcesController {
108118

109119
@Post(':resourceId/move/:targetId')
110120
async move(
111-
@Req() req: Request,
121+
@UserId() userId: string,
112122
@Param('namespaceId') namespaceId: string,
113123
@Param('resourceId') resourceId: string,
114124
@Param('targetId') targetId: string,
115125
) {
116126
return await this.namespaceResourcesService.move(
117127
namespaceId,
118128
resourceId,
119-
req.user!.id,
129+
userId,
120130
targetId,
121131
);
122132
}
@@ -138,74 +148,83 @@ export class NamespaceResourcesController {
138148

139149
@Get(':resourceId')
140150
async get(
141-
@Req() req: Request,
151+
@UserId() userId: string,
142152
@Param('namespaceId') namespaceId: string,
143153
@Param('resourceId') resourceId: string,
144154
) {
145155
return await this.namespaceResourcesService.getResource({
146156
namespaceId,
147157
resourceId,
148-
userId: req.user!.id,
158+
userId,
149159
});
150160
}
151161

152162
@Patch(':resourceId')
153163
async update(
154-
@Req() req: Request,
164+
@UserId() userId: string,
155165
@Param('namespaceId') namespaceId: string,
156166
@Param('resourceId') resourceId: string,
157167
@Body() data: UpdateResourceDto,
158168
) {
159169
const hasPermission = await this.permissionsService.userHasPermission(
160170
namespaceId,
161171
resourceId,
162-
req.user!.id,
172+
userId,
163173
ResourcePermission.CAN_EDIT,
164174
);
165175
if (!hasPermission) {
166176
throw new ForbiddenException('Not authorized');
167177
}
168-
await this.namespaceResourcesService.update(req.user!.id, resourceId, data);
178+
await this.namespaceResourcesService.update(userId, resourceId, data);
169179
return await this.namespaceResourcesService.getResource({
170180
namespaceId,
171181
resourceId,
172-
userId: req.user!.id,
182+
userId,
173183
});
174184
}
175185

176186
@Delete(':resourceId')
177187
async delete(
178-
@Req() req: Request,
188+
@UserId() userId: string,
179189
@Param('namespaceId') namespaceId: string,
180190
@Param('resourceId') resourceId: string,
181191
) {
182192
const hasPermission = await this.permissionsService.userHasPermission(
183193
namespaceId,
184194
resourceId,
185-
req.user!.id,
195+
userId,
186196
ResourcePermission.CAN_EDIT,
187197
);
188198
if (!hasPermission) {
189199
throw new ForbiddenException('Not authorized');
190200
}
191201
return await this.namespaceResourcesService.delete(
192-
req.user!.id,
202+
userId,
193203
namespaceId,
194204
resourceId,
195205
);
196206
}
197207

198208
@Post(':resourceId/restore')
199209
async restore(
200-
@Req() req: Request,
210+
@UserId() userId: string,
201211
@Param('namespaceId') namespaceId: string,
202212
@Param('resourceId') resourceId: string,
203213
) {
204-
await this.namespaceResourcesService.restore(req.user!.id, resourceId);
205-
return await this.namespaceResourcesService.getResource({
214+
await this.namespaceResourcesService.restore(userId, resourceId);
215+
const resource = await this.namespaceResourcesService.getResource({
206216
namespaceId,
207217
resourceId,
208-
userId: req.user!.id,
218+
userId,
209219
});
220+
const hasChildren = await this.namespaceResourcesService.hasChildren(
221+
userId,
222+
namespaceId,
223+
resourceId,
224+
);
225+
return {
226+
...resource,
227+
hasChildren,
228+
};
210229
}
211230
}

src/namespace-resources/namespace-resources.service.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,19 @@ export class NamespaceResourcesService {
449449
});
450450
}
451451

452+
async hasChildren(
453+
userId: string,
454+
namespaceId: string,
455+
resourceId: string,
456+
): Promise<boolean> {
457+
const children = await this.getSubResourcesByUser(
458+
userId,
459+
namespaceId,
460+
resourceId,
461+
);
462+
return children.length > 0;
463+
}
464+
452465
async listChildren(
453466
namespaceId: string,
454467
resourceId: string,
@@ -666,8 +679,8 @@ export class NamespaceResourcesService {
666679
parentId?: string,
667680
resourceId?: string,
668681
) {
669-
const originalName = getOriginalFileName(fileName);
670-
const encodedName = encodeFileName(fileName);
682+
const originalName = decodeURIComponent(fileName);
683+
const encodedName = encodeFileName(originalName);
671684

672685
let resource: Resource;
673686
if (resourceId) {

0 commit comments

Comments
 (0)