generated from VLADIMIR/template
up
This commit is contained in:
+31
-5
@@ -3,6 +3,7 @@ package app
|
||||
import (
|
||||
"context"
|
||||
|
||||
"evening_detective_server/internal/modules/processor_jwt"
|
||||
"evening_detective_server/internal/modules/roles"
|
||||
proto "evening_detective_server/proto"
|
||||
|
||||
@@ -73,10 +74,19 @@ func (s *server) GetGame(ctx context.Context, req *proto.GetGameReq) (*proto.Get
|
||||
if claimsErr != nil {
|
||||
return nil, claimsErr
|
||||
}
|
||||
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
||||
allowed, restricted := gameReadAccess(claims, s.uiService != nil && s.uiService.Environment == "venue")
|
||||
if !allowed {
|
||||
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
||||
}
|
||||
|
||||
if restricted {
|
||||
game, err := s.gameService.GetGame(ctx, int(req.ID))
|
||||
if err != nil {
|
||||
return nil, gameErrorStatus(err, "не удалось получить игру")
|
||||
}
|
||||
return &proto.GetGameRsp{Game: mapGameSummary(game)}, nil
|
||||
}
|
||||
|
||||
game, err := s.gameService.GetFullGame(ctx, int(req.ID))
|
||||
if err != nil {
|
||||
return nil, gameErrorStatus(err, "не удалось получить игру")
|
||||
@@ -132,7 +142,8 @@ func (s *server) GetGames(ctx context.Context, _ *proto.GetGamesReq) (*proto.Get
|
||||
if claimsErr != nil {
|
||||
return nil, claimsErr
|
||||
}
|
||||
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
||||
allowed, restricted := gameReadAccess(claims, s.uiService != nil && s.uiService.Environment == "venue")
|
||||
if !allowed {
|
||||
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
||||
}
|
||||
|
||||
@@ -141,9 +152,14 @@ func (s *server) GetGames(ctx context.Context, _ *proto.GetGamesReq) (*proto.Get
|
||||
return nil, gameErrorStatus(err, "не удалось получить игры")
|
||||
}
|
||||
|
||||
return &proto.GetGamesRsp{
|
||||
Games: mapGames(games),
|
||||
}, nil
|
||||
if restricted {
|
||||
result := make([]*proto.Game, 0, len(games))
|
||||
for _, game := range games {
|
||||
result = append(result, mapGameSummary(game))
|
||||
}
|
||||
return &proto.GetGamesRsp{Games: result}, nil
|
||||
}
|
||||
return &proto.GetGamesRsp{Games: mapGames(games)}, nil
|
||||
}
|
||||
|
||||
func (s *server) UpdateGame(ctx context.Context, req *proto.UpdateGameReq) (*proto.UpdateGameRsp, error) {
|
||||
@@ -260,3 +276,13 @@ func (s *server) ResetGame(ctx context.Context, req *proto.ResetGameReq) (*proto
|
||||
|
||||
return &proto.ResetGameRsp{}, nil
|
||||
}
|
||||
|
||||
func gameReadAccess(claims *processor_jwt.JWTClaims, venue bool) (allowed, restricted bool) {
|
||||
if roles.HasRole(claims, roles.Organizer) || roles.HasRole(claims, roles.Author) {
|
||||
return true, false
|
||||
}
|
||||
if venue && roles.HasRole(claims, roles.Developer) {
|
||||
return true, true
|
||||
}
|
||||
return false, false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user