Search code examples
phpmysql

What is the best way to wait for an administrator to validate something before committing it?


I'm building a web application where several groups have their own page but if they want to modify it, an administrator has to validate it before. For example, can change to change its logo, post new photo, change their phone number, their name, their location etc... Basically they can edit a value in the database but only if the administrator accepts it. The administrator has to validate every modification because... our customer asked us to.

That's why we have to create a system that could be called "pending queries" management.

At the beginning I thought that keeping the query in the database and executing when an administrator validated it was a good idea, but if we choose this option we can't use PDO to build prepared statements since we have to concatenate string to build our own statement, with obvious security issues.

Then we thought that we should keep PHP code that calls the right methods (that use PDO) in our database and that we will execute with eval() when the administrator validates it. But again, it seems that using eval() is a very bad idea. As says this Rasmus Lerford's quote : "If eval() is the answer, you're almost certainly asking the wrong question".

I thought about using eval because I want to call methods that uses PDO to deal with the database.

So, what is the best way to solve this problem? It seems that there is no safe way to implement it.


Solution

  • Here's one possible approach, with an attempt to keep the things organised to an extent, as the system begins to scale:

    • Create a table called PendingRequests. This will have to have most of the following fields and maybe quite a few more:

      (id, request_type, request_contents, reqeust_made_by, request_made_timestamp, 
           request_approved_by, request_approved_timestamp, ....)
      


    • Request_contents is a broad term and it may not just be confined to one column alone. How you gather the data for this column will depend on the front-end environment you provide to the users (WYSIWYG, etc).

    • Request_approved_by will be NULL when the data is first inserted in the table (i.e. user has made an initial request). This way, you'll know which requests to present in the administration panel. Once an admin approves it, this column will be updated to reflect the id of the admin that approved it and the approved changes could eventually go live.

    • So far, we've only talked about managing the requests. Once that process is established, then the next question would be to determine how to finally map the approved requests to users. As such, it'd actually require a bit of study of the currently proposed system and its workflow. Though, in short, there may be two school of thoughts:

      • Method 1:
        Create a new table each for everything (logo, phone number, name, etc) that is customisable.
        Or
      • Method 2:
        Simply add them as columns in one of your tables (which would essentially be in a 1:1 relationship with the user table, as far as attributes such as logo, name, etc. are concerned).

    • This brings us to Request_type. This is the field that will hold values / flags for the system to determine which field or table (depending on Method 1 or Method 2) the changes will be incident upon - after an admin has approved the changes.

    • No matter what requirement or approach it is to go about database management, PHP and PDO are both flexible enough to help write customisable and secure queries.

    • As an aside, it might be a good idea to maintain a table for history of all the changes / updates made. By now, it should probably be apparent that the number of history tables will once again depend on Method 1 or Method 2.

    Hope that helps.