Skip to content

Commit

Permalink
Merge pull request #90 from TomWor/delete-assets
Browse files Browse the repository at this point in the history
Added delete asset route. Fixes #67
  • Loading branch information
bojidar-bg committed Sep 13, 2017
2 parents a9e8ef3 + 589b4d2 commit 6f7ea84
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/Helpers/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Godot\AssetLibrary\Helpers;

use PDO;

class Utils
{
private $c;
Expand Down Expand Up @@ -87,6 +89,30 @@ public function errorResponseIfNotUserHasLevel($currentStatus, &$response, $user
return false;
}

public function errorResponseIfNotOwner($currentStatus, &$response, $user, $asset_id, $message = 'You are not authorized to do this')
{
if($user === false || $currentStatus) {
return true;
}

$query = $this->c->queries['asset']['get_one'];
$query->bindValue(':id', (int) $asset_id, PDO::PARAM_INT);
$query->execute();

if($query->rowCount() <= 0) {
return $response->withJson(['error' => 'Couldn\'t find asset with id '.$asset_id.'!'], 404);
}

$asset = $query->fetch();

if($asset['author_id'] != $user['user_id']) {
$response = $response->withJson(['error' => $message], 403);
return true;
}

return false;
}

public function errorResponseIfMissingOrNotString($currentStatus, &$response, $object, $property)
{
if ($currentStatus) {
Expand Down
3 changes: 2 additions & 1 deletion src/queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,6 @@

'set_asset_id' => 'UPDATE `as_asset_edits` SET asset_id=:asset_id WHERE edit_id=:edit_id',
'set_status_and_reason' => 'UPDATE `as_asset_edits` SET status=:status, reason=:reason WHERE edit_id=:edit_id',
],
'delete' => 'UPDATE `as_assets` SET searchable=0 WHERE asset_id=:asset_id'
]
];
27 changes: 27 additions & 0 deletions src/routes/asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,30 @@
'url' => 'asset/' . $args['id'],
], 200);
});

/*
* Delete asset from library
*/
$app->post('/asset/{id:[0-9]+}/delete', function ($request, $response, $args) {

$body = $request->getParsedBody();

$error = $this->utils->ensureLoggedIn(false, $response, $body, $user);
$error = $this->utils->errorResponseIfNotOwner($error, $response, $user, $args['id']);

if($error) return $response;

$query = $this->queries['asset_edit']['delete'];
$query->bindValue(':asset_id', (int) $args['id'], PDO::PARAM_INT);
$query->execute();

$error = $this->utils->errorResponseIfQueryBad(false, $response, $query);
if($error) return $response;

return $response->withJson([
'changed' => true,
'url' => 'asset/',
], 200);
});


1 change: 1 addition & 0 deletions src/routes/asset_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ function _add_previews_to_edit($c, $error, &$response, $edit_id, $previews, $ass
], 200);
});


// Get an edit
$get_edit = function ($request, $response, $args) {
$query = $this->queries['asset_edit']['get_one'];
Expand Down
7 changes: 7 additions & 0 deletions templates/edit_asset.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
</div>
</div>

<div class="form-group">
<div class="col-md-4 col-md-push-9 align-right">
<form action="<?php echo raw($basepath) ?>/asset/<?php echo url($data['asset_id']) ?>/delete" method="POST">
<button type="submit" id="delete" class="btn btn-danger" onclick="javascript:if(window.confirm('Do you really want to delete this asset?')){ return true; }else{ return false; }">Delete asset from library</button>
</form>
</div>
</div>
</fieldset>
</form>
<?php include("_footer.phtml") ?>

0 comments on commit 6f7ea84

Please sign in to comment.