75 lines
2.2 KiB
Go
75 lines
2.2 KiB
Go
package gapi
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"log/slog"
|
|
|
|
"github.com/itsscb/df/bff/pb"
|
|
"google.golang.org/genproto/googleapis/rpc/errdetails"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
func (server *Server) ListSessions(ctx context.Context, req *pb.ListSessionsRequest) (*pb.ListSessionsResponse, error) {
|
|
authPayload, err := server.authorizeUser(ctx)
|
|
if err != nil {
|
|
return nil, unauthenticatedError(err)
|
|
}
|
|
|
|
violations := validateListSessionsRequest(req)
|
|
if violations != nil {
|
|
return nil, invalidArgumentError(violations)
|
|
}
|
|
|
|
account, err := server.store.GetAccountByEmail(ctx, authPayload.Email)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, status.Errorf(codes.NotFound, "account not found")
|
|
}
|
|
slog.Error("list_sessions (get_account)", slog.String("invoked_by", authPayload.Email), slog.Int64("account_id", int64(req.GetAccountId())), slog.String("error", err.Error()))
|
|
return nil, status.Error(codes.Internal, "failed to get account")
|
|
}
|
|
|
|
if authPayload.Email != account.Email {
|
|
if !server.isAdmin(ctx, authPayload) {
|
|
return nil, status.Error(codes.NotFound, "account not found")
|
|
}
|
|
}
|
|
|
|
if account.ID != req.GetAccountId() {
|
|
if !server.isAdmin(ctx, authPayload) {
|
|
return nil, status.Error(codes.NotFound, "account not found")
|
|
}
|
|
}
|
|
|
|
dbSessions, err := server.store.ListSessions(ctx, account.Email)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, status.Error(codes.NotFound, "no accounts found")
|
|
}
|
|
slog.Error("list_sessions (db)", slog.String("invoked_by", authPayload.Email), slog.Int64("account_id", int64(req.GetAccountId())), slog.String("error", err.Error()))
|
|
return nil, status.Error(codes.NotFound, "failed to get accounts")
|
|
}
|
|
|
|
var sessions []*pb.Session
|
|
for _, s := range dbSessions {
|
|
sessions = append(sessions, convertSession(s))
|
|
}
|
|
|
|
rsp := &pb.ListSessionsResponse{
|
|
Sessions: sessions,
|
|
}
|
|
|
|
return rsp, nil
|
|
}
|
|
|
|
func validateListSessionsRequest(req *pb.ListSessionsRequest) (violations []*errdetails.BadRequest_FieldViolation) {
|
|
if req.GetAccountId() < 1 {
|
|
violations = append(violations, fieldViolation("account_id", errors.New("must be greater than 0")))
|
|
}
|
|
|
|
return violations
|
|
}
|