Skip to content

Commit e7f881c

Browse files
authored
Merge pull request #221 from topcoder-platform/issues-133
Issues-185
2 parents 0147cae + 3ec715b commit e7f881c

File tree

5 files changed

+323
-15
lines changed

5 files changed

+323
-15
lines changed

config/vanilla/bootstrap.before.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -372,19 +372,24 @@ function checkGroupPermission($groupID,$permission = null, $fullMatch = true) {
372372
}
373373
}
374374

375-
if(!function_exists('updateTopcoderRolePermissions')) {
375+
if(!function_exists('updateRolePermissions')) {
376376

377-
function updateTopcoderRolePermissions($topcoderRoles) {
377+
/**
378+
* Update role permissions
379+
* @param $roleType
380+
* @param $roles
381+
*/
382+
function updateRolePermissions($roleType, $roles) {
378383
$RoleModel = new RoleModel();
379384
$PermissionModel = new PermissionModel();
380-
// Configure default permission for Topcoder roles
381-
$allRoles = $RoleModel->getByType(RoleModel::TYPE_TOPCODER)->resultArray();
385+
// Configure default permission for roles
386+
$allRoles = $RoleModel->getByType($roleType)->resultArray();
382387
foreach ($allRoles as $role) {
383388
$allPermissions = $PermissionModel->getRolePermissions($role['RoleID']);
384389
foreach ($allPermissions as $permission) {
385390
$roleName = $role['Name'];
386-
if (array_key_exists($roleName, $topcoderRoles)) {
387-
$globalRolePermissions = $topcoderRoles[$roleName];
391+
if (array_key_exists($roleName, $roles)) {
392+
$globalRolePermissions = $roles[$roleName];
388393
foreach ($globalRolePermissions as $key => $value) {
389394
$permission[$key] = $globalRolePermissions[$key];
390395
}
@@ -393,5 +398,4 @@ function updateTopcoderRolePermissions($topcoderRoles) {
393398
}
394399
}
395400
}
396-
397401
}

config/vanilla/bootstrap.late.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
'Groups.EmailInvitations.Add',
2323
'Groups.Group.Archive']);
2424

25-
// TODO: need to be sure that all roles and permissions haven't be changed manually in prod/dev
26-
updateTopcoderRolePermissions(RoleModel::TOPCODER_ROLES);
27-
// updateTopcoderRolePermissions(RoleModel::TOPCODER_PROJECT_ROLES);
25+
updateRolePermissions(RoleModel::TYPE_GUEST, RoleModel::VANILLA_GUEST_ROLES);
26+
27+
// TODO: Role permission might be configured manually in the env
28+
// Before uncommenting the next lines:
29+
// Check all roles in the env and update all role permissions in RoleModel
30+
// updateRolePermissions(RoleModel::TYPE_TOPCODER, RoleModel::TOPCODER_ROLES);
31+
// updateTopcoderRolePermissions(RoleModel::TYPE_TOPCODER,RoleModel::TOPCODER_PROJECT_ROLES);
2832
}

config/vanilla/config.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,14 @@
157157
$Configuration['Garden']['EmailTemplate']['Image']='https://www.dropbox.com/s/zddbsvh6f4h308o/e09141aacc790f0f31b80cc0bfd81cb9.png?dl=1';
158158
// Email Logo size
159159
$Configuration['Garden']['EmailTemplate']['ImageMaxWidth']='400';
160-
$Configuration['Garden']['EmailTemplate']['ImageMaxHeight']='300';
160+
$Configuration['Garden']['EmailTemplate']['ImageMaxHeight']='300';
161+
162+
// Profile Configuration
163+
// Hide/Show the options in User Notification Preferences:
164+
// 'Email.WallComment' = 'Notify me when people write on my wall.'
165+
// 'Email.ActivityComment' = 'Notify me when people reply to my wall comments.'
166+
// 'Popup.WallComment' = 'Notify me when people write on my wall.'
167+
// 'Popup.ActivityComment' = 'Notify me when people reply to my wall comments.'
168+
// 'Email.ConversationMessage' = 'Notify me of private messages.'
169+
// 'Popup.ConversationMessage' = 'Notify me of private messages.'
170+
$Configuration['Garden']['Profile']['ShowActivities']=false;
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
<?php
2+
/**
3+
* Hooks for Conversations.
4+
*
5+
* @copyright 2009-2019 Vanilla Forums Inc.
6+
* @license GPL-2.0-only
7+
* @package Conversations
8+
* @since 2.0
9+
*/
10+
11+
use Garden\Container\Container;
12+
use Garden\Container\Reference;
13+
14+
/**
15+
* Handles hooks into Dashboard and Vanilla.
16+
*/
17+
class ConversationsHooks implements Gdn_IPlugin {
18+
/**
19+
* Handle the container init event to register things with the container.
20+
*
21+
* @param Container $dic
22+
*/
23+
public function container_init(Container $dic) {
24+
$dic->rule(\Vanilla\Menu\CounterModel::class)
25+
->addCall('addProvider', [new Reference(ConversationCounterProvider::class)])
26+
;
27+
}
28+
29+
/**
30+
*
31+
*
32+
* @param DbaController $sender
33+
*/
34+
public function dbaController_countJobs_handler($sender) {
35+
$counts = [
36+
'Conversation' => ['CountMessages', 'CountParticipants', 'FirstMessageID', 'LastMessageID', 'DateUpdated', 'UpdateUserID']
37+
];
38+
39+
foreach ($counts as $table => $columns) {
40+
foreach ($columns as $column) {
41+
$name = "Recalculate $table.$column";
42+
$url = "/dba/counts.json?".http_build_query(['table' => $table, 'column' => $column]);
43+
44+
$sender->Data['Jobs'][$name] = $url;
45+
}
46+
}
47+
}
48+
49+
/**
50+
* Remove data when deleting a user.
51+
*
52+
* @since 2.0.0
53+
* @access public
54+
*/
55+
public function userModel_beforeDeleteUser_handler($sender) {
56+
$userID = val('UserID', $sender->EventArguments);
57+
$options = val('Options', $sender->EventArguments, []);
58+
$options = is_array($options) ? $options : [];
59+
60+
$deleteMethod = val('DeleteMethod', $options, 'delete');
61+
if ($deleteMethod == 'delete') {
62+
/** @var Gdn_SQLDriver $sql */
63+
$sql = $sender->SQL;
64+
$sql
65+
->from('UserConversation as uc')
66+
->join('Conversation as c', 'c.ConversationID = uc.ConversationID')
67+
->where(['c.InsertUserID' => $userID])
68+
->orWhere(['c.UpdateUserID' => $userID])
69+
->delete();
70+
$sql
71+
->from('ConversationMessage as cm')
72+
->join('Conversation as c', 'c.ConversationID = cm.ConversationID')
73+
->where(['c.InsertUserID' => $userID])
74+
->orWhere(['c.UpdateUserID' => $userID])
75+
->delete();
76+
77+
$sender->SQL->delete('Conversation', ['InsertUserID' => $userID]);
78+
$sender->SQL->delete('Conversation', ['UpdateUserID' => $userID]);
79+
} elseif ($deleteMethod == 'wipe') {
80+
$sender->SQL->update('ConversationMessage')
81+
->set('Body', t('The user and all related content has been deleted.'))
82+
->set('Format', 'Deleted')
83+
->where('InsertUserID', $userID)
84+
->put();
85+
}
86+
// Remove the user's profile information related to this application
87+
$sender->SQL->update('User')
88+
->set('CountUnreadConversations', 0)
89+
->where('UserID', $userID)
90+
->put();
91+
}
92+
93+
/**
94+
* Add 'Inbox' to profile menu.
95+
*
96+
* @since 2.0.0
97+
* @access public
98+
*/
99+
public function profileController_addProfileTabs_handler($sender) {
100+
if (Gdn::session()->isValid()) {
101+
$inbox = t('Inbox');
102+
$inboxHtml = sprite('SpInbox').' '.$inbox;
103+
$inboxLink = '/messages/all';
104+
105+
if (Gdn::session()->UserID != $sender->User->UserID) {
106+
// Accomodate admin access
107+
if (c('Conversations.Moderation.Allow', false) && Gdn::session()->checkPermission('Conversations.Moderation.Manage')) {
108+
$countUnread = $sender->User->CountUnreadConversations;
109+
$inboxLink .= "?userid={$sender->User->UserID}";
110+
} else {
111+
return;
112+
}
113+
} else {
114+
// Current user
115+
$countUnread = Gdn::session()->User->CountUnreadConversations;
116+
}
117+
118+
if (is_numeric($countUnread) && $countUnread > 0) {
119+
$inboxHtml .= ' <span class="Aside"><span class="Count">'.$countUnread.'</span></span>';
120+
}
121+
$sender->addProfileTab($inbox, $inboxLink, 'Inbox', $inboxHtml);
122+
}
123+
}
124+
125+
/**
126+
* Add "Message" option to profile options.
127+
*/
128+
public function profileController_beforeProfileOptions_handler($sender, $args) {
129+
if (!$sender->EditMode &&
130+
Gdn::session()->UserID != $sender->User->UserID &&
131+
Gdn::session()->checkPermission('Conversations.Conversations.Add')
132+
) {
133+
$sender->EventArguments['MemberOptions'][] = [
134+
'Text' => sprite('SpMessage').' '.t('Message'),
135+
'Url' => '/messages/add/'.rawurlencode($sender->User->Name),
136+
'CssClass' => 'MessageUser'
137+
];
138+
}
139+
}
140+
141+
142+
/**
143+
* Additional options for the Preferences screen.
144+
*
145+
* @since 2.0.0
146+
* @access public
147+
*/
148+
public function profileController_afterPreferencesDefined_handler($sender) {
149+
if (c('Garden.Profile.ShowActivities', true)) {
150+
$sender->Preferences['Notifications']['Email.ConversationMessage'] = t('Notify me of private messages.');
151+
$sender->Preferences['Notifications']['Popup.ConversationMessage'] = t('Notify me of private messages.');
152+
}
153+
}
154+
155+
/**
156+
* Add 'Inbox' to global menu.
157+
*
158+
* @since 2.0.0
159+
* @access public
160+
*/
161+
public function base_render_before($sender) {
162+
// Add the menu options for conversations
163+
if ($sender->Menu && Gdn::session()->isValid()) {
164+
$inbox = t('Inbox');
165+
$countUnreadConversations = val('CountUnreadConversations', Gdn::session()->User);
166+
if (is_numeric($countUnreadConversations) && $countUnreadConversations > 0) {
167+
$inbox .= ' <span class="Alert">'.$countUnreadConversations.'</span>';
168+
}
169+
170+
$sender->Menu->addLink('Conversations', $inbox, '/messages/all', false, ['Standard' => true]);
171+
}
172+
}
173+
174+
/**
175+
* Let us add Messages to the Inbox page.
176+
*/
177+
public function base_afterGetLocationData_handler($sender, $args) {
178+
$args['ControllerData']['Conversations/messages/inbox'] = t('Inbox Page');
179+
}
180+
181+
/**
182+
* Provide default permissions for roles, based on the value in their Type column.
183+
*
184+
* @param PermissionModel $sender Instance of permission model that fired the event
185+
*/
186+
public function permissionModel_defaultPermissions_handler($sender) {
187+
$sender->addDefault(
188+
RoleModel::TYPE_MEMBER,
189+
['Conversations.Conversations.Add' => 1]
190+
);
191+
$sender->addDefault(
192+
RoleModel::TYPE_MODERATOR,
193+
['Conversations.Conversations.Add' => 1]
194+
);
195+
$sender->addDefault(
196+
RoleModel::TYPE_ADMINISTRATOR,
197+
['Conversations.Conversations.Add' => 1]
198+
);
199+
}
200+
201+
/**
202+
* Database & config changes to be done upon enable.
203+
*
204+
* @since 2.0.0
205+
* @access public
206+
*/
207+
public function setup() {
208+
$Database = Gdn::database();
209+
$Config = Gdn::factory(Gdn::AliasConfig);
210+
$Drop = false;
211+
$Validation = new Gdn_Validation(); // This is going to be needed by structure.php to validate permission names
212+
include(PATH_APPLICATIONS.DS.'conversations'.DS.'settings'.DS.'structure.php');
213+
include(PATH_APPLICATIONS.DS.'conversations'.DS.'settings'.DS.'stub.php');
214+
}
215+
}

vanilla/applications/dashboard/models/class.rolemodel.php

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class RoleModel extends Gdn_Model {
6666
'Groups.Category.Manage' => 1,
6767
'Groups.Group.Delete' => 1,
6868
'Groups.Group.Edit' => 1,
69+
'Groups.Group.Archive' => 1,
6970
'Garden.Uploads.Add' => 1,
7071
'Vanilla.Tagging.Add' => 1,
7172
'Conversations.Moderation.Manage' => 1,
@@ -83,17 +84,82 @@ class RoleModel extends Gdn_Model {
8384
'Vanilla.Comments.Edit' => 1,
8485
'Vanilla.Comments.Delete' => 1,
8586
'Plugins.Attachments.Upload.Allow' => 1,
86-
'Groups.Group.Archive' => 1
87+
];
88+
89+
const GUEST_PERMISSIONS = [
90+
'Garden.Email.View' => 0,
91+
'Garden.Settings.Manage' => 0,
92+
'Garden.Settings.View' => 0,
93+
'Garden.SignIn.Allow' => 0,
94+
'Garden.Users.Add' => 0,
95+
'Garden.Users.Edit' => 0,
96+
'Garden.Users.Delete' => 0,
97+
'Garden.Users.Approve' => 0,
98+
'Garden.Activity.Delete' => 0,
99+
'Garden.Activity.View' => 0,
100+
'Garden.Profiles.View' => 0,
101+
'Garden.Profiles.Edit' => 0,
102+
'Garden.Curation.Manage' => 0,
103+
'Garden.Moderation.Manage' => 0,
104+
'Garden.PersonalInfo.View' => 0,
105+
'Garden.AdvancedNotifications.Allow' => 0,
106+
'Garden.Community.Manage' => 0,
107+
'Garden.Tokens.Add' => 0,
108+
'Groups.Group.Add' => 0,
109+
'Groups.Moderation.Manage' => 0,
110+
'Groups.EmailInvitations.Add' => 0,
111+
'Groups.Category.Manage' => 0,
112+
'Groups.Group.Delete' => 0,
113+
'Groups.Group.Edit' => 0,
114+
'Groups.Group.Archive' => 0,
115+
'Garden.Uploads.Add' => 0,
116+
'Vanilla.Tagging.Add' => 0,
117+
'Conversations.Moderation.Manage' => 0,
118+
'Conversations.Conversations.Add'=> 0,
119+
'Vanilla.Approval.Require' => 1, //
120+
'Vanilla.Comments.Me' => 0,
121+
'Vanilla.Discussions.View' => 0,
122+
'Vanilla.Discussions.Add' => 0,
123+
'Vanilla.Discussions.Edit' => 0,
124+
'Vanilla.Discussions.Announce' => 0,
125+
'Vanilla.Discussions.Sink' => 0,
126+
'Vanilla.Discussions.Close' => 0,
127+
'Vanilla.Discussions.Delete' => 0,
128+
'Vanilla.Comments.Add' => 0,
129+
'Vanilla.Comments.Edit' => 0,
130+
'Vanilla.Comments.Delete' => 0,
131+
'Plugins.Attachments.Upload.Allow' => 0
132+
];
87133

134+
const VANILLA_GUEST_ROLES = [
135+
'Guest' => self::GUEST_PERMISSIONS //The default Vanilla role for Guests
88136
];
137+
89138
const TOPCODER_ROLES = [
90139
'administrator' => self::ALL_VANILLA_PERMISSIONS,
91-
'Connect Manager' => [],
140+
'Connect Manager' => [
141+
'Groups.Category.Manage' => 1,
142+
],
92143
'Connect Account Manager' => [],
93144
'Connect Copilot' => [
94145
'Groups.Category.Manage' => 1,
95146
'Groups.Moderation.Manage' => 1,
96-
'Groups.EmailInvitations.Add' => 1
147+
'Groups.EmailInvitations.Add' => 1,
148+
'Groups.Category.Manage' => 1,
149+
'Groups.Moderation.Manage' => 1,
150+
'Groups.EmailInvitations.Add' => 1,
151+
'Garden.Uploads.Add' => 1,
152+
'Plugins.Attachments.Upload.Allow' => 1,
153+
'Vanilla.Discussions.View' => 1,
154+
'Vanilla.Discussions.Add' => 1,
155+
'Vanilla.Discussions.Edit' => 1,
156+
'Vanilla.Discussions.Announce' => 1,
157+
'Vanilla.Discussions.Sink' => 1,
158+
'Vanilla.Discussions.Close' => 0,
159+
'Vanilla.Discussions.Delete' => 1,
160+
'Vanilla.Comments.Add' => 1,
161+
'Vanilla.Comments.Edit' => 0,
162+
'Vanilla.Comments.Delete' => 1,
97163
],
98164
'Connect Admin' => self::ALL_VANILLA_PERMISSIONS,
99165
'Connect Copilot Manager' => [
@@ -119,7 +185,16 @@ class RoleModel extends Gdn_Model {
119185
'copilot' => [
120186
'Groups.Category.Manage' => 1,
121187
'Groups.Moderation.Manage' => 1,
122-
'Groups.EmailInvitations.Add' => 1
188+
'Groups.EmailInvitations.Add' => 1,
189+
'Garden.Uploads.Add' => 1,
190+
'Plugins.Attachments.Upload.Allow' => 1,
191+
'Vanilla.Discussions.View' => 1,
192+
'Vanilla.Discussions.Add' => 1,
193+
'Vanilla.Discussions.Edit' => 1,
194+
'Vanilla.Discussions.Announce' => 1,
195+
'Vanilla.Discussions.Sink' => 0,
196+
'Vanilla.Discussions.Close' => 1,
197+
'Vanilla.Discussions.Delete' => 1,
123198
],
124199
'customer' => [],
125200
'observer'=> [],

0 commit comments

Comments
 (0)