Can respond to a membership request

This commit is contained in:
Pierre HUBERT 2018-07-09 15:24:56 +02:00
parent 4fd5cfde37
commit aee09dee43
2 changed files with 59 additions and 0 deletions

View File

@ -382,6 +382,36 @@ class GroupsController {
return array("success" => "The membership has been successfully deleted!");
}
/**
* Respond to a membership request
*
* @url POST /groups/respond_request
*/
public function respondRequest() : array {
user_login_required();
//Get the ID of the target group
$groupID = getPostGroupIdWithAccess("groupID", GroupInfo::MODERATOR_ACCESS);
//Get user ID
$userID = getPostUserID("userID");
//Get the response
$accept = postBool("accept");
//Check if the user membership is really pending or not
if(components()->groups->getMembershipLevel($userID, $groupID) != GroupMember::PENDING)
Rest_fatal_error(401, "This user has not requested a membership in this group!");
//Respond to the request
if(!components()->groups->respondRequest($userID, $groupID, $accept))
Rest_fatal_error(500, "Could not respond to the membership request!");
//Success
return array("success" => "The response to the request has been successfully saved!");
}
/**
* Parse a GroupInfo object into an array for the API
*

View File

@ -296,6 +296,24 @@ class GroupsComponent {
return $this->updateMembershipLevel($userID, $groupID, GroupMember::MEMBER);
}
/**
* Respond to a membership request
*
* @param int $userID The ID of the target user
* @param int $groupID The ID of the related group
* @param bool $accept Set whether the request was accepted or not
* @return bool TRUE for a success / FALSE else
*/
public function respondRequest(int $userID, int $groupID, bool $accept) : bool {
//If the user reject the invitation, delete it
if(!$accept)
return $this->deleteRequest($userID, $groupID);
//Upgrade the user as member
return $this->updateMembershipLevel($userID, $groupID, GroupMember::MEMBER);
}
/**
* Delete a membership invitation
*
@ -307,6 +325,17 @@ class GroupsComponent {
return $this->deleteMembershipWithStatus($userID, $groupID, GroupMember::INVITED);
}
/**
* Delete a membership request
*
* @param int $userID The ID of the target user
* @param int $groupID The ID of the related group
* @return bool TRUE for a success / FALSE else
*/
public function deleteRequest(int $userID, int $groupID) : bool {
return $this->deleteMembershipWithStatus($userID, $groupID, GroupMember::PENDING);
}
/**
* Cancel a membership request
*