another one

This commit is contained in:
2026-02-09 22:31:39 +11:00
parent 6d5983c376
commit 299c775aba

View File

@@ -2,7 +2,6 @@ package db
import ( import (
"context" "context"
"database/sql"
"git.haelnorr.com/h/oslstats/internal/roles" "git.haelnorr.com/h/oslstats/internal/roles"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -66,17 +65,13 @@ func HasRole(ctx context.Context, tx bun.Tx, userID int, roleName roles.Role) (b
if roleName == "" { if roleName == "" {
return false, errors.New("roleName cannot be empty") return false, errors.New("roleName cannot be empty")
} }
user := new(User) user, err := GetByID[User](tx, userID).
err := tx.NewSelect(). Relation("Roles").GetFirst(ctx)
Model(user).
Relation("Roles").
Where("u.id = ?", userID).
Scan(ctx)
if err != nil { if err != nil {
if errors.Is(err, sql.ErrNoRows) { return false, errors.Wrap(err, "GetByID")
return false, nil }
} if user == nil {
return false, errors.Wrap(err, "tx.NewSelect") return false, nil
} }
for _, role := range user.Roles { for _, role := range user.Roles {
if role.Name == roleName { if role.Name == roleName {