| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * BEdita, API-first content management framework | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright 2022 Atlas Srl, Chialab Srl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * This file is part of BEdita: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * it under the terms of the GNU Lesser General Public License as published | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * by the Free Software Foundation, either version 3 of the License, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | namespace BEdita\Placeholders\Controller\Component; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Cake\Controller\Component; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Cake\Http\Exception\ForbiddenException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Placeholders component | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | class PlaceholdersComponent extends Component | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * Default configuration. | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @var array<string, mixed> | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     protected $_defaultConfig = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         'relations' => ['placeholder', 'placeholded'], | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * Block POST|PATCH|DELETE requests on `/objects/{id}/relationships/(placeholder|placeholded)` endpoints. | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function beforeFilter(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $request = $this->getController()->getRequest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         if ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $request->getParam('action') !== 'relationships' || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             !in_array($request->getParam('relationship'), (array)$this->getConfig('relations')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         if (!in_array(mb_strtoupper($request->getMethod()), ['GET', 'HEAD', 'OPTIONS'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             throw new ForbiddenException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 __d( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                     'placeholders', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                     'Relationships of type {0} can only be managed saving an object', | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |                     $request->getParam('relationship') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |  |