Skip to content

Commit

Permalink
Add minimum_length to FormatAssigner (CFM-163)
Browse files Browse the repository at this point in the history
  • Loading branch information
Benn Oshrin committed Apr 16, 2025
1 parent 624446f commit 42b7dd2
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ msgstr "Minimum Collision Value"
msgid "field.FormatAssigners.minimum.desc"
msgstr "The minimum value for randomly generated collision numbers, or the starting value for sequences"

msgid "field.FormatAssigners.minimum_length"
msgstr "Minimum Length"

msgid "field.FormatAssigners.permitted_characters"
msgstr "Permitted Characters"

Expand Down
39 changes: 28 additions & 11 deletions app/plugins/CoreAssigner/src/Model/Table/FormatAssignersTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ public function assign($ia, $entity): string {

if(!in_array($candidate, $tested)
// Also check that we didn't get an empty string
&& trim($candidate) != false) {
&& trim($candidate) != false
// Or that the candidate is too short
&& (empty($ia->format_assigner->minimum_length)
|| ($ia->format_assigner->minimum_length > 0
&& strlen($candidate) >= $ia->format_assigner->minimum_length))) {
// We have a new candidate (ie: one that wasn't generated on a previous loop),
// so let's see if it is already in use.

Expand Down Expand Up @@ -398,13 +402,17 @@ protected function substituteParameters(

switch($format[$i]) {
case 'f':
$base .= sprintf("%.".$width."s",
preg_replace($charregex, '', strtolower($entity->primary_name->family)));
break;
if(!empty($entity->primary_name->family)) {
$base .= sprintf("%.".$width."s",
preg_replace($charregex, '', strtolower($entity->primary_name->family)));
break;
}
case 'F':
$base .= sprintf("%.".$width."s",
preg_replace($charregex, '', $entity->primary_name->family));
break;
if(!empty($entity->primary_name->family)) {
$base .= sprintf("%.".$width."s",
preg_replace($charregex, '', $entity->primary_name->family));
break;
}
case 'g':
$base .= sprintf("%.".$width."s",
preg_replace($charregex, '', strtolower($entity->primary_name->given)));
Expand Down Expand Up @@ -465,12 +473,16 @@ protected function substituteParameters(
}
break;
case 'm':
$base .= sprintf("%.".$width."s",
preg_replace($charregex, '', strtolower( $entity->primary_name->middle)));
if(!empty($entity->primary_name->middle)) {
$base .= sprintf("%.".$width."s",
preg_replace($charregex, '', strtolower($entity->primary_name->middle)));
}
break;
case 'M':
$base .= sprintf("%.".$width."s",
preg_replace($charregex, '', $entity->primary_name->middle));
if(!empty($entity->primary_name->middle)) {
$base .= sprintf("%.".$width."s",
preg_replace($charregex, '', $entity->primary_name->middle));
}
break;
case 'n':
$base .= sprintf("%.".$width."s",
Expand Down Expand Up @@ -524,6 +536,11 @@ public function validationDefault(Validator $validator): Validator {

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

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

$validator->add('minimum', [
'content' => ['rule' => 'isInteger']
]);
Expand Down
1 change: 1 addition & 0 deletions app/plugins/CoreAssigner/src/config/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"id": {},
"identifier_assignment_id": {},
"format": { "type": "string", "size": 256 },
"minimum_length": { "type": "integer" },
"minimum": { "type": "integer" },
"maximum": { "type": "integer" },
"collision_mode": { "type": "string", "size": 2 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ if($vv_action == 'edit') {

foreach([
'permitted_characters',
'minimum_length',
'minimum',
'maximum',
] as $field) {
Expand Down
8 changes: 4 additions & 4 deletions app/plugins/CoreServer/src/Model/Table/HttpServersTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @link https://www.internet2.edu/comanage COmanage Project
* @package registry-plugins
* @since COmanage Registry v5.2.0
* @since COmanage Registry v5.1.0
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*/

Expand Down Expand Up @@ -49,7 +49,7 @@ class HttpServersTable extends Table {
/**
* Perform Cake Model initialization.
*
* @since COmanage Registry v5.2.0
* @since COmanage Registry v5.1.0
* @param array $config Configuration options passed to constructor
*/

Expand Down Expand Up @@ -96,7 +96,7 @@ public function initialize(array $config): void {
/**
* Create an HTTP Client from the HttpServer configuration.
*
* @since COmanage Registry v5.2.0
* @since COmanage Registry v5.1.0
* @param int $id HttpServer ID
* @return Client Cake Http Client
*/
Expand Down Expand Up @@ -131,7 +131,7 @@ public function createHttpClient(int $id): Client {
/**
* Set validation rules.
*
* @since COmanage Registry v5.2.0
* @since COmanage Registry v5.1.0
* @param Validator $validator Validator
* @return Validator Validator
*/
Expand Down
6 changes: 3 additions & 3 deletions app/plugins/CoreServer/src/Model/Table/MatchServersTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @link https://www.internet2.edu/comanage COmanage Project
* @package registry-plugins
* @since COmanage Registry v5.2.0
* @since COmanage Registry v5.1.0
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*/

Expand All @@ -40,7 +40,7 @@ class MatchServersTable extends HttpServersTable {
/**
* Perform Cake Model initialization.
*
* @since COmanage Registry v5.2.0
* @since COmanage Registry v5.1.0
* @param array $config Configuration options passed to constructor
*/

Expand Down Expand Up @@ -80,7 +80,7 @@ public function initialize(array $config): void {
/**
* Set validation rules.
*
* @since COmanage Registry v5.2.0
* @since COmanage Registry v5.1.0
* @param Validator $validator Validator
* @return Validator Validator
*/
Expand Down
2 changes: 0 additions & 2 deletions app/plugins/CoreServer/src/Model/Table/SqlServersTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

// Even though CoreServer is a plugin, it should always be enabled
use CoreServer\Lib\Enum\RdbmsTypeEnum;

class SqlServersTable extends Table {
Expand Down
36 changes: 36 additions & 0 deletions app/src/Lib/Enum/RequiredEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* COmanage Registry Boolean Enum, Set Variant
*
* 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.1.0
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*/

declare(strict_types = 1);

namespace App\Lib\Enum;

class RequiredEnum extends StandardEnum {
const Required = "R";
const Optional = "O";
const NotPermitted = "NP";
}

0 comments on commit 42b7dd2

Please sign in to comment.