Skip to content

Commit

Permalink
Enable audit by configuration #163
Browse files Browse the repository at this point in the history
- audit of following actions:
- user X created circle Z;
- user X removed circle Z;
- user X change name of circle Z for circle W;
- user X was added to circle U by user Z;
- user X shared file/folder with circle Y;
- user X, that created circle, unshared file/folder with circle Y
- member X accepted invitation to circle Y by user Z;
- member X left circle Y;
- user X change role of member Y in circle Z for W.
- user X was invited to circle U by user Z
- adjust circles to emit signals of sharing to activity
  • Loading branch information
Flávio Gomes da Silva Lisboa committed Aug 16, 2018
1 parent 8a2fdbb commit 8bf3788
Show file tree
Hide file tree
Showing 15 changed files with 621 additions and 36 deletions.
6 changes: 5 additions & 1 deletion l10n/pt_BR.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ OC.L10N.register(
"Members limit:" : "Limite de membros:",
"Default limit to the number of members in a circle." : "Limite de membros em um círculo.",
"Allow linking of groups:" : "Permitir links dos grupos:",
"Allow federated circles:" : "Permitir círculos federados:"
"Allow federated circles:" : "Permitir círculos federados:",
"You shared {file} with circle {circle}" : "Você compartilhou {file} com o círculo {circle}",
"{author} shared {file} with the circle {circle}" : "{author} compartilhou {file} com o círculo {circle}",
"You unshared {file} with the circle {circle}" : "Você descompartilhou {file} com o círculo {circle}",
"{author} unshared {file} with the circle {circle}" : "{author} descompartilhou {file} com o círculo {circle}"
},
"nplurals=2; plural=(n > 1);");
6 changes: 5 additions & 1 deletion l10n/pt_BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@
"Members limit:" : "Limite de membros:",
"Default limit to the number of members in a circle." : "Limite de membros em um círculo.",
"Allow linking of groups:" : "Permitir links dos grupos:",
"Allow federated circles:" : "Permitir círculos federados:"
"Allow federated circles:" : "Permitir círculos federados:",
"You shared {file} with circle {circle}" : "Você compartilhou {file} com o círculo {circle}",
"{author} shared {file} with the circle {circle}" : "{author} compartilhou {file} com o círculo {circle}",
"You unshared {file} with the circle {circle}" : "Você descompartilhou {file} com o círculo {circle}",
"{author} unshared {file} with the circle {circle}" : "{author} descompartilhou {file} com o círculo {circle}"
},"pluralForm" :"nplurals=2; plural=(n > 1);"
}
93 changes: 93 additions & 0 deletions lib/Activity/Consumer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/**
* Circles - Bring cloud-users closer together.
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Flávio Gomes da Silva Lisboa <flavio.lisboa@fgsl.eti.br>
* @copyright 2018
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Circles\Activity;

use OCP\Activity\IConsumer;
use OCP\Activity\IManager;
use OCA\Activity\Data;
use OCA\Activity\UserSettings;
use OCP\L10N\IFactory;
use OCP\Activity\IEvent;

class Consumer implements IConsumer {
/** @var Data */
protected $data;
/** @var IManager */
protected $manager;

/** @var UserSettings */
protected $userSettings;

/** @var IFactory */
protected $l10nFactory;

/**
* Constructor
*
* @param Data $data
* @param IManager $manager
* @param UserSettings $userSettings
* @param IFactory $l10nFactory
*/
public function __construct(Data $data, IManager $manager, UserSettings $userSettings, IFactory $l10nFactory) {
$this->data = $data;
$this->manager = $manager;
$this->userSettings = $userSettings;
$this->l10nFactory = $l10nFactory;
}

/**
* Send an event to the notifications of a user
*
* @param IEvent $event
* @return null
*/
public function receive(IEvent $event) {
$selfAction = $event->getAffectedUser() === $event->getAuthor();
$emailSetting = $this->userSettings->getUserSetting($event->getAffectedUser(), 'email', $event->getType());
$emailSetting = ($emailSetting) ? $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'batchtime') : false;

