-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/feature/saterday-night-fixes'
- Loading branch information
Showing
10 changed files
with
422 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace OCA\OpenRegister\Db; | ||
|
||
use DateTime; | ||
use JsonSerializable; | ||
use OCP\AppFramework\Db\Entity; | ||
|
||
class AuditTrail extends Entity implements JsonSerializable | ||
{ | ||
protected ?string $uuid = null; | ||
protected ?int $object = null; | ||
protected ?string $action = null; | ||
protected ?array $changed = null; | ||
protected ?string $user = null; | ||
protected ?string $userName = null; | ||
protected ?string $session = null; | ||
protected ?string $request = null; | ||
protected ?string $ipAddress = null; | ||
protected ?DateTime $created = null; | ||
|
||
public function __construct() { | ||
$this->addType(fieldName: 'uuid', type: 'string'); | ||
$this->addType(fieldName: 'object', type: 'integer'); | ||
$this->addType(fieldName: 'action', type: 'string'); | ||
$this->addType(fieldName: 'changed', type: 'json'); | ||
$this->addType(fieldName: 'user', type: 'string'); | ||
$this->addType(fieldName: 'userName', type: 'string'); | ||
$this->addType(fieldName: 'session', type: 'string'); | ||
$this->addType(fieldName: 'request', type: 'string'); | ||
$this->addType(fieldName: 'ipAddress', type: 'string'); | ||
$this->addType(fieldName: 'created', type: 'datetime'); | ||
} | ||
|
||
public function getJsonFields(): array | ||
{ | ||
return array_keys( | ||
array_filter($this->getFieldTypes(), function ($field) { | ||
return $field === 'json'; | ||
}) | ||
); | ||
} | ||
|
||
public function hydrate(array $object): self | ||
{ | ||
$jsonFields = $this->getJsonFields(); | ||
|
||
foreach ($object as $key => $value) { | ||
if (in_array($key, $jsonFields) === true && $value === []) { | ||
$value = null; | ||
} | ||
|
||
$method = 'set'.ucfirst($key); | ||
|
||
try { | ||
$this->$method($value); | ||
} catch (\Exception $exception) { | ||
} | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function jsonSerialize(): array | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'uuid' => $this->uuid, | ||
'object' => $this->object, | ||
'action' => $this->action, | ||
'changed' => $this->changed, | ||
'user' => $this->user, | ||
'userName' => $this->userName, | ||
'session' => $this->session, | ||
'request' => $this->request, | ||
'ipAddress' => $this->ipAddress, | ||
'created' => isset($this->created) ? $this->created->format('c') : null | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace OCA\OpenRegister\Db; | ||
|
||
use OCA\OpenRegister\Db\Log; | ||
use OCP\AppFramework\Db\Entity; | ||
use OCP\AppFramework\Db\QBMapper; | ||
use OCP\DB\QueryBuilder\IQueryBuilder; | ||
use OCP\IDBConnection; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
class AuditTrailMapper extends QBMapper | ||
{ | ||
public function __construct(IDBConnection $db) | ||
{ | ||
parent::__construct($db, 'openregister_audit_trails'); | ||
} | ||
|
||
public function find(int $id): Log | ||
{ | ||
$qb = $this->db->getQueryBuilder(); | ||
|
||
$qb->select('*') | ||
->from('openregister_audit_trails') | ||
->where( | ||
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)) | ||
); | ||
|
||
return $this->findEntity(query: $qb); | ||
} | ||
|
||
public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): array | ||
{ | ||
$qb = $this->db->getQueryBuilder(); | ||
|
||
$qb->select('*') | ||
->from('openregister_audit_trails') | ||
->setMaxResults($limit) | ||
->setFirstResult($offset); | ||
|
||
foreach ($filters as $filter => $value) { | ||
if ($value === 'IS NOT NULL') { | ||
$qb->andWhere($qb->expr()->isNotNull($filter)); | ||
} elseif ($value === 'IS NULL') { | ||
$qb->andWhere($qb->expr()->isNull($filter)); | ||
} else { | ||
$qb->andWhere($qb->expr()->eq($filter, $qb->createNamedParameter($value))); | ||
} | ||
} | ||
|
||
if (!empty($searchConditions)) { | ||
$qb->andWhere('(' . implode(' OR ', $searchConditions) . ')'); | ||
foreach ($searchParams as $param => $value) { | ||
$qb->setParameter($param, $value); | ||
} | ||
} | ||
|
||
return $this->findEntities(query: $qb); | ||
} | ||
|
||
public function createFromArray(array $object): Log | ||
{ | ||
$log = new Log(); | ||
$log->hydrate(object: $object); | ||
|
||
// Set uuid if not provided | ||
if ($log->getUuid() === null) { | ||
$log->setUuid(Uuid::v4()); | ||
} | ||
|
||
return $this->insert(entity: $log); | ||
} | ||
|
||
// We dont need update as we dont change the log | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\OpenRegister\Migration; | ||
|
||
use Closure; | ||
use OCP\DB\ISchemaWrapper; | ||
use OCP\DB\Types; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\SimpleMigrationStep; | ||
|
||
class Version1Date20241020231700 extends SimpleMigrationStep { | ||
|
||
/** | ||
* @param IOutput $output | ||
* @param Closure(): ISchemaWrapper $schemaClosure | ||
* @param array $options | ||
*/ | ||
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { | ||
} | ||
|
||
/** | ||
* @param IOutput $output | ||
* @param Closure(): ISchemaWrapper $schemaClosure | ||
* @param array $options | ||
* @return null|ISchemaWrapper | ||
*/ | ||
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
/** @var ISchemaWrapper $schema */ | ||
$schema = $schemaClosure(); | ||
|
||
// create the openregister_logs table | ||
if (!$schema->hasTable('openregister_audit_trails')) { | ||
$table = $schema->createTable('openregister_audit_trails'); | ||
$table->addColumn('id', Types::INTEGER, ['autoincrement' => true, 'notnull' => true]); | ||
$table->addColumn('uuid', Types::STRING, ['notnull' => false, 'length' => 255]); | ||
$table->addColumn('object', Types::INTEGER, ['notnull' => true]); | ||
$table->addColumn('action', Types::STRING, ['notnull' => true, 'default' => 'update']); | ||
$table->addColumn('changed', Types::JSON, ['notnull' => true]); | ||
$table->addColumn('user', Types::STRING, ['notnull' => true, 'length' => 255]); | ||
$table->addColumn('user_name', Types::STRING, ['notnull' => true, 'length' => 255]); | ||
$table->addColumn('session', Types::STRING, ['notnull' => true, 'length' => 255]); | ||
$table->addColumn('request', Types::STRING, ['notnull' => false, 'length' => 255]); | ||
$table->addColumn('ip_address', Types::STRING, ['notnull' => false, 'length' => 255]); | ||
$table->addColumn('created', Types::DATETIME, ['notnull' => true]); | ||
|
||
$table->setPrimaryKey(['id']); | ||
$table->addIndex(['user'], 'openregister_logs_user_index'); | ||
$table->addIndex(['uuid'], 'openregister_logs_uuid_index'); | ||
} | ||
|
||
///Update the openregister_objects table | ||
$table = $schema->getTable('openregister_objects'); | ||
if (!$table->hasColumn('text_representation')) { | ||
$table->addColumn(name: 'text_representation', typeName: Types::TEXT, options: ['notnull' => false]); | ||
} | ||
|
||
return $schema; | ||
} | ||
|
||
/** | ||
* @param IOutput $output | ||
* @param Closure(): ISchemaWrapper $schemaClosure | ||
* @param array $options | ||
*/ | ||
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { | ||
} | ||
} |
Oops, something went wrong.