From 4d844ccbadca7cc781300c6e9479839f1df31afd Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 24 Jun 2020 13:34:09 +0200 Subject: [PATCH] Get the ID of a group included in a request --- src/controllers/groups_controller.rs | 10 ++++++++++ src/controllers/routes.rs | 2 ++ src/data/group.rs | 22 ++++++++++++++++++++++ src/data/http_request_handler.rs | 24 +++++++++++++++++++++++- src/helpers/database.rs | 5 +++++ src/helpers/groups_helper.rs | 8 ++++++++ 6 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/controllers/groups_controller.rs b/src/controllers/groups_controller.rs index fce9165..8c164cc 100644 --- a/src/controllers/groups_controller.rs +++ b/src/controllers/groups_controller.rs @@ -4,6 +4,7 @@ use crate::api_data::res_create_group::GroupCreationResult; use crate::controllers::routes::RequestResult; +use crate::data::group::GroupAccessLevel; use crate::data::http_request_handler::HttpRequestHandler; use crate::data::new_group::NewGroup; use crate::helpers::groups_helper; @@ -28,4 +29,13 @@ pub fn get_list_user(r: &mut HttpRequestHandler) -> RequestResult { .collect::>(); r.set_response(list) +} + +/// Get information about a single group +pub fn get_info_single(r: &mut HttpRequestHandler) -> RequestResult { + let group_id = r.post_group_id_with_access("id", GroupAccessLevel::LIMITED_ACCESS)?; + + println!("Group to get: {:?}", group_id); + + r.success("continue implementation") } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 7fdf37e..c77403e 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -132,6 +132,8 @@ pub fn get_routes() -> Vec { Route::post("/groups/get_my_list", Box::new(groups_controller::get_list_user)), + Route::post("/groups/get_info", Box::new(groups_controller::get_info_single)), + // Virtual directory controller Route::post("/user/findbyfolder", Box::new(virtual_directory_controller::find_user)), diff --git a/src/data/group.rs b/src/data/group.rs index 4091e73..22373f1 100644 --- a/src/data/group.rs +++ b/src/data/group.rs @@ -8,4 +8,26 @@ pub enum GroupVisibilityLevel { OPEN_GROUP, PRIVATE_GROUP, SECRETE_GROUP, +} + +#[allow(non_camel_case_types)] +#[derive(Eq, PartialEq, Hash)] +pub enum GroupAccessLevel { + //Can not even know if the group exists or not + NO_ACCESS = 0, + + //Access to the name of the group only + LIMITED_ACCESS = 1, + + //Can see the posts of the group, but not a member of the group + VIEW_ACCESS = 2, + + //Member access (same as view access but as member) + MEMBER_ACCESS = 3, + + //Can create posts, even if posts creation is restricted + MODERATOR_ACCESS = 4, + + //Can do everything + ADMIN_ACCESS = 5, } \ No newline at end of file diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index 4bc0bbb..a157e17 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -14,8 +14,10 @@ use crate::controllers::routes::RequestResult; use crate::data::api_client::APIClient; use crate::data::config::conf; use crate::data::error::{ExecError, ResultBoxError}; +use crate::data::group::GroupAccessLevel; +use crate::data::group_id::GroupID; use crate::data::user::UserID; -use crate::helpers::{account_helper, api_helper, conversations_helper, user_helper}; +use crate::helpers::{account_helper, api_helper, conversations_helper, groups_helper, user_helper}; use crate::utils::string_utils::remove_html_nodes; use crate::utils::user_data_utils::{generate_new_user_data_file_name, prepare_file_creation, user_data_path}; use crate::utils::virtual_directories_utils::check_virtual_directory; @@ -448,4 +450,24 @@ impl HttpRequestHandler { Ok(conv_id) } + + /// Get the ID + pub fn post_group_id(&mut self, name: &str) -> ResultBoxError { + let group_id = GroupID::new(self.post_u64(name)?); + + if !groups_helper::exists(&group_id)? { + self.not_found("Specified group not found!".to_string())?; + } + + Ok(group_id) + } + + /// Get the ID of a group included in a request with a check for access level of current user + pub fn post_group_id_with_access(&mut self, name: &str, min_level: GroupAccessLevel) -> ResultBoxError { + let group_id = self.post_group_id(name)?; + + // TODO : add security checks + + Ok(group_id) + } } \ No newline at end of file diff --git a/src/helpers/database.rs b/src/helpers/database.rs index ec5f472..83c2c0c 100644 --- a/src/helpers/database.rs +++ b/src/helpers/database.rs @@ -143,6 +143,11 @@ impl QueryInfo { self } + pub fn cond_group_id(mut self, key: &str, val: &GroupID) -> QueryInfo { + self.conditions.insert(key.to_string(), val.id().to_string()); + self + } + pub fn cond_legacy_bool(mut self, key: &str, val: bool) -> QueryInfo { let val = match val { true => "1".to_string(), diff --git a/src/helpers/groups_helper.rs b/src/helpers/groups_helper.rs index 01f39fc..53c1fad 100644 --- a/src/helpers/groups_helper.rs +++ b/src/helpers/groups_helper.rs @@ -81,6 +81,14 @@ pub fn get_list_user(user_id: UserID, only_followed: bool) -> ResultBoxError ResultBoxError { + database::QueryInfo::new(GROUPS_LIST_TABLE) + .cond_group_id("id", group_id) + .exec_count() + .map(|m| m > 0) +} + /// Find a group id by virtual directory pub fn find_by_virtual_directory(dir: &str) -> ResultBoxError { database::QueryInfo::new(GROUPS_LIST_TABLE)