30 lines
1.3 KiB
Go
30 lines
1.3 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"git.cin.su/m/Assistant/contracts/authority"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type AuthorityRepository struct{ pool *pgxpool.Pool }
|
|
|
|
func OpenAuthority(ctx context.Context, c ContinuityConfig) (*AuthorityRepository, error) {
|
|
p, e := pgxpool.New(ctx, fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s", c.Host, c.Port, c.Database, c.User, c.Password))
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
return &AuthorityRepository{p}, nil
|
|
}
|
|
func (r *AuthorityRepository) Close() { r.pool.Close() }
|
|
func (r *AuthorityRepository) LoadGrant(ctx context.Context, id authority.GrantID) (authority.AuthorityGrant, authority.Scope, error) {
|
|
var grant authority.AuthorityGrant
|
|
var scope authority.Scope
|
|
err := r.pool.QueryRow(ctx, `SELECT g.grant_id, g.actor_id, g.principal_id, g.scope_id, g.grant_class, g.revision, g.active, s.scope_id, s.global_scope, s.label FROM authority.authority_grants g JOIN authority.scopes s ON s.scope_id=g.scope_id WHERE g.grant_id=$1`, id).Scan(&grant.ID, &grant.ActorID, &grant.PrincipalID, &grant.ScopeID, &grant.Class, &grant.Revision, &grant.Active, &scope.ID, &scope.Global, &scope.Label)
|
|
if err != nil {
|
|
return authority.AuthorityGrant{}, authority.Scope{}, fmt.Errorf("load authority grant: %w", err)
|
|
}
|
|
return grant, scope, nil
|
|
}
|