another one
This commit is contained in:
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user