Skip to content

Commit

Permalink
Initial implementation of SqlAssigner (CFM-379
Browse files Browse the repository at this point in the history
  • Loading branch information
Benn Oshrin committed Mar 6, 2024
1 parent fbeaad0 commit 7f137c2
Show file tree
Hide file tree
Showing 12 changed files with 352 additions and 31 deletions.
1 change: 0 additions & 1 deletion app/availableplugins/ApiConnector/config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
// of Cake routes, so either can be specified here.

// ApiSource API routes
// We place these under /v2 since the Registry v4 plugin essentially implemented v1

$routes->scope('/api/apisource', function (RouteBuilder $builder) {
// Register scoped middleware for in scopes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class SqlProvisionersTable extends Table {
'source' => 'People',
'source_table' => 'people',
'related' => [
// XXX partial reversion of CFM-363
'AdHocAttributes',
'Addresses',
'EmailAddresses',
Expand Down
18 changes: 18 additions & 0 deletions app/plugins/CoreAssigner/resources/locales/en_US/core_assigner.po
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
msgid "controller.FormatAssigners"
msgstr "{0,plural,=1{Format Assigner} other{Format Assigners}}"

msgid "controller.SqlAssigners"
msgstr "{0,plural,=1{SQL Assigner} other{SQL Assigners}}"

msgid "enumeration.CollisionModeEnum.R"
msgstr "Random"

Expand All @@ -43,6 +46,12 @@ msgstr "AlphaNumeric and Dot, Dash, Underscore, Apostrophe"
msgid "enumeration.PermittedCharactersEnum.AL"
msgstr "Any"

msgid "error.SqlAssigners.failed"
msgstr "Could not map key Identifier to target Identifier"

msgid "error.SqlAssigners.key.none"
msgstr "Could not find key Identifier in Person record"

msgid "field.FormatAssigners.collision_mode"
msgstr "Collision Mode"

Expand Down Expand Up @@ -72,3 +81,12 @@ msgstr "Permitted Characters"

msgid "field.FormatAssigners.permitted_characters.desc"
msgstr "When substituting parameters in a format, only permit these characters to be used"

msgid "field.SqlAssigners.source_table"
msgstr "Source Table Name"

msgid "field.SqlAssigners.type_id"
msgstr "Key Identifier Type"

msgid "field.SqlAssigners.type_id.desc"
msgstr "Type of existing Identifier used to query the Source Table"
40 changes: 40 additions & 0 deletions app/plugins/CoreAssigner/src/Controller/SqlAssignersController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* COmanage Registry SQL Assigners Controller
*
* Portions licensed to the University Corporation for Advanced Internet
* Development, Inc. ("UCAID") under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
*
* UCAID licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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-plugins
* @since COmanage Registry v5.0.0
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*/

declare(strict_types=1);

namespace CoreAssigner\Controller;

use App\Controller\StandardPluginController;

class SqlAssignersController extends StandardPluginController {
public $paginate = [
'order' => [
'SqlAssigners.server_id' => 'asc'
]
];
}
49 changes: 49 additions & 0 deletions app/plugins/CoreAssigner/src/Model/Entity/SqlAssigner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* COmanage Registry SQL Assigner Entity
*
* Portions licensed to the University Corporation for Advanced Internet
* Development, Inc. ("UCAID") under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
*
* UCAID licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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-plugins
* @since COmanage Registry v5.0.0
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*/

declare(strict_types=1);

namespace CoreAssigner\Model\Entity;

use Cake\ORM\Entity;

class SqlAssigner extends Entity {
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* @var array<string, bool>
*/
protected $_accessible = [
'*' => true,
'id' => false,
'slug' => false,
];
}
177 changes: 177 additions & 0 deletions app/plugins/CoreAssigner/src/Model/Table/SqlAssignersTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php
/**
* COmanage Registry SQL Assigners Table
*
* Portions licensed to the University Corporation for Advanced Internet
* Development, Inc. ("UCAID") under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
*
* UCAID licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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-plugins
* @since COmanage Registry v5.0.0
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*/

declare(strict_types=1);

namespace CoreAssigner\Model\Table;

use Cake\Datasource\ConnectionManager;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Utility\Hash;
use Cake\Validation\Validator;

class SqlAssignersTable extends Table {
use \App\Lib\Traits\AutoViewVarsTrait;
use \App\Lib\Traits\CoLinkTrait;
use \App\Lib\Traits\PermissionsTrait;
use \App\Lib\Traits\PrimaryLinkTrait;
use \App\Lib\Traits\TableMetaTrait;
use \App\Lib\Traits\ValidationTrait;

/**
* Perform Cake Model initialization.
*
* @since COmanage Registry v5.0.0
* @param array $config Configuration options passed to constructor
*/

public function initialize(array $config): void {
parent::initialize($config);

$this->addBehavior('Changelog');
$this->addBehavior('Log');
// Timestamp behavior handles created/modified updates
$this->addBehavior('Timestamp');

$this->setTableType(\App\Lib\Enum\TableTypeEnum::Configuration);

// Define associations
$this->belongsTo('IdentifierAssignments');
$this->belongsTo('Servers');
$this->belongsTo('Types');

$this->setDisplayField('source_table');

$this->setPrimaryLink('identifier_assignment_id');
$this->setRequiresCO(true);

$this->setAutoViewVars([
'servers' => [
'type' => 'select',
'model' => 'Servers',
'where' => ['plugin' => 'CoreServer.SqlServers']
],
'types' => [
'type' => 'type',
'attribute' => 'Identifiers.type'
]
]);

$this->setPermissions([
// Actions that operate over an entity (ie: require an $id)
'entity' => [
'delete' => false, // Delete the pluggable object instead
'edit' => ['platformAdmin', 'coAdmin'],
'view' => ['platformAdmin', 'coAdmin']
],
// Actions that operate over a table (ie: do not require an $id)
'table' => [
'add' => false, // This is added by the parent model
'index' => ['platformAdmin', 'coAdmin']
]
]);
}

/**
* Assign an identifier.
*
* @since COmanage Registry v5.0.0
* @param IdentifierAssignment $ia Identifier Assignment describing the requested configuration
* @param object $entity The entity (Person, Group, Department) to assign an Identifier for
* @return string The newly proposed Identifier
* @throws InvalidArgumentException
* @throws RuntimeException
*/

public function assign($ia, $entity): string {
// Find the key identifier type in the $entity data
$keyIdentifier = Hash::extract($entity->identifiers, '{n}[type_id='.$ia->sql_assigner->type_id.']');

if(empty($keyIdentifier)) {
throw new \InvalidArgumentException(__d('core_assigner', 'error.SqlAssigners.key.none'));
}

$SqlServer = TableRegistry::getTableLocator()->get('CoreServer.SqlServers');

$SqlServer->connect($ia->sql_assigner->server_id, 'sqlassigner');

$options = [
'table' => $ia->sql_assigner->source_table,
'alias' => 'SourceIdentifiers',
'connection' => ConnectionManager::get('sqlassigner')
];

$SourceTable = TableRegistry::getTableLocator()->get(
alias: 'SourceIdentifiers',
options: $options
);

$identifier = $SourceTable->find()
->where(['key' => $keyIdentifier[0]->identifier])
->first();

if(!empty($identifier->identifier)) {
return $identifier->identifier;
}

throw new \InvalidArgumentException(__d('core_assigner', 'error.SqlAssigners.failed'));
}

/**
* Set validation rules.
*
* @since COmanage Registry v5.0.0
* @param Validator $validator Validator
* @return Validator Validator
*/

public function validationDefault(Validator $validator): Validator {
$schema = $this->getSchema();

$validator->add('identifier_assignment_id', [
'content' => ['rule' => 'isInteger']
]);
$validator->notEmptyString('identifier_assignment_id');

$validator->add('server_id', [
'content' => ['rule' => 'isInteger']
]);
$validator->notEmptyString('server_id');

$this->registerStringValidation($validator, $schema, 'source_table', true);

$validator->add('type_id', [
'content' => ['rule' => 'isInteger']
]);
$validator->notEmptyString('type_id');

return $validator;
}
}
21 changes: 20 additions & 1 deletion app/plugins/CoreAssigner/src/config/plugin.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"types": {
"assigner": [
"FormatAssigners"
"FormatAssigners",
"SqlAssigners"
]
},
"schema": {
Expand Down Expand Up @@ -32,6 +33,24 @@
"format_assigner_sequences_i2": { "needed": false, "columns": [ "format_assigner_id" ] }
},
"changelog": false
},
"sql_assigners": {
"columns": {
"id": {},
"identifier_assignment_id": {},
"server_id": {
"notnull": false,
"comment": "type_id isn't available on the initial row insert by StandardPluggableController::instantiatePlugin"
},
"source_table": { "type": "string", "size": 80 },
"type_id": {
"notnull": false,
"comment": "type_id isn't available on the initial row insert by StandardPluggableController::instantiatePlugin"
}
},
"indexes": {
"sql_assigners": { "columns": [ "identifier_assignment_id" ] }
}
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions app/plugins/CoreAssigner/templates/SqlAssigners/fields.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* COmanage Registry SQL Assigners Fields
*
* Portions licensed to the University Corporation for Advanced Internet
* Development, Inc. ("UCAID") under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
*
* UCAID licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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)
*/

// This view does currently not support read-only, and add is not used
if($vv_action == 'edit') {
print $this->Field->control('server_id');

print $this->Field->control('source_table');

print $this->Field->control('type_id');
}
Loading

0 comments on commit 7f137c2

Please sign in to comment.