Skip to content

Commit dbe4b71

Browse files
committed
Fixed issues-178 with permissions
1 parent d064429 commit dbe4b71

File tree

9 files changed

+205
-280
lines changed

9 files changed

+205
-280
lines changed

addon.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
"settingsPermission": "Garden.Settings.Manage",
1313
"registerPermissions": [
1414
"Groups.Group.Add",
15+
"Groups.Group.Edit",
16+
"Groups.Group.Delete",
1517
"Groups.Moderation.Manage",
1618
"Groups.EmailInvitations.Add",
1719
"Groups.Category.Manage"

class.groups.plugin.php

Lines changed: 62 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class GroupsPlugin extends Gdn_Plugin {
1717
const GROUPS_ROUTE = '/groups';
1818
const GROUP_ROUTE = '/group/';
1919
const GROUPS_GROUP_ADD_PERMISSION = 'Groups.Group.Add';
20+
const GROUPS_GROUP_EDIT_PERMISSION = 'Groups.Group.Edit';
21+
const GROUPS_GROUP_DELETE_PERMISSION = 'Groups.Group.Delete';
2022
const GROUPS_CATEGORY_MANAGE_PERMISSION = 'Groups.Category.Manage';
2123
const GROUPS_MODERATION_MANAGE_PERMISSION = 'Groups.Moderation.Manage';
2224
const GROUPS_EMAIL_INVITATIONS_PERMISSION = 'Groups.EmailInvitations.Add';
@@ -155,25 +157,43 @@ public function onDisable() {
155157
// nothing
156158
}
157159

160+
public function categoryModel_beforeSaveCategory_handler($sender, $args){
161+
162+
}
163+
158164
public function base_render_before($sender) {
159165
$sender->addJsFile('vendors/prettify/prettify.js', 'plugins/Groups');
160166
$sender->addJsFile('dashboard.js', 'plugins/Groups');
161167

162-
if(inSection('CategoryList')) {
163-
$categoryTree = $sender->data('CategoryTree');
164-
$selectedCategory = $sender->data('Category');
165-
if($categoryTree) {
166-
$displayedCategoryTree = $this->groupModel->checkGroupCategoryPermissions($categoryTree);
167-
$sender->setData('CategoryTree', $displayedCategoryTree);
168-
}
168+
$categoryModel = new CategoryModel();
169+
170+
self::log('a list of IDs of categories visible to the current user', ['visibleCategories' => $categoryModel->getVisibleCategories([])]);
169171

170-
if($selectedCategory) {
171-
$displayedCategory = $this->groupModel->checkGroupCategoryPermissions($selectedCategory);
172-
$sender->setData('Category', $displayedCategory);
173-
}
174-
}
175172
}
176173

174+
public function base_groupOptionsDropdown_handler($sender, $args){
175+
$group = $args['Group'];
176+
$currentTopcoderProjectRoles = $sender->Data['ChallengeCurrentUserProjectRoles'];
177+
$groupModel = new GroupModel();
178+
$groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles);
179+
$groupID = $group->GroupID;
180+
$canEdit = $groupModel->canEdit($group) ;
181+
$canDelete = $groupModel->canDelete($group) ;
182+
$canLeave = $groupModel->canLeave($group);
183+
$canInviteMember = $groupModel->canInviteNewMember($group);
184+
$canManageMembers = $groupModel->canManageMembers($group);
185+
$canManageCategories = $groupModel->canManageCategories($group);
186+
$canFollow = $groupModel->canFollowGroup($group);
187+
$canWatch = $groupModel->canWatchGroup($group);
188+
$hasFollowed = $groupModel->hasFollowedGroup($group);
189+
$hasWatched = $groupModel->hasWatchedGroup($group);
190+
191+
// self::log('base_groupOptionsDropdown_handler', ['Group' => $group->GroupID,
192+
// 'currentUserTopcoderProjectRoles' =>$currentTopcoderProjectRoles,
193+
// 'canDelete' => $canDelete, 'canEdit' => $canEdit, 'canLeave' => $canLeave,
194+
// 'canInviteMember' =>$canInviteMember, 'canManageMembers' => $canManageMembers, 'canManageCategories ' =>
195+
// $canManageCategories, 'canFollow' => $canFollow ]);
196+
}
177197

178198
/**
179199
* Load CSS into head for the plugin
@@ -198,82 +218,12 @@ public function settingsController_groups_create($sender) {
198218
$cf->renderAll();
199219
}
200220

201-
private function discussionModelWhere($requestPath, $args){
202-
GroupsPlugin::log('discussionModelWhere:enter', ['Wheres'=> $args['Wheres']]);
203-
$wheres = [];
204-
if(array_key_exists('Wheres', $args)) {
205-
$wheres = &$args['Wheres'];
206-
}
207-
if(strpos($requestPath, 'discussions/mine') === 0) {
208-
// show all my discussions
209-
} else if (strpos($requestPath, 'discussions/bookmarked') === 0) {
210-
// show all bookmarked by default
211-
} else if (strpos($requestPath, 'discussions') === 0) {
212-
$wheres['d.GroupID'] = ['is null'];
213-
} else if (strpos($requestPath, 'categories') === 0) {
214-
//checkPermissions
215-
$groupModel = new GroupModel();
216-
$userGroups = $groupModel->memberOf(Gdn::session()->UserID);
217-
$publicGroupIDs = $groupModel->getAllPublicGroupIDs();
218-
$userGroupsIDs = array();
219-
foreach ($userGroups as $userGroup) {
220-
array_push($userGroupsIDs, $userGroup->GroupID);
221-
}
222-
$showGroupsIDs = array_merge($userGroupsIDs, $publicGroupIDs);
223-
self::log('discussionModelWhere', ['userGroups' => $userGroups, 'publicGroupsIDs'=> $publicGroupIDs, 'showGroupIDs' => $showGroupsIDs]);
224-
$wheres['d.GroupID'] = $showGroupsIDs;
225-
$wheres['Groups'] = 'all';
226-
}
227-
228-
GroupsPlugin::log('discussionModelWhere:exit', ['Updated Wheres'=> $wheres]);
229-
230-
}
231-
232-
public function discussionModel_beforeGet_handler($sender, $args) {
233-
GroupsPlugin::log('discussionModel_beforeGet_handler', []);
234-
$this->discussionModelWhere(Gdn::request()->path(), $args);
235-
}
236-
237-
public function discussionModel_beforeGetCount_handler($sender, $args){
238-
GroupsPlugin::log('discussionModel_beforeGetCount_handler');
239-
$this->discussionModelWhere(Gdn::request()->path(), $args);
240-
}
241-
242-
public function discussionModel_beforeGetAnnouncements_handler($sender, $args){
243-
GroupsPlugin::log('discussionModel_beforeGetAnnouncements_handler');
244-
//FIX: it throws exceptions
245-
// $this->discussionModelWhere(Gdn::request()->path(), $args);
246-
}
247-
248-
/**
249-
* Show categories based on group membership
250-
* @param $sender
251-
* @param $args
252-
*/
253-
public function base_beforeCategoryDropDown_handler($sender, $args) {
254-
self::log('base_beforeCategoryDropDown_hander', ['args' => $args]);
255-
$options = &$args['Options'];
256-
$categoryData = val('CategoryData', $options);
257-
258-
// Grab the category data.
259-
if (!$categoryData) {
260-
$categoryData = CategoryModel::getByPermission(
261-
'Discussions.Add',
262-
null,
263-
val('Filter', $options, ['Archived' => 0]),
264-
val('PermFilter', $options, [])
265-
);
266-
267-
$displayedCategories = $this->groupModel->checkGroupCategoryPermissions($categoryData);
268-
$options['CategoryData'] = $displayedCategories;
269-
return;
270-
}
271-
}
272-
273221
public function discussionController_render_before($sender, $args) {
274222
$Discussion = $sender->data('Discussion');
275223
if($Discussion && $Discussion->GroupID != null) {
276224
$groupModel = new GroupModel();
225+
$currentTopcoderProjectRoles = $sender->Data['ChallengeCurrentUserProjectRoles'];
226+
$groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles);
277227
$Group = $groupModel->getByGroupID($Discussion->GroupID);
278228
if (!$groupModel->canView($Group)) {
279229
throw permissionException();
@@ -300,11 +250,13 @@ public function base_BeforeCommentForm_handler($sender, $args) {
300250
*/
301251
public function base_discussionOptionsDropdown_handler($sender, $args){
302252
$Discussion = $args['Discussion'];
253+
$currentTopcoderProjectRoles = $sender->Data['ChallengeCurrentUserProjectRoles'];
303254
if($Discussion) {
304255
$groupModel = new GroupModel();
256+
$groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles);
257+
$canView = $groupModel->canView($Discussion);
305258
$canEdit = $groupModel->canEditDiscussion($Discussion);
306259
$canDelete = $groupModel->canDeleteDiscussion($Discussion);
307-
308260
$canDismiss = $groupModel->canDismissDiscussion($Discussion);
309261
$canAnnounce = $groupModel->canAnnounceDiscussion($Discussion);
310262
$canSink = $groupModel->canSinkDiscussion($Discussion);
@@ -346,8 +298,9 @@ public function base_discussionOptionsDropdown_handler($sender, $args){
346298
}
347299

348300
self::log('discussionController_discussionOptionsDropdown_handler', ['Discussion' => $Discussion->DiscussionID,
349-
'canDelete' => $canDelete, 'canEdit' => $canEdit, 'canDiscmiss' => $canDismiss,
350-
'canAnnounce' =>$canAnnounce, 'canSink' => $canSink, 'canMove' => $canMove, 'canFetch' => $canRefetch ]);
301+
'currentUserTopcoderProjectRoles' =>$currentTopcoderProjectRoles, 'canView' => $canView,
302+
'canDelete' => $canDelete, 'canEdit' => $canEdit, 'canDismiss' => $canDismiss,
303+
'canAnnounce' =>$canAnnounce, 'canSink' => $canSink, 'canMove' => $canMove, 'canReFetch' => $canRefetch ]);
351304
}
352305
}
353306

@@ -571,9 +524,9 @@ public function base_userAnchor_handler($sender, $args){
571524
if($sender instanceof DiscussionController || $sender instanceof GroupController) {
572525
$user = $args['User'];
573526
$anchorText = &$args['Text'];
574-
$resources = $sender->data('Resources');
575-
$roleResources = $sender->data('RoleResources');
576-
$anchorText = $anchorText . $this->getTopcoderRoles($user, $resources, $roleResources);
527+
$resources = $sender->data('ChallengeResources');
528+
$roleResources = $sender->data('ChallengeRoleResources');
529+
$anchorText = $anchorText . $this->topcoderProjectRolesText($user, $resources, $roleResources);
577530
}
578531
}
579532

@@ -586,16 +539,29 @@ public function base_userPhoto_handler($sender, $args){
586539
if($sender instanceof DiscussionController || $sender instanceof GroupController) {
587540
$user = $args['User'];
588541
$anchorText = &$args['Title'];
589-
$resources = $sender->data('Resources');
590-
$roleResources = $sender->data('RoleResources');
591-
$anchorText = $anchorText . $this->getTopcoderRoles($user, $resources, $roleResources);
542+
$resources = $sender->data('ChallengeResources');
543+
$roleResources = $sender->data('ChallengeRoleResources');
544+
$anchorText = $anchorText . $this->topcoderProjectRolesText($user, $resources, $roleResources);
592545
}
593546
}
594547

595-
private function getTopcoderRoles($user, $resources = null, $roleResources = null) {
548+
private function topcoderProjectRolesText($user, $resources = null, $roleResources = null) {
549+
$roles = $this->getTopcoderProjectRoles($user, $resources, $roleResources);
550+
return count($roles) > 0 ? '(' . implode(', ', $roles) . ')' : '';
551+
552+
}
553+
554+
/**
555+
* Get a list of Topcoder Project Roles for an user
556+
* @param $user object User
557+
* @param array $resources
558+
* @param array $roleResources
559+
* @return array
560+
*/
561+
private function getTopcoderProjectRoles($user, $resources = null, $roleResources = null) {
596562
$topcoderUsername = val('Name', $user, t('Unknown'));
563+
$roles = [];
597564
if (isset($resources) && isset($roleResources)) {
598-
$roles = [];
599565
$allResourcesByMember = array_filter($resources, function ($k) use ($topcoderUsername) {
600566
return $k->memberHandle == $topcoderUsername;
601567
});
@@ -605,11 +571,8 @@ private function getTopcoderRoles($user, $resources = null, $roleResources = nul
605571
});
606572
array_push($roles, reset($roleResource)->name);
607573
}
608-
return count($roles) > 0 ? '(' . implode(', ', $roles) . ')' : '';
609-
610574
}
611-
612-
return '';
575+
return $roles;
613576
}
614577

