generated from VLADIMIR/template
add full auth
This commit is contained in:
@@ -2,8 +2,13 @@ package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"evening_detective_server/internal/modules/processor_jwt"
|
||||
"evening_detective_server/internal/services/users_service"
|
||||
proto "evening_detective_server/proto"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
type server struct {
|
||||
@@ -73,3 +78,34 @@ func (s *server) Refresh(ctx context.Context, req *proto.RefreshReq) (*proto.Ref
|
||||
RefreshToken: refreshToken,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *server) GetUsers(ctx context.Context, req *proto.GetUsersReq) (*proto.GetUsersRsp, error) {
|
||||
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
||||
if !claims.HasRole("admin") {
|
||||
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
||||
}
|
||||
users, err := s.usersService.GetUsers(ctx)
|
||||
if err != nil {
|
||||
return &proto.GetUsersRsp{
|
||||
Error: err.Error(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
resUsers := make([]*proto.User, 0, len(users))
|
||||
for _, user := range users {
|
||||
resUsers = append(
|
||||
resUsers,
|
||||
&proto.User{
|
||||
Id: int32(user.ID),
|
||||
Username: user.Username,
|
||||
Email: user.Email,
|
||||
Roles: user.Roles,
|
||||
IsActive: user.IsActive,
|
||||
CreatedAt: timestamppb.New(user.CreatedAt),
|
||||
},
|
||||
)
|
||||
}
|
||||
return &proto.GetUsersRsp{
|
||||
Users: resUsers,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user