283 lines
6.9 KiB
Go
283 lines
6.9 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.29.0
|
|
// source: roles.sql
|
|
|
|
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const addPermissionsToRoleByKey = `-- name: AddPermissionsToRoleByKey :exec
|
|
INSERT INTO role_permissions (role_id, permission_id)
|
|
SELECT
|
|
$1,
|
|
p.id
|
|
FROM
|
|
permissions p
|
|
JOIN
|
|
unnest($2::text[]) AS key_str
|
|
ON key_str = p.scope || '_' || p.name
|
|
`
|
|
|
|
type AddPermissionsToRoleByKeyParams struct {
|
|
RoleID uuid.UUID `json:"role_id"`
|
|
PermissionKeys []string `json:"permission_keys"`
|
|
}
|
|
|
|
func (q *Queries) AddPermissionsToRoleByKey(ctx context.Context, arg AddPermissionsToRoleByKeyParams) error {
|
|
_, err := q.db.Exec(ctx, addPermissionsToRoleByKey, arg.RoleID, arg.PermissionKeys)
|
|
return err
|
|
}
|
|
|
|
const assignRolePermission = `-- name: AssignRolePermission :exec
|
|
INSERT INTO role_permissions (role_id, permission_id)
|
|
VALUES (
|
|
$1,
|
|
(
|
|
SELECT id
|
|
FROM permissions p
|
|
WHERE p.scope = split_part($2, '_', 1)
|
|
AND p.name = right($2, length($2) - position('_' IN $2))
|
|
)
|
|
)
|
|
`
|
|
|
|
type AssignRolePermissionParams struct {
|
|
RoleID uuid.UUID `json:"role_id"`
|
|
Key string `json:"key"`
|
|
}
|
|
|
|
func (q *Queries) AssignRolePermission(ctx context.Context, arg AssignRolePermissionParams) error {
|
|
_, err := q.db.Exec(ctx, assignRolePermission, arg.RoleID, arg.Key)
|
|
return err
|
|
}
|
|
|
|
const assignUserRole = `-- name: AssignUserRole :exec
|
|
INSERT INTO user_roles (user_id, role_id)
|
|
VALUES ($1, (
|
|
SELECT id FROM roles r
|
|
WHERE r.scope = split_part($2, '_', 1)
|
|
AND r.name = right($2, length($2) - position('_' IN $2))
|
|
))
|
|
`
|
|
|
|
type AssignUserRoleParams struct {
|
|
UserID uuid.UUID `json:"user_id"`
|
|
Key string `json:"key"`
|
|
}
|
|
|
|
func (q *Queries) AssignUserRole(ctx context.Context, arg AssignUserRoleParams) error {
|
|
_, err := q.db.Exec(ctx, assignUserRole, arg.UserID, arg.Key)
|
|
return err
|
|
}
|
|
|
|
const createRole = `-- name: CreateRole :one
|
|
INSERT INTO roles (name, scope, description)
|
|
VALUES ($1, $2, $3)
|
|
RETURNING id, name, scope, description
|
|
`
|
|
|
|
type CreateRoleParams struct {
|
|
Name string `json:"name"`
|
|
Scope string `json:"scope"`
|
|
Description *string `json:"description"`
|
|
}
|
|
|
|
func (q *Queries) CreateRole(ctx context.Context, arg CreateRoleParams) (Role, error) {
|
|
row := q.db.QueryRow(ctx, createRole, arg.Name, arg.Scope, arg.Description)
|
|
var i Role
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Scope,
|
|
&i.Description,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const findRole = `-- name: FindRole :one
|
|
SELECT id, name, scope, description
|
|
FROM roles
|
|
WHERE scope = $1 AND name = $2
|
|
`
|
|
|
|
type FindRoleParams struct {
|
|
Scope string `json:"scope"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
func (q *Queries) FindRole(ctx context.Context, arg FindRoleParams) (Role, error) {
|
|
row := q.db.QueryRow(ctx, findRole, arg.Scope, arg.Name)
|
|
var i Role
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Scope,
|
|
&i.Description,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const findUserRole = `-- name: FindUserRole :one
|
|
SELECT user_id, role_id FROM user_roles
|
|
WHERE user_id = $1 AND role_id = (SELECT id FROM roles r WHERE r.scope = split_part($2, '_', 1) AND r.name = right($2, length($2) - position('_' IN $2)))
|
|
LIMIT 1
|
|
`
|
|
|
|
type FindUserRoleParams struct {
|
|
UserID uuid.UUID `json:"user_id"`
|
|
Key string `json:"key"`
|
|
}
|
|
|
|
func (q *Queries) FindUserRole(ctx context.Context, arg FindUserRoleParams) (UserRole, error) {
|
|
row := q.db.QueryRow(ctx, findUserRole, arg.UserID, arg.Key)
|
|
var i UserRole
|
|
err := row.Scan(&i.UserID, &i.RoleID)
|
|
return i, err
|
|
}
|
|
|
|
const getRoleAssignment = `-- name: GetRoleAssignment :one
|
|
SELECT role_id, permission_id FROM role_permissions
|
|
WHERE role_id = $1 AND permission_id = (SELECT id FROM permissions p WHERE p.scope = split_part($2, '_', 1) AND p.name = right($2, length($2) - position('_' IN $2)))
|
|
LIMIT 1
|
|
`
|
|
|
|
type GetRoleAssignmentParams struct {
|
|
RoleID uuid.UUID `json:"role_id"`
|
|
Key string `json:"key"`
|
|
}
|
|
|
|
func (q *Queries) GetRoleAssignment(ctx context.Context, arg GetRoleAssignmentParams) (RolePermission, error) {
|
|
row := q.db.QueryRow(ctx, getRoleAssignment, arg.RoleID, arg.Key)
|
|
var i RolePermission
|
|
err := row.Scan(&i.RoleID, &i.PermissionID)
|
|
return i, err
|
|
}
|
|
|
|
const getRolesGroupedWithPermissions = `-- name: GetRolesGroupedWithPermissions :many
|
|
SELECT
|
|
r.scope,
|
|
json_agg (
|
|
json_build_object (
|
|
'id',
|
|
r.id,
|
|
'name',
|
|
r.name,
|
|
'scope',
|
|
r.scope,
|
|
'description',
|
|
r.description,
|
|
'permissions',
|
|
COALESCE(p_list.permissions, '[]')
|
|
)
|
|
ORDER BY
|
|
r.name
|
|
) AS roles
|
|
FROM
|
|
roles r
|
|
LEFT JOIN (
|
|
SELECT
|
|
rp.role_id,
|
|
json_agg (
|
|
json_build_object (
|
|
'id',
|
|
p.id,
|
|
'name',
|
|
p.name,
|
|
'scope',
|
|
p.scope,
|
|
'description',
|
|
p.description
|
|
)
|
|
ORDER BY
|
|
p.name
|
|
) AS permissions
|
|
FROM
|
|
role_permissions rp
|
|
JOIN permissions p ON p.id = rp.permission_id
|
|
GROUP BY
|
|
rp.role_id
|
|
) p_list ON p_list.role_id = r.id
|
|
GROUP BY
|
|
r.scope
|
|
`
|
|
|
|
type GetRolesGroupedWithPermissionsRow struct {
|
|
Scope string `json:"scope"`
|
|
Roles []byte `json:"roles"`
|
|
}
|
|
|
|
func (q *Queries) GetRolesGroupedWithPermissions(ctx context.Context) ([]GetRolesGroupedWithPermissionsRow, error) {
|
|
rows, err := q.db.Query(ctx, getRolesGroupedWithPermissions)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []GetRolesGroupedWithPermissionsRow
|
|
for rows.Next() {
|
|
var i GetRolesGroupedWithPermissionsRow
|
|
if err := rows.Scan(&i.Scope, &i.Roles); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getUserRoles = `-- name: GetUserRoles :many
|
|
SELECT r.id, r.name, r.scope, r.description FROM roles r
|
|
JOIN user_roles ur ON r.id = ur.role_id
|
|
WHERE ur.user_id = $1
|
|
`
|
|
|
|
func (q *Queries) GetUserRoles(ctx context.Context, userID uuid.UUID) ([]Role, error) {
|
|
rows, err := q.db.Query(ctx, getUserRoles, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Role
|
|
for rows.Next() {
|
|
var i Role
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Scope,
|
|
&i.Description,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const unassignUserRole = `-- name: UnassignUserRole :exec
|
|
DELETE FROM user_roles
|
|
WHERE user_id = $1 AND role_id = (
|
|
SELECT id FROM roles r
|
|
WHERE r.scope = split_part($2, '_', 1)
|
|
AND r.name = right($2, length($2) - position('_' IN $2))
|
|
)
|
|
`
|
|
|
|
type UnassignUserRoleParams struct {
|
|
UserID uuid.UUID `json:"user_id"`
|
|
Key string `json:"key"`
|
|
}
|
|
|
|
func (q *Queries) UnassignUserRole(ctx context.Context, arg UnassignUserRoleParams) error {
|
|
_, err := q.db.Exec(ctx, unassignUserRole, arg.UserID, arg.Key)
|
|
return err
|
|
}
|