| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @author  Russell Michell 2018 <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @package silverstripe-verifiable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace PhpTek\Verifiable\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\Control\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\Control\HTTPRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SilverStripe\Versioned\Versioned; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SilverStripe\ORM\DataObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Accepts incoming requests for data verification e.g. from within the CMS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * or framework's admin area, and sends them on their way. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Will proxy validation requests to the currently configured backend for both | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * {@link SiteTree} and {@link DataObject} subclasses. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class VerificationController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private static $allowed_actions = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         'verify', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Responds to URIs of the following prototype: /verifiable/verify/<model>/<ID> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * by echoing a JSON response for consumption by client-side logic. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Also provides x2 extension points, both of which are passed a copy of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * the contents of the managed record's "Proof" {@link ChainpointProof} "Proof" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * field, an indirect {@link DBField} subclass. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |      * - onBeforeVerify() | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * - onAfterVerify() | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @param  HTTPRequest $request | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * @todo Update "Status" to be one of: "Pending" | "Verified" | "Failed" | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     public function verify(HTTPRequest $request) | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $class = $request->param('ClassName'); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $id = $request->param('ModelID'); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         if (empty($id) || !is_numeric($id) || empty($class)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             return $this->httpError(400, 'Bad request'); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         if (!$record = $this->getVersionedRecord($class, $id)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             return $this->httpError(400, 'Bad request'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         echo json_encode([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             'ID' => "$record->ID", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             'Class' => get_class($record), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             'Status' => $this->verifiableService->getVerificationStatus($record), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         ], JSON_UNESCAPED_UNICODE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * Fetch a record directly from the relevant table, for the given class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * and ID. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param  string     $class A fully-qualified PHP class name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param  int        $id    The RecordID of the desired Versioned record. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @return DataObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @todo Add an instanceof DataObject check to prevent "SiteTree" being passed for example | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     private function getVersionedRecord(string $class, int $id) : DataObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         return Versioned::get_latest_version($class, $id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |  |