1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-02-16 22:12:39 +00:00

Get the ID of a group included in a request

This commit is contained in:
Pierre HUBERT 2020-06-24 13:34:09 +02:00
parent c245045d67
commit 4d844ccbad
6 changed files with 70 additions and 1 deletions

View File

@ -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::<Vec<u64>>();
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")
}

View File

@ -132,6 +132,8 @@ pub fn get_routes() -> Vec<Route> {
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)),

View File

@ -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,
}

View File

@ -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<GroupID> {
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<GroupID> {
let group_id = self.post_group_id(name)?;
// TODO : add security checks
Ok(group_id)
}
}

View File

@ -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(),

View File

@ -81,6 +81,14 @@ pub fn get_list_user(user_id: UserID, only_followed: bool) -> ResultBoxError<Vec
query.exec(|row| row.get_group_id("groups_id"))
}
/// Check out whether a group exists or not
pub fn exists(group_id: &GroupID) -> ResultBoxError<bool> {
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<GroupID> {
database::QueryInfo::new(GROUPS_LIST_TABLE)