615578
/**

controllers/api/GroupsApiController.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ public function post_members($id, array $body) {
203203
* @throws NotFoundException if the group or user could not be found.
204204
*/
205205
public function delete_members($id, $userid) {
206-
$this->permission();
207206

208207
$this->idUserIdParamSchema()->setDescription('Remove a member from a group.');
209208
$this->schema([], 'out');
@@ -218,9 +217,9 @@ public function delete_members($id, $userid) {
218217
throw new NotFoundException('User');
219218
}
220219

221-
if(!$this->groupModel->canRemoveMember($group)) {
220+
if(!$this->groupModel->canRemoveMember($group)) {
222221
throw new ClientException('Don\'t have permissions to remove this member from the group.');
223-
}
222+
}
224223

225224
$this->groupModel->removeMember($group->GroupID, $user->UserID);
226225
}

controllers/class.groupcontroller.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ public function follow($GroupID) {
394394
}
395395
$this->setData('Group', $Group);
396396
if ($this->Form->authenticatedPostBack(true)) {
397-
$this->GroupModel->followGroup($Group);
397+
$this->GroupModel->followGroup($Group, Gdn::session()->UserID);
398398
$this->setRedirectTo('group/' . $GroupID);
399399
}
400400
$this->render();
@@ -412,7 +412,7 @@ public function unfollow($GroupID) {
412412
}
413413
$this->setData('Group', $Group);
414414
if ($this->Form->authenticatedPostBack(true)) {
415-
$this->GroupModel->unfollowGroup($Group);
415+
$this->GroupModel->unfollowGroup($Group, Gdn::session()->UserID);
416416
$this->setRedirectTo('group/'.$GroupID);
417417
}
418418
$this->render();
@@ -430,7 +430,7 @@ public function watch($GroupID) {
430430
}
431431
$this->setData('Group', $Group);
432432
if ($this->Form->authenticatedPostBack(true)) {
433-
$this->GroupModel->watchGroup($Group);
433+
$this->GroupModel->watchGroup($Group, Gdn::session()->UserID);
434434
$this->setRedirectTo('group/' . $GroupID);
435435
}
436436
$this->render();
@@ -449,7 +449,7 @@ public function unwatch($GroupID) {
449449
}
450450
$this->setData('Group', $Group);
451451
if ($this->Form->authenticatedPostBack(true)) {
452-
$this->GroupModel->unwatchGroup($Group);
452+
$this->GroupModel->unwatchGroup($Group, Gdn::session()->UserID);
453453
$this->setRedirectTo('group/'.$GroupID);
454454
}
455455
$this->render();
@@ -742,7 +742,7 @@ public function setDiscussionData($Group,$Announce = '0') {
742742
public function category($GroupID) {
743743
$Group = $this->findGroup($GroupID);
744744

745-
if(!$this->GroupModel->canManageCategories()) {
745+
if(!$this->GroupModel->canManageCategories($Group)) {
746746
throw permissionException();
747747
}
748748

@@ -754,6 +754,7 @@ public function category($GroupID) {
754754
$this->Form->addHidden('DisplayAs', 'Discussions');
755755
$this->Form->addHidden('AllowFileUploads',1);
756756
$this->Form->addHidden('UrlCode','');
757+
$this->Form->addHidden('GroupID',$GroupID);
757758
if ($this->Form->authenticatedPostBack(true)) {
758759
$this->Form->setFormValue('UrlCode', $Group->ChallengeID.'-'.$slugify->slugify($this->Form->getValue('Name'), '-'));
759760
$newCategoryID = $this->Form->save();

0 commit comments

Comments
 (0)