Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/admin_audit/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@
<dependencies>
<nextcloud min-version="14" max-version="14" />
</dependencies>
<background-jobs>
<job>OCA\AdminAudit\BackgroundJobs\Rotate</job>
</background-jobs>
</info>
1 change: 1 addition & 0 deletions apps/admin_audit/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
'OCA\\AdminAudit\\Actions\\UserManagement' => $baseDir . '/../lib/Actions/UserManagement.php',
'OCA\\AdminAudit\\Actions\\Versions' => $baseDir . '/../lib/Actions/Versions.php',
'OCA\\AdminAudit\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
'OCA\\AdminAudit\\BackgroundJobs\\Rotate' => $baseDir . '/../lib/BackgroundJobs/Rotate.php',
);
1 change: 1 addition & 0 deletions apps/admin_audit/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ComposerStaticInitAdminAudit
'OCA\\AdminAudit\\Actions\\UserManagement' => __DIR__ . '/..' . '/../lib/Actions/UserManagement.php',
'OCA\\AdminAudit\\Actions\\Versions' => __DIR__ . '/..' . '/../lib/Actions/Versions.php',
'OCA\\AdminAudit\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
'OCA\\AdminAudit\\BackgroundJobs\\Rotate' => __DIR__ . '/..' . '/../lib/BackgroundJobs/Rotate.php',
);

public static function getInitializer(ClassLoader $loader)
Expand Down
98 changes: 57 additions & 41 deletions apps/admin_audit/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,26 @@

class Application extends App {

/** @var ILogger */
protected $logger;

public function __construct() {
parent::__construct('admin_audit');
$this->initLogger();
}

public function initLogger() {
$c = $this->getContainer()->getServer();
$config = $c->getConfig();

$default = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/audit.log';
$logFile = $config->getAppValue('admin_audit', 'logfile', $default);
if($logFile === null) {
$this->logger = $c->getLogger();
return;
}
$this->logger = $c->getLogFactory()->getCustomLogger($logFile);

}

public function register() {
Expand All @@ -65,26 +83,24 @@ public function register() {
* Register hooks in order to log them
*/
protected function registerHooks() {
$logger = $this->getContainer()->getServer()->getLogger();

$this->userManagementHooks($logger);
$this->groupHooks($logger);
$this->authHooks($logger);
$this->userManagementHooks();
$this->groupHooks();
$this->authHooks();

$this->consoleHooks($logger);
$this->appHooks($logger);
$this->consoleHooks();
$this->appHooks();

$this->sharingHooks($logger);
$this->sharingHooks();

$this->fileHooks($logger);
$this->trashbinHooks($logger);
$this->versionsHooks($logger);
$this->fileHooks();
$this->trashbinHooks();
$this->versionsHooks();

$this->securityHooks($logger);
$this->securityHooks();
}

protected function userManagementHooks(ILogger $logger) {
$userActions = new UserManagement($logger);
protected function userManagementHooks() {
$userActions = new UserManagement($this->logger);

Util::connectHook('OC_User', 'post_createUser', $userActions, 'create');
Util::connectHook('OC_User', 'post_deleteUser', $userActions, 'delete');
Expand All @@ -97,8 +113,8 @@ protected function userManagementHooks(ILogger $logger) {
$userSession->listen('\OC\User', 'postUnassignedUserId', [$userActions, 'unassign']);
}

protected function groupHooks(ILogger $logger) {
$groupActions = new GroupManagement($logger);
protected function groupHooks() {
$groupActions = new GroupManagement($this->logger);

/** @var IGroupManager|Manager $groupManager */
$groupManager = $this->getContainer()->getServer()->getGroupManager();
Expand All @@ -108,8 +124,8 @@ protected function groupHooks(ILogger $logger) {
$groupManager->listen('\OC\Group', 'postCreate', [$groupActions, 'createGroup']);
}

protected function sharingHooks(ILogger $logger) {
$shareActions = new Sharing($logger);
protected function sharingHooks() {
$shareActions = new Sharing($this->logger);

Util::connectHook(Share::class, 'post_shared', $shareActions, 'shared');
Util::connectHook(Share::class, 'post_unshare', $shareActions, 'unshare');
Expand All @@ -119,42 +135,42 @@ protected function sharingHooks(ILogger $logger) {
Util::connectHook(Share::class, 'share_link_access', $shareActions, 'shareAccessed');
}

protected function authHooks(ILogger $logger) {
$authActions = new Auth($logger);
protected function authHooks() {
$authActions = new Auth($this->logger);

Util::connectHook('OC_User', 'pre_login', $authActions, 'loginAttempt');
Util::connectHook('OC_User', 'post_login', $authActions, 'loginSuccessful');
Util::connectHook('OC_User', 'logout', $authActions, 'logout');
}

protected function appHooks(ILogger $logger) {
protected function appHooks() {

$eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
$eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function(ManagerEvent $event) use ($logger) {
$appActions = new AppManagement($logger);
$eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function(ManagerEvent $event) {
$appActions = new AppManagement($this->logger);
$appActions->enableApp($event->getAppID());
});
$eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, function(ManagerEvent $event) use ($logger) {
$appActions = new AppManagement($logger);
$eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, function(ManagerEvent $event) {
$appActions = new AppManagement($this->logger);
$appActions->enableAppForGroups($event->getAppID(), $event->getGroups());
});
$eventDispatcher->addListener(ManagerEvent::EVENT_APP_DISABLE, function(ManagerEvent $event) use ($logger) {
$appActions = new AppManagement($logger);
$eventDispatcher->addListener(ManagerEvent::EVENT_APP_DISABLE, function(ManagerEvent $event) {
$appActions = new AppManagement($this->logger);
$appActions->disableApp($event->getAppID());
});

}

protected function consoleHooks(ILogger $logger) {
protected function consoleHooks() {
$eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
$eventDispatcher->addListener(ConsoleEvent::EVENT_RUN, function(ConsoleEvent $event) use ($logger) {
$appActions = new Console($logger);
$eventDispatcher->addListener(ConsoleEvent::EVENT_RUN, function(ConsoleEvent $event) {
$appActions = new Console($this->logger);
$appActions->runCommand($event->getArguments());
});
}

protected function fileHooks(ILogger $logger) {
$fileActions = new Files($logger);
protected function fileHooks() {
$fileActions = new Files($this->logger);
$eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
$eventDispatcher->addListener(
IPreview::EVENT,
Expand Down Expand Up @@ -215,26 +231,26 @@ function(GenericEvent $event) use ($fileActions) {
);
}

protected function versionsHooks(ILogger $logger) {
$versionsActions = new Versions($logger);
protected function versionsHooks() {
$versionsActions = new Versions($this->logger);
Util::connectHook('\OCP\Versions', 'rollback', $versionsActions, 'rollback');
Util::connectHook('\OCP\Versions', 'delete',$versionsActions, 'delete');
}

protected function trashbinHooks(ILogger $logger) {
$trashActions = new Trashbin($logger);
protected function trashbinHooks() {
$trashActions = new Trashbin($this->logger);
Util::connectHook('\OCP\Trashbin', 'preDelete', $trashActions, 'delete');
Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $trashActions, 'restore');
}

protected function securityHooks(ILogger $logger) {
protected function securityHooks() {
$eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
$eventDispatcher->addListener(IProvider::EVENT_SUCCESS, function(GenericEvent $event) use ($logger) {
$security = new Security($logger);
$eventDispatcher->addListener(IProvider::EVENT_SUCCESS, function(GenericEvent $event) {
$security = new Security($this->logger);
$security->twofactorSuccess($event->getSubject(), $event->getArguments());
});
$eventDispatcher->addListener(IProvider::EVENT_FAILED, function(GenericEvent $event) use ($logger) {
$security = new Security($logger);
$eventDispatcher->addListener(IProvider::EVENT_FAILED, function(GenericEvent $event) {
$security = new Security($this->logger);
$security->twofactorFailed($event->getSubject(), $event->getArguments());
});
}
Expand Down
52 changes: 52 additions & 0 deletions apps/admin_audit/lib/BackgroundJobs/Rotate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @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\AdminAudit\BackgroundJobs;

use OC\BackgroundJob\TimedJob;
use OCP\Log\RotationTrait;

class Rotate extends TimedJob {
use RotationTrait;

public function __construct() {
$this->setInterval(60*60*3);
}

protected function run($argument) {
$config = \OC::$server->getConfig();
$default = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/audit.log';
$this->filePath = $config->getAppValue('admin_audit', 'logfile', $default);

if($this->filePath === '') {
// default log file, nothing to do
return;
}

$this->maxSize = $config->getSystemValue('log_rotate_size', 100 * 1024 * 1024);

if($this->shouldRotateBySize()) {
$this->rotate();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private function init() {
});

$this->server = new Server();
$this->logger = new TestLogger(Log\File::class, $config);
$this->logger = new TestLogger(new Log\File(\OC::$SERVERROOT.'/data/nextcloud.log', '', $config), $config);
$this->plugin = new PluginToTest('unit-test', $this->logger);
$this->plugin->initialize($this->server);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@
'OCP\\Lock\\ILockingProvider' => $baseDir . '/lib/public/Lock/ILockingProvider.php',
'OCP\\Lock\\LockedException' => $baseDir . '/lib/public/Lock/LockedException.php',
'OCP\\Lockdown\\ILockdownManager' => $baseDir . '/lib/public/Lockdown/ILockdownManager.php',
'OCP\\Log\\IFileBased' => $baseDir . '/lib/public/Log/IFileBased.php',
'OCP\\Log\\ILogFactory' => $baseDir . '/lib/public/Log/ILogFactory.php',
'OCP\\Log\\IWriter' => $baseDir . '/lib/public/Log/IWriter.php',
'OCP\\Log\\RotationTrait' => $baseDir . '/lib/public/Log/RotationTrait.php',
'OCP\\Mail\\IAttachment' => $baseDir . '/lib/public/Mail/IAttachment.php',
'OCP\\Mail\\IEMailTemplate' => $baseDir . '/lib/public/Mail/IEMailTemplate.php',
'OCP\\Mail\\IMailer' => $baseDir . '/lib/public/Mail/IMailer.php',
Expand Down Expand Up @@ -750,6 +754,7 @@
'OC\\Log\\Errorlog' => $baseDir . '/lib/private/Log/Errorlog.php',
'OC\\Log\\ExceptionSerializer' => $baseDir . '/lib/private/Log/ExceptionSerializer.php',
'OC\\Log\\File' => $baseDir . '/lib/private/Log/File.php',
'OC\\Log\\LogFactory' => $baseDir . '/lib/private/Log/LogFactory.php',
'OC\\Log\\Rotate' => $baseDir . '/lib/private/Log/Rotate.php',
'OC\\Log\\Syslog' => $baseDir . '/lib/private/Log/Syslog.php',
'OC\\Mail\\Attachment' => $baseDir . '/lib/private/Mail/Attachment.php',
Expand Down
5 changes: 5 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Lock\\ILockingProvider' => __DIR__ . '/../../..' . '/lib/public/Lock/ILockingProvider.php',
'OCP\\Lock\\LockedException' => __DIR__ . '/../../..' . '/lib/public/Lock/LockedException.php',
'OCP\\Lockdown\\ILockdownManager' => __DIR__ . '/../../..' . '/lib/public/Lockdown/ILockdownManager.php',
'OCP\\Log\\IFileBased' => __DIR__ . '/../../..' . '/lib/public/Log/IFileBased.php',
'OCP\\Log\\ILogFactory' => __DIR__ . '/../../..' . '/lib/public/Log/ILogFactory.php',
'OCP\\Log\\IWriter' => __DIR__ . '/../../..' . '/lib/public/Log/IWriter.php',
'OCP\\Log\\RotationTrait' => __DIR__ . '/../../..' . '/lib/public/Log/RotationTrait.php',
'OCP\\Mail\\IAttachment' => __DIR__ . '/../../..' . '/lib/public/Mail/IAttachment.php',
'OCP\\Mail\\IEMailTemplate' => __DIR__ . '/../../..' . '/lib/public/Mail/IEMailTemplate.php',
'OCP\\Mail\\IMailer' => __DIR__ . '/../../..' . '/lib/public/Mail/IMailer.php',
Expand Down Expand Up @@ -780,6 +784,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Log\\Errorlog' => __DIR__ . '/../../..' . '/lib/private/Log/Errorlog.php',
'OC\\Log\\ExceptionSerializer' => __DIR__ . '/../../..' . '/lib/private/Log/ExceptionSerializer.php',
'OC\\Log\\File' => __DIR__ . '/../../..' . '/lib/private/Log/File.php',
'OC\\Log\\LogFactory' => __DIR__ . '/../../..' . '/lib/private/Log/LogFactory.php',
'OC\\Log\\Rotate' => __DIR__ . '/../../..' . '/lib/private/Log/Rotate.php',
'OC\\Log\\Syslog' => __DIR__ . '/../../..' . '/lib/private/Log/Syslog.php',
'OC\\Mail\\Attachment' => __DIR__ . '/../../..' . '/lib/private/Mail/Attachment.php',
Expand Down
46 changes: 12 additions & 34 deletions lib/private/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
use InterfaSys\LogNormalizer\Normalizer;

use OC\Log\ExceptionSerializer;
use OC\Log\File;
use OCP\Log\IFileBased;
use OCP\Log\IWriter;
use OCP\ILogger;
use OCP\Support\CrashReport\IRegistry;
use OCP\Util;
Expand All @@ -54,7 +55,7 @@
*/
class Log implements ILogger {

/** @var string */
/** @var IWriter */
private $logger;

/** @var SystemConfig */
Expand All @@ -70,27 +71,19 @@ class Log implements ILogger {
private $crashReporters;

/**
* @param string $logger The logger that should be used
* @param IWriter $logger The logger that should be used
* @param SystemConfig $config the system config object
* @param Normalizer|null $normalizer
* @param IRegistry|null $registry
*/
public function __construct($logger = null, SystemConfig $config = null, $normalizer = null, IRegistry $registry = null) {
public function __construct(IWriter $logger, SystemConfig $config = null, $normalizer = null, IRegistry $registry = null) {
// FIXME: Add this for backwards compatibility, should be fixed at some point probably
if ($config === null) {
$config = \OC::$server->getSystemConfig();
}

$this->config = $config;

// FIXME: Add this for backwards compatibility, should be fixed at some point probably
if ($logger === null) {
$logType = $this->config->getValue('log_type', 'file');
$this->logger = static::getLogClass($logType);
call_user_func([$this->logger, 'init']);
} else {
$this->logger = $logger;
}
$this->logger = $logger;
if ($normalizer === null) {
$this->normalizer = new Normalizer();
} else {
Expand Down Expand Up @@ -302,7 +295,7 @@ public function logException(\Throwable $exception, array $context = []) {
array_walk($context, [$this->normalizer, 'format']);

if ($level >= $minLevel) {
if ($this->logger !== File::class) {
if (!$this->logger instanceof IFileBased) {
$data = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR);
}
$this->writeLog($app, $data, $level);
Expand All @@ -320,28 +313,13 @@ public function logException(\Throwable $exception, array $context = []) {
* @param int $level
*/
protected function writeLog(string $app, $entry, int $level) {
call_user_func([$this->logger, 'write'], $app, $entry, $level);
$this->logger->write($app, $entry, $level);
}

/**
* @param string $logType
* @return string
* @internal
*/
public static function getLogClass(string $logType): string {
switch (strtolower($logType)) {
case 'errorlog':
return \OC\Log\Errorlog::class;
case 'syslog':
return \OC\Log\Syslog::class;
case 'file':
return \OC\Log\File::class;

// Backwards compatibility for old and fallback for unknown log types
case 'owncloud':
case 'nextcloud':
default:
return \OC\Log\File::class;
public function getLogPath():string {
if($this->logger instanceof IFileBased) {
return $this->logger->getLogFilePath();
}
throw new \RuntimeException('Log implementation has no path');
}
}
Loading