Files
phylum/server/internal/db/permissions.sql.go
2024-08-29 11:45:18 +05:30

335 lines
8.2 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 getInheritedPermissionsForResource = `-- name: GetInheritedPermissionsForResource :many
WITH RECURSIVE nodes(id, parent) AS (
SELECT r.id, r.parent
FROM resources r
WHERE r.id = $1::uuid
UNION ALL (
SELECT r.id, r.parent
FROM resources r
JOIN nodes n
ON r.id = n.parent
)
)
SELECT p.user_id, CAST(MAX(p.permission) AS INT) AS permission
FROM permissions p
JOIN nodes n
ON n.id = p.resource_id
GROUP BY p.user_id
`
type GetInheritedPermissionsForResourceRow struct {
UserID int32
Permission int32
}
func (q *Queries) GetInheritedPermissionsForResource(ctx context.Context, resourceID uuid.UUID) ([]GetInheritedPermissionsForResourceRow, error) {
rows, err := q.db.Query(ctx, getInheritedPermissionsForResource, resourceID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetInheritedPermissionsForResourceRow
for rows.Next() {
var i GetInheritedPermissionsForResourceRow
if err := rows.Scan(&i.UserID, &i.Permission); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getLocalPermissionsForResource = `-- name: GetLocalPermissionsForResource :many
SELECT p.user_id, CAST(p.permission AS INT) AS permission
FROM permissions p
WHERE p.resource_id = $1::uuid
`
type GetLocalPermissionsForResourceRow struct {
UserID int32
Permission int32
}
func (q *Queries) GetLocalPermissionsForResource(ctx context.Context, resourceID uuid.UUID) ([]GetLocalPermissionsForResourceRow, error) {
rows, err := q.db.Query(ctx, getLocalPermissionsForResource, resourceID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetLocalPermissionsForResourceRow
for rows.Next() {
var i GetLocalPermissionsForResourceRow
if err := rows.Scan(&i.UserID, &i.Permission); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const readDir = `-- name: ReadDir :many
WITH RECURSIVE nodes(id, parent, name, dir, created, modified, size, sha256sum, depth, path, permission) AS (
SELECT r.id, r.parent, r.name, r.dir, r.created, r.modified, r.size, r.sha256sum, 0, ''::text, $2::int
FROM resources r
WHERE r.id = $3::uuid
UNION ALL
SELECT r.id, r.parent, r.name, r.dir, r.created, r.modified, r.size, r.sha256sum, n.depth + 1, concat(n.path, '/', r.name),
CASE
WHEN p.permission > n.permission THEN p.permission
ELSE n.permission
END
FROM resources r JOIN nodes n on r.parent = n.id
LEFT JOIN permissions p
ON r.id = p.resource_id
AND p.user_id = $4::int
WHERE deleted IS NULL
AND r.id != $3::uuid
AND CASE WHEN $5::boolean THEN true ELSE depth < 1 END
)
SELECT id, parent, name, dir, created, modified, size, sha256sum, depth, path, permission from nodes
WHERE CASE WHEN $1::boolean THEN true ELSE depth > 0 END
`
type ReadDirParams struct {
IncludeRoot bool
Permission int32
ID uuid.UUID
UserID int32
Recursive bool
}
type ReadDirRow struct {
ID uuid.UUID
Parent *uuid.UUID
Name string
Dir bool
Created pgtype.Timestamp
Modified pgtype.Timestamp
Size pgtype.Int8
Sha256sum pgtype.Text
Depth int32
Path string
Permission int32
}
func (q *Queries) ReadDir(ctx context.Context, arg ReadDirParams) ([]ReadDirRow, error) {
rows, err := q.db.Query(ctx, readDir,
arg.IncludeRoot,
arg.Permission,
arg.ID,
arg.UserID,
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.Size,
&i.Sha256sum,
&i.Depth,
&i.Path,
&i.Permission,
); 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, found, permission) AS (
SELECT $1::uuid, r.id, r.parent,
CASE
WHEN r.id = $2::uuid THEN true
ELSE false
END,
CASE
WHEN p.permission IS NOT NULL THEN p.permission
ELSE 0
END
FROM resources r
LEFT JOIN permissions p
ON p.resource_id = r.id
AND p.user_id = $3::int
WHERE r.id = $1::uuid
UNION ALL
SELECT n.resid, r.id, r.parent,
CASE
WHEN r.id = $2::uuid THEN true
ELSE n.found
END,
CASE
WHEN p.permission > n.permission THEN p.permission
ELSE n.permission
END
FROM resources r
JOIN nodes n
ON r.id = n.parent
LEFT JOIN permissions p
ON r.id = p.resource_id AND p.user_id = $3::int
)
SELECT resid AS id, found, permission, r.parent, name, dir, created, modified, deleted, size, sha256sum FROM nodes n
JOIN resources r
ON r.id = n.resid
WHERE n.parent IS NULL
`
type ResourceByIDParams struct {
ResourceID uuid.UUID
Root uuid.UUID
UserID int32
}
type ResourceByIDRow struct {
ID uuid.UUID
Found bool
Permission int32
Parent *uuid.UUID
Name string
Dir bool
Created pgtype.Timestamp
Modified pgtype.Timestamp
Deleted pgtype.Timestamp
Size pgtype.Int8
Sha256sum pgtype.Text
}
func (q *Queries) ResourceByID(ctx context.Context, arg ResourceByIDParams) (ResourceByIDRow, error) {
row := q.db.QueryRow(ctx, resourceByID, arg.ResourceID, arg.Root, arg.UserID)
var i ResourceByIDRow
err := row.Scan(
&i.ID,
&i.Found,
&i.Permission,
&i.Parent,
&i.Name,
&i.Dir,
&i.Created,
&i.Modified,
&i.Deleted,
&i.Size,
&i.Sha256sum,
)
return i, err
}
const resourceByPath = `-- name: ResourceByPath :one
WITH RECURSIVE nodes(id, parent, name, dir, created, modified, size, sha256sum, depth, path, search, permission) AS (
SELECT r.id, r.parent, r.name, r.dir, r.created, r.modified, r.size, r.sha256sum, 0, ''::text, $1::text[], $2::int
FROM resources r
WHERE r.id = $3::uuid
UNION ALL
SELECT r.id, r.parent, r.name, r.dir, r.created, r.modified, r.size, r.sha256sum, n.depth + 1, concat(n.path, '/', r.name), n.search,
CASE
WHEN p.permission > n.permission THEN p.permission
ELSE n.permission
END
FROM resources r
JOIN nodes n ON r.parent = n.id
LEFT JOIN permissions p
ON r.id = p.resource_id
AND p.user_id = $4::int
WHERE deleted IS NULL
AND r.name = n.search[n.depth + 1]
)
SELECT id, parent, name, dir, created, modified, size, sha256sum, depth, path, search, permission FROM nodes WHERE cardinality(search) = depth
`
type ResourceByPathParams struct {
Search []string
Permission int32
Root uuid.UUID
UserID int32
}
type ResourceByPathRow struct {
ID uuid.UUID
Parent *uuid.UUID
Name string
Dir bool
Created pgtype.Timestamp
Modified pgtype.Timestamp
Size pgtype.Int8
Sha256sum pgtype.Text
Depth int32
Path string
Search []string
Permission int32
}
func (q *Queries) ResourceByPath(ctx context.Context, arg ResourceByPathParams) (ResourceByPathRow, error) {
row := q.db.QueryRow(ctx, resourceByPath,
arg.Search,
arg.Permission,
arg.Root,
arg.UserID,
)
var i ResourceByPathRow
err := row.Scan(
&i.ID,
&i.Parent,
&i.Name,
&i.Dir,
&i.Created,
&i.Modified,
&i.Size,
&i.Sha256sum,
&i.Depth,
&i.Path,
&i.Search,
&i.Permission,
)
return i, err
}
const updatePermissionsForResource = `-- name: UpdatePermissionsForResource :exec
INSERT INTO permissions(resource_id, user_id, permission)
VALUES($1::uuid, $2::int, $3::int)
ON CONFLICT(resource_id, user_id) DO UPDATE SET permission = $3::int
`
type UpdatePermissionsForResourceParams struct {
ResourceID uuid.UUID
UserID int32
Permission int32
}
func (q *Queries) UpdatePermissionsForResource(ctx context.Context, arg UpdatePermissionsForResourceParams) error {
_, err := q.db.Exec(ctx, updatePermissionsForResource, arg.ResourceID, arg.UserID, arg.Permission)
return err
}