| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * BEdita, API-first content management framework | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright 2024 ChannelWeb 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 |  |  | namespace BEdita\ImportTools\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use BEdita\ImportTools\Utility\Project; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Cake\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Cake\Console\Arguments; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Cake\Console\ConsoleIo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * Command to prepare `applications` and `users` to import a project in a new environment. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * Since applications api keys and users passwords will generally differ this command will try to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * sync those values, when possible, in order to enable migration. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * The new project database you want to import must be reachable via an `import` key Datasource configuration. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * This new project will be modified this way: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  *  - new project `applications` must be present as name on the current project DB => current api keys are saved in the new imported project | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  *  - users password hashes are changed (if set) in the new imported project using current users password hashes on records with the same `username` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * After this command is finished the new imported project can be used in the current environment and replace current project/database. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | class ImportProjectCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * Main command execution: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * - applications and users are loaded from current and imported project | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * - applications api keys and users passwords are updated to be used in current environment | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     public function execute(Arguments $args, ConsoleIo $io) | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $io->out('Start'); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $project = new Project($io); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         if ($project->checkDatasourceConfig() === false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             $this->abort(); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         if ($project->reviewApplications() === false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             $this->abort(); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         if ($project->reviewUsers() === false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             $this->abort(); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $io->success('Import project done.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $io->out('End'); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         return static::CODE_SUCCESS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 61 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |  |