I am currently writing a blog PoC on EOS. I would like the ability to delete a post. Now deleting by the ID alone is simple, I have it implemented as follows:

whisper::post_index posts(_code, _code.value);
//Find the post with the key
auto itr = posts.find(key);
if (itr != posts.end())

Now obviously anyone can then just pass on an ID and delete a post, so I would like to ensure that only the owner of the post can delete their post.

My posts table does store the owner of the post, it is as follows:

 struct [[ eosio::table, eosio::contract("whisper") ]] posts
    uint64_t key; //Primary key (ID)
    name account; //Account that made the post
    std::string headerImage; //Header image of the post
    std::string title; //Title of the post
    std::string excerpt; //Excerpt of the post
    std::string content; //Content of the post

    uint64_t primary_key() const { return key; }

Is there a way to use .find on the table to search for both the primary key and the owner of the post? If not - how would you suggest on ensuring that only the owner can delete the post?+


Okay so all I did was to check that the account of the iterator after the find was equal to the user making the call. So it ended up looking like this:

//Get the posts
whisper::post_index posts(_code, _code.value);
//Find the post with the key
auto iterator = posts.find(key);
//Check that the post could be found
eosio_assert(iterator != posts.end(), "Post could not be found");
//Check that the user owns the post
eosio_assert(iterator->account == user, "User does not own the post.");
if (iterator != posts.end())
    iterator = posts.erase(iterator);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.