feat: add permissions on user create, CORS middleware, cors server playground.

This commit is contained in:
2026-04-24 10:55:40 +02:00
parent d629bd52eb
commit 2fd3a1d57b
8 changed files with 179 additions and 3 deletions
@@ -4,6 +4,8 @@ import (
"context"
"database/sql"
"time"
"github.com/lib/pq"
)
// Define a Permissions slice, which we will use to will hold the permission codes (like
@@ -64,3 +66,18 @@ func (m PermissionModel) GetAllForUser(userID int64) (Permissions, error) {
return permissions, nil
}
// AddForUser - Add the provided permission codes for a specific user. Notice that we're using a
// variadic parameter for the codes so that we can assign multiple permissions in a
// single call.
func (m PermissionModel) AddForUser(userID int64, codes ...string) error {
query := `
INSERT INTO users_permissions
SELECT $1, permissions.id FROM permissions WHERE permissions.code = ANY($2)`
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
_, err := m.DB.ExecContext(ctx, query, userID, pq.Array(codes))
return err
}