// User is not the author or wants to see their own actions
$createStream = !$selfAction || $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'self');

// Add activity to stream
try {
$this->data->send($event);
} catch (\Exception $e) {
OC::$server->getLogger()->logException($e);
}
// User is not the author or wants to see their own actions
$createEmail = !$selfAction || $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'selfemail');

// Add activity to mail queue
if ($emailSetting !== false && $createEmail) {
$latestSend = $event->getTimestamp() + $emailSetting;
$this->data->storeMail($event, $latestSend);
}
}
}
30 changes: 26 additions & 4 deletions lib/Activity/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class Provider implements IProvider {

/** @var ProviderSubjectLink */
private $parserLink;

/** @var ProviderSubjectShared */
private $parserShared;

/** @var MiscService */
protected $miscService;
Expand All @@ -66,7 +69,7 @@ class Provider implements IProvider {
public function __construct(
IManager $activityManager, MiscService $miscService, ProviderSubjectCircle $parserCircle,
ProviderSubjectMember $parserMember, ProviderSubjectGroup $parserGroup,
ProviderSubjectLink $parserLink
ProviderSubjectLink $parserLink, ProviderSubjectShared $parserShared
) {
$this->activityManager = $activityManager;
$this->miscService = $miscService;
Expand All @@ -75,6 +78,7 @@ public function __construct(
$this->parserMember = $parserMember;
$this->parserGroup = $parserGroup;
$this->parserLink = $parserLink;
$this->parserShared = $parserShared;
}


Expand All @@ -92,9 +96,11 @@ public function parse($lang, IEvent $event, IEvent $previousEvent = null) {
$this->setIcon($event, $circle);
$this->parseAsMember($event, $circle, $params);
$this->parseAsModerator($event, $circle, $params);

} catch (FakeException $e) {
/** clean exit */
} catch (InvalidArgumentException $e) {
/** sharing is a special case because app is files **/
$this->parseShared($event, $params);
}

return $event;
Expand All @@ -110,7 +116,7 @@ private function initActivityParser(IEvent $event, $params) {
throw new InvalidArgumentException();
}

if (!key_exists('circle', $params)) {
if ($event->getApp() === Application::APP_NAME && !key_exists('circle', $params)) {
throw new InvalidArgumentException();
}
}
Expand Down Expand Up @@ -252,5 +258,21 @@ private function parseLinkAsModerator(IEvent &$event, Circle $circle, $params) {
$this->parserLink->parseLinkRemove($event, $circle, $remote);
}


/**
* @param IEvent $event
* @param array $params
*
* @throws FakeException
*/
private function parseShared(IEvent &$event, $params) {
if ($event->getSubject() === 'shared_circle_self') {
$this->parserShared->parseSubjectSharedWithCircle($event, $params);
return;
}
if ($event->getSubject() === 'unshared_circle_self') {
$this->parserShared->parseSubjectUnsharedWithCircle($event, $params);
return;
}
throw new InvalidArgumentException();
}
}
6 changes: 3 additions & 3 deletions lib/Activity/ProviderParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected function parseCircleEvent(IEvent &$event, Circle $circle, $remote, $ow
* @param string $line
* @param array $data
*/
private function setSubject(IEvent $event, $line, $data) {
protected function setSubject(IEvent $event, $line, $data) {
$this->setParsedSubject($event, $line, $data);
$this->setRichSubject($event, $line, $data);
}
Expand All @@ -110,7 +110,7 @@ private function setSubject(IEvent $event, $line, $data) {
* @param string $line
* @param array $data
*/
private function setRichSubject(IEvent $event, $line, $data) {
protected function setRichSubject(IEvent $event, $line, $data) {
$ak = array_keys($data);
foreach ($ak as $k) {
$subAk = array_keys($data[$k]);
Expand All @@ -130,7 +130,7 @@ private function setRichSubject(IEvent $event, $line, $data) {
* @param string $line
* @param array $data
*/
private function setParsedSubject(IEvent $event, $line, $data) {
protected function setParsedSubject(IEvent $event, $line, $data) {
$ak = array_keys($data);
$replace = [];
foreach ($ak as $k) {
Expand Down
109 changes: 109 additions & 0 deletions lib/Activity/ProviderSubjectShared.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* Circles - Bring cloud-users closer together.
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Flávio Gomes da Silva Lisboa <flavio.lisboa@fgsl.eti.br>
* @copyright 2018
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Circles\Activity;

use OCA\Circles\Exceptions\FakeException;
use OCA\Circles\Model\Circle;
use OCA\Circles\Model\FederatedLink;
use OCA\Circles\Model\Member;
use OCP\Activity\IEvent;
use OCA\Circles\Api\v1\Circles;

class ProviderSubjectShared extends ProviderParser {
/**
* @param IEvent $event
* @param array $params
*
* @throws FakeException
*/
public function parseSubjectSharedWithCircle(IEvent &$event, array $params) {
$this->parseSharedEvent(
$event, $params, null,
$this->l10n->t('You shared {file} with circle {circle}'),
$this->l10n->t('{author} shared {file} with the circle {circle}')
);
}

/**
* @param IEvent $event
* @param array $params
*
* @throws FakeException
*/
public function parseSubjectUnsharedWithCircle(IEvent &$event, array $params) {
$this->parseSharedEvent(
$event, $params, null,
$this->l10n->t('You unshared {file} with the circle {circle}'),
$this->l10n->t('{author} unshared {file} with the circle {circle}')
);
}

/**
* general function to generate Circle event.
*
* @param IEvent $event
* @param array $params
* @param FederatedLink|null $remote
* @param string $ownEvent
* @param string $othersEvent
*/
protected function parseSharedEvent(IEvent &$event, array $params, $remote, $ownEvent, $othersEvent
) {
$circle = Circles::infoCircleByName($params['circle']['name']);
$path = Circles::getViewPath($params['file']['id']);

$data = [
'author' => [
'type' => 'user',
'id' => $params['author']['id'],
'name' => $params['author']['name'],
'_parsed' => $params['author']['name']
],
'circle' => [
'type' => 'circle',
'id' => $circle->getId(),
'name' => $circle->getName(),
'_parsed' => $circle->getName(),
'link' => Circles::generateLink($circle->getUniqueId())
],
'file' => [
'type' => 'file',
'id' => $params['file']['id'],
'name' => $params['file']['name'],
'path' => $path,
'link' => \OC::$server->getURLGenerator ()->linkToRouteAbsolute('files.view.index', array (
'dir' => ($params['file']['type'] !== 'file') ? dirname($path) : $path) )
]
];

if ($this->isViewerTheAuthor($circle, $this->activityManager->getCurrentUserId())) {
$this->setSubject($event, $ownEvent, $data);
return;
}

$this->setSubject($event, $othersEvent, $data);
}
}
32 changes: 32 additions & 0 deletions lib/Api/v1/Circles.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use OCA\Circles\Service\SharingFrameService;
use OCP\AppFramework\QueryException;
use OCP\Util;
use OC\Files\View;

class Circles {

Expand Down Expand Up @@ -529,4 +530,35 @@ public static function getFilesForCircles($circleUniqueIds) {
return $c->query(CirclesService::class)
->getFilesForCircles($circleUniqueIds);
}

/**
* Get a list of objects which are shred with $circleUniqueId.
*
* @since 0.15.0
*
* @param string $circleName
*
* @return Circle|null
* @throws CircleDoesNotExistException
*/
public static function infoCircleByName($circleName) {
$c = self::getContainer();
return $c->query(CirclesService::class)
->infoCircleByName($circleName);
}

/**
* Get path of a file from id.
*
* @since 0.15.0
*
* @param string $id
*
* @return string|null
*/
public static function getViewPath($id) {
$c = self::getContainer();
return $c->query(View::class)
->getPath($id);
}
}
Loading

0 comments on commit 8bf3788

Please sign in to comment.