在控制器中,我为用户对象添加了防护,注入了一些服务并调用该服务以获得一些响应。为了简洁起见,我删除了很多代码。
@Controller()
@UseGuards(AuthGuard())
export class UserController() {
constructor(private readonly userService: UsersService) {
}
@Get(':id')
async findOne(@Param('id') id) {
return await this.userService.findOne(id);
}
}
Run Code Online (Sandbox Code Playgroud)
由于具有AuthGuard
,我现在知道用户在输入:id
路由之前已登录。
在服务中,我会做类似的事情
@Injectable()
export class UsersService {
async findOne(id: number): Promise<User> {
return await this.usersRepository.findOne({where: {id: id}});
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当然,我们希望进行一些检查,以确保登录的用户可以访问正在查询的用户。现在的问题是如何获取当前登录的用户。我可以从控制器将其作为参数发送,但是由于很多后端都需要在当前用户上检查安全性,因此我不确定这是一个好主意。
@Get(':id)
async findOne(@Param('id') id, @Req() req: any) {
return await this.userService.findOne(id, req.user);
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,这行不通,我可以在UserService中获取它:
async findOne(id: number, @Req req: any): Promise<User> {
if (id === req.user.id || req.user.roles.contains('ADMIN')) {
return await this.userRepository.findOne({where: {id: …
Run Code Online (Sandbox Code Playgroud) 我正在尝试建立一个有很多类型的游戏,每种类型都有很多尺寸,每种尺寸都有很多难度.但RoomConfig Build指出:
public RoomConfig.Builder setVariant (int variant)
Sets the variant for the room when calling createRoom(RoomConfig). This is an optional, developer-controlled parameter describing the type of game to play, and is used for auto-matching criteria. Must be either a value from 1 to 1023 (inclusive), or ROOM_VARIANT_ANY (the default) if not desired.
public RoomConfig.Builder setVariant (int variant)
Sets the variant for the room when calling createRoom(RoomConfig). This is an optional, developer-controlled parameter describing the type of game to play, and …