Skip to content

Commit a21c5f7

Browse files
authored
Merge pull request #24 from topcoder-platform/issues-149
Fixed issues-178 with permissions
2 parents 664b2ff + d8b0b06 commit a21c5f7

File tree

9 files changed

+224
-280
lines changed

9 files changed

+224
-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: 81 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,62 @@ public function onDisable() {
155157
// nothing
156158
}
157159

160+
/**
161+
* Add challenge/Group name in discussion item
162+
* @param $sender
163+
* @param $args
164+
*/
165+
public function discussionsController_beforeDiscussionMetaData_handler($sender, $args){
166+
if($args['Discussion']) {
167+
$discussion = $args['Discussion'];
168+
if ($discussion->GroupID) {
169+
$result = '/group/' . $discussion->GroupID;
170+
$url = url($result, true);
171+
$groupModel = new GroupModel();
172+
$group = $groupModel->getByGroupID($discussion->GroupID);
173+
echo '<div class="Meta Meta-Discussion Group-Info">'.
174+
'<span class="MItem ">'.
175+
'<span class="label">Challenge: </span>'.
176+
'<span class="value">'.anchor($group->Name, $url).'</span>'.
177+
'</span>'.
178+
'</div>';
179+
}
180+
}
181+
}
182+
158183
public function base_render_before($sender) {
159184
$sender->addJsFile('vendors/prettify/prettify.js', 'plugins/Groups');
160185
$sender->addJsFile('dashboard.js', 'plugins/Groups');
161186

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-
}
187+
$categoryModel = new CategoryModel();
188+
189+
self::log('a list of IDs of categories visible to the current user', ['visibleCategories' => $categoryModel->getVisibleCategories([])]);
169190

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

193+
public function base_groupOptionsDropdown_handler($sender, $args){
194+
$group = $args['Group'];
195+
$currentTopcoderProjectRoles = $sender->Data['ChallengeCurrentUserProjectRoles'];
196+
$groupModel = new GroupModel();
197+
$groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles);
198+
$groupID = $group->GroupID;
199+
$canEdit = $groupModel->canEdit($group) ;
200+
$canDelete = $groupModel->canDelete($group) ;
201+
$canLeave = $groupModel->canLeave($group);
202+
$canInviteMember = $groupModel->canInviteNewMember($group);
203+
$canManageMembers = $groupModel->canManageMembers($group);
204+
$canManageCategories = $groupModel->canManageCategories($group);
205+
$canFollow = $groupModel->canFollowGroup($group);
206+
$canWatch = $groupModel->canWatchGroup($group);
207+
$hasFollowed = $groupModel->hasFollowedGroup($group);
208+
$hasWatched = $groupModel->hasWatchedGroup($group);
209+
210+
// self::log('base_groupOptionsDropdown_handler', ['Group' => $group->GroupID,
211+
// 'currentUserTopcoderProjectRoles' =>$currentTopcoderProjectRoles,
212+
// 'canDelete' => $canDelete, 'canEdit' => $canEdit, 'canLeave' => $canLeave,
213+
// 'canInviteMember' =>$canInviteMember, 'canManageMembers' => $canManageMembers, 'canManageCategories ' =>
214+
// $canManageCategories, 'canFollow' => $canFollow ]);
215+
}
177216

