diff --git a/RestControllers/GroupsController.php b/RestControllers/GroupsController.php index eb703f3..04d96d2 100644 --- a/RestControllers/GroupsController.php +++ b/RestControllers/GroupsController.php @@ -412,6 +412,30 @@ class GroupsController { return array("success" => "The response to the request has been successfully saved!"); } + /** + * Get information about a membership + * + * @url POST /groups/get_membership + */ + public function getMembership() : array { + + //Get the ID of the target group + $groupID = getPostGroupIdWithAccess("groupID", GroupInfo::MODERATOR_ACCESS); + + //Get user ID + $userID = getPostUserID("userID"); + + //Check if the user has a membership or not + if(!components()->groups->hasMembership($userID, $groupID)) + Rest_fatal_error(404, "Specified user does not have any membership in this group!"); + + //Get user membership + $membership = components()->groups->getMembership($userID, $groupID); + + //Parse and return result + return self::GroupMemberToAPI($membership); + } + /** * Parse a GroupInfo object into an array for the API * diff --git a/classes/components/GroupsComponent.php b/classes/components/GroupsComponent.php index 1c4a717..6d0c047 100644 --- a/classes/components/GroupsComponent.php +++ b/classes/components/GroupsComponent.php @@ -240,7 +240,7 @@ class GroupsComponent { * @param int $groupID The ID of the target group * @return bool TRUE if the database includes a membership for the user / FALSE else */ - private function hasMembership(int $userID, int $groupID) : bool { + public function hasMembership(int $userID, int $groupID) : bool { return db()->count( self::GROUPS_MEMBERS_TABLE, "WHERE groups_id = ? AND user_id = ?", @@ -375,6 +375,28 @@ class GroupsComponent { return $results[0]["level"]; } + /** + * Get information the membership of a user over a group + * + * @param int $userID The ID of the target user + * @param int $groupID The ID of the target group + * @param GroupMember User membership + */ + public function getMembership(int $userID, int $groupID) : GroupMember { + //Fetch the database to get membership + $results = db()->select( + self::GROUPS_MEMBERS_TABLE, + "WHERE groups_id = ? AND user_id = ?", + array($groupID, $userID) + ); + + //Check for results + if(count($results) < 0) + return new GroupMember(); //Invalid object + + return $this->dbToGroupMember($results[0]); + } + /** * Check whether a user is an administrator of a group * or not