mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-02-05 19:18:59 -06:00
208 lines
5.4 KiB
Go
208 lines
5.4 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.26.0
|
|
// source: permissions.sql
|
|
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const readDir = `-- name: ReadDir :many
|
|
WITH RECURSIVE nodes(id, parent, name, dir, created, modified, content_size, content_type, content_sha256, depth, path, permissions) AS (
|
|
SELECT r.id, r.parent, r.name, r.dir, r.created, r.modified, r.content_size, r.content_type, r.content_sha256, 0, ''::text, r.permissions
|
|
FROM resources r
|
|
WHERE r.id = $2::uuid
|
|
UNION ALL
|
|
SELECT r.id, r.parent, r.name, r.dir, r.created, r.modified, r.content_size, r.content_type, r.content_sha256, n.depth + 1, concat(n.path, '/', r.name), r.permissions
|
|
FROM resources r JOIN nodes n on r.parent = n.id
|
|
WHERE deleted IS NULL
|
|
AND r.id != $2::uuid
|
|
AND CASE WHEN $3::boolean THEN true ELSE depth < 1 END
|
|
)
|
|
SELECT id, parent, name, dir, created, modified, content_size, content_type, content_sha256, depth, path, permissions from nodes
|
|
WHERE CASE WHEN $1::boolean THEN true ELSE depth > 0 END
|
|
`
|
|
|
|
type ReadDirParams struct {
|
|
IncludeRoot bool
|
|
ID uuid.UUID
|
|
Recursive bool
|
|
}
|
|
|
|
type ReadDirRow struct {
|
|
ID uuid.UUID
|
|
Parent *uuid.UUID
|
|
Name string
|
|
Dir bool
|
|
Created pgtype.Timestamp
|
|
Modified pgtype.Timestamp
|
|
ContentSize int64
|
|
ContentType string
|
|
ContentSha256 string
|
|
Depth int32
|
|
Path string
|
|
Permissions []byte
|
|
}
|
|
|
|
func (q *Queries) ReadDir(ctx context.Context, arg ReadDirParams) ([]ReadDirRow, error) {
|
|
rows, err := q.db.Query(ctx, readDir, arg.IncludeRoot, arg.ID, arg.Recursive)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ReadDirRow
|
|
for rows.Next() {
|
|
var i ReadDirRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Parent,
|
|
&i.Name,
|
|
&i.Dir,
|
|
&i.Created,
|
|
&i.Modified,
|
|
&i.ContentSize,
|
|
&i.ContentType,
|
|
&i.ContentSha256,
|
|
&i.Depth,
|
|
&i.Path,
|
|
&i.Permissions,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const resourceByID = `-- name: ResourceByID :one
|
|
WITH RECURSIVE nodes(resid, id, parent, inherited_permissions, found) AS (
|
|
SELECT $2::uuid, r.id, r.parent, '{}'::jsonb,
|
|
CASE
|
|
WHEN r.id = $3::uuid THEN true
|
|
ELSE false
|
|
END
|
|
FROM resources r
|
|
WHERE r.id = $2::uuid
|
|
UNION ALL
|
|
SELECT n.resid, r.id, r.parent, jsonb_bitwise_or(r.permissions, n.inherited_permissions),
|
|
CASE
|
|
WHEN r.id = $3::uuid THEN true
|
|
ELSE n.found
|
|
END
|
|
FROM resources r
|
|
JOIN nodes n
|
|
ON r.id = n.parent
|
|
)
|
|
SELECT resid AS id, r.permissions, n.inherited_permissions, COALESCE((jsonb_bitwise_or(r.permissions, n.inherited_permissions)->($1::text))::int, 0)::int AS user_permission, found, r.parent, name, dir, created, modified, deleted, content_size, content_type, content_sha256 FROM nodes n
|
|
JOIN resources r
|
|
ON r.id = n.resid
|
|
WHERE n.parent IS NULL
|
|
`
|
|
|
|
type ResourceByIDParams struct {
|
|
Username string
|
|
ResourceID uuid.UUID
|
|
Root uuid.UUID
|
|
}
|
|
|
|
type ResourceByIDRow struct {
|
|
ID uuid.UUID
|
|
Permissions []byte
|
|
InheritedPermissions []byte
|
|
UserPermission int32
|
|
Found bool
|
|
Parent *uuid.UUID
|
|
Name string
|
|
Dir bool
|
|
Created pgtype.Timestamp
|
|
Modified pgtype.Timestamp
|
|
Deleted pgtype.Timestamp
|
|
ContentSize int64
|
|
ContentType string
|
|
ContentSha256 string
|
|
}
|
|
|
|
func (q *Queries) ResourceByID(ctx context.Context, arg ResourceByIDParams) (ResourceByIDRow, error) {
|
|
row := q.db.QueryRow(ctx, resourceByID, arg.Username, arg.ResourceID, arg.Root)
|
|
var i ResourceByIDRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Permissions,
|
|
&i.InheritedPermissions,
|
|
&i.UserPermission,
|
|
&i.Found,
|
|
&i.Parent,
|
|
&i.Name,
|
|
&i.Dir,
|
|
&i.Created,
|
|
&i.Modified,
|
|
&i.Deleted,
|
|
&i.ContentSize,
|
|
&i.ContentType,
|
|
&i.ContentSha256,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const resourceByPath = `-- name: ResourceByPath :one
|
|
WITH RECURSIVE nodes(id, parent, search, depth) AS (
|
|
SELECT r.id, r.parent, $1::text[], 0
|
|
FROM resources r
|
|
WHERE r.id = $2::uuid
|
|
UNION ALL
|
|
SELECT r.id, r.parent, n.search, n.depth + 1
|
|
FROM resources r
|
|
JOIN nodes n ON r.parent = n.id
|
|
WHERE deleted IS NULL
|
|
AND r.name = n.search[n.depth + 1]
|
|
)
|
|
SELECT id, parent, search, depth FROM nodes WHERE cardinality(search) = depth
|
|
`
|
|
|
|
type ResourceByPathParams struct {
|
|
Search []string
|
|
Root uuid.UUID
|
|
}
|
|
|
|
type ResourceByPathRow struct {
|
|
ID uuid.UUID
|
|
Parent *uuid.UUID
|
|
Search []string
|
|
Depth int32
|
|
}
|
|
|
|
func (q *Queries) ResourceByPath(ctx context.Context, arg ResourceByPathParams) (ResourceByPathRow, error) {
|
|
row := q.db.QueryRow(ctx, resourceByPath, arg.Search, arg.Root)
|
|
var i ResourceByPathRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Parent,
|
|
&i.Search,
|
|
&i.Depth,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const updatePermissionsForResource = `-- name: UpdatePermissionsForResource :exec
|
|
UPDATE resources SET permissions[$1::text] = to_json($2::int) WHERE id = $3::uuid
|
|
`
|
|
|
|
type UpdatePermissionsForResourceParams struct {
|
|
Username string
|
|
Permission int32
|
|
ResourceID uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) UpdatePermissionsForResource(ctx context.Context, arg UpdatePermissionsForResourceParams) error {
|
|
_, err := q.db.Exec(ctx, updatePermissionsForResource, arg.Username, arg.Permission, arg.ResourceID)
|
|
return err
|
|
}
|