v2: fixed oauth errors, oauth clients native control added
This commit is contained in:
@@ -1,88 +0,0 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/dusiburg/kotosoft-id/internal/models"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type CitizenshipRepository struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewCitizenshipRepository(db *pgxpool.Pool) *CitizenshipRepository {
|
||||
return &CitizenshipRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *CitizenshipRepository) GetApplicationByUserId(ctx context.Context, userID uuid.UUID) (*models.CitizenshipApplication, error) {
|
||||
var a models.CitizenshipApplication
|
||||
|
||||
err := r.db.QueryRow(
|
||||
ctx,
|
||||
`SELECT id, user_id, status, region_id, reviewed_by, reviewed_at, reject_reason, created_at
|
||||
FROM citizenship_applications WHERE user_id = $1`,
|
||||
userID,
|
||||
).Scan(
|
||||
&a.Id, &a.UserId, &a.Status, &a.RegionId,
|
||||
&a.ReviewedBy, &a.ReviewedAt, &a.RejectReason, &a.CreatedAt,
|
||||
)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &a, nil
|
||||
}
|
||||
|
||||
func (r *CitizenshipRepository) GetApplicationById(ctx context.Context, id uuid.UUID) (*models.CitizenshipApplication, error) {
|
||||
var a models.CitizenshipApplication
|
||||
|
||||
err := r.db.QueryRow(
|
||||
ctx,
|
||||
`SELECT id, user_id, status, region_id, reviewed_by, reviewed_at, reject_reason, created_at
|
||||
FROM citizenship_applications WHERE id = $1`,
|
||||
id,
|
||||
).Scan(
|
||||
&a.Id, &a.UserId, &a.Status, &a.RegionId,
|
||||
&a.ReviewedBy, &a.ReviewedAt, &a.RejectReason, &a.CreatedAt,
|
||||
)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &a, nil
|
||||
}
|
||||
|
||||
func (r *CitizenshipRepository) CreateApplication(ctx context.Context, application models.CitizenshipApplication) (*models.CitizenshipApplication, error) {
|
||||
err := r.db.QueryRow(
|
||||
ctx,
|
||||
`INSERT INTO citizenship_applications (user_id, region_id, status)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id, status, created_at`,
|
||||
application.UserId, application.RegionId, application.Status,
|
||||
).Scan(&application.Id, &application.Status, &application.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &application, nil
|
||||
}
|
||||
|
||||
func (r *CitizenshipRepository) UpdateApplication(ctx context.Context, id uuid.UUID, status models.CitizenshipApplicationStatus, reviewedBy uuid.UUID, rejectReason *string) error {
|
||||
_, err := r.db.Exec(ctx, `
|
||||
UPDATE citizenship_applications
|
||||
SET status = $1, reviewed_by = $2, reviewed_at = now(), reject_reason = $3
|
||||
WHERE id = $4
|
||||
`, status, reviewedBy, rejectReason, id)
|
||||
|
||||
return err
|
||||
}
|
||||
@@ -63,6 +63,77 @@ func (r *OAuthRepository) GetOAuthClientByID(ctx context.Context, id uuid.UUID)
|
||||
return &client, nil
|
||||
}
|
||||
|
||||
func (r *OAuthRepository) ListOAuthClients(ctx context.Context) ([]models.OAuthClient, error) {
|
||||
rows, err := r.db.Query(ctx, `
|
||||
SELECT id, client_id, client_secret, name, redirect_uris, grant_types, scopes, is_active, trusted, created_at, updated_at
|
||||
FROM oauth_clients
|
||||
ORDER BY created_at DESC
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var clients []models.OAuthClient
|
||||
for rows.Next() {
|
||||
var c models.OAuthClient
|
||||
if err := rows.Scan(
|
||||
&c.ID, &c.ClientID, &c.ClientSecret, &c.Name,
|
||||
&c.RedirectURIs, &c.GrantTypes, &c.Scopes,
|
||||
&c.IsActive, &c.Trusted, &c.CreatedAt, &c.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
clients = append(clients, c)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return clients, nil
|
||||
}
|
||||
|
||||
func (r *OAuthRepository) GetOAuthClientByIDAdmin(ctx context.Context, id uuid.UUID) (*models.OAuthClient, error) {
|
||||
var client models.OAuthClient
|
||||
err := r.db.QueryRow(ctx, `
|
||||
SELECT id, client_id, client_secret, name, redirect_uris, grant_types, scopes, is_active, trusted, created_at, updated_at
|
||||
FROM oauth_clients WHERE id = $1
|
||||
`, id).Scan(
|
||||
&client.ID, &client.ClientID, &client.ClientSecret, &client.Name,
|
||||
&client.RedirectURIs, &client.GrantTypes, &client.Scopes,
|
||||
&client.IsActive, &client.Trusted, &client.CreatedAt, &client.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &client, nil
|
||||
}
|
||||
|
||||
func (r *OAuthRepository) UpdateOAuthClient(ctx context.Context, client models.OAuthClient) (*models.OAuthClient, error) {
|
||||
err := r.db.QueryRow(ctx, `
|
||||
UPDATE oauth_clients
|
||||
SET name = $1,
|
||||
redirect_uris = $2,
|
||||
grant_types = $3,
|
||||
scopes = $4,
|
||||
trusted = $5,
|
||||
is_active = $6,
|
||||
updated_at = now()
|
||||
WHERE id = $7
|
||||
RETURNING updated_at
|
||||
`, client.Name, client.RedirectURIs, client.GrantTypes, client.Scopes,
|
||||
client.Trusted, client.IsActive, client.ID).
|
||||
Scan(&client.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("update oauth client: %w", err)
|
||||
}
|
||||
return &client, nil
|
||||
}
|
||||
|
||||
func (r *OAuthRepository) DeactivateOAuthClient(ctx context.Context, id uuid.UUID) error {
|
||||
_, err := r.db.Exec(ctx, `UPDATE oauth_clients SET is_active = false, updated_at = now() WHERE id = $1`, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *OAuthRepository) CreateAuthorizationCode(ctx context.Context, row models.AuthorizationCodeRow) (*models.AuthorizationCodeRow, error) {
|
||||
err := r.db.QueryRow(ctx, `
|
||||
INSERT INTO authorization_codes (code_hash, user_id, client_id, redirect_uri, scopes, code_challenge, code_challenge_method, expires_at, consumed_at, created_at)
|
||||
|
||||
@@ -1,474 +0,0 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/dusiburg/kotosoft-id/internal/models"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type PositionRepository struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewPositionRepository(db *pgxpool.Pool) *PositionRepository {
|
||||
return &PositionRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *PositionRepository) GetAllPermissions(ctx context.Context, locale string) ([]models.Permission, error) {
|
||||
rows, err := r.db.Query(ctx, `
|
||||
SELECT p.id,
|
||||
p.code,
|
||||
COALESCE(pl.name, pe.name, p.code) AS name,
|
||||
COALESCE(pl.description, pe.description, p.description, '') AS description,
|
||||
p.created_at
|
||||
FROM permissions p
|
||||
LEFT JOIN permission_translations pl
|
||||
ON pl.permission_id = p.id AND pl.locale = $1
|
||||
LEFT JOIN permission_translations pe
|
||||
ON pe.permission_id = p.id AND pe.locale = 'en'
|
||||
ORDER BY code
|
||||
`, strings.ToLower(locale))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var result []models.Permission
|
||||
for rows.Next() {
|
||||
var p models.Permission
|
||||
if err := rows.Scan(&p.ID, &p.Code, &p.Name, &p.Description, &p.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, p)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
func (r *PositionRepository) CreatePermission(ctx context.Context, code, locale string, description *string) error {
|
||||
tx, err := r.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
var permissionID uuid.UUID
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO permissions (code, description)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id
|
||||
`, code, description).Scan(&permissionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO permission_translations (permission_id, locale, name, description)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (permission_id, locale) DO UPDATE
|
||||
SET name = EXCLUDED.name, description = EXCLUDED.description
|
||||
`, permissionID, strings.ToLower(locale), code, description)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (r *PositionRepository) UpdatePermission(ctx context.Context, code, locale string, description *string) error {
|
||||
tx, err := r.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
var permissionID uuid.UUID
|
||||
err = tx.QueryRow(ctx, `
|
||||
UPDATE permissions
|
||||
SET description = $2
|
||||
WHERE code = $1
|
||||
RETURNING id
|
||||
`, code, description).Scan(&permissionID)
|
||||
if err != nil {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO permission_translations (permission_id, locale, name, description)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (permission_id, locale) DO UPDATE
|
||||
SET name = EXCLUDED.name, description = EXCLUDED.description
|
||||
`, permissionID, strings.ToLower(locale), code, description)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (r *PositionRepository) DeletePermission(ctx context.Context, code string) error {
|
||||
tag, err := r.db.Exec(ctx, `DELETE FROM permissions WHERE code = $1`, code)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *PositionRepository) GetPermissionByCode(ctx context.Context, code string) (*models.Permission, error) {
|
||||
var p models.Permission
|
||||
err := r.db.QueryRow(ctx, `
|
||||
SELECT id, code, COALESCE(description, ''), created_at
|
||||
FROM permissions WHERE code = $1
|
||||
`, code).Scan(&p.ID, &p.Code, &p.Description, &p.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &p, nil
|
||||
}
|
||||
|
||||
func (r *PositionRepository) GetPositionPermissions(ctx context.Context, positionID uuid.UUID, locale string) ([]models.Permission, error) {
|
||||
rows, err := r.db.Query(ctx, `
|
||||
SELECT p.id,
|
||||
p.code,
|
||||
COALESCE(pl.name, pe.name, p.code) AS name,
|
||||
COALESCE(pl.description, pe.description, p.description, '') AS description,
|
||||
p.created_at
|
||||
FROM position_permissions pp
|
||||
JOIN permissions p ON pp.permission_id = p.id
|
||||
LEFT JOIN permission_translations pl
|
||||
ON pl.permission_id = p.id AND pl.locale = $2
|
||||
LEFT JOIN permission_translations pe
|
||||
ON pe.permission_id = p.id AND pe.locale = 'en'
|
||||
WHERE pp.position_id = $1
|
||||
ORDER BY p.code
|
||||
`, positionID, strings.ToLower(locale))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var result []models.Permission
|
||||
for rows.Next() {
|
||||
var p models.Permission
|
||||
if err := rows.Scan(&p.ID, &p.Code, &p.Name, &p.Description, &p.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, p)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
func (r *PositionRepository) AddPermissionToPosition(ctx context.Context, positionID, permissionID uuid.UUID) error {
|
||||
_, err := r.db.Exec(ctx, `
|
||||
INSERT INTO position_permissions (position_id, permission_id)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT DO NOTHING
|
||||
`, positionID, permissionID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *PositionRepository) RemovePermissionFromPosition(ctx context.Context, positionID, permissionID uuid.UUID) error {
|
||||
_, err := r.db.Exec(ctx, `
|
||||
DELETE FROM position_permissions
|
||||
WHERE position_id = $1 AND permission_id = $2
|
||||
`, positionID, permissionID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *PositionRepository) GetPositionByCode(ctx context.Context, code string) (*models.Position, error) {
|
||||
var p models.Position
|
||||
err := r.db.QueryRow(ctx, `
|
||||
SELECT id, code, created_at, updated_at
|
||||
FROM positions WHERE code = $1
|
||||
`, code).Scan(&p.ID, &p.Code, &p.CreatedAt, &p.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &p, nil
|
||||
}
|
||||
|
||||
func (r *PositionRepository) GetUserPositions(ctx context.Context, userID uuid.UUID, locale string) ([]models.PositionLocalized, error) {
|
||||
rows, err := r.db.Query(ctx, `
|
||||
SELECT p.id, p.code,
|
||||
COALESCE(pt.name, p.code) AS name,
|
||||
pt.description
|
||||
FROM user_positions up
|
||||
JOIN positions p ON up.position_id = p.id
|
||||
LEFT JOIN position_translations pt
|
||||
ON pt.position_id = p.id AND pt.locale = $2
|
||||
WHERE up.user_id = $1
|
||||
AND (up.valid_to IS NULL OR up.valid_to > now())
|
||||
ORDER BY name
|
||||
`, userID, strings.ToLower(locale))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var positions []posRow
|
||||
for rows.Next() {
|
||||
var r posRow
|
||||
if err := rows.Scan(&r.id, &r.p.Code, &r.p.Name, &r.p.Description); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
positions = append(positions, r)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
permMap, err := r.getPositionPermissionsBatch(ctx, positionIDs(positions), locale)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make([]models.PositionLocalized, len(positions))
|
||||
for i, pos := range positions {
|
||||
pos.p.Permissions = permMap[pos.id]
|
||||
if pos.p.Permissions == nil {
|
||||
pos.p.Permissions = []models.Permission{}
|
||||
}
|
||||
result[i] = pos.p
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *PositionRepository) GetAllPositions(ctx context.Context, locale string) ([]models.PositionLocalized, error) {
|
||||
rows, err := r.db.Query(ctx, `
|
||||
SELECT p.id, p.code,
|
||||
COALESCE(pt.name, p.code) AS name,
|
||||
pt.description
|
||||
FROM positions p
|
||||
LEFT JOIN position_translations pt
|
||||
ON pt.position_id = p.id AND pt.locale = $1
|
||||
ORDER BY name
|
||||
`, strings.ToLower(locale))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var positions []posRow
|
||||
for rows.Next() {
|
||||
var r posRow
|
||||
if err := rows.Scan(&r.id, &r.p.Code, &r.p.Name, &r.p.Description); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
positions = append(positions, r)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
permMap, err := r.getPositionPermissionsBatch(ctx, positionIDs(positions), locale)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make([]models.PositionLocalized, len(positions))
|
||||
for i, pos := range positions {
|
||||
pos.p.Permissions = permMap[pos.id]
|
||||
if pos.p.Permissions == nil {
|
||||
pos.p.Permissions = []models.Permission{}
|
||||
}
|
||||
result[i] = pos.p
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *PositionRepository) GetUsersWithPositions(ctx context.Context, locale string) ([]models.UserWithPositions, error) {
|
||||
rows, err := r.db.Query(ctx, `
|
||||
SELECT id, email, first_name, second_name, is_admin
|
||||
FROM users
|
||||
ORDER BY first_name, second_name, email
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var result []models.UserWithPositions
|
||||
for rows.Next() {
|
||||
var u models.UserWithPositions
|
||||
if err := rows.Scan(&u.ID, &u.Email, &u.FirstName, &u.SecondName, &u.IsAdmin); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
positions, err := r.GetUserPositions(ctx, u.ID, locale)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if positions == nil {
|
||||
positions = []models.PositionLocalized{}
|
||||
}
|
||||
u.Positions = positions
|
||||
|
||||
result = append(result, u)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
type posRow struct {
|
||||
id uuid.UUID
|
||||
p models.PositionLocalized
|
||||
}
|
||||
|
||||
func (pr posRow) getID() uuid.UUID { return pr.id }
|
||||
|
||||
func positionIDs(items []posRow) []uuid.UUID {
|
||||
ids := make([]uuid.UUID, len(items))
|
||||
for i, item := range items {
|
||||
ids[i] = item.id
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func (r *PositionRepository) getPositionPermissionsBatch(ctx context.Context, pIDs []uuid.UUID, locale string) (map[uuid.UUID][]models.Permission, error) {
|
||||
if len(pIDs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
rows, err := r.db.Query(ctx, `
|
||||
SELECT pp.position_id,
|
||||
p.id, p.code,
|
||||
COALESCE(pl.name, pe.name, p.code) AS name,
|
||||
COALESCE(pl.description, pe.description, p.description, '') AS description,
|
||||
p.created_at
|
||||
FROM position_permissions pp
|
||||
JOIN permissions p ON pp.permission_id = p.id
|
||||
LEFT JOIN permission_translations pl
|
||||
ON pl.permission_id = p.id AND pl.locale = $2
|
||||
LEFT JOIN permission_translations pe
|
||||
ON pe.permission_id = p.id AND pe.locale = 'en'
|
||||
WHERE pp.position_id = ANY($1)
|
||||
ORDER BY pp.position_id, p.code
|
||||
`, pIDs, strings.ToLower(locale))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
result := make(map[uuid.UUID][]models.Permission)
|
||||
for rows.Next() {
|
||||
var positionID uuid.UUID
|
||||
var perm models.Permission
|
||||
if err := rows.Scan(&positionID, &perm.ID, &perm.Code, &perm.Name, &perm.Description, &perm.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result[positionID] = append(result[positionID], perm)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
func (r *PositionRepository) UpsertPositionForLocale(ctx context.Context, code, locale, name string, description *string) error {
|
||||
tx, err := r.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
var positionID uuid.UUID
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO positions (code)
|
||||
VALUES ($1)
|
||||
ON CONFLICT (code) DO UPDATE SET updated_at = now()
|
||||
RETURNING id
|
||||
`, code).Scan(&positionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO position_translations (position_id, locale, name, description)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (position_id, locale) DO UPDATE
|
||||
SET name = EXCLUDED.name, description = EXCLUDED.description
|
||||
`, positionID, strings.ToLower(locale), name, description)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (r *PositionRepository) DeletePositionByCode(ctx context.Context, code string) error {
|
||||
_, err := r.db.Exec(ctx, `DELETE FROM positions WHERE code = $1`, code)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *PositionRepository) AssignPosition(ctx context.Context, userID, positionID uuid.UUID, assignedBy *uuid.UUID, validFrom time.Time, validTo *time.Time) error {
|
||||
_, err := r.db.Exec(ctx, `
|
||||
INSERT INTO user_positions (user_id, position_id, assigned_by, valid_from, valid_to)
|
||||
VALUES ($1, $2, $3, COALESCE($4, now()), $5)
|
||||
`, userID, positionID, assignedBy, validFrom, validTo)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *PositionRepository) RevokePosition(ctx context.Context, userID, positionID uuid.UUID) error {
|
||||
_, err := r.db.Exec(ctx, `
|
||||
UPDATE user_positions
|
||||
SET valid_to = now()
|
||||
WHERE user_id = $1
|
||||
AND position_id = $2
|
||||
AND valid_to IS NULL
|
||||
`, userID, positionID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *PositionRepository) UserHasPermission(ctx context.Context, userID uuid.UUID, permissionCode string) (bool, string, error) {
|
||||
var positionCode string
|
||||
err := r.db.QueryRow(ctx, `
|
||||
SELECT p.code
|
||||
FROM user_positions up
|
||||
JOIN positions p ON up.position_id = p.id
|
||||
JOIN position_permissions pp ON pp.position_id = p.id
|
||||
JOIN permissions perm ON pp.permission_id = perm.id
|
||||
WHERE up.user_id = $1
|
||||
AND perm.code = $2
|
||||
AND (up.valid_to IS NULL OR up.valid_to > now())
|
||||
LIMIT 1
|
||||
`, userID, permissionCode).Scan(&positionCode)
|
||||
if err != nil {
|
||||
return false, "", nil
|
||||
}
|
||||
return true, positionCode, nil
|
||||
}
|
||||
|
||||
func (r *PositionRepository) GetUserPermissions(ctx context.Context, userID uuid.UUID, locale string) ([]models.Permission, error) {
|
||||
rows, err := r.db.Query(ctx, `
|
||||
SELECT DISTINCT perm.id,
|
||||
perm.code,
|
||||
COALESCE(pl.name, pe.name, perm.code) AS name,
|
||||
COALESCE(pl.description, pe.description, perm.description, '') AS description,
|
||||
perm.created_at
|
||||
FROM user_positions up
|
||||
JOIN position_permissions pp ON pp.position_id = up.position_id
|
||||
JOIN permissions perm ON pp.permission_id = perm.id
|
||||
LEFT JOIN permission_translations pl
|
||||
ON pl.permission_id = perm.id AND pl.locale = $2
|
||||
LEFT JOIN permission_translations pe
|
||||
ON pe.permission_id = perm.id AND pe.locale = 'en'
|
||||
WHERE up.user_id = $1
|
||||
AND (up.valid_to IS NULL OR up.valid_to > now())
|
||||
ORDER BY perm.code
|
||||
`, userID, strings.ToLower(locale))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var result []models.Permission
|
||||
for rows.Next() {
|
||||
var p models.Permission
|
||||
if err := rows.Scan(&p.ID, &p.Code, &p.Name, &p.Description, &p.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, p)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
Reference in New Issue
Block a user