Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List Access Info #66

Merged
merged 35 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
0b82ec6
update readme with non-static ip for dev and test database
ViktorPlatz Nov 27, 2023
bbb70e7
Merge branch 'token-refactor' of https://github.com/ECDAR-AAU-SW-P5/E…
ViktorPlatz Nov 27, 2023
b937156
Create get_access_by_uid
ViktorPlatz Nov 27, 2023
8a723b3
Create list_access_info
ViktorPlatz Nov 27, 2023
ac59ebd
auto gen
ViktorPlatz Nov 27, 2023
16245c6
protibuf change
ViktorPlatz Nov 27, 2023
ee7ca1e
merge
ViktorPlatz Nov 27, 2023
bdfd994
changed some functions and things because mismatch in protobuf submod…
ViktorPlatz Nov 27, 2023
1210c2f
i hate submodules
ViktorPlatz Nov 28, 2023
4bbeb8f
cargo fmt
ViktorPlatz Nov 28, 2023
9cd009b
remove unnecessary comment
ViktorPlatz Nov 28, 2023
f6b41ae
Handle case that no access were found
ViktorPlatz Nov 28, 2023
6137836
Tests for list access info
ViktorPlatz Nov 28, 2023
e4f29bf
cargo fmt :(
ViktorPlatz Nov 28, 2023
fa53fa3
temp
ViktorPlatz Nov 28, 2023
7574744
temp
ViktorPlatz Nov 28, 2023
750602f
temp
ViktorPlatz Nov 28, 2023
8004b83
changed listAccessInfo back
ViktorPlatz Nov 28, 2023
8c1cf82
new crud to get user info regarding models
ViktorPlatz Nov 28, 2023
e551c7c
change it to utilize protobuf structs
ViktorPlatz Nov 28, 2023
df8c89b
same
ViktorPlatz Nov 28, 2023
f00515f
new tests for endpoint
ViktorPlatz Nov 28, 2023
33de224
cargo fmt and protobuf changes
ViktorPlatz Nov 28, 2023
05a9ed2
unused imports
ViktorPlatz Nov 28, 2023
bff731f
unused import and crud tests for get_model_info_by_uid
ViktorPlatz Nov 28, 2023
8518d04
clippy : )
ViktorPlatz Nov 28, 2023
32e3962
merge
ViktorPlatz Nov 28, 2023
bf134a5
clippy
ViktorPlatz Nov 28, 2023
75d31ed
merge and cgfmt
ViktorPlatz Nov 29, 2023
ac46448
double test include fix
ViktorPlatz Nov 29, 2023
c768962
cargo fmt yay
ViktorPlatz Nov 29, 2023
f352f53
name change
ViktorPlatz Nov 29, 2023
a8c1a01
cargo fmt :(
ViktorPlatz Nov 29, 2023
9227250
cargo clippy
ViktorPlatz Nov 29, 2023
5aa1a94
as'd;mqwpogqwiopgnqw
ViktorPlatz Nov 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
POSTGRES_TEST_IP=localhost
POSTGRES_DEV_IP=localhost
POSTGRES_TEST_PORT=5433
POSTGRES_DEV_PORT=5432

POSTGRES_DB=ecdar_api
POSTGRES_PASSWORD=1234

DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD}@localhost:${POSTGRES_DEV_PORT}/${POSTGRES_DB}
TEST_DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD}@localhost:${POSTGRES_TEST_PORT}/${POSTGRES_DB}
DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD}@${POSTGRES_DEV_IP}:${POSTGRES_DEV_PORT}/${POSTGRES_DB}
TEST_DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD}@${POSTGRES_TEST_IP}:${POSTGRES_TEST_PORT}/${POSTGRES_DB}
# TEST_DATABASE_URL=sqlite::memory:

REVEAAL_ADDRESS=http://{IP}:{PORT}
Expand Down
2 changes: 1 addition & 1 deletion Ecdar-ProtoBuf
Submodule Ecdar-ProtoBuf updated 2 files
+46 −1 api.proto
+3 −3 services.proto
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Prerequisites:

