Can respond to a membership invitation.

This commit is contained in:
Pierre HUBERT 2018-07-06 06:53:59 +02:00
parent d55cca75b2
commit c3bdbedb30
2 changed files with 458 additions and 358 deletions

View File

@ -119,6 +119,8 @@ class GroupsController {
*/ */
public function getSettings(){ public function getSettings(){
user_login_required();
//Get the ID of the group (with admin access) //Get the ID of the group (with admin access)
$groupID = getPostGroupIdWithAccess("id", GroupInfo::ADMIN_ACCESS); $groupID = getPostGroupIdWithAccess("id", GroupInfo::ADMIN_ACCESS);
@ -140,6 +142,8 @@ class GroupsController {
*/ */
public function setSettings(){ public function setSettings(){
user_login_required();
//Get the ID of the group (with admin access) //Get the ID of the group (with admin access)
$groupID = getPostGroupIdWithAccess("id", GroupInfo::ADMIN_ACCESS); $groupID = getPostGroupIdWithAccess("id", GroupInfo::ADMIN_ACCESS);
@ -176,6 +180,8 @@ class GroupsController {
*/ */
public function uploadLogo(){ public function uploadLogo(){
user_login_required();
//Get the ID of the group (with admin access) //Get the ID of the group (with admin access)
$groupID = getPostGroupIdWithAccess("id", GroupInfo::ADMIN_ACCESS); $groupID = getPostGroupIdWithAccess("id", GroupInfo::ADMIN_ACCESS);
@ -211,6 +217,8 @@ class GroupsController {
*/ */
public function deleteLogo(){ public function deleteLogo(){
user_login_required();
//Get the ID of the group (with admin access) //Get the ID of the group (with admin access)
$groupID = getPostGroupIdWithAccess("id", GroupInfo::ADMIN_ACCESS); $groupID = getPostGroupIdWithAccess("id", GroupInfo::ADMIN_ACCESS);
@ -225,6 +233,33 @@ class GroupsController {
); );
} }
/**
* Respond to a membership invitation
*
* @url POST /groups/respond_invitation
*/
public function respondInvitation(){
user_login_required();
//Get the ID of the group (with basic access)
$groupID = getPostGroupIdWithAccess("id", GroupInfo::LIMITED_ACCESS);
//Get the response to the invitation
$accept = postBool("accept");
//Check if the user received an invitation or not
if(!components()->groups->receivedInvitation(userID, $groupID))
Rest_fatal_error(404, "Invitation not found!");
//Try to respond to the invitation
if(!components()->groups->respondInvitation(userID, $groupID, $accept))
Rest_fatal_error(500, "An error occurred while trying to respond to membership invitation!");
//Success
return array("success" => "The response to the invitation was saved!");
}
/** /**
* Parse a GroupInfo object into an array for the API * Parse a GroupInfo object into an array for the API
* *

View File

@ -183,6 +183,23 @@ class GroupsComponent {
)); ));
} }
/**
* Update a membership level
*
* @param int $userID The ID of the target user
* @param int $groupID The ID of the related group
* @param int $level The target level
* @return bool TRUE for a success / FALSE else
*/
private function updateMembershipLevel(int $userID, int $groupID, int $level) : bool {
return db()->updateDB(
self::GROUPS_MEMBERS_TABLE,
"user_id = ? AND groups_id = ?",
array("level" => $level),
array($userID, $groupID)
);
}
/** /**
* Check whether a user has already a saved membership in a group or not * Check whether a user has already a saved membership in a group or not
* *
@ -197,6 +214,54 @@ class GroupsComponent {
array($groupID, $userID)) > 0; array($groupID, $userID)) > 0;
} }
/**
* Check whether a user received an invitation or not
*
* @param int $userID The ID of the user to check
* @param int $groupID The ID of the related group
* @return bool TRUE if the user received an invitation / FALSE else
*/
public function receivedInvitation(int $userID, int $groupID) : bool {
return db()->count(
self::GROUPS_MEMBERS_TABLE,
"WHERE groups_id = ? AND user_ID = ? AND level = ?",
array($groupID, $userID, GroupMember::INVITED)
) > 0;
}
/**
* Respond to a membership invitation
*
* @param int $userID The ID of the target user
* @param int $groupID The ID of the related group
* @param bool $accept Set wether the user accept the invitation or not
* @return bool TRUE for a success / FALSE else
*/
public function respondInvitation(int $userID, int $groupID, bool $accept) : bool {
//If the user reject the invitation, delete it
if(!$accept)
return $this->deleteInvitation($userID, $groupID);
//Upgrade the user as member
return $this->updateMembershipLevel($userID, $groupID, GroupMember::MEMBER);
}
/**
* Delete a membership invitation
*
* @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 deleteInvitation(int $userID, int $groupID) : bool {
return db()->deleteEntry(
self::GROUPS_MEMBERS_TABLE,
"groups_id = ? AND user_id = ? AND level = ?",
array($groupID, $userID, GroupMember::INVITED)
);
}
/** /**
* Get the membership level of a user to a group * Get the membership level of a user to a group
* *