diff --git a/app/src/Command/SetupCommand.php b/app/src/Command/SetupCommand.php index 4b00f2911..e8c6d5d14 100644 --- a/app/src/Command/SetupCommand.php +++ b/app/src/Command/SetupCommand.php @@ -18,43 +18,46 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * + * * @link https://www.internet2.edu/comanage COmanage Project * @package registry * @since COmanage Registry v5.0.0 * @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) */ -declare(strict_types = 1); +declare(strict_types=1); namespace App\Command; -use App\Application; +use Cake\ORM\Locator\LocatorAwareTrait; use Cake\Console\Arguments; use Cake\Console\Command; -use Cake\Console\CommandRunner; use Cake\Console\ConsoleIo; use Cake\Console\ConsoleOptionParser; -use Cake\ORM\TableRegistry; use Cake\Utility\Security; -use \App\Lib\Enum\PermissionEnum; +use App\Lib\Enum\PermissionEnum; +use App\Lib\Enum\StatusEnum; + -class SetupCommand extends Command { +class SetupCommand extends Command +{ /** * Register command specific options. * - * @since COmanage Registry v6.0.0 - * @param ConsoleOptionParser $parser Console Option Parser + * @param ConsoleOptionParser $parser Console Option Parser + * * @return ConsoleOptionParser Console Option Parser + * @since COmanage Registry v6.0.0 */ - - public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser { + + public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser + { $parser->addOption('admin-username', [ - 'help' => __d('command', 'opt.admin-username') - ])->addOption('force', [ - 'help' => __d('command', 'opt.force'), - 'boolean' => true - ]); + 'help' => __d('command', 'opt.admin-username'), + ])->addOption('force', [ + 'help' => __d('command', 'opt.force'), + 'boolean' => true, + ]); return $parser; } @@ -62,80 +65,61 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar /** * Execute the Setup Command. * + * @param Arguments $args Command Arguments + * @param ConsoleIo $io Console IO + * * @since COmanage Registry v5.0.0 - * @param Arguments $args Command Arguments - * @param ConsoleIo $io Console IO */ - - public function execute(Arguments $args, ConsoleIo $io) { + + public function execute(Arguments $args, ConsoleIo $io) + { global $argv; - + // Check if the security salt file already exists, and if so abort. - - $securitySaltFile = LOCAL . DS . "Config" . DS . "security.salt"; - - if(file_exists($securitySaltFile)) { - $io->out(__d('command', 'se.already')); - - if(!$args->getOption('force')) { - exit; + + $securitySaltFile = LOCAL . DS . "config" . DS . "security.salt"; + + // XXX For multi host deployments we do not need to generate the salt + // If force is provided we will generate a new salt, otherwise we + // will skip the step + if (!file_exists($securitySaltFile) + || $args->getOption('force')) { + // Set the salt now in case we need it. (Normally this is done in bootstrap.php. + $salt = hash('sha256', Security::randomBytes(64)); + Security::setSalt($salt); + + // Write out the salt file + $io->out(__d('command', 'se.salt')); + + if (file_put_contents($securitySaltFile, $salt) === false) { + $err = error_get_last(); + throw new \RuntimeException($err[message]); } + // We set 444 to prevent accidental changing of the salt, but also so the + // web server user can read it if this script is run by (say) root. + // We assume we're not installed on a shared, semi-public server. + chmod($securitySaltFile, 0444); + } else { + $io->out(__d('command', 'se.already')); } - - // Before we get going, prompt for whatever information we need in case - // the user hits ctrl-c. - /* - $user = $args->getOption('admin-username'); - - while(!$user) { - $user = $io->ask(__('match.cmd.se.admin.user')); - } - */ - // Set the salt now in case we need it. (Normally this is done in bootstrap.php.) - // We'll write it out after we're done with the database updates. - $salt = hash('sha256', Security::randomBytes(64)); - Security::setSalt($salt); - - // Perform database related setup. Start by trying to run the database schema. -/* - // Build the runner with an application and root executable name. (based on bin/cake.php) - $runner = new CommandRunner(new Application(dirname(__DIR__) . DS . '..' . DS . 'config'), 'cake'); - $runner->run([ $argv[0], 'database' ]); - - // Create the initial admin permission - $io->out(__('match.cmd.se.admin')); - - $permissionsTable = TableRegistry::get('Permissions'); - $permission = $permissionsTable->newEntity(); - - $permission->username = $user; - $permission->matchgrid_id = null; - $permission->permission = PermissionEnum::PlatformAdmin; - - if(!$permissionsTable->save($permission)) { - throw new \RuntimeException(__('match.er.save', ['Permissions'])); - } - - // Register the current version for future upgrade purposes - // Read the current release from the VERSION file - $versionFile = CONFIG . "VERSION"; - - $targetVersion = rtrim(file_get_contents($versionFile)); - - $metaTable = TableRegistry::get('Meta'); - $metaTable->setUpgradeVersion($targetVersion, true); - */ - // Write out the salt file - $io->out(__d('command', 'se.salt')); - - if(file_put_contents($securitySaltFile, $salt)===false) { - $err = error_get_last(); - throw new \RuntimeException($err[message]); + + // We need the following: + // - The COmanage CO + // - Register the current version for future upgrade purposes + + // Start with the COmanage CO + + $io->out(__d('command', 'se.db.co')); + + $coTable = $this->getTableLocator()->get("Cos"); + $comanage_co = $coTable->newEmptyEntity(); + $comanage_co->name = __d('command', 'product.comanage'); + $comanage_co->description = __d('command', 'registry.co.desc'); + $comanage_co->status = StatusEnum::Active; + + $co_id = null; + if ($coTable->save($comanage_co)) { + $co_id = $comanage_co->id; } - - // We set 444 to prevent accidental changing of the salt, but also so the - // web server user can read it if this script is run by (say) root. - // We assume we're not installed on a shared, semi-public server. - chmod($securitySaltFile, 0444); } } \ No newline at end of file diff --git a/app/src/Command/TransmogrifyCommand.php b/app/src/Command/TransmogrifyCommand.php index f20eeafe6..e4e3ef3f8 100644 --- a/app/src/Command/TransmogrifyCommand.php +++ b/app/src/Command/TransmogrifyCommand.php @@ -356,7 +356,7 @@ public function execute(Arguments $args, ConsoleIo $io) { $outdb = ConnectionManager::get('default'); $outcfg = $outdb->config(); - if(empty($incfg)) { + if(empty($outcfg)) { throw new \InvalidArgumentException(__d('error', 'db.config', ["default"])); } diff --git a/app/src/Model/Table/CosTable.php b/app/src/Model/Table/CosTable.php index 245024f22..9776c1ab9 100644 --- a/app/src/Model/Table/CosTable.php +++ b/app/src/Model/Table/CosTable.php @@ -175,17 +175,18 @@ public function duplicate($id) { public function findCOmanageCO(Query $query): Query { return $query->where(['lower(name)' => 'comanage']); } - + /** * Application Rule to determine if the current entity is the COmanage CO. * + * @param Entity $entity Entity to be validated + * @param array $options Application rule options + * + * @return string|bool true if the Rule check passes, false otherwise * @since COmanage Registry v5.0.0 - * @param Entity $entity Entity to be validated - * @param array $options Application rule options - * @return boolean true if the Rule check passes, false otherwise */ - public function ruleIsCOmanageCO($entity, $options): bool { + public function ruleIsCOmanageCO(Entity $entity, array $options): string|bool { // We want negative logic since we want to fail if we're editing the COmanage CO if($entity->isCOmanageCO()) { return __d('error', 'edit.comanage'); @@ -193,19 +194,20 @@ public function ruleIsCOmanageCO($entity, $options): bool { return true; } - + /** * Application Rule to determine if the current entity is not Active. * + * @param Entity $entity Entity to be validated + * @param array $options Application rule options + * + * @return bool|string true if the Rule check passes, false otherwise * @since COmanage Registry v5.0.0 - * @param Entity $entity Entity to be validated - * @param array $options Application rule options - * @return boolean true if the Rule check passes, false otherwise */ - public function ruleIsActive($entity, $options): bool { + public function ruleIsActive(Entity $entity, array $options): bool|string { // We want negative logic since we want to fail if the record is Active - if($entity->status == TemplateableStatusEnum::Active) { + if($entity->status === TemplateableStatusEnum::Active) { return __d('error', 'delete.active'); }