Set up docker postgresql db:
- `docker-compose up -d`
- `DATABASE_URL=postgresql://postgres:{POSTGRES_PASSWORD}@localhost:{POSTGRES_DEV_PORT}/{POSTGRES_DB} sea-orm-cli migrate up`
- `DATABASE_URL=postgresql://postgres:{POSTGRES_PASSWORD}@localhost:{POSTGRES_TEST_PORT}/{POSTGRES_DB} sea-orm-cli migrate up`
- `DATABASE_URL=postgresql://postgres:{POSTGRES_PASSWORD}@{POSTGRES_DEV_IP}:{POSTGRES_DEV_PORT}/{POSTGRES_DB} sea-orm-cli migrate up`
- `DATABASE_URL=postgresql://postgres:{POSTGRES_PASSWORD}@{POSTGRES_TEST_IP}:{POSTGRES_TEST_PORT}/{POSTGRES_DB} sea-orm-cli migrate up`

After modifying DB:
- `sea-orm-cli migrate fresh`
Expand Down
77 changes: 68 additions & 9 deletions src/api/ecdar_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ use tonic::{Code, Request, Response, Status};
use crate::api::auth::{RequestExt, Token, TokenType};
use crate::database::{session_context::SessionContextTrait, user_context::UserContextTrait};

use super::server::server::DeleteModelRequest;
use super::server::server::{
ecdar_api_auth_server::EcdarApiAuth,
ecdar_api_server::EcdarApi,
ecdar_backend_server::EcdarBackend,
get_auth_token_request::{user_credentials, UserCredentials},
CreateAccessRequest, CreateQueryRequest, CreateUserRequest, DeleteAccessRequest,
DeleteQueryRequest, GetAuthTokenRequest, GetAuthTokenResponse, QueryRequest, QueryResponse,
SimulationStartRequest, SimulationStepRequest, SimulationStepResponse, UpdateAccessRequest,
UpdateQueryRequest, UpdateUserRequest, UserTokenResponse,
CreateAccessRequest, CreateModelRequest, CreateModelResponse, CreateQueryRequest,
CreateUserRequest, DeleteAccessRequest, DeleteModelRequest, DeleteQueryRequest,
GetAuthTokenRequest, GetAuthTokenResponse, GetModelRequest, GetModelResponse,
ListModelsInfoResponse, QueryRequest, QueryResponse, SimulationStartRequest,
SimulationStepRequest, SimulationStepResponse, UpdateAccessRequest, UpdateQueryRequest,
UpdateUserRequest, UserTokenResponse,
};
use crate::entities::{access, query, session, user};

