Skip to content

Commit

Permalink
Introduce NoAccess public_sharing_level, update queries to include pu…
Browse files Browse the repository at this point in the history
…blic_sharing_level, update client and server to handle public_sharing_level on database creation
  • Loading branch information
marcua committed Sep 26, 2024
1 parent 195bcbc commit cc80eec
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 24 deletions.
13 changes: 8 additions & 5 deletions src/ayb_db/db_interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,15 @@ RETURNING entity_id, short_token, hash, status
) -> Result<InstantiatedDatabase, AybError> {
let db: InstantiatedDatabase = sqlx::query_as(
r#"
INSERT INTO database ( entity_id, slug, db_type )
VALUES ( $1, $2, $3 )
RETURNING id, entity_id, slug, db_type
INSERT INTO database ( entity_id, slug, db_type, public_sharing_level )
VALUES ( $1, $2, $3, $4 )
RETURNING id, entity_id, slug, db_type, public_sharing_level
"#,
)
.bind(database.entity_id)
.bind(&database.slug)
.bind(database.db_type)
.bind(database.public_sharing_level)
.fetch_one(&self.pool)
.await
.or_else(|err| match err {
Expand Down Expand Up @@ -180,7 +181,8 @@ SELECT
database.id,
database.slug,
database.entity_id,
database.db_type
database.db_type,
database.public_sharing_level
FROM database
JOIN entity on database.entity_id = entity.id
WHERE
Expand Down Expand Up @@ -388,7 +390,8 @@ SELECT
id,
entity_id,
slug,
db_type
db_type,
public_sharing_level
FROM database
WHERE database.entity_id = $1
"#,
Expand Down
32 changes: 18 additions & 14 deletions src/ayb_db/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,29 +119,33 @@ impl AuthenticationMethodStatus {
)]
#[repr(i16)]
pub enum PublicSharingLevel {
Metadata = 0,
Fork = 1,
ReadOnly = 2,
NoAccess = 0,
Metadata = 1,
Fork = 2,
ReadOnly = 3,
}

from_str!(PublicSharingLevel, {
"no-access" => PublicSharingLevel::NoAccess,
"metadata" => PublicSharingLevel::Metadata,
"fork" => PublicSharingLevel::Fork,
"read_only" => PublicSharingLevel::ReadOnly
"read-only" => PublicSharingLevel::ReadOnly
});

try_from_i16!(PublicSharingLevel, {
0 => PublicSharingLevel::Metadata,
1 => PublicSharingLevel::Fork,
3 => PublicSharingLevel::ReadOnly,
0 => PublicSharingLevel::NoAccess,
1 => PublicSharingLevel::Metadata,
2 => PublicSharingLevel::Fork,
3 => PublicSharingLevel::ReadOnly
});

impl PublicSharingLevel {
pub fn to_str(&self) -> &str {
match self {
PublicSharingLevel::NoAccess => "no-access",
PublicSharingLevel::Metadata => "metadata",
PublicSharingLevel::Fork => "fork",
PublicSharingLevel::ReadOnly => "read_only",
PublicSharingLevel::ReadOnly => "read-only",
}
}
}
Expand Down Expand Up @@ -287,22 +291,22 @@ pub enum EntityDatabaseSharingLevel {
}

from_str!(EntityDatabaseSharingLevel, {
"read_only" => EntityDatabaseSharingLevel::ReadOnly
"read_write" => EntityDatabaseSharingLevel::ReadWrite,
"manager" => EntityDatabaseSharingLevel::Manager,
"read-only" => EntityDatabaseSharingLevel::ReadOnly,
"read-write" => EntityDatabaseSharingLevel::ReadWrite,
"manager" => EntityDatabaseSharingLevel::Manager
});

try_from_i16!(EntityDatabaseSharingLevel, {
0 => EntityDatabaseSharingLevel::ReadOnly,
1 => EntityDatabaseSharingLevel::ReadWrite,
2 => EntityDatabaseSharingLevel::Manager,
2 => EntityDatabaseSharingLevel::Manager
});

impl EntityDatabaseSharingLevel {
pub fn to_str(&self) -> &str {
match self {
EntityDatabaseSharingLevel::ReadOnly => "read_only",
EntityDatabaseSharingLevel::ReadWrite => "read_write",
EntityDatabaseSharingLevel::ReadOnly => "read-only",
EntityDatabaseSharingLevel::ReadWrite => "read-write",
EntityDatabaseSharingLevel::Manager => "manager",
}
}
Expand Down
19 changes: 16 additions & 3 deletions src/client/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ayb_db::models::{DBType, EntityType};
use crate::ayb_db::models::{DBType, EntityType, PublicSharingLevel};
use crate::client::config::ClientConfig;
use crate::client::http::AybClient;
use crate::error::AybError;
Expand Down Expand Up @@ -95,7 +95,14 @@ pub fn client_commands() -> Command {
.value_parser(value_parser!(DBType))
.default_value(DBType::Sqlite.to_str())
.required(false)
)
.arg(
arg!(<public_sharing_level> "The sharing level that the public/an anonymous user will have")
.value_parser(value_parser!(PublicSharingLevel))
.default_value(PublicSharingLevel::NoAccess.to_str())
.required(false)
),

)
.subcommand(
Command::new("query")
Expand Down Expand Up @@ -244,12 +251,18 @@ pub async fn execute_client_command(matches: &ArgMatches) -> std::io::Result<()>
};

if let Some(matches) = matches.subcommand_matches("create_database") {
if let (Some(entity_database), Some(db_type)) = (
if let (Some(entity_database), Some(db_type), Some(public_sharing_level)) = (
matches.get_one::<EntityDatabasePath>("database"),
matches.get_one::<DBType>("type"),
matches.get_one::<PublicSharingLevel>("public_sharing_level"),
) {
match client
.create_database(&entity_database.entity, &entity_database.database, db_type)
.create_database(
&entity_database.entity,
&entity_database.database,
db_type,
public_sharing_level,
)
.await
{
Ok(_response) => {
Expand Down
7 changes: 6 additions & 1 deletion src/client/http.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ayb_db::models::{DBType, EntityType};
use crate::ayb_db::models::{DBType, EntityType, PublicSharingLevel};
use crate::error::AybError;
use crate::hosted_db::QueryResult;
use crate::http::structs::{APIToken, Database, EmptyResponse, EntityQueryResponse, SnapshotList};
Expand Down Expand Up @@ -99,12 +99,17 @@ impl AybClient {
entity: &str,
database: &str,
db_type: &DBType,
public_sharing_level: &PublicSharingLevel,
) -> Result<Database, AybError> {
let mut headers = HeaderMap::new();
headers.insert(
HeaderName::from_static("db-type"),
HeaderValue::from_str(db_type.to_str()).unwrap(),
);
headers.insert(
HeaderName::from_static("public-sharing-level"),
HeaderValue::from_str(public_sharing_level.to_str()).unwrap(),
);
self.add_bearer_token(&mut headers)?;

let response = reqwest::Client::new()
Expand Down
4 changes: 3 additions & 1 deletion src/server/endpoints/create_database.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::ayb_db::db_interfaces::AybDb;
use crate::ayb_db::models::{DBType, Database, InstantiatedEntity};
use crate::ayb_db::models::{DBType, Database, InstantiatedEntity, PublicSharingLevel};
use std::str::FromStr;

use crate::error::AybError;
Expand All @@ -24,10 +24,12 @@ async fn create_database(
let entity_slug = &path.entity;
let entity = ayb_db.get_entity_by_slug(entity_slug).await?;
let db_type = get_header(&req, "db-type")?;
let public_sharing_level = get_header(&req, "public-sharing-level")?;
let database = Database {
entity_id: entity.id,
slug: path.database.clone(),
db_type: DBType::from_str(&db_type)? as i16,
public_sharing_level: PublicSharingLevel::from_str(&public_sharing_level)? as i16,
};
let authenticated_entity = unwrap_authenticated_entity(&authenticated_entity)?;
if can_create_database(&authenticated_entity, &entity) {
Expand Down

0 comments on commit cc80eec

Please sign in to comment.