mirror of
https://github.com/pierre42100/ComunicAPI
synced 2024-11-27 07:49:27 +00:00
Can respond to a membership invitation.
This commit is contained in:
parent
d55cca75b2
commit
c3bdbedb30
@ -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
|
||||||
*
|
*
|
||||||
|
@ -7,328 +7,393 @@
|
|||||||
|
|
||||||
class GroupsComponent {
|
class GroupsComponent {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Groups list table
|
* Groups list table
|
||||||
*/
|
*/
|
||||||
const GROUPS_LIST_TABLE = DBprefix . "groups";
|
const GROUPS_LIST_TABLE = DBprefix . "groups";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Groups members table
|
* Groups members table
|
||||||
*/
|
*/
|
||||||
const GROUPS_MEMBERS_TABLE = DBprefix."groups_members";
|
const GROUPS_MEMBERS_TABLE = DBprefix."groups_members";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new group
|
* Create a new group
|
||||||
*
|
*
|
||||||
* @param NewGroup $newGroup Information about the new group
|
* @param NewGroup $newGroup Information about the new group
|
||||||
* to create
|
* to create
|
||||||
* @return int The ID of the created group / -1 in case of failure
|
* @return int The ID of the created group / -1 in case of failure
|
||||||
*/
|
*/
|
||||||
public function create(NewGroup $newGroup) : int {
|
public function create(NewGroup $newGroup) : int {
|
||||||
|
|
||||||
//Insert the group in the database
|
//Insert the group in the database
|
||||||
db()->addLine(self::GROUPS_LIST_TABLE, array(
|
db()->addLine(self::GROUPS_LIST_TABLE, array(
|
||||||
"time_create" => $newGroup->get_time_sent(),
|
"time_create" => $newGroup->get_time_sent(),
|
||||||
"userid_create" => $newGroup->get_userID(),
|
"userid_create" => $newGroup->get_userID(),
|
||||||
"name" => $newGroup->get_name()
|
"name" => $newGroup->get_name()
|
||||||
));
|
));
|
||||||
|
|
||||||
//Get the ID of the last inserted group
|
//Get the ID of the last inserted group
|
||||||
$groupID = db()->getLastInsertedID();
|
$groupID = db()->getLastInsertedID();
|
||||||
|
|
||||||
//Check for errors
|
//Check for errors
|
||||||
if(!$groupID > 0)
|
if(!$groupID > 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
//Register the user who created the group as an admin of the group
|
//Register the user who created the group as an admin of the group
|
||||||
$member = new GroupMember;
|
$member = new GroupMember;
|
||||||
$member->set_group_id($groupID);
|
$member->set_group_id($groupID);
|
||||||
$member->set_userID($newGroup->get_userID());
|
$member->set_userID($newGroup->get_userID());
|
||||||
$member->set_time_sent($newGroup->get_time_sent());
|
$member->set_time_sent($newGroup->get_time_sent());
|
||||||
$member->set_level(GroupMember::ADMINISTRATOR);
|
$member->set_level(GroupMember::ADMINISTRATOR);
|
||||||
$this->insertMember($member);
|
$this->insertMember($member);
|
||||||
|
|
||||||
return $groupID;
|
return $groupID;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether a group exists or not
|
* Check whether a group exists or not
|
||||||
*
|
*
|
||||||
* @param int $id The ID of the target group
|
* @param int $id The ID of the target group
|
||||||
* @return bool TRUE if the group exists / FALSE else
|
* @return bool TRUE if the group exists / FALSE else
|
||||||
*/
|
*/
|
||||||
public function exists(int $id) : bool {
|
public function exists(int $id) : bool {
|
||||||
|
|
||||||
return db()->count(
|
return db()->count(
|
||||||
self::GROUPS_LIST_TABLE,
|
self::GROUPS_LIST_TABLE,
|
||||||
"WHERE id = ?",
|
"WHERE id = ?",
|
||||||
array($id)
|
array($id)
|
||||||
) > 0;
|
) > 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the visibility level of a group
|
* Get the visibility level of a group
|
||||||
*
|
*
|
||||||
* @param int $id The ID of the target group
|
* @param int $id The ID of the target group
|
||||||
* @return int The visibility level of the group
|
* @return int The visibility level of the group
|
||||||
*/
|
*/
|
||||||
public function getVisiblity(int $id) : int {
|
public function getVisiblity(int $id) : int {
|
||||||
$data = db()->select(
|
$data = db()->select(
|
||||||
self::GROUPS_LIST_TABLE,
|
self::GROUPS_LIST_TABLE,
|
||||||
"WHERE id = ?",
|
"WHERE id = ?",
|
||||||
array($id),
|
array($id),
|
||||||
array("visibility")
|
array("visibility")
|
||||||
);
|
);
|
||||||
|
|
||||||
if(count($data) < 1)
|
if(count($data) < 1)
|
||||||
throw new Exception("Group " + $id + " does not exists!");
|
throw new Exception("Group " + $id + " does not exists!");
|
||||||
|
|
||||||
return $data[0]["visibility"];
|
return $data[0]["visibility"];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get and return information about a group
|
* Get and return information about a group
|
||||||
*
|
*
|
||||||
* @param int $id The ID of the target group
|
* @param int $id The ID of the target group
|
||||||
* @return GroupInfo Information about the group / invalid
|
* @return GroupInfo Information about the group / invalid
|
||||||
* object in case of failure
|
* object in case of failure
|
||||||
*/
|
*/
|
||||||
public function get_info(int $id) : GroupInfo {
|
public function get_info(int $id) : GroupInfo {
|
||||||
|
|
||||||
//Query the database
|
//Query the database
|
||||||
$info = db()->select(self::GROUPS_LIST_TABLE, "WHERE id = ?", array($id));
|
$info = db()->select(self::GROUPS_LIST_TABLE, "WHERE id = ?", array($id));
|
||||||
|
|
||||||
//Check for results
|
//Check for results
|
||||||
if(count($info) == 0)
|
if(count($info) == 0)
|
||||||
return new GroupInfo(); //Return invalid object
|
return new GroupInfo(); //Return invalid object
|
||||||
|
|
||||||
//Create and fill GroupInfo object with database entry
|
//Create and fill GroupInfo object with database entry
|
||||||
return $this->dbToGroupInfo($info[0]);
|
return $this->dbToGroupInfo($info[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get and return advanced information about a group
|
* Get and return advanced information about a group
|
||||||
*
|
*
|
||||||
* @param int $id The ID of the target group
|
* @param int $id The ID of the target group
|
||||||
* @return GroupInfo Information about the group / invalid
|
* @return GroupInfo Information about the group / invalid
|
||||||
* object in case of failure
|
* object in case of failure
|
||||||
*/
|
*/
|
||||||
public function get_advanced_info(int $id) : AdvancedGroupInfo {
|
public function get_advanced_info(int $id) : AdvancedGroupInfo {
|
||||||
|
|
||||||
//Query the database
|
//Query the database
|
||||||
$info = db()->select(self::GROUPS_LIST_TABLE, "WHERE id = ?", array($id));
|
$info = db()->select(self::GROUPS_LIST_TABLE, "WHERE id = ?", array($id));
|
||||||
|
|
||||||
//Check for results
|
//Check for results
|
||||||
if(count($info) == 0)
|
if(count($info) == 0)
|
||||||
return new AdvancedGroupInfo(); //Return invalid object
|
return new AdvancedGroupInfo(); //Return invalid object
|
||||||
|
|
||||||
//Create and fill GroupInfo object with database entry
|
//Create and fill GroupInfo object with database entry
|
||||||
return $this->dbToAdvancedGroupInfo($info[0]);
|
return $this->dbToAdvancedGroupInfo($info[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a group settings
|
* Get a group settings
|
||||||
*
|
*
|
||||||
* @param int $id The ID of the target group
|
* @param int $id The ID of the target group
|
||||||
* @return GroupSettings The settings of the group / invalid
|
* @return GroupSettings The settings of the group / invalid
|
||||||
* GroupSettings object in case of failure
|
* GroupSettings object in case of failure
|
||||||
*/
|
*/
|
||||||
public function get_settings(int $id) : GroupSettings {
|
public function get_settings(int $id) : GroupSettings {
|
||||||
|
|
||||||
//Query the database
|
//Query the database
|
||||||
$info = db()->select(self::GROUPS_LIST_TABLE, "WHERE id = ?", array($id));
|
$info = db()->select(self::GROUPS_LIST_TABLE, "WHERE id = ?", array($id));
|
||||||
|
|
||||||
//Check for results
|
//Check for results
|
||||||
if(count($info) == 0)
|
if(count($info) == 0)
|
||||||
return new GroupSettings(); //Return invalid object
|
return new GroupSettings(); //Return invalid object
|
||||||
|
|
||||||
//Create and fill GroupInfo object with database entry
|
//Create and fill GroupInfo object with database entry
|
||||||
return $this->dbToGroupSettings($info[0]);
|
return $this->dbToGroupSettings($info[0]);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set (update) group settings
|
* Set (update) group settings
|
||||||
*
|
*
|
||||||
* @param GroupSettings $settings The settings to update
|
* @param GroupSettings $settings The settings to update
|
||||||
* @return bool TRUE for a success / FALSE
|
* @return bool TRUE for a success / FALSE
|
||||||
*/
|
*/
|
||||||
public function set_settings(GroupSettings $settings) : bool {
|
public function set_settings(GroupSettings $settings) : bool {
|
||||||
|
|
||||||
//Generate database entry
|
//Generate database entry
|
||||||
$modif = $this->GroupSettingsToDB($settings);
|
$modif = $this->GroupSettingsToDB($settings);
|
||||||
|
|
||||||
//Apply update
|
//Apply update
|
||||||
return db()->updateDB(
|
return db()->updateDB(
|
||||||
self::GROUPS_LIST_TABLE,
|
self::GROUPS_LIST_TABLE,
|
||||||
"id = ?",
|
"id = ?",
|
||||||
$modif,
|
$modif,
|
||||||
array($settings->get_id()));
|
array($settings->get_id()));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert a new group member
|
* Insert a new group member
|
||||||
*
|
*
|
||||||
* @param GroupMember $member Information about the member to insert
|
* @param GroupMember $member Information about the member to insert
|
||||||
* @return bool TRUE for a success / FALSE else
|
* @return bool TRUE for a success / FALSE else
|
||||||
*/
|
*/
|
||||||
private function insertMember(GroupMember $member) : bool {
|
private function insertMember(GroupMember $member) : bool {
|
||||||
return db()->addLine(self::GROUPS_MEMBERS_TABLE, array(
|
return db()->addLine(self::GROUPS_MEMBERS_TABLE, array(
|
||||||
"groups_id" => $member->get_group_id(),
|
"groups_id" => $member->get_group_id(),
|
||||||
"user_id" => $member->get_userID(),
|
"user_id" => $member->get_userID(),
|
||||||
"time_create" => $member->get_time_sent(),
|
"time_create" => $member->get_time_sent(),
|
||||||
"level" => $member->get_level()
|
"level" => $member->get_level()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether a user has already a saved membership in a group or not
|
* Update a membership level
|
||||||
*
|
*
|
||||||
* @param int $userID The ID of the target user
|
* @param int $userID The ID of the target user
|
||||||
* @param int $groupID The ID of the target group
|
* @param int $groupID The ID of the related group
|
||||||
* @return bool TRUE if the database includes a membership for the user / FALSE else
|
* @param int $level The target level
|
||||||
*/
|
* @return bool TRUE for a success / FALSE else
|
||||||
private function hasMembership(int $userID, int $groupID) : bool {
|
*/
|
||||||
return db()->count(
|
private function updateMembershipLevel(int $userID, int $groupID, int $level) : bool {
|
||||||
self::GROUPS_MEMBERS_TABLE,
|
return db()->updateDB(
|
||||||
"WHERE groups_id = ? AND user_id = ?",
|
self::GROUPS_MEMBERS_TABLE,
|
||||||
array($groupID, $userID)) > 0;
|
"user_id = ? AND groups_id = ?",
|
||||||
}
|
array("level" => $level),
|
||||||
|
array($userID, $groupID)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the membership level of a user to a group
|
* Check whether a user has already a saved membership in a group or not
|
||||||
*
|
*
|
||||||
* @param int $userID The ID of the queried user
|
* @param int $userID The ID of the target user
|
||||||
* @param int $groupID The ID of the target group
|
* @param int $groupID The ID of the target group
|
||||||
* @return int The membership level of the user
|
* @return bool TRUE if the database includes a membership for the user / FALSE else
|
||||||
*/
|
*/
|
||||||
public function getMembershipLevel(int $userID, int $groupID) : int {
|
private function hasMembership(int $userID, int $groupID) : bool {
|
||||||
|
return db()->count(
|
||||||
|
self::GROUPS_MEMBERS_TABLE,
|
||||||
|
"WHERE groups_id = ? AND user_id = ?",
|
||||||
|
array($groupID, $userID)) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
//Check for membership
|
/**
|
||||||
if(!$this->hasMembership($userID, $groupID))
|
* Check whether a user received an invitation or not
|
||||||
return GroupMember::VISITOR;
|
*
|
||||||
|
* @param int $userID The ID of the user to check
|
||||||
//Fetch the database to get membership
|
* @param int $groupID The ID of the related group
|
||||||
$results = db()->select(
|
* @return bool TRUE if the user received an invitation / FALSE else
|
||||||
self::GROUPS_MEMBERS_TABLE,
|
*/
|
||||||
"WHERE groups_id = ? AND user_id = ?",
|
public function receivedInvitation(int $userID, int $groupID) : bool {
|
||||||
array($groupID, $userID),
|
return db()->count(
|
||||||
array("level")
|
self::GROUPS_MEMBERS_TABLE,
|
||||||
);
|
"WHERE groups_id = ? AND user_ID = ? AND level = ?",
|
||||||
|
array($groupID, $userID, GroupMember::INVITED)
|
||||||
|
) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
//Check for results
|
/**
|
||||||
if(count($results) < 0)
|
* Respond to a membership invitation
|
||||||
return GroupMember::VISITOR; //Security first
|
*
|
||||||
|
* @param int $userID The ID of the target user
|
||||||
return $results[0]["level"];
|
* @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
|
||||||
* Check whether a user is an administrator of a group
|
if(!$accept)
|
||||||
* or not
|
return $this->deleteInvitation($userID, $groupID);
|
||||||
*
|
|
||||||
* @param int $userID Requested user ID to check
|
|
||||||
* @param int $groupID Requested group to check
|
|
||||||
* @return bool TRUE if the user is an admin / FALSE else
|
|
||||||
*/
|
|
||||||
public function isAdmin(int $userID, int $groupID) : bool {
|
|
||||||
return $this->getMembershipLevel($userID, $groupID)
|
|
||||||
== GroupMember::ADMINISTRATOR;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
//Upgrade the user as member
|
||||||
* Check whether a group is open or not
|
return $this->updateMembershipLevel($userID, $groupID, GroupMember::MEMBER);
|
||||||
*
|
}
|
||||||
* @param int $groupID The ID of the target group
|
|
||||||
* @return bool TRUE if the group is open / FALSE else
|
|
||||||
*/
|
|
||||||
public function isOpen(int $groupID) : bool {
|
|
||||||
return db()->count(
|
|
||||||
self::GROUPS_LIST_TABLE,
|
|
||||||
"WHERE id = ? AND visibility = ?",
|
|
||||||
array($groupID, GroupInfo::OPEN_GROUP)) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether a group is secret or not
|
* Delete a membership invitation
|
||||||
*
|
*
|
||||||
* @param int $groupID The ID of the target group
|
* @param int $userID The ID of the target user
|
||||||
* @return bool TRUE if the group is open / FALSE else
|
* @param int $groupID The ID of the related group
|
||||||
*/
|
* @return bool TRUE for a success / FALSE else
|
||||||
public function isSecret(int $groupID) : bool {
|
*/
|
||||||
return db()->count(
|
public function deleteInvitation(int $userID, int $groupID) : bool {
|
||||||
self::GROUPS_LIST_TABLE,
|
return db()->deleteEntry(
|
||||||
"WHERE id = ? AND visibility = ?",
|
self::GROUPS_MEMBERS_TABLE,
|
||||||
array($groupID, GroupInfo::SECRET_GROUP)) > 0;
|
"groups_id = ? AND user_id = ? AND level = ?",
|
||||||
}
|
array($groupID, $userID, GroupMember::INVITED)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Count the number of members of a group
|
* Get the membership level of a user to a group
|
||||||
*
|
*
|
||||||
* @param int $id The ID of the target group
|
* @param int $userID The ID of the queried user
|
||||||
* @return int The number of members of the group
|
* @param int $groupID The ID of the target group
|
||||||
*/
|
* @return int The membership level of the user
|
||||||
private function countMembers(int $id) : int {
|
*/
|
||||||
return db()->count(self::GROUPS_MEMBERS_TABLE,
|
public function getMembershipLevel(int $userID, int $groupID) : int {
|
||||||
"WHERE groups_id = ?",
|
|
||||||
array($id));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
//Check for membership
|
||||||
* Get and return the access level of a user over a group
|
if(!$this->hasMembership($userID, $groupID))
|
||||||
*
|
return GroupMember::VISITOR;
|
||||||
* @param int $groupID The ID of the target group
|
|
||||||
* @param int $userID The ID of the user
|
//Fetch the database to get membership
|
||||||
* @return int The visiblity access level of the user
|
$results = db()->select(
|
||||||
*/
|
self::GROUPS_MEMBERS_TABLE,
|
||||||
public function getAccessLevel(int $groupID, int $userID) : int {
|
"WHERE groups_id = ? AND user_id = ?",
|
||||||
|
array($groupID, $userID),
|
||||||
|
array("level")
|
||||||
|
);
|
||||||
|
|
||||||
if($userID > 0)
|
//Check for results
|
||||||
//Get the membership level of the user
|
if(count($results) < 0)
|
||||||
$membership_level = $this->getMembershipLevel($userID, $groupID);
|
return GroupMember::VISITOR; //Security first
|
||||||
|
|
||||||
else
|
return $results[0]["level"];
|
||||||
$membership_level = GroupMember::VISITOR; //Signed out users are all visitors
|
}
|
||||||
|
|
||||||
//Check if the user is a confirmed member of group
|
/**
|
||||||
if($membership_level == GroupMember::ADMINISTRATOR)
|
* Check whether a user is an administrator of a group
|
||||||
return GroupInfo::ADMIN_ACCESS;
|
* or not
|
||||||
if($membership_level == GroupMember::MODERATOR)
|
*
|
||||||
return GroupInfo::MODERATOR_ACCESS;
|
* @param int $userID Requested user ID to check
|
||||||
if($membership_level == GroupMember::MEMBER)
|
* @param int $groupID Requested group to check
|
||||||
return GroupInfo::MEMBER_ACCESS;
|
* @return bool TRUE if the user is an admin / FALSE else
|
||||||
|
*/
|
||||||
//Get the visibility level of the group
|
public function isAdmin(int $userID, int $groupID) : bool {
|
||||||
$group_visibility_level = $this->getVisiblity($groupID);
|
return $this->getMembershipLevel($userID, $groupID)
|
||||||
|
== GroupMember::ADMINISTRATOR;
|
||||||
|
}
|
||||||
|
|
||||||
//If the group is open, everyone has view access
|
/**
|
||||||
if($group_visibility_level == GroupInfo::OPEN_GROUP)
|
* Check whether a group is open or not
|
||||||
return GroupInfo::VIEW_ACCESS;
|
*
|
||||||
|
* @param int $groupID The ID of the target group
|
||||||
|
* @return bool TRUE if the group is open / FALSE else
|
||||||
|
*/
|
||||||
|
public function isOpen(int $groupID) : bool {
|
||||||
|
return db()->count(
|
||||||
|
self::GROUPS_LIST_TABLE,
|
||||||
|
"WHERE id = ? AND visibility = ?",
|
||||||
|
array($groupID, GroupInfo::OPEN_GROUP)) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
//Else, all pending and invited membership get limited access
|
/**
|
||||||
if($membership_level == GroupMember::PENDING ||
|
* Check whether a group is secret or not
|
||||||
$membership_level == GroupMember::INVITED)
|
*
|
||||||
return GroupInfo::LIMITED_ACCESS;
|
* @param int $groupID The ID of the target group
|
||||||
|
* @return bool TRUE if the group is open / FALSE else
|
||||||
|
*/
|
||||||
|
public function isSecret(int $groupID) : bool {
|
||||||
|
return db()->count(
|
||||||
|
self::GROUPS_LIST_TABLE,
|
||||||
|
"WHERE id = ? AND visibility = ?",
|
||||||
|
array($groupID, GroupInfo::SECRET_GROUP)) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
//Private groups gives limited access
|
/**
|
||||||
if($group_visibility_level == GroupInfo::PRIVATE_GROUP)
|
* Count the number of members of a group
|
||||||
return GroupInfo::LIMITED_ACCESS;
|
*
|
||||||
|
* @param int $id The ID of the target group
|
||||||
//Else the user can not see the group
|
* @return int The number of members of the group
|
||||||
return GroupInfo::NO_ACCESS;
|
*/
|
||||||
}
|
private function countMembers(int $id) : int {
|
||||||
|
return db()->count(self::GROUPS_MEMBERS_TABLE,
|
||||||
|
"WHERE groups_id = ?",
|
||||||
|
array($id));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete current group logo (if any)
|
* Get and return the access level of a user over a group
|
||||||
*
|
*
|
||||||
* @param int $id The ID of the target group
|
* @param int $groupID The ID of the target group
|
||||||
* @return bool TRUE if the logo was deleted / FALSE else
|
* @param int $userID The ID of the user
|
||||||
*/
|
* @return int The visiblity access level of the user
|
||||||
public function deleteLogo(int $id) : bool {
|
*/
|
||||||
|
public function getAccessLevel(int $groupID, int $userID) : int {
|
||||||
|
|
||||||
//Get the current settings of the group
|
if($userID > 0)
|
||||||
|
//Get the membership level of the user
|
||||||
|
$membership_level = $this->getMembershipLevel($userID, $groupID);
|
||||||
|
|
||||||
|
else
|
||||||
|
$membership_level = GroupMember::VISITOR; //Signed out users are all visitors
|
||||||
|
|
||||||
|
//Check if the user is a confirmed member of group
|
||||||
|
if($membership_level == GroupMember::ADMINISTRATOR)
|
||||||
|
return GroupInfo::ADMIN_ACCESS;
|
||||||
|
if($membership_level == GroupMember::MODERATOR)
|
||||||
|
return GroupInfo::MODERATOR_ACCESS;
|
||||||
|
if($membership_level == GroupMember::MEMBER)
|
||||||
|
return GroupInfo::MEMBER_ACCESS;
|
||||||
|
|
||||||
|
//Get the visibility level of the group
|
||||||
|
$group_visibility_level = $this->getVisiblity($groupID);
|
||||||
|
|
||||||
|
//If the group is open, everyone has view access
|
||||||
|
if($group_visibility_level == GroupInfo::OPEN_GROUP)
|
||||||
|
return GroupInfo::VIEW_ACCESS;
|
||||||
|
|
||||||
|
//Else, all pending and invited membership get limited access
|
||||||
|
if($membership_level == GroupMember::PENDING ||
|
||||||
|
$membership_level == GroupMember::INVITED)
|
||||||
|
return GroupInfo::LIMITED_ACCESS;
|
||||||
|
|
||||||
|
//Private groups gives limited access
|
||||||
|
if($group_visibility_level == GroupInfo::PRIVATE_GROUP)
|
||||||
|
return GroupInfo::LIMITED_ACCESS;
|
||||||
|
|
||||||
|
//Else the user can not see the group
|
||||||
|
return GroupInfo::NO_ACCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete current group logo (if any)
|
||||||
|
*
|
||||||
|
* @param int $id The ID of the target group
|
||||||
|
* @return bool TRUE if the logo was deleted / FALSE else
|
||||||
|
*/
|
||||||
|
public function deleteLogo(int $id) : bool {
|
||||||
|
|
||||||
|
//Get the current settings of the group
|
||||||
$settings = $this->get_settings($id);
|
$settings = $this->get_settings($id);
|
||||||
|
|
||||||
//Check if the group has currently an group logo or not
|
//Check if the group has currently an group logo or not
|
||||||
@ -337,105 +402,105 @@ class GroupsComponent {
|
|||||||
//Delete the previous logo
|
//Delete the previous logo
|
||||||
if(file_exists($settings->get_logo_sys_path()))
|
if(file_exists($settings->get_logo_sys_path()))
|
||||||
if(!unlink($settings->get_logo_sys_path()))
|
if(!unlink($settings->get_logo_sys_path()))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
//Save new information
|
//Save new information
|
||||||
$settings->set_logo("null");
|
$settings->set_logo("null");
|
||||||
return $this->set_settings($settings);
|
return $this->set_settings($settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Success (nothing to be done)
|
//Success (nothing to be done)
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Turn a database entry into a GroupInfo object
|
* Turn a database entry into a GroupInfo object
|
||||||
*
|
*
|
||||||
* @param array $data Database entry
|
* @param array $data Database entry
|
||||||
* @param GroupInfo $group The object to fill with the information (optionnal)
|
* @param GroupInfo $group The object to fill with the information (optionnal)
|
||||||
* @return GroupInfo Generated object
|
* @return GroupInfo Generated object
|
||||||
*/
|
*/
|
||||||
private function dbToGroupInfo(array $data, GroupInfo $info = null) : GroupInfo {
|
private function dbToGroupInfo(array $data, GroupInfo $info = null) : GroupInfo {
|
||||||
|
|
||||||
if($info == null)
|
if($info == null)
|
||||||
$info = new GroupInfo();
|
$info = new GroupInfo();
|
||||||
|
|
||||||
$info->set_id($data["id"]);
|
$info->set_id($data["id"]);
|
||||||
$info->set_name($data["name"]);
|
$info->set_name($data["name"]);
|
||||||
$info->set_number_members($this->countMembers($info->get_id()));
|
$info->set_number_members($this->countMembers($info->get_id()));
|
||||||
$info->set_membership_level($this->getMembershipLevel(userID, $info->get_id()));
|
$info->set_membership_level($this->getMembershipLevel(userID, $info->get_id()));
|
||||||
$info->set_visibility($data["visibility"]);
|
$info->set_visibility($data["visibility"]);
|
||||||
$info->set_registration_level($data["registration_level"]);
|
$info->set_registration_level($data["registration_level"]);
|
||||||
|
|
||||||
if($data["path_logo"] != null && $data["path_logo"] != "" && $data["path_logo"] != "null")
|
if($data["path_logo"] != null && $data["path_logo"] != "" && $data["path_logo"] != "null")
|
||||||
$info->set_logo($data["path_logo"]);
|
$info->set_logo($data["path_logo"]);
|
||||||
|
|
||||||
return $info;
|
return $info;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Turn a database group entry into AdvancedGroupInfo object entry
|
* Turn a database group entry into AdvancedGroupInfo object entry
|
||||||
*
|
*
|
||||||
* @param array $data Database entry
|
* @param array $data Database entry
|
||||||
* @param AdvancedGroupInfo $info Optionnal, fill an existing object
|
* @param AdvancedGroupInfo $info Optionnal, fill an existing object
|
||||||
* instead of creating a new one
|
* instead of creating a new one
|
||||||
* @return AdvancedGroupInfo Advanced information about the group
|
* @return AdvancedGroupInfo Advanced information about the group
|
||||||
*/
|
*/
|
||||||
private function dbToAdvancedGroupInfo(array $data, AdvancedGroupInfo $info = null) : AdvancedGroupInfo {
|
private function dbToAdvancedGroupInfo(array $data, AdvancedGroupInfo $info = null) : AdvancedGroupInfo {
|
||||||
|
|
||||||
if($info == null)
|
if($info == null)
|
||||||
$info = new AdvancedGroupInfo();
|
$info = new AdvancedGroupInfo();
|
||||||
|
|
||||||
//Parse basical information about the group
|
//Parse basical information about the group
|
||||||
$this->dbToGroupInfo($data, $info);
|
$this->dbToGroupInfo($data, $info);
|
||||||
|
|
||||||
//Parse advanced information
|
//Parse advanced information
|
||||||
$info->set_time_create($data["time_create"]);
|
$info->set_time_create($data["time_create"]);
|
||||||
|
|
||||||
return $info;
|
return $info;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Turn a database group entry into GroupSettings object
|
* Turn a database group entry into GroupSettings object
|
||||||
*
|
*
|
||||||
* @param array $data Database entry
|
* @param array $data Database entry
|
||||||
* @return GroupSettings The settings of the group
|
* @return GroupSettings The settings of the group
|
||||||
*/
|
*/
|
||||||
private function dbToGroupSettings(array $data) : GroupSettings {
|
private function dbToGroupSettings(array $data) : GroupSettings {
|
||||||
|
|
||||||
//Parse advanced settings about the group
|
//Parse advanced settings about the group
|
||||||
$info = new GroupSettings();
|
$info = new GroupSettings();
|
||||||
$this->dbToAdvancedGroupInfo($data, $info);
|
$this->dbToAdvancedGroupInfo($data, $info);
|
||||||
|
|
||||||
return $info;
|
return $info;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Turn a GroupSettings object into a database entry
|
* Turn a GroupSettings object into a database entry
|
||||||
*
|
*
|
||||||
* @param GroupSettings $settings The object to convert
|
* @param GroupSettings $settings The object to convert
|
||||||
* @return array Generated database entry
|
* @return array Generated database entry
|
||||||
*/
|
*/
|
||||||
private function GroupSettingsToDB(GroupSettings $settings) : array {
|
private function GroupSettingsToDB(GroupSettings $settings) : array {
|
||||||
$data = array();
|
$data = array();
|
||||||
|
|
||||||
if($settings->has_name())
|
if($settings->has_name())
|
||||||
$data["name"] = $settings->get_name();
|
$data["name"] = $settings->get_name();
|
||||||
|
|
||||||
if($settings->has_logo())
|
if($settings->has_logo())
|
||||||
$data["path_logo"] = $settings->get_logo();
|
$data["path_logo"] = $settings->get_logo();
|
||||||
|
|
||||||
if($settings->has_visibility())
|
if($settings->has_visibility())
|
||||||
$data["visibility"] = $settings->get_visibility();
|
$data["visibility"] = $settings->get_visibility();
|
||||||
|
|
||||||
if($settings->has_registration_level())
|
if($settings->has_registration_level())
|
||||||
$data["registration_level"] = $settings->get_registration_level();
|
$data["registration_level"] = $settings->get_registration_level();
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Register component
|
//Register component
|
||||||
|
Loading…
Reference in New Issue
Block a user