Skip to content

Commit

Permalink
Setup script should create COmanage CO
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis Igoumenos committed Jun 20, 2022
1 parent c30962d commit 323afd1
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 102 deletions.
9 changes: 9 additions & 0 deletions app/resources/locales/en_US/command.po
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,14 @@ msgstr "Calculate changes but do not apply"
msgid "se.already"
msgstr "Setup appears to have already run"

msgid "se.salt.already"
msgstr "Salt setup appears to have already run"

msgid "se.db.co"
msgstr "Creating COmanage CO"

msgid "se.db.co.done"
msgstr "COmanage CO created - CO Id: {0}"

msgid "se.salt"
msgstr "Generating salt file"
3 changes: 3 additions & 0 deletions app/resources/locales/en_US/default.po
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ msgstr "registry"
msgid "product.comanage"
msgstr "COmanage"

msgid "registry.co.desc"
msgstr "COmanage Registry Internal CO"

# This should match the ISO 639-1 two letter language code for the translation
msgid "registry.meta.lang"
msgstr "en"
Expand Down
153 changes: 69 additions & 84 deletions app/src/Command/SetupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,124 +18,109 @@
* 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;
}

/**
* 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.salt.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;
$io->out(__d('command', 'se.db.co.done', [$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);
}
}
2 changes: 1 addition & 1 deletion app/src/Command/TransmogrifyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,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"]));
}

Expand Down
37 changes: 21 additions & 16 deletions app/src/Model/Table/CosTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Validation\Validator;
use \App\Lib\Enum\TemplateableStatusEnum;
use App\Lib\Enum\TemplateableStatusEnum;

class CosTable extends Table {
use \App\Lib\Traits\AutoViewVarsTrait;
Expand Down Expand Up @@ -158,7 +158,7 @@ public function duplicate($id) {
public function findCOmanageCO(Query $query): Query {
return $query->where(['lower(name)' => 'comanage']);
}

/**
* Callback after model save.
*
Expand All @@ -182,33 +182,38 @@ public function localAfterSave(\Cake\Event\EventInterface $event, \Cake\Datasour
/**
* 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, array $options): string|bool {
// First check if the COmanage CO already exists
$comanage = $this->find()->where(['lower(name)' => 'comanage'])->count();
// We want negative logic since we want to fail if we're editing the COmanage CO
if($entity->isCOmanageCO()) {
if($entity->isCOmanageCO()
&& $comanage > 0) {
return __d('error', 'edit.comanage');
}

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, 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');
}

Expand Down
7 changes: 6 additions & 1 deletion app/src/Model/Table/TypesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,12 @@ public function addDefault(int $coId, string $attribute) {
// We need the appropriate model for $attribute to manipulate the default types
// $table = (eg) NamesTable
$table = TableRegistry::getTableLocator()->get($attr[0]);


// Not every Model has default types
if(!method_exists($table, 'availableTypes')) {
return true;
}

// The current set of types for this model, of the form value => display_name
$current = $table->availableTypes($coId, $attribute);

Expand Down

0 comments on commit 323afd1

Please sign in to comment.