Expand Down Expand Up @@ -99,11 +100,17 @@ impl ConcreteEcdarApi {

#[tonic::async_trait]
impl EcdarApi for ConcreteEcdarApi {
async fn get_model(&self, _request: Request<()>) -> Result<Response<()>, Status> {
async fn get_model(
&self,
_request: Request<GetModelRequest>,
) -> Result<Response<GetModelResponse>, Status> {
todo!()
}

async fn create_model(&self, _request: Request<()>) -> Result<Response<()>, Status> {
async fn create_model(
&self,
_request: Request<CreateModelRequest>,
) -> Result<Response<CreateModelResponse>, Status> {
todo!()
}

Expand Down Expand Up @@ -150,8 +157,60 @@ impl EcdarApi for ConcreteEcdarApi {
}
}

async fn list_models_info(&self, _request: Request<()>) -> Result<Response<()>, Status> {
todo!()
async fn list_models_info(
&self,
request: Request<()>,
) -> Result<Response<ListModelsInfoResponse>, Status> {
let uid = request
.uid()
.ok_or(Status::internal("Could not get uid from request metadata"))?;

match self
.contexts
.model_context
.get_models_info_by_uid(uid)
.await
{
Ok(model_info_list) => {
if model_info_list.is_empty() {
return Err(Status::new(
Code::NotFound,
"No access found for given user",
));
} else {
Ok(Response::new(ListModelsInfoResponse { model_info_list }))
}
}
Err(error) => Err(Status::new(Code::Internal, error.to_string())),
}
/*let mut model_info_list_vector: Vec<ModelInfo> = Vec::new(); // Initialize the Vec

// Get all the models that the user has access to
match self.model_context.get_model_info_by_uid(uid).await {
Ok(model_info_list) => {
for model_info in model_info_list {
let model_info_test = ModelInfo {
model_id: model_info.model_id,
model_name: model_info.model_name,
model_owner_id: model_info.model_owner_id,
user_role_on_model: model_info.user_role_on_model,
};
model_info_list_vector.push(model_info_test);
}

if model_info_list_vector.is_empty() {
return Err(Status::new(
Code::NotFound,
"No access found for given user",
));
} else {
Ok(Response::new(ListModelInfoResponse {
model_info_list: model_info_list_vector,
}))
}
}
Err(error) => Err(Status::new(Code::Internal, error.to_string())),
}*/
}

/// Creates an access in the database.
Expand Down
14 changes: 14 additions & 0 deletions src/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
fn main() {
tonic_build::compile_protos("Ecdar-ProtoBuf/services.proto").unwrap();
tonic_build::configure()
.type_attribute(
"ComponentsInfo",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute(
"Component",
"#[derive(serde::Serialize, serde::Deserialize)]",
)
.type_attribute("ModelInfo", "#[derive(sea_orm::FromQueryResult)]")
.enum_attribute("rep", "#[derive(serde::Serialize, serde::Deserialize)]")
.compile(&["Ecdar-ProtoBuf/services.proto"], &["Ecdar-ProtoBuf/"])
.unwrap();

// Tell cargo to invalidate the crate when the protobuf repository changes
println!("cargo:rerun-if-changed=Ecdar-ProtoBuf");
}
17 changes: 14 additions & 3 deletions src/database/access_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@ use crate::database::entity_context::EntityContextTrait;
use crate::entities::access;
use sea_orm::prelude::async_trait::async_trait;
use sea_orm::ActiveValue::{Set, Unchanged};
use sea_orm::{ActiveModelTrait, DbErr, EntityTrait};
use sea_orm::{ActiveModelTrait, ColumnTrait, DbErr, EntityTrait, QueryFilter};
use std::sync::Arc;

pub struct AccessContext {
db_context: Arc<dyn DatabaseContextTrait>,
}

pub trait AccessContextTrait: EntityContextTrait<access::Model> {}
#[async_trait]
pub trait AccessContextTrait: EntityContextTrait<access::Model> {
async fn get_access_by_uid(&self, uid: i32) -> Result<Vec<access::Model>, DbErr>;
}

impl AccessContextTrait for AccessContext {}
#[async_trait]
impl AccessContextTrait for AccessContext {
async fn get_access_by_uid(&self, uid: i32) -> Result<Vec<access::Model>, DbErr> {
access::Entity::find()
.filter(access::Column::UserId.eq(uid))
.all(&self.db_context.get_connection())
.await
}
}

impl AccessContext {
pub fn new(db_context: Arc<dyn DatabaseContextTrait>) -> AccessContext {
Expand Down
51 changes: 47 additions & 4 deletions src/database/model_context.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,60 @@
use crate::database::database_context::DatabaseContextTrait;
use crate::entities::{model, query};
use crate::entities::{access, model, query};

use crate::api::server::server::ModelInfo;
use crate::EntityContextTrait;
use async_trait::async_trait;
use sea_orm::{ActiveModelTrait, DbErr, EntityTrait, IntoActiveModel, ModelTrait, Set, Unchanged};
use sea_orm::{
ActiveModelTrait, ColumnTrait, DbErr, EntityTrait, IntoActiveModel, JoinType, ModelTrait,
QueryFilter, QuerySelect, RelationTrait, Set, Unchanged,
};
use std::sync::Arc;

pub struct ModelContext {
db_context: Arc<dyn DatabaseContextTrait>,
}

pub trait ModelContextTrait: EntityContextTrait<model::Model> {}
#[async_trait]
pub trait ModelContextTrait: EntityContextTrait<model::Model> {
async fn get_models_info_by_uid(&self, uid: i32) -> Result<Vec<ModelInfo>, DbErr>;
}

#[async_trait]
impl ModelContextTrait for ModelContext {
async fn get_models_info_by_uid(&self, uid: i32) -> Result<Vec<ModelInfo>, DbErr> {
/*let querytest = access::Entity::find()
.select_only()
.column_as(model::Column::Id, "model_id")
.column_as(model::Column::Name, "model_name")
.column_as(model::Column::OwnerId, "model_owner_id")
.column_as(access::Column::Role, "user_role_on_model")
.join(JoinType::InnerJoin, access::Relation::Model.def())
.join(JoinType::InnerJoin, access::Relation::Role.def())
.group_by(model::Column::Id)
.group_by(access::Column::Role)
.filter(access::Column::UserId.eq(uid))
.build(DbBackend::Postgres)
.to_string();

println!("SQL Query: {}", querytest);*/

impl ModelContextTrait for ModelContext {}
//join model, access and role tables
access::Entity::find()
.select_only()
.column_as(model::Column::Id, "model_id")
.column_as(model::Column::Name, "model_name")
.column_as(model::Column::OwnerId, "model_owner_id")
.column_as(access::Column::Role, "user_role_on_model")
.join(JoinType::InnerJoin, access::Relation::Model.def())
.join(JoinType::InnerJoin, access::Relation::Role.def())
.group_by(model::Column::Id)
.group_by(access::Column::Role)
.filter(access::Column::UserId.eq(uid))
.into_model::<ModelInfo>()
.all(&self.db_context.get_connection())
.await
}
}

impl ModelContext {
pub fn new(db_context: Arc<dyn DatabaseContextTrait>) -> ModelContext {
Expand Down
2 changes: 1 addition & 1 deletion src/entities/access.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.5
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6

use sea_orm::entity::prelude::*;

Expand Down
2 changes: 1 addition & 1 deletion src/entities/in_use.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.5
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6

use sea_orm::entity::prelude::*;

Expand Down
2 changes: 1 addition & 1 deletion src/entities/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.5
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6

pub mod prelude;

Expand Down
2 changes: 1 addition & 1 deletion src/entities/model.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.5
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6

use sea_orm::entity::prelude::*;

Expand Down
2 changes: 1 addition & 1 deletion src/entities/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.5
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6

pub use super::access::Entity as Access;
pub use super::in_use::Entity as InUse;
Expand Down
2 changes: 1 addition & 1 deletion src/entities/query.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.5
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6

use sea_orm::entity::prelude::*;

Expand Down
2 changes: 1 addition & 1 deletion src/entities/role.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.5
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6

use sea_orm::entity::prelude::*;

Expand Down
2 changes: 1 addition & 1 deletion src/entities/session.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.5
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6

use sea_orm::entity::prelude::*;

Expand Down
2 changes: 1 addition & 1 deletion src/entities/user.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.5
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6

use sea_orm::entity::prelude::*;

Expand Down
9 changes: 7 additions & 2 deletions src/tests/api/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::api::context_collection::ContextCollection;
use crate::api::ecdar_api::ConcreteEcdarApi;
use crate::api::hashing_context::HashingContextTrait;
use crate::api::server::server::ecdar_backend_server::EcdarBackend;
use crate::api::server::server::ModelInfo;
use crate::api::server::server::{
QueryRequest, QueryResponse, SimulationStartRequest, SimulationStepRequest,
SimulationStepResponse, UserTokenResponse,
Expand Down Expand Up @@ -71,7 +72,9 @@ mock! {
async fn delete(&self, entity_id: i32) -> Result<access::Model, DbErr>;
}
#[async_trait]
impl AccessContextTrait for AccessContext {}
impl AccessContextTrait for AccessContext {
async fn get_access_by_uid(&self, uid: i32) -> Result<Vec<access::Model>, DbErr>;
}
}

mock! {
Expand Down Expand Up @@ -99,7 +102,9 @@ mock! {
async fn delete(&self, entity_id: i32) -> Result<model::Model, DbErr>;
}
#[async_trait]
impl ModelContextTrait for ModelContext {}
impl ModelContextTrait for ModelContext {
async fn get_models_info_by_uid(&self, uid: i32) -> Result<Vec<ModelInfo>, DbErr>;
}
}

mock! {
Expand Down
Loading
Loading