Skip to content

Issues-315: fixed performance issues with Category/Group permissions #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 42 additions & 17 deletions models/class.groupmodel.php
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ public function join($GroupID, $UserID, $watched = true, $followed = true ){
}
$this->followGroup($GroupID, $UserID, $followed);
$this->watchGroup($GroupID, $UserID, $watched);

self::clearUserGroupCache($UserID);
}

/**
Expand All @@ -860,7 +860,7 @@ public function accept($groupID, $userID){
}

/**
* Returntur if user is a member of the group
* Return true if user is a member of the group
*
*/
public function isMemberOfGroup($userID, $groupID) {
Expand All @@ -882,6 +882,7 @@ public function isMemberOfGroup($userID, $groupID) {
* @throws Exception
*/
public function setRole($GroupID, $MemberID, $Role){
self::clearUserGroupCache($MemberID);
return $this->SQL->update('UserGroup')
->set('Role' , $Role)
->where('GroupID' , $GroupID)
Expand All @@ -897,10 +898,12 @@ public function setRole($GroupID, $MemberID, $Role){
* @return bool|Gdn_DataSet|object|string|void
*/
public function removeMember($GroupID, $MemberID){
$result = $this->unbookmarkGroupDiscussions($GroupID, $MemberID);
$this->unbookmarkGroupDiscussions($GroupID, $MemberID);
$this->unwatchGroup($GroupID, $MemberID);
$this->unfollowGroup($GroupID, $MemberID);
return $this->SQL->delete('UserGroup', ['GroupID' => $GroupID, 'UserID' => $MemberID]);
$result = $this->SQL->delete('UserGroup', ['GroupID' => $GroupID, 'UserID' => $MemberID]);
self::clearUserGroupCache($MemberID);
return $result;

}

Expand Down Expand Up @@ -1124,25 +1127,33 @@ public function countOfMembers($groupId, $role = null){
* @return array|mixed|null
*/
public function memberOf($userID){
$sql = $this->SQL;
$result = $sql->select('ug.Role, ug.GroupID')
->from('UserGroup ug')
->where('UserID', $userID)
->get();
return $result->result();
$key = 'UserGroup_'.$userID;
$result = Gdn::cache()->get($key);
if ($result === Gdn_Cache::CACHEOP_FAILURE) {
$sql = clone $this->SQL;
$sql->reset();
$result = $sql->select('ug.Role, ug.GroupID')
->from('UserGroup ug')
->where('UserID', $userID)
->get()->result();
Gdn::cache()->store($key, $result);
return $result;
} else {
return $result;
}
}

/**
* Get a group role
*/
public function getGroupRoleFor($userID, $groupID) {
$sql = $this->SQL;
$result = $sql->select('ug.Role')
->from('UserGroup ug')
->where('UserID', $userID)
->where('GroupID', $groupID)
->get()->firstRow();
return $result;
$groups = $this->memberOf($userID);
foreach ($groups as $group) {
if ($group->GroupID == $groupID) {
return $group;
}
}
return false;
}

/**
Expand Down Expand Up @@ -1857,4 +1868,18 @@ public function getRootGroupCategory($group){

return -1; //return Vanilla root
}

/**
* Clear the cached UserGroup data for a specific user.
*
* @param int|null $userID The user to clear. Use `null` for the current user.
*/
public static function clearUserGroupCache($userID = null) {
if ($userID === null) {
$userID = Gdn::session()->UserID;
}

$key = 'UserGroup_'.$userID;
Gdn::cache()->remove($key);
}
}