178217
/**
179218
* Load CSS into head for the plugin
@@ -198,82 +237,12 @@ public function settingsController_groups_create($sender) {
198237
$cf->renderAll();
199238
}
200239

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-
273240
public function discussionController_render_before($sender, $args) {
274241
$Discussion = $sender->data('Discussion');
275242
if($Discussion && $Discussion->GroupID != null) {
276243
$groupModel = new GroupModel();
244+
$currentTopcoderProjectRoles = $sender->Data['ChallengeCurrentUserProjectRoles'];
245+
$groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles);
277246
$Group = $groupModel->getByGroupID($Discussion->GroupID);
278247
if (!$groupModel->canView($Group)) {
279248
throw permissionException();
@@ -300,11 +269,13 @@ public function base_BeforeCommentForm_handler($sender, $args) {
300269
*/
301270
public function base_discussionOptionsDropdown_handler($sender, $args){
302271
$Discussion = $args['Discussion'];
272+
$currentTopcoderProjectRoles = $sender->Data['ChallengeCurrentUserProjectRoles'];
303273
if($Discussion) {
304274
$groupModel = new GroupModel();
275+
$groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles);
276+
$canView = $groupModel->canView($Discussion);
305277
$canEdit = $groupModel->canEditDiscussion($Discussion);
306278
$canDelete = $groupModel->canDeleteDiscussion($Discussion);
307-
308279
$canDismiss = $groupModel->canDismissDiscussion($Discussion);
309280
$canAnnounce = $groupModel->canAnnounceDiscussion($Discussion);
310281
$canSink = $groupModel->canSinkDiscussion($Discussion);
@@ -346,8 +317,9 @@ public function base_discussionOptionsDropdown_handler($sender, $args){
346317
}
347318

348319
self::log('discussionController_discussionOptionsDropdown_handler', ['Discussion' => $Discussion->DiscussionID,
349-
'canDelete' => $canDelete, 'canEdit' => $canEdit, 'canDiscmiss' => $canDismiss,
350-
'canAnnounce' =>$canAnnounce, 'canSink' => $canSink, 'canMove' => $canMove, 'canFetch' => $canRefetch ]);
320+
'currentUserTopcoderProjectRoles' =>$currentTopcoderProjectRoles, 'canView' => $canView,
321+
'canDelete' => $canDelete, 'canEdit' => $canEdit, 'canDismiss' => $canDismiss,
322+
'canAnnounce' =>$canAnnounce, 'canSink' => $canSink, 'canMove' => $canMove, 'canReFetch' => $canRefetch ]);
351323
}
352324
}
353325

@@ -571,9 +543,9 @@ public function base_userAnchor_handler($sender, $args){
571543
if($sender instanceof DiscussionController || $sender instanceof GroupController) {
572544
$user = $args['User'];
573545
$anchorText = &$args['Text'];
574-
$resources = $sender->data('Resources');
575-
$roleResources = $sender->data('RoleResources');
576-
$anchorText = $anchorText . $this->getTopcoderRoles($user, $resources, $roleResources);
546+
$resources = $sender->data('ChallengeResources');
547+
$roleResources = $sender->data('ChallengeRoleResources');
548+
$anchorText = $anchorText . $this->topcoderProjectRolesText($user, $resources, $roleResources);
577549
}
578550
}
579551

@@ -586,16 +558,29 @@ public function base_userPhoto_handler($sender, $args){
586558
if($sender instanceof DiscussionController || $sender instanceof GroupController) {
587559
$user = $args['User'];
588560
$anchorText = &$args['Title'];
589-
$resources = $sender->data('Resources');
590-
$roleResources = $sender->data('RoleResources');
591-
$anchorText = $anchorText . $this->getTopcoderRoles($user, $resources, $roleResources);
561+
$resources = $sender->data('ChallengeResources');
562+
$roleResources = $sender->data('ChallengeRoleResources');
563+
$anchorText = $anchorText . $this->topcoderProjectRolesText($user, $resources, $roleResources);
592564
}
593565
}
594566

595-
private function getTopcoderRoles($user, $resources = null, $roleResources = null) {
567+
private function topcoderProjectRolesText($user, $resources = null, $roleResources = null) {
568+
$roles = $this->getTopcoderProjectRoles($user, $resources, $roleResources);
569+
return count($roles) > 0 ? '(' . implode(', ', $roles) . ')' : '';
570+
571+
}
572+
573+
/**
574+
* Get a list of Topcoder Project Roles for an user
575+
* @param $user object User
576+
* @param array $resources
577+
* @param array $roleResources
578+
* @return array
579+
*/
580+
private function getTopcoderProjectRoles($user, $resources = null, $roleResources = null) {
596581
$topcoderUsername = val('Name', $user, t('Unknown'));
582+
$roles = [];
597583
if (isset($resources) && isset($roleResources)) {
598-
$roles = [];
599584
$allResourcesByMember = array_filter($resources, function ($k) use ($topcoderUsername) {
600585
return $k->memberHandle == $topcoderUsername;
601586
});
@@ -605,11 +590,8 @@ private function getTopcoderRoles($user, $resources = null, $roleResources = nul
605590
});
606591
array_push($roles, reset($roleResource)->name);
607592
}
608-
return count($roles) > 0 ? '(' . implode(', ', $roles) . ')' : '';
609-
610593
}
611-
612-
return '';
594+
return $roles;
613595
}
614596

615597
/**

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)