Upgraded groups visibility system.

This commit is contained in:
Pierre HUBERT
2018-07-04 17:28:29 +02:00
parent e8d8fffbd1
commit 796a325590
4 changed files with 144 additions and 36 deletions

View File

@ -67,6 +67,26 @@ class GroupsComponent {
}
/**
* Get the visibility level of a group
*
* @param int $id The ID of the target group
* @return int The visibility level of the group
*/
public function getVisiblity(int $id) : int {
$data = db()->select(
self::GROUPS_LIST_TABLE,
"WHERE id = ?",
array($id),
array("visibility")
);
if(count($data) < 1)
throw new Exception("Group " + $id + " does not exists!");
return $data[0]["visibility"];
}
/**
* Get and return information about a group
*
@ -210,13 +230,40 @@ class GroupsComponent {
* or not
*
* @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){
public function isAdmin(int $userID, int $groupID) : bool {
return $this->getMembershipLevel($userID, $groupID)
== GroupMember::ADMINISTRATOR;
}
/**
* Check whether a group is open or not
*
* @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
*
* @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;
}
/**
* Count the number of members of a group
*
@ -229,6 +276,49 @@ class GroupsComponent {
array($id));
}
/**
* Get and return the access level of a user over a group
*
* @param int $groupID The ID of the target group
* @param int $userID The ID of the user
* @return int The visiblity access level of the user
*/
public function getAccessLevel(int $groupID, int $userID) : int {
if($userID > 0)
//Get the membership level of the user
$membership_level = $this->getMembershipLevel($userID, $groupID);
else
$membership_level = GroupInfo::VISITOR; //Signed out users are all visitors
//Check if the user is a confirmed member of group
if($membership_level == GroupInfo::ADMINISTRATOR)
return GroupInfo::ADMIN_ACCESS;
if($membership_level == GroupInfo::MODERATOR)
return GroupInfo::MODERATOR_ACCESS;
if($membership_level == GroupInfo::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 membership gives limited access
if($membership_level == GroupInfo::PENDING)
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)
*

View File

@ -21,7 +21,21 @@ class GroupInfo extends BaseUniqueObject {
private $logo;
private $membership_level = -1;
private $visiblity = -1;
//User access to a group
const NO_ACCESS = 0; //Can not even know if the group exists or not
const LIMITED_ACCESS = 1; //Access to the name of the group only
const VIEW_ACCESS = 2; //Can see the posts of the group, but not a member of the group
const MEMBER_ACCESS = 3; //Member access (same as view access but as member)
const MODERATOR_ACCESS = 4; //Can create posts, even if posts creation is restricted
const ADMIN_ACCESS = 5; //Can do everything
//Membership levels
const ADMINISTRATOR = 0;
const MODERATOR = 1;
const MEMBER = 2;
const PENDING = 3;
const VISITOR = 4;
//Get and set the name of group
public function set_name(string $name){