diff --git a/class.groups.plugin.php b/class.groups.plugin.php index d882dc7..5716f22 100644 --- a/class.groups.plugin.php +++ b/class.groups.plugin.php @@ -211,9 +211,9 @@ public function base_render_before($sender) { public function base_groupOptionsDropdown_handler($sender, $args){ $group = $args['Group']; - $currentTopcoderProjectRoles = $sender->Data['ChallengeCurrentUserProjectRoles']; + // $currentTopcoderProjectRoles = $sender->Data['ChallengeCurrentUserProjectRoles']; $groupModel = new GroupModel(); - $groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles); + // $groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles); $groupID = $group->GroupID; $canEdit = $groupModel->canEdit($group) ; $canDelete = $groupModel->canDelete($group) ; @@ -293,9 +293,9 @@ public function base_discussionOptionsDropdown_handler($sender, $args){ // The list of Topcoder Project Roles are added to a sender by Topcoder plugin before each request // for DiscussionController/GroupController $data = $sender->Data; - $currentTopcoderProjectRoles = val('ChallengeCurrentUserProjectRoles', $data, []); + // $currentTopcoderProjectRoles = val('ChallengeCurrentUserProjectRoles', $data, []); $groupModel = new GroupModel(); - $groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles); + // $groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles); $canView = $groupModel->canViewDiscussion($Discussion); $canEdit = $groupModel->canEditDiscussion($Discussion); $canDelete = $groupModel->canDeleteDiscussion($Discussion); @@ -468,7 +468,7 @@ private function watchCategory($sender, $categoryID = null, $watched = null, $t throw notFoundException('Category'); } - $hasPermission = $categoryModel::checkPermission($categoryID, 'Vanilla.Discussions.View'); + $hasPermission = CategoryModel::checkPermission($categoryID, 'Vanilla.Discussions.View'); if (!$hasPermission) { throw permissionException('Vanilla.Discussion.View'); } @@ -803,11 +803,13 @@ private function getTopcoderProjectRoles($user, $resources = null, $roleResource $allResourcesByMember = array_filter($resources, function ($k) use ($topcoderUsername) { return $k->memberHandle == $topcoderUsername; }); - foreach ($allResourcesByMember as $resource) { - $roleResource = array_filter($roleResources, function ($k) use ($resource) { - return $k->id == $resource->roleId; - }); - array_push($roles, reset($roleResource)->name); + if($allResourcesByMember) { + foreach ($allResourcesByMember as $resource) { + $roleResource = array_filter($roleResources, function ($k) use ($resource) { + return $k->id == $resource->roleId; + }); + array_push($roles, reset($roleResource)->name); + } } } return $roles; diff --git a/controllers/api/GroupsApiController.php b/controllers/api/GroupsApiController.php index bf033a4..5e1dc6f 100644 --- a/controllers/api/GroupsApiController.php +++ b/controllers/api/GroupsApiController.php @@ -319,7 +319,7 @@ public function patch_member($id, $userid, array $body) { throw new NotFoundException('Group'); } - $isMember = $this->groupModel->isMemberOfGroup($user->UserID, $group->GroupID); + $isMember = GroupModel::isMemberOfGroup($user->UserID, $group->GroupID); if(!$isMember) { throw new ClientException('User is not a member of this group'); } @@ -354,7 +354,7 @@ public function get_member($id, $userid) { throw new NotFoundException('Group'); } - $isMember = $this->groupModel->isMemberOfGroup($user->UserID, $group->GroupID); + $isMember = GroupModel::isMemberOfGroup($user->UserID, $group->GroupID); if(!$isMember) { throw new ClientException('User is not a member of this group'); } diff --git a/controllers/class.groupcontroller.php b/controllers/class.groupcontroller.php index dd47944..f0c933c 100644 --- a/controllers/class.groupcontroller.php +++ b/controllers/class.groupcontroller.php @@ -84,7 +84,7 @@ public function index($GroupID = '') { $this->title($Group->Name); $this->setData('Breadcrumbs', $this->buildBreadcrumb($Group)); - $this->setData('CurrentUserGroups', $this->GroupModel->memberOf(Gdn::session()->UserID)); + // $this->setData('CurrentUserGroups', GroupModel::memberOf(Gdn::session()->UserID)); $this->setData('TotalMembers', $this->GroupModel->countOfMembers($GroupID)); $this->setData('Leaders', $this->GroupModel->getLeaders($GroupID)); $this->setData('Members', $this->GroupModel->getMembers($GroupID,[],'',30,0)); @@ -402,7 +402,7 @@ public function invite($GroupID) { $this->Form->addError('You are a member of "'.$Group->Name.'".'); } else { try { - if($this->GroupModel->isMemberOfGroup($user->UserID, $GroupID)) { + if(GroupModel::isMemberOfGroup($user->UserID, $GroupID)) { $this->Form->addError('User is a member of "'.$Group->Name.'".'); } else { $this->GroupModel->invite($GroupID, $user->UserID); @@ -524,8 +524,7 @@ public function unwatch($GroupID) { * @throws Gdn_UserException */ public function accept($GroupID, $UserID) { - $Group = $this->findGroup($GroupID); - if($this->GroupModel->isMemberOfGroup($UserID, $GroupID) !== true) { + if(!GroupModel::isMemberOfGroup($UserID, $GroupID) ) { $this->GroupModel->accept($GroupID, $UserID); } redirectTo(GroupsPlugin::GROUP_ROUTE.$GroupID); diff --git a/controllers/class.groupscontroller.php b/controllers/class.groupscontroller.php index 984c754..f375633 100644 --- a/controllers/class.groupscontroller.php +++ b/controllers/class.groupscontroller.php @@ -102,7 +102,7 @@ public function index($Page=false, $filter) { $countOfGroups = $GroupModel->countMyGroups($where); //$AvailableGroupData = $GroupModel->getAvailableGroups($where, $defaultSort, $Limit, $Offset); - $this->setData('CurrentUserGroups', $GroupModel->memberOf(Gdn::session()->UserID)); + // $this->setData('CurrentUserGroups', GroupModel::memberOf(Gdn::session()->UserID)); $this->setData('Groups', $GroupData); $this->setData('CountOfGroups', $countOfGroups); @@ -138,7 +138,7 @@ public function mine($Page = false,$filter ='') { $countOfRegularGroups = $GroupModel->countMyGroups($regularGroupsWhere); $this->setData('RegularGroups', $regularGroupsData); $this->setData('CountOfRegularGroups', $countOfRegularGroups); - $this->setData('CurrentUserGroups', $GroupModel->memberOf(Gdn::session()->UserID)); + // $this->setData('CurrentUserGroups', GroupModel::memberOf(Gdn::session()->UserID)); $this->render(); } @@ -166,7 +166,7 @@ private function mygroups($Page = false, $filter = '') { $CountGroups = $GroupModel->countMyGroups($where); $this->setData('CountGroups', $CountGroups); $this->setData('Groups', $GroupData, true); - $this->setData('CurrentUserGroups', $GroupModel->memberOf(Gdn::session()->UserID)); + // $this->setData('CurrentUserGroups', GroupModel::memberOf(Gdn::session()->UserID)); // Build a pager $PagerFactory = new Gdn_PagerFactory(); @@ -234,7 +234,7 @@ public function all($Page = false, $filter = '') { $CountGroups = $GroupModel->countAvailableGroups($where); $this->setData('CountGroups', $CountGroups); $this->setData('Groups', $GroupData, true); - $this->setData('CurrentUserGroups', $GroupModel->memberOf(Gdn::session()->UserID)); + // $this->setData('CurrentUserGroups', GroupModel::memberOf(Gdn::session()->UserID)); // Build a pager $PagerFactory = new Gdn_PagerFactory(); diff --git a/models/class.groupmodel.php b/models/class.groupmodel.php index bafd0a5..afe3dc5 100644 --- a/models/class.groupmodel.php +++ b/models/class.groupmodel.php @@ -477,9 +477,9 @@ public static function removeSort($key) { } } - public function setCurrentUserTopcoderProjectRoles($topcoderProjectRoles = []){ - $this->currentUserTopcoderProjectRoles = $topcoderProjectRoles; - } + // public function setCurrentUserTopcoderProjectRoles($topcoderProjectRoles = []){ + // $this->currentUserTopcoderProjectRoles = $topcoderProjectRoles; + // } /** * Clear the groups cache. @@ -811,7 +811,7 @@ public function countMyGroups($where =[]) { * If false, the user only needs one of the specified permissions. * @return boolean Returns **true** if the user has permission or **false** otherwise. */ - public function checkPermission($userID,$groupID,$categoryID = null, $permissionCategoryID = null, $permissions = null, $fullMatch = true){ + public static function checkPermission($userID,$groupID,$categoryID = null, $permissionCategoryID = null, $permissions = null, $fullMatch = true, $groupIDs = []){ if($userID === Gdn::session()->UserID) { $userPermissions = Gdn::session()->getPermissions(); } else { @@ -820,7 +820,7 @@ public function checkPermission($userID,$groupID,$categoryID = null, $permission // Check access to a category $result = false; - if($this->isMemberOfGroup($userID,$groupID)) { + if(GroupModel::isMemberOfGroup($userID,$groupID)) { if ($permissions == null) { $result = true; } else { @@ -888,14 +888,9 @@ public function accept($groupID, $userID){ * @param $groupID * @return bool */ - public function isMemberOfGroup($userID, $groupID) { - $groups = $this->memberOf($userID); - foreach ($groups as $group) { - if ($group->GroupID == $groupID) { - return true; - } - } - return false; + public static function isMemberOfGroup($userID, $groupID) { + $groups = self::memberOf($userID); + return array_key_exists($groupID, $groups); } /** @@ -1153,17 +1148,23 @@ public function countOfMembers($groupId, $role = null){ * @param $userID * @return array|mixed|null */ - public function memberOf($userID){ + public static function memberOf($userID){ $key = 'UserGroup_'.$userID; $result = Gdn::cache()->get($key); - if ($result === Gdn_Cache::CACHEOP_FAILURE) { - $sql = clone $this->SQL; + if ($result === Gdn_Cache::CACHEOP_FAILURE || $result == false) { + $sql = Gdn::sql(); + $sql = clone $sql; $sql->reset(); $result = $sql->select('ug.Role, ug.GroupID') ->from('UserGroup ug') ->where('UserID', $userID) - ->get()->result(); - Gdn::cache()->store($key, $result); + ->get()->result(DATASET_TYPE_ARRAY); + $map = array(); + foreach ($result as $row) { + $map[$row['GroupID']][] = $row['Role']; + } + + Gdn::cache()->store($key, $map); return $result; } else { return $result; @@ -1172,13 +1173,14 @@ public function memberOf($userID){ /** * Get a group role + * @param $userID + * @param $groupID + * @return mixed */ public function getGroupRoleFor($userID, $groupID) { - $groups = $this->memberOf($userID); - foreach ($groups as $group) { - if ($group->GroupID == $groupID) { - return $group; - } + $groups = self::memberOf($userID); + if($groups && array_key_exists($groupID, $groups)) { + return $groups[$groupID]; } return false; } @@ -1191,8 +1193,7 @@ public function canView($group) { if($group->Privacy == self::PRIVACY_PUBLIC){ return true; } else { - $result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); - $groupRole = val('Role', $result, null); + $groupRole = self::getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); if($groupRole || Gdn::session()->checkPermission(GroupsPlugin::GROUPS_MODERATION_MANAGE_PERMISSION)) { return true; } @@ -1207,8 +1208,8 @@ public function canView($group) { */ public function canFollowGroup($group) { if($group->ChallengeID) { - $result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); - return val('Role', $result, false); + $groupRole = self::getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); + return $groupRole; } return false; @@ -1239,8 +1240,8 @@ public function hasFollowedGroup($group, $userID = null) { */ public function canWatchGroup($group) { if($group->ChallengeID) { - $result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); - return val('Role', $result, false); + $groupRole = self::getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); + return $groupRole; } return false; @@ -1396,8 +1397,7 @@ public function canEdit($group) { if((int)$group->Archived === 1) { return false; } - $result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); - $groupRole = val('Role', $result, null); + $groupRole = self::getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); if($groupRole == GroupModel::ROLE_LEADER || Gdn::session()->UserID == $group->OwnerID || $this->isProjectCopilot() || $this->isProjectManager() || @@ -1439,8 +1439,7 @@ public function canRemoveMember($group) { if((int)$group->Archived === 1) { return false; } - $result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); - $groupRole = val('Role', $result, null); + $groupRole = self::getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); if($groupRole == GroupModel::ROLE_LEADER || Gdn::session()->UserID == $group->OwnerID) { return true; @@ -1456,8 +1455,7 @@ public function canChangeGroupRole($group) { if((int)$group->Archived === 1) { return false; } - $result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); - $groupRole = val('Role', $result, null); + $groupRole = self::getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); if($groupRole == GroupModel::ROLE_LEADER || Gdn::session()->UserID == $group->OwnerID) { return true; @@ -1476,14 +1474,12 @@ public function canLeave($group) { if(isset($group->ChallengeID)) { return false; } - - $result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); - $groupRole = val('Role', $result, null); if(Gdn::session()->UserID == $group->OwnerID) { return false; } - return $groupRole != null; + $groupRole = self::getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); + return $groupRole != false; } /** @@ -1494,8 +1490,7 @@ public function canManageMembers($group) { if((int)$group->Archived === 1) { return false; } - $result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); - $groupRole = val('Role', $result, null); + $groupRole = self::getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); if($groupRole == GroupModel::ROLE_LEADER || Gdn::session()->UserID == $group->OwnerID) { return true; @@ -1511,8 +1506,7 @@ public function canInviteNewMember($group) { if((int)$group->Archived === 1) { return false; } - $result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); - $groupRole = val('Role', $result, null); + $groupRole = self::getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); if($groupRole === GroupModel::ROLE_LEADER || Gdn::session()->UserID === $group->OwnerID || Gdn::session()->checkPermission(GroupsPlugin::GROUPS_EMAIL_INVITATIONS_PERMISSION)) { @@ -1529,8 +1523,7 @@ public function canAddDiscussion($group) { if((int)$group->Archived === 1) { return false; } - $result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); - $groupRole = val('Role', $result, null); + $groupRole = self::getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); if($groupRole || Gdn::session()->UserID == $group->OwnerID) { return true; } @@ -1545,9 +1538,9 @@ public function canAddAnnouncement($group) { if((int)$group->Archived === 1) { return false; } - $result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); - $groupRole = val('Role', $result, null); - if($groupRole === GroupModel::ROLE_LEADER || Gdn::session()->UserID == $group->OwnerID + + $groupRole = self::getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); + if($groupRole === GroupModel::ROLE_LEADER || Gdn::session()->UserID === $group->OwnerID || $this->isProjectCopilot() || $this->isProjectManager()) { return true; } @@ -1563,8 +1556,7 @@ public function canViewDiscussion($discussion) { if(!$groupID) { return true; } - $result = $this->getGroupRoleFor(Gdn::session()->UserID, $groupID); - $groupRole = val('Role', $result, null); + $groupRole = self::getGroupRoleFor(Gdn::session()->UserID, $groupID); if($groupRole || Gdn::session()->checkPermission(GroupsPlugin::GROUPS_MODERATION_MANAGE_PERMISSION)) { return true; } @@ -1581,8 +1573,8 @@ public function canEditDiscussion($discussion) { if(!$groupID) { return $canEditDiscussion; } - $result = $this->getGroupRoleFor(Gdn::session()->UserID, $groupID); - $groupRole = val('Role', $result, null); + + $groupRole = self::getGroupRoleFor(Gdn::session()->UserID, $groupID); if(($groupRole && $discussion->InsertUserID == Gdn::session()->UserID) || Gdn::session()->checkPermission(GroupsPlugin::GROUPS_MODERATION_MANAGE_PERMISSION)) { @@ -1611,8 +1603,7 @@ public function canDismissDiscussion($discussion) { } $group = $this->getByGroupID($groupID); - $result = $this->getGroupRoleFor(Gdn::session()->UserID, $groupID); - $groupRole = val('Role', $result, null); + $groupRole = self::getGroupRoleFor(Gdn::session()->UserID, $groupID); if($groupRole === GroupModel::ROLE_LEADER || Gdn::session()->UserID === $group->OwnerID || $this->isProjectCopilot() || $this->isProjectManager() || Gdn::session()->checkPermission(GroupsPlugin::GROUPS_MODERATION_MANAGE_PERMISSION)) { @@ -1638,8 +1629,7 @@ public function canAnnounceDiscussion($discussion) { } $group = $this->getByGroupID($groupID); - $result = $this->getGroupRoleFor(Gdn::session()->UserID, $groupID); - $groupRole = val('Role', $result, null); + $groupRole = self::getGroupRoleFor(Gdn::session()->UserID, $groupID); if($groupRole === GroupModel::ROLE_LEADER || Gdn::session()->UserID === $group->OwnerID || $this->isProjectCopilot() || $this->isProjectManager() || @@ -1664,8 +1654,7 @@ public function canAddComment($categoryID, $groupID) { } $group = $this->getByGroupID($groupID); - $result = $this->getGroupRoleFor(Gdn::session()->UserID, $groupID); - $groupRole = val('Role', $result, null); + $groupRole = self::getGroupRoleFor(Gdn::session()->UserID, $groupID); if($groupRole || Gdn::session()->UserID === $group->OwnerID || Gdn::session()->checkPermission(GroupsPlugin::GROUPS_MODERATION_MANAGE_PERMISSION)) { return true; @@ -1689,8 +1678,7 @@ public function canSinkDiscussion($discussion) { } $group = $this->getByGroupID($groupID); - $result = $this->getGroupRoleFor(Gdn::session()->UserID, $groupID); - $groupRole = val('Role', $result, null); + $groupRole = self::getGroupRoleFor(Gdn::session()->UserID, $groupID); if($groupRole === GroupModel::ROLE_LEADER || Gdn::session()->UserID === $group->OwnerID || $this->isProjectCopilot() || $this->isProjectManager() || @@ -1716,8 +1704,7 @@ public function canCloseDiscussion($discussion) { } $group = $this->getByGroupID($groupID); - $result = $this->getGroupRoleFor(Gdn::session()->UserID, $groupID); - $groupRole = val('Role', $result, null); + $groupRole = self::getGroupRoleFor(Gdn::session()->UserID, $groupID); if($groupRole === GroupModel::ROLE_LEADER || Gdn::session()->UserID === $group->OwnerID || $this->isProjectCopilot() || $this->isProjectManager() || @@ -1739,8 +1726,7 @@ public function canMoveDiscussion($discussion) { $groupID = $this->findGroupIDFromDiscussion($discussion); if($groupID) { $group = $this->getByGroupID($groupID); - $result = $this->getGroupRoleFor(Gdn::session()->UserID, $groupID); - $groupRole = val('Role', $result, null); + $groupRole = self::getGroupRoleFor(Gdn::session()->UserID, $groupID); if ($groupRole === GroupModel::ROLE_LEADER || Gdn::session()->UserID === $group->OwnerID || Gdn::session()->checkPermission(GroupsPlugin::GROUPS_MODERATION_MANAGE_PERMISSION)) { return true; diff --git a/views/group/helper_functions.php b/views/group/helper_functions.php index b9b961a..96845d5 100644 --- a/views/group/helper_functions.php +++ b/views/group/helper_functions.php @@ -20,15 +20,9 @@ function allMembersUrl($group, $page = '', $withDomain = true) { } if(!function_exists('getRoleInGroupForCurrentUser')) { function getRoleInGroupForCurrentUser($groupId, $groups = null) { - $sender = Gdn::controller(); if ($groups == null) { - $groups = $sender->data('CurrentUserGroups'); - } - - foreach($groups as $group) { - if($group->GroupID == $groupId) { - return $group->Role; - } + $groupRole = GroupModel::getGroupRoleFor(Gdn::session()->UserID, $groupId); + return $groupRole? $groupRole : null; } return null; } @@ -39,15 +33,14 @@ function getRoleInGroupForCurrentUser($groupId, $groups = null) { * Constructs an options dropdown menu for a group. * * @param object|array|null $group The group to get the dropdown options for. - * @param object|array|null $currentUserGroups * @return DropdownModule A dropdown consisting of discussion options. */ function getGroupOptionsDropdown($group = null) { $dropdown = new DropdownModule('dropdown', '', 'OptionsMenu'); $sender = Gdn::controller(); $groupModel = new GroupModel(); - $currentTopcoderProjectRoles = Gdn::controller()->data('ChallengeCurrentUserProjectRoles'); - $groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles); + // $currentTopcoderProjectRoles = Gdn::controller()->data('ChallengeCurrentUserProjectRoles'); + // $groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles); if ($group == null) { $group = $sender->data('Group'); } @@ -115,8 +108,8 @@ function writeGroupMembersWithPhoto($members) { */ function writeGroupMembersWithDetails($members, $group) { $groupModel = new GroupModel(); - $currentTopcoderProjectRoles = Gdn::controller()->data('ChallengeCurrentUserProjectRoles'); - $groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles); + // $currentTopcoderProjectRoles = Gdn::controller()->data('ChallengeCurrentUserProjectRoles'); + // $groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles); foreach ($members as $member) { $memberObj = (object)$member; diff --git a/views/group/index.php b/views/group/index.php index f2e6bfb..88f1e61 100644 --- a/views/group/index.php +++ b/views/group/index.php @@ -17,7 +17,7 @@ $bannerCssClass = $Group->Banner ? 'HasBanner':'NoBanner'; $groupModel = new GroupModel(); $currentTopcoderProjectRoles = Gdn::controller()->data('ChallengeCurrentUserProjectRoles'); -$groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles); +//$groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles); ?> diff --git a/views/groups/index.php b/views/groups/index.php index 31b6d3b..46698a7 100644 --- a/views/groups/index.php +++ b/views/groups/index.php @@ -1,8 +1,8 @@ data('ChallengeCurrentUserProjectRoles'); -$groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles); +//$currentTopcoderProjectRoles = Gdn::controller()->data('ChallengeCurrentUserProjectRoles'); +//$groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles); $canAddGroup = $groupModel->canAddGroup();