1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-28 14:38:52 +00:00

Can delete group members

This commit is contained in:
Pierre HUBERT 2020-06-27 18:28:38 +02:00
parent c28196f80e
commit 926892a29b
3 changed files with 60 additions and 6 deletions

View File

@ -245,5 +245,27 @@ pub fn cancel_request(r: &mut HttpRequestHandler) -> RequestResult {
/// Remove a member from a group (as a moderator or an admin) /// Remove a member from a group (as a moderator or an admin)
pub fn delete_member(r: &mut HttpRequestHandler) -> RequestResult { pub fn delete_member(r: &mut HttpRequestHandler) -> RequestResult {
r.success("Implement me") let group_id = r.post_group_id_with_access("groupID", GroupAccessLevel::MODERATOR_ACCESS)?;
// Get the membership of the user making the request
let curr_user_membership = groups_helper::get_membership(&group_id, r.user_id_opt())?;
// Get information about the member to delete
let user_id = r.post_user_id("userID")?;
let membership = groups_helper::get_membership(&group_id, Some(user_id.clone()))?;
if user_id == r.user_id()? && groups_helper::is_last_admin(&group_id, &r.user_id()?)? {
r.forbidden("You are the last administrator of this group!".to_string())?;
}
// Only administrator can delete members that are more than members (moderators & administrators)
if membership.level < GroupMembershipLevel::MEMBER && curr_user_membership.level != GroupMembershipLevel::ADMINISTRATOR {
r.forbidden("Only administrators can delete this membership!".to_string())?;
}
groups_helper::delete_member(&group_id, &user_id)?;
// TODO : Delete related notifications
r.success("Membership of the user has been successfully deleted!")
} }

View File

@ -2,17 +2,21 @@
//! //!
//! @author Pierre Hubert //! @author Pierre Hubert
use crate::data::user::UserID;
use crate::data::group_id::GroupID; use crate::data::group_id::GroupID;
use crate::data::user::UserID;
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq, PartialOrd)]
pub enum GroupMembershipLevel { pub enum GroupMembershipLevel {
ADMINISTRATOR = 0, ADMINISTRATOR = 0,
MODERATOR = 1, MODERATOR = 1,
MEMBER = 2, MEMBER = 2,
INVITED = 3, INVITED = 3,
PENDING = 4, //When the group membership has not been approved yet
VISITOR = 5, //Simple visit // When the group membership has not been approved yet
PENDING = 4,
// Simple visit
VISITOR = 5,
} }
impl GroupMembershipLevel { impl GroupMembershipLevel {
@ -34,5 +38,19 @@ pub struct GroupMember {
pub group_id: GroupID, pub group_id: GroupID,
pub time_create: u64, pub time_create: u64,
pub level: GroupMembershipLevel, pub level: GroupMembershipLevel,
pub following: bool pub following: bool,
}
#[cfg(test)]
mod tests {
use crate::data::group_member::GroupMembershipLevel;
#[test]
fn membership_level_coherence() {
assert!(GroupMembershipLevel::ADMINISTRATOR < GroupMembershipLevel::MODERATOR);
assert!(GroupMembershipLevel::MODERATOR < GroupMembershipLevel::MEMBER);
assert!(GroupMembershipLevel::MEMBER < GroupMembershipLevel::INVITED);
assert!(GroupMembershipLevel::INVITED < GroupMembershipLevel::PENDING);
assert!(GroupMembershipLevel::PENDING < GroupMembershipLevel::VISITOR);
}
} }

View File

@ -297,6 +297,20 @@ pub fn count_members(group_id: &GroupID) -> ResultBoxError<usize> {
.exec_count() .exec_count()
} }
/// Count the number of group's members at a specific level
pub fn count_members_at_level(group_id: &GroupID, level: GroupMembershipLevel) -> ResultBoxError<usize> {
database::QueryInfo::new(GROUPS_MEMBERS_TABLE)
.cond_group_id("groups_id", group_id)
.cond_u32("level", level.to_db())
.exec_count()
}
/// Check out whether a user is the last administrator of a group
pub fn is_last_admin(group_id: &GroupID, user_id: &UserID) -> ResultBoxError<bool> {
Ok(get_membership_level(group_id, Some(user_id.clone()))? == GroupMembershipLevel::ADMINISTRATOR &&
count_members_at_level(&group_id, GroupMembershipLevel::ADMINISTRATOR)? == 1)
}
/// Check the availability of a virtual directory for a group /// Check the availability of a virtual directory for a group
pub fn check_directory_availability(dir: &str, group_id: Option<GroupID>) -> ResultBoxError<bool> { pub fn check_directory_availability(dir: &str, group_id: Option<GroupID>) -> ResultBoxError<bool> {
let group = find_by_virtual_directory(dir); let group = find_by_virtual_directory(dir);