Initial commit

This commit is contained in:
2025-03-13 01:41:08 +01:00
commit 0c4e7eb9ab
16 changed files with 3377 additions and 0 deletions

13
queries/Cargo.toml Normal file
View File

@@ -0,0 +1,13 @@
[package]
name = "queries"
version = "0.1.0"
edition = "2021"
[dependencies]
cynic = { workspace = true }
[dev-dependencies]
insta = { workspace = true }
[build-dependencies]
cynic-codegen = "3.10.0"

7
queries/build.rs Normal file
View File

@@ -0,0 +1,7 @@
fn main() {
cynic_codegen::register_schema("lldap")
.from_sdl_file("schemas/lldap.graphql")
.unwrap()
.as_default()
.unwrap();
}

View File

@@ -0,0 +1,199 @@
type AttributeValue {
name: String!
value: [String!]!
schema: AttributeSchema!
}
type Mutation {
createUser(user: CreateUserInput!): User!
createGroup(name: String!): Group!
createGroupWithDetails(request: CreateGroupInput!): Group!
updateUser(user: UpdateUserInput!): Success!
updateGroup(group: UpdateGroupInput!): Success!
addUserToGroup(userId: String!, groupId: Int!): Success!
removeUserFromGroup(userId: String!, groupId: Int!): Success!
deleteUser(userId: String!): Success!
deleteGroup(groupId: Int!): Success!
addUserAttribute(
name: String!
attributeType: AttributeType!
isList: Boolean!
isVisible: Boolean!
isEditable: Boolean!
): Success!
addGroupAttribute(
name: String!
attributeType: AttributeType!
isList: Boolean!
isVisible: Boolean!
isEditable: Boolean!
): Success!
deleteUserAttribute(name: String!): Success!
deleteGroupAttribute(name: String!): Success!
addUserObjectClass(name: String!): Success!
addGroupObjectClass(name: String!): Success!
deleteUserObjectClass(name: String!): Success!
deleteGroupObjectClass(name: String!): Success!
}
type Group {
id: Int!
displayName: String!
creationDate: DateTimeUtc!
uuid: String!
# User-defined attributes.
attributes: [AttributeValue!]!
# The groups to which this user belongs.
users: [User!]!
}
# A filter for requests, specifying a boolean expression based on field constraints. Only one of
# the fields can be set at a time.
input RequestFilter {
any: [RequestFilter!]
all: [RequestFilter!]
not: RequestFilter
eq: EqualityConstraint
memberOf: String
memberOfId: Int
}
# DateTime
scalar DateTimeUtc
type Query {
apiVersion: String!
user(userId: String!): User!
users(filters: RequestFilter): [User!]!
groups: [Group!]!
group(groupId: Int!): Group!
schema: Schema!
}
# The details required to create a user.
input CreateUserInput {
id: String!
email: String
displayName: String
firstName: String
lastName: String
# Base64 encoded JpegPhoto.
avatar: String
# User-defined attributes.
attributes: [AttributeValueInput!]
}
type AttributeSchema {
name: String!
attributeType: AttributeType!
isList: Boolean!
isVisible: Boolean!
isEditable: Boolean!
isHardcoded: Boolean!
isReadonly: Boolean!
}
# The fields that can be updated for a user.
input UpdateUserInput {
id: String!
email: String
displayName: String
firstName: String
lastName: String
# Base64 encoded JpegPhoto.
avatar: String
# Attribute names to remove.
# They are processed before insertions.
removeAttributes: [String!]
# Inserts or updates the given attributes.
# For lists, the entire list must be provided.
insertAttributes: [AttributeValueInput!]
}
input EqualityConstraint {
field: String!
value: String!
}
type Schema {
userSchema: AttributeList!
groupSchema: AttributeList!
}
# The fields that can be updated for a group.
input UpdateGroupInput {
# The group ID.
id: Int!
# The new display name.
displayName: String
# Attribute names to remove.
# They are processed before insertions.
removeAttributes: [String!]
# Inserts or updates the given attributes.
# For lists, the entire list must be provided.
insertAttributes: [AttributeValueInput!]
}
input AttributeValueInput {
# The name of the attribute. It must be present in the schema, and the type informs how
# to interpret the values.
name: String!
# The values of the attribute.
# If the attribute is not a list, the vector must contain exactly one element.
# Integers (signed 64 bits) are represented as strings.
# Dates are represented as strings in RFC3339 format, e.g. "2019-10-12T07:20:50.52Z".
# JpegPhotos are represented as base64 encoded strings. They must be valid JPEGs.
value: [String!]!
}
# The details required to create a group.
input CreateGroupInput {
displayName: String!
# User-defined attributes.
attributes: [AttributeValueInput!]
}
type User {
id: String!
email: String!
displayName: String!
firstName: String!
lastName: String!
avatar: String
creationDate: DateTimeUtc!
uuid: String!
# User-defined attributes.
attributes: [AttributeValue!]!
# The groups to which this user belongs.
groups: [Group!]!
}
enum AttributeType {
STRING
INTEGER
JPEG_PHOTO
DATE_TIME
}
type AttributeList {
attributes: [AttributeSchema!]!
extraLdapObjectClasses: [String!]!
}
type Success {
ok: Boolean!
}

154
queries/src/lib.rs Normal file
View File

@@ -0,0 +1,154 @@
#[cynic::schema("lldap")]
pub(crate) mod schema {}
#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query")]
pub struct GetUserAttributes {
pub schema: Schema,
}
#[derive(cynic::QueryFragment, Debug)]
pub struct Schema {
pub user_schema: AttributeList,
}
#[derive(cynic::QueryFragment, Debug)]
pub struct AttributeList {
pub attributes: Vec<AttributeSchema>,
}
#[derive(cynic::QueryFragment, Debug)]
pub struct AttributeSchema {
pub name: String,
}
#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Mutation")]
pub struct CreateManagedUserAttribute {
#[arguments(attributeType: "INTEGER", isEditable: false, isList: false, isVisible: false, name: "managed")]
pub add_user_attribute: Success,
}
#[derive(cynic::QueryFragment, Debug)]
pub struct Success {
pub ok: bool,
}
#[derive(cynic::Enum, Clone, Copy, Debug)]
pub enum AttributeType {
String,
Integer,
JpegPhoto,
DateTime,
}
#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query")]
pub struct ListManagedUsers {
#[arguments(filters: { eq: { field: "managed", value: "1" } })]
pub users: Vec<User>,
}
#[derive(cynic::QueryFragment, Debug)]
pub struct User {
pub id: String,
}
#[derive(cynic::QueryVariables, Debug)]
pub struct DeleteUserVariables<'a> {
pub id: &'a str,
}
#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Mutation", variables = "DeleteUserVariables")]
pub struct DeleteUser {
#[arguments(userId: $id)]
pub delete_user: Success,
}
#[derive(cynic::QueryVariables, Debug)]
pub struct CreateUserVariables<'a> {
pub id: &'a str,
}
#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Mutation", variables = "CreateUserVariables")]
pub struct CreateUser {
#[arguments(user: { attributes: { name: "managed", value: "1" }, email: $id, id: $id })]
pub create_user: User,
}
#[derive(cynic::QueryVariables, Debug)]
pub struct AddUserToGroupVariables<'a> {
pub group: i32,
pub id: &'a str,
}
#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Mutation", variables = "AddUserToGroupVariables")]
pub struct AddUserToGroup {
#[arguments(groupId: $group, userId: $id)]
pub add_user_to_group: Success,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn get_user_attributes_gql_output() {
use cynic::QueryBuilder;
let operation = GetUserAttributes::build(());
insta::assert_snapshot!(operation.query);
}
#[test]
fn create_managed_user_attribute_gql_output() {
use cynic::MutationBuilder;
let operation = CreateManagedUserAttribute::build(());
insta::assert_snapshot!(operation.query);
}
#[test]
fn list_managed_users_gql_output() {
use cynic::QueryBuilder;
let operation = ListManagedUsers::build(());
insta::assert_snapshot!(operation.query);
}
#[test]
fn delete_user_gql_output() {
use cynic::MutationBuilder;
let operation = DeleteUser::build(DeleteUserVariables { id: "user" });
insta::assert_snapshot!(operation.query);
}
#[test]
fn create_user_gql_output() {
use cynic::MutationBuilder;
let operation = CreateUser::build(CreateUserVariables { id: "user" });
insta::assert_snapshot!(operation.query);
}
#[test]
fn add_user_to_group_gql_output() {
use cynic::MutationBuilder;
let operation = AddUserToGroup::build(AddUserToGroupVariables {
id: "user",
group: 3,
});
insta::assert_snapshot!(operation.query);
}
}

View File

@@ -0,0 +1,9 @@
---
source: src/lib.rs
expression: operation.query
---
mutation AddUserToGroup($group: Int!, $id: String!) {
addUserToGroup(groupId: $group, userId: $id) {
ok
}
}

View File

@@ -0,0 +1,9 @@
---
source: queries/src/lib.rs
expression: operation.query
---
mutation CreateManagedUserAttribute {
addUserAttribute(attributeType: INTEGER, isEditable: false, isList: false, isVisible: false, name: "managed") {
ok
}
}

View File

@@ -0,0 +1,10 @@
---
source: src/lib.rs
assertion_line: 142
expression: operation.query
---
mutation CreateUser($id: String!) {
createUser(user: {attributes: [{name: "managed", value: ["1"]}], email: $id, id: $id}) {
id
}
}

View File

@@ -0,0 +1,9 @@
---
source: src/lib.rs
expression: operation.query
---
mutation DeleteUser($id: String!) {
deleteUser(userId: $id) {
ok
}
}

View File

@@ -0,0 +1,13 @@
---
source: src/lib.rs
expression: operation.query
---
query GetUserAttributes {
schema {
userSchema {
attributes {
name
}
}
}
}

View File

@@ -0,0 +1,9 @@
---
source: src/lib.rs
expression: operation.query
---
query ListManagedUsers {
users(filters: {eq: {field: "managed", value: "1"}}) {
id
}
}