add acl
This commit is contained in:
+199
-5
@@ -398,6 +398,45 @@ func (q *Queries) GetClusterPrincipal(ctx context.Context) (Principal, error) {
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getDocument = `-- name: GetDocument :one
|
||||
|
||||
SELECT id, kind, owner_id, created_by, parent_id, name, name_key, path, depth, position, head_version_id, metadata, policy_id, visibility, acl_inherit, rev, created_at, updated_at, deleted_at, deleted_by, trash_batch
|
||||
FROM document
|
||||
WHERE id = ?1
|
||||
`
|
||||
|
||||
// ============================================================================
|
||||
// document
|
||||
// ============================================================================
|
||||
func (q *Queries) GetDocument(ctx context.Context, id ident.UUID) (Document, error) {
|
||||
row := q.db.QueryRowContext(ctx, getDocument, id)
|
||||
var i Document
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Kind,
|
||||
&i.OwnerID,
|
||||
&i.CreatedBy,
|
||||
&i.ParentID,
|
||||
&i.Name,
|
||||
&i.NameKey,
|
||||
&i.Path,
|
||||
&i.Depth,
|
||||
&i.Position,
|
||||
&i.HeadVersionID,
|
||||
&i.Metadata,
|
||||
&i.PolicyID,
|
||||
&i.Visibility,
|
||||
&i.AclInherit,
|
||||
&i.Rev,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeletedAt,
|
||||
&i.DeletedBy,
|
||||
&i.TrashBatch,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getMembership = `-- name: GetMembership :one
|
||||
SELECT group_id, member_id, role, created_at
|
||||
FROM membership
|
||||
@@ -572,6 +611,53 @@ func (q *Queries) IncrementFailedAttempts(ctx context.Context, principalID ident
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listActiveACLEntriesForDocument = `-- name: ListActiveACLEntriesForDocument :many
|
||||
SELECT id, document_id, subject_id, permissions, propagate, expires_at, granted_by, created_at, revoked_at
|
||||
FROM acl_entry
|
||||
WHERE document_id = ?1
|
||||
AND revoked_at IS NULL
|
||||
AND (expires_at IS NULL OR expires_at > CAST(?2 AS INTEGER))
|
||||
ORDER BY created_at
|
||||
`
|
||||
|
||||
type ListActiveACLEntriesForDocumentParams struct {
|
||||
DocumentID ident.UUID
|
||||
Now int64
|
||||
}
|
||||
|
||||
func (q *Queries) ListActiveACLEntriesForDocument(ctx context.Context, arg ListActiveACLEntriesForDocumentParams) ([]AclEntry, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listActiveACLEntriesForDocument, arg.DocumentID, arg.Now)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []AclEntry
|
||||
for rows.Next() {
|
||||
var i AclEntry
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.DocumentID,
|
||||
&i.SubjectID,
|
||||
&i.Permissions,
|
||||
&i.Propagate,
|
||||
&i.ExpiresAt,
|
||||
&i.GrantedBy,
|
||||
&i.CreatedAt,
|
||||
&i.RevokedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listActivePrincipals = `-- name: ListActivePrincipals :many
|
||||
SELECT id, kind, name, public_key, status, successor_id, created_at, disabled_at
|
||||
FROM principal
|
||||
@@ -735,7 +821,7 @@ func (q *Queries) ListMemberGroups(ctx context.Context, memberID ident.UUID) ([]
|
||||
}
|
||||
|
||||
const listMemberGroupsWithRole = `-- name: ListMemberGroupsWithRole :many
|
||||
SELECT p.id, p.name, m.role
|
||||
SELECT p.id, p.name, p.status, m.role
|
||||
FROM membership m
|
||||
JOIN principal p ON p.id = m.group_id
|
||||
WHERE m.member_id = ?1
|
||||
@@ -743,9 +829,10 @@ ORDER BY p.name
|
||||
`
|
||||
|
||||
type ListMemberGroupsWithRoleRow struct {
|
||||
ID ident.UUID
|
||||
Name string
|
||||
Role string
|
||||
ID ident.UUID
|
||||
Name string
|
||||
Status string
|
||||
Role string
|
||||
}
|
||||
|
||||
func (q *Queries) ListMemberGroupsWithRole(ctx context.Context, memberID ident.UUID) ([]ListMemberGroupsWithRoleRow, error) {
|
||||
@@ -757,7 +844,12 @@ func (q *Queries) ListMemberGroupsWithRole(ctx context.Context, memberID ident.U
|
||||
var items []ListMemberGroupsWithRoleRow
|
||||
for rows.Next() {
|
||||
var i ListMemberGroupsWithRoleRow
|
||||
if err := rows.Scan(&i.ID, &i.Name, &i.Role); err != nil {
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.Status,
|
||||
&i.Role,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
@@ -1093,6 +1185,38 @@ func (q *Queries) ResetFailedAttempts(ctx context.Context, principalID ident.UUI
|
||||
return i, err
|
||||
}
|
||||
|
||||
const revokeACLEntry = `-- name: RevokeACLEntry :one
|
||||
UPDATE acl_entry
|
||||
SET revoked_at = ?1
|
||||
WHERE document_id = ?2
|
||||
AND subject_id = ?3
|
||||
AND revoked_at IS NULL
|
||||
RETURNING id, document_id, subject_id, permissions, propagate, expires_at, granted_by, created_at, revoked_at
|
||||
`
|
||||
|
||||
type RevokeACLEntryParams struct {
|
||||
RevokedAt sql.NullInt64
|
||||
DocumentID ident.UUID
|
||||
SubjectID ident.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) RevokeACLEntry(ctx context.Context, arg RevokeACLEntryParams) (AclEntry, error) {
|
||||
row := q.db.QueryRowContext(ctx, revokeACLEntry, arg.RevokedAt, arg.DocumentID, arg.SubjectID)
|
||||
var i AclEntry
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.DocumentID,
|
||||
&i.SubjectID,
|
||||
&i.Permissions,
|
||||
&i.Propagate,
|
||||
&i.ExpiresAt,
|
||||
&i.GrantedBy,
|
||||
&i.CreatedAt,
|
||||
&i.RevokedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const revokeSession = `-- name: RevokeSession :one
|
||||
UPDATE session
|
||||
SET revoked_at = ?1
|
||||
@@ -1425,3 +1549,73 @@ func (q *Queries) UpdatePrincipalSuccessor(ctx context.Context, arg UpdatePrinci
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const upsertACLEntry = `-- name: UpsertACLEntry :one
|
||||
|
||||
INSERT INTO acl_entry (
|
||||
id,
|
||||
document_id,
|
||||
subject_id,
|
||||
permissions,
|
||||
propagate,
|
||||
expires_at,
|
||||
granted_by,
|
||||
created_at
|
||||
) VALUES (
|
||||
?1,
|
||||
?2,
|
||||
?3,
|
||||
?4,
|
||||
?5,
|
||||
?6,
|
||||
?7,
|
||||
?8
|
||||
)
|
||||
ON CONFLICT(document_id, subject_id) DO UPDATE SET
|
||||
permissions = excluded.permissions,
|
||||
propagate = excluded.propagate,
|
||||
expires_at = excluded.expires_at,
|
||||
granted_by = excluded.granted_by,
|
||||
revoked_at = NULL
|
||||
RETURNING id, document_id, subject_id, permissions, propagate, expires_at, granted_by, created_at, revoked_at
|
||||
`
|
||||
|
||||
type UpsertACLEntryParams struct {
|
||||
ID ident.UUID
|
||||
DocumentID ident.UUID
|
||||
SubjectID ident.UUID
|
||||
Permissions int64
|
||||
Propagate int64
|
||||
ExpiresAt sql.NullInt64
|
||||
GrantedBy ident.UUID
|
||||
CreatedAt int64
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// acl_entry
|
||||
// ============================================================================
|
||||
func (q *Queries) UpsertACLEntry(ctx context.Context, arg UpsertACLEntryParams) (AclEntry, error) {
|
||||
row := q.db.QueryRowContext(ctx, upsertACLEntry,
|
||||
arg.ID,
|
||||
arg.DocumentID,
|
||||
arg.SubjectID,
|
||||
arg.Permissions,
|
||||
arg.Propagate,
|
||||
arg.ExpiresAt,
|
||||
arg.GrantedBy,
|
||||
arg.CreatedAt,
|
||||
)
|
||||
var i AclEntry
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.DocumentID,
|
||||
&i.SubjectID,
|
||||
&i.Permissions,
|
||||
&i.Propagate,
|
||||
&i.ExpiresAt,
|
||||
&i.GrantedBy,
|
||||
&i.CreatedAt,
|
||||
&i.RevokedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user