Skip to content

Commit

Permalink
last changes
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Fritsche <chris.fritsche@cunio.de>
  • Loading branch information
Chris Fritsche committed Oct 5, 2020
1 parent 629e02d commit ccc57c4
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 4 deletions.
11 changes: 10 additions & 1 deletion js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,14 @@ $(document).ready(function() {
'activity', 'enable_email',
$(this).attr('checked') === 'checked' ? 'yes' : 'no'
);
})
});

$('#activity_system_users_group_list').each(function (index, element) {
OC.Settings.setupGroupsSelect($(element));
$(element).change(function(ev) {
var groups = ev.val || [];
groups = JSON.stringify(groups);
OCP.AppConfig.setValue('activity', $(this).attr('name'), groups);
});
});
});
96 changes: 96 additions & 0 deletions js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,99 @@ $(document).ready(function() {
saveSettings();
});
});

OC.Settings = OC.Settings || {};
OC.Settings = _.extend(OC.Settings, {

_cachedGroups: null,

/**
* Setup selection box for group selection.
*
* Values need to be separated by a pipe "|" character.
* (mostly because a comma is more likely to be used
* for groups)
*
* @param $elements jQuery element (hidden input) to setup select2 on
* @param {Array} [extraOptions] extra options hash to pass to select2
* @param {Array} [options] extra options
* @param {Array} [options.excludeAdmins=false] flag whether to exclude admin groups
*/
setupGroupsSelect: function($elements, extraOptions, options) {
var self = this;
options = options || {};
if ($elements.length > 0) {
// Let's load the data and THEN init our select
$.ajax({
url: OC.linkToOCS('cloud/groups', 2) + 'details',
dataType: 'json',
success: function(data) {
var results = [];

if (data.ocs.data.groups && data.ocs.data.groups.length > 0) {

data.ocs.data.groups.forEach(function(group) {
if (!options.excludeAdmins || group.id !== 'admin') {
results.push({ id: group.id, displayname: group.displayname });
}
})

// note: settings are saved through a "change" event registered
// on all input fields
$elements.select2(_.extend({
placeholder: t('core', 'Groups'),
allowClear: true,
multiple: true,
toggleSelect: true,
separator: '|',
data: { results: results, text: 'displayname' },
initSelection: function(element, callback) {
var groups = $(element).val();
var selection;
if (groups && results.length > 0) {
selection = _.map(_.filter((groups || []).split('|').sort(), function(groupId) {
return results.find(function(group) {
return group.id === groupId
}) !== undefined
}), function(groupId) {
return {
id: groupId,
displayname: results.find(function(group) {
return group.id === groupId
}).displayname
}
})
} else if (groups) {
selection = _.map((groups || []).split('|').sort(), function(groupId) {
return {
id: groupId,
displayname: groupId
};
});
}
callback(selection);
},
formatResult: function(element) {
return escapeHTML(element.displayname);
},
formatSelection: function(element) {
return escapeHTML(element.displayname);
},
escapeMarkup: function(m) {
// prevent double markup escape
return m;
}
}, extraOptions || {}));
} else {
OC.Notification.show(t('settings', 'Group list is empty'), { type: 'error' });
console.log(data);
}
},
error: function(data) {
OC.Notification.show(t('settings', 'Unable to retrieve the group list'), { type: 'error' });
console.log(data);
}
});
}
}
});
92 changes: 89 additions & 3 deletions lib/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@
use OCP\Activity\IFilter;
use OCP\Activity\IManager;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserManager;

/**
* @brief Class for managing the data in the activities
Expand All @@ -44,13 +48,34 @@ class Data {
/** @var IDBConnection */
protected $connection;

/** @var IConfig */
protected $config;

/** @var IGroupManager */
protected $groupManager;

/** @var IUserManager */
protected $userManager;

/**
* @param IManager $activityManager
* @param IDBConnection $connection
* @param IConfig $config
* @param IGroupManager $groupManager
* @param IUserManager $userManager
*/
public function __construct(IManager $activityManager, IDBConnection $connection) {
public function __construct(
IManager $activityManager,
IDBConnection $connection,
IConfig $config,
IGroupManager $groupManager,
IUserManager $userManager
) {
$this->activityManager = $activityManager;
$this->connection = $connection;
$this->config = $config;
$this->groupManager = $groupManager;
$this->userManager = $userManager;
}

/**
Expand Down Expand Up @@ -113,7 +138,7 @@ public function send(IEvent $event): int {
*/
public function storeMail(IEvent $event, int $latestSendTime): bool {
$affectedUser = $event->getAffectedUser();
if ($affectedUser === '' || $affectedUser === null) {
if ($affectedUser === '' || $affectedUser === null || $this->isSystemUser($event->getAuthor())) {
return false;
}

Expand Down Expand Up @@ -223,6 +248,16 @@ public function get(GroupHelper $groupHelper, UserSettings $userSettings, $user,
}
}

// if the user is not a system user, hide system users activities
if (!$this->isSystemUser($user)){
$systemUsers = $this->getSystemUsers();
if(!empty($systemUsers)){
$query->andWhere($query->expr()->notIn('user', $query->createNamedParameter(
$systemUsers, IQueryBuilder::PARAM_STR_ARRAY
)));
}
}

/**
* Order and specify the offset
*/
Expand Down Expand Up @@ -457,4 +492,55 @@ public function getActivitySince(string $user, int $since, bool $byOthers) {

return $query->execute()->fetch();
}

/**
* @param mixed $user
* @return bool
*/
protected function isSystemUser($user)
{
if(!($user instanceof IUser)){
$user = $this->userManager->get($user);
}
foreach ($this->getSystemUserGroups() as $group){
$group = $this->groupManager->get($group);
if (!($group instanceof IGroup)) {
continue;
}
if($group->inGroup($user)){
return true;
}
}

return false;
}

/**
* @return mixed
*/
protected function getSystemUserGroups()
{
$systemUsersGroupList = $this->config->getAppValue('activity', 'activity_system_users_group_list', '');
return json_decode($systemUsersGroupList);
}

/**
* @return array
*/
protected function getSystemUsers()
{
$users = [];
foreach ($this->getSystemUserGroups() as $group) {
$group = $this->groupManager->get($group);
if (!($group instanceof IGroup)) {
continue;
}
$groupUsers = $group->getUsers();
foreach ($groupUsers as $user){
array_push($users, $user->getUID());
}
}

return $users;
}
}
9 changes: 9 additions & 0 deletions templates/settings/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
<input id="activity_email_enabled" name="activity_email_enabled" type="checkbox" class="checkbox"
value="1" <?php if ($_['email_enabled']) { print_unescaped('checked="checked"'); } ?> />
<label for="activity_email_enabled"><?php p($l->t('Enable notification emails')); ?></label>
<br />
<br />
<br />

<p class="settings-hint indent">
<?php p($l->t('Define groups, such as system users groups, whose activities will not be displayed (except for themselves).')); ?>
<br />
<input name="activity_system_users_group_list" id="activity_system_users_group_list" value="<?php p($_['systemGroupList']) ?>" style="width: 400px" class="noJSAutoUpdate"/>
</p>

</div>

Expand Down

0 comments on commit ccc57c4

Please sign in to comment.