Skip to content

Commit

Permalink
update activity message required
Browse files Browse the repository at this point in the history
  • Loading branch information
wellingguzman committed Feb 24, 2018
1 parent 6aab84a commit 5a7eeac
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 52 deletions.
1 change: 1 addition & 0 deletions src/core/Directus/Database/Schema/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class SchemaManager
{
// Tables
const TABLE_ACTIVITY = 'directus_activity';
const TABLE_COLLECTIONS = 'directus_collections';
const TABLE_COLLECTION_PRESETS = 'directus_collection_presets';
const TABLE_FIELDS = 'directus_fields';
Expand Down
33 changes: 33 additions & 0 deletions src/core/Directus/Database/TableGateway/RelationalTableGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use Directus\Database\Query\Builder;
use Directus\Database\RowGateway\BaseRowGateway;
use Directus\Database\Schema\Object\FieldRelationship;
use Directus\Database\Schema\SchemaManager;
use Directus\Database\TableSchema;
use Directus\Exception\ErrorException;
use Directus\Util\ArrayUtils;
use Directus\Util\DateUtils;
use Directus\Util\StringUtils;
Expand Down Expand Up @@ -71,6 +73,37 @@ class RelationalTableGateway extends BaseTableGateway
'nbetween' => ['operator' => 'between', 'not' => true],
];

public function deleteRecord($id, array $params = [])
{
// TODO: Add "item" hook, different from "table" hook
$success = $this->delete([
$this->primaryKeyFieldName => $id
]);

if (!$success) {
throw new ErrorException(
sprintf('Error deleting a record in %s with id %s', $this->table, $id)
);
}

if ($this->table !== SchemaManager::TABLE_ACTIVITY) {
$parentLogEntry = BaseRowGateway::makeRowGatewayFromTableName('id', 'directus_activity', $this->adapter);
$logData = [
'type' => DirectusActivityTableGateway::makeLogTypeFromTableName($this->table),
'action' => DirectusActivityTableGateway::ACTION_DELETE,
'user' => $this->acl->getUserId(),
'datetime' => DateUtils::now(),
'ip' => get_request_ip(),
'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '',
'collection' => $this->table,
'item' => $id,
'message' => ArrayUtils::get($params, 'activity_message')
];
$parentLogEntry->populate($logData, false);
$parentLogEntry->save();
}
}

/**
* @param array $data
* @param array $params
Expand Down
23 changes: 5 additions & 18 deletions src/core/Directus/Services/FilesServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,7 @@ public function delete($id, array $params = [])
$files->delete($file);

// Delete file record
$success = $tableGateway->delete([
$tableGateway->primaryKeyFieldName => $id
]);

if (!$success) {
throw new ErrorException('Error deleting file record: ' . $id);
}

return $success;
return $tableGateway->deleteRecord($id);
}

public function findAll(array $params = [])
Expand All @@ -106,7 +98,7 @@ public function createFolder(array $data, array $params = [])

$foldersTableGateway = $this->createTableGateway($collection);

$newFolder = $foldersTableGateway->updateRecord($data);
$newFolder = $foldersTableGateway->updateRecord($data, $this->getCRUDParams($params));

return $foldersTableGateway->wrapData(
$newFolder->toArray(),
Expand All @@ -125,10 +117,11 @@ public function findFolder($id, array $params = [])

public function updateFolder($id, array $data, array $params = [])
{
$this->enforcePermissions('directus_folders', $data, $params);
$foldersTableGateway = $this->createTableGateway('directus_folders');

$data['id'] = $id;
$group = $foldersTableGateway->updateRecord($data);
$group = $foldersTableGateway->updateRecord($data, $this->getCRUDParams($params));

return $foldersTableGateway->wrapData(
$group->toArray(),
Expand All @@ -155,12 +148,6 @@ public function deleteFolder($id, array $params = [])
'id' => $id
]);

$success = $foldersTableGateway->delete(['id' => $id]);

if (!$success) {
throw new ErrorException('Error deleting the folder: ' . $id);
}

return $success;
return $foldersTableGateway->deleteRecord($id, $this->getCRUDParams($params));
}
}
8 changes: 3 additions & 5 deletions src/core/Directus/Services/GroupsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function create(array $data, array $params = [])
$groupsTableGateway = $this->createTableGateway($this->collection);
// make sure to create new one instead of update
unset($data[$groupsTableGateway->primaryKeyFieldName]);
$newGroup = $groupsTableGateway->updateRecord($data);
$newGroup = $groupsTableGateway->updateRecord($data, $this->getCRUDParams($params));

return $groupsTableGateway->wrapData(
$newGroup->toArray(),
Expand Down Expand Up @@ -75,7 +75,7 @@ public function update($id, array $data, array $params = [])
$groupsTableGateway = $this->getTableGateway();

$data['id'] = $id;
$group = $groupsTableGateway->updateRecord($data);
$group = $groupsTableGateway->updateRecord($data, $this->getCRUDParams($params));

return $groupsTableGateway->wrapData(
$group->toArray(),
Expand Down Expand Up @@ -107,9 +107,7 @@ public function delete($id, array $params = [])

$tableGateway = $this->getTableGateway();

if (!$tableGateway->delete(['id' => $id])) {
throw new ErrorException('Unable to delete group record for: ' . $id);
}
$tableGateway->deleteRecord($id, $this->getCRUDParams($params));

return true;
}
Expand Down
10 changes: 5 additions & 5 deletions src/core/Directus/Services/ItemsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ public function delete($collection, $id, array $params = [])
$success = $tableGateway->update([
$tableGateway->getStatusColumnName() => $tableGateway->getDeletedValue()
], $condition);
} else {
$success = $tableGateway->delete($condition);
}

if (!$success) {
throw new ErrorException('Error deleting item id: ' . $id);
if (!$success) {
throw new ErrorException('Error soft-deleting item id: ' . $id);
}
} else {
$tableGateway->deleteRecord($id, $this->getCRUDParams($params));
}

return true;
Expand Down
10 changes: 3 additions & 7 deletions src/core/Directus/Services/PermissionsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function create(array $data, array $params = [])
$this->validatePayload($this->collection, null, $data, $params);

$tableGateway = $this->getTableGateway();
$newGroup = $tableGateway->updateRecord($data);
$newGroup = $tableGateway->updateRecord($data, $this->getCRUDParams($params));

return $tableGateway->wrapData(
$newGroup->toArray(),
Expand All @@ -55,7 +55,7 @@ public function update($id, array $data, array $params = [])

$tableGateway = $this->getTableGateway();
$data['id'] = $id;
$newGroup = $tableGateway->updateRecord($data);
$newGroup = $tableGateway->updateRecord($data, $this->getCRUDParams($params));

return $tableGateway->wrapData(
$newGroup->toArray(),
Expand All @@ -73,11 +73,7 @@ public function delete($id, array $params = [])
'id' => $id
]);

$success = $tableGateway->delete(['id' => $id]);

if (!$success) {
throw new ErrorException('Unable to delete permission with id: ' . $id);
}
$tableGateway->deleteRecord($id, $this->getCRUDParams($params));

return true;
}
Expand Down
6 changes: 2 additions & 4 deletions src/endpoints/Collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,12 @@ public function update(Request $request, Response $response)
public function delete(Request $request, Response $response)
{
$service = new TablesService($this->container);
$ok = $service->delete(
$service->delete(
$request->getAttribute('name'),
$request->getQueryParams()
);

if ($ok) {
$response = $response->withStatus(204);
}
$response = $response->withStatus(204);

return $this->responseWithData($request, $response, []);
}
Expand Down
6 changes: 2 additions & 4 deletions src/endpoints/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,13 @@ public function delete(Request $request, Response $response)
{
$service = new TablesService($this->container);

$ok = $service->deleteField(
$service->deleteField(
$request->getAttribute('collection'),
$request->getAttribute('field'),
$request->getQueryParams()
);

if ($ok) {
$response = $response->withStatus(204);
}
$response = $response->withStatus(204);

return $this->responseWithData($request, $response, []);
}
Expand Down
15 changes: 6 additions & 9 deletions src/endpoints/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,12 @@ public function update(Request $request, Response $response)
public function delete(Request $request, Response $response)
{
$service = new FilesServices($this->container);
$ok = $service->delete(
$service->delete(
$request->getAttribute('id'),
$request->getQueryParams()
);

if ($ok) {
$response = $response->withStatus(204);
}
$response = $response->withStatus(204);

return $this->responseWithData($request, $response, []);
}
Expand Down Expand Up @@ -191,13 +189,12 @@ public function allFolder(Request $request, Response $response)
public function deleteFolder(Request $request, Response $response)
{
$service = new FilesServices($this->container);
$ok = $service->deleteFolder(
$request->getAttribute('id')
$service->deleteFolder(
$request->getAttribute('id'),
$request->getQueryParams()
);

if ($ok) {
$response = $response->withStatus(204);
}
$response = $response->withStatus(204);

return $this->responseWithData($request, $response, []);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/io/ActivityMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public static function resetDatabase()
{
self::clearData();
truncate_table(static::$db, 'directus_permissions');
truncate_table(static::$db, 'directus_folders');
truncate_table(static::$db, 'directus_activity');
drop_table(static::$db, 'test');
drop_table(static::$db, 'test2');
}
Expand Down Expand Up @@ -89,6 +91,7 @@ public function testWithoutFlag()
$this->doCollections();
$this->doFields();
$this->doFiles();
$this->doFilesFolders();
$this->doGroups();
$this->doItems();
$this->doPermissions();
Expand All @@ -112,6 +115,9 @@ public function testWithFlagOff()
$this->setFlagOff('directus_files');
$this->doFiles();

$this->setFlagOff('directus_folders');
$this->doFilesFolders();

$this->setFlagOff('directus_groups');
$this->doGroups();

Expand Down Expand Up @@ -148,6 +154,10 @@ public function testWithFlagOn()
$this->doFiles(true);
$this->doFiles(false, 'message');

$this->setFlagOn('directus_folders');
$this->doFilesFolders(true);
$this->doFilesFolders(false, 'message');

$this->setFlagOn('directus_groups');
$this->doGroups(true);
$this->doGroups(false, 'message');
Expand Down Expand Up @@ -180,6 +190,16 @@ protected function doFiles($error = false, $message = null)
$this->delete('files/2', $error, $message);
}

protected function doFilesFolders($error = false, $message = null)
{
$data = [
'name' => 'folder'
];
$this->create('files/folders', $data, $error, $message);
$this->update('files/folders/1', ['name' => 'logos'], $error, $message);
$this->delete('files/folders/1', $error, $message);
}

protected function doGroups($error = false, $message = null)
{
$data = ['name' => 'new-group'];
Expand Down

0 comments on commit 5a7eeac

Please sign in to comment.