Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions app/plugins/CoreEnroller/src/Lib/Enum/MagicEnvNameFieldsEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* COmanage Registry Magic Environment Variable Name Fields Enum
*
* Name defaults may be sourced from environment variables. Since names have
* multiple components, the configured base variable name is appended with one
* of the suffixes below to derive the per-component variable name.
*
* Example:
* Base: ENV_OIS_NAME
* Given: ENV_OIS_NAME_GIVEN
*
* 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.2.0
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*/

declare(strict_types = 1);

namespace CoreEnroller\Lib\Enum;

use App\Lib\Enum\StandardEnum;

class MagicEnvNameFieldsEnum extends StandardEnum {
/**
* Suffix delimiter used to construct component env var names.
*/
const Delimiter = '_';

/**
* Suffixes appended to a configured base env var name.
*/
const HonorificSuffix = '_HONORIFIC';
const GivenSuffix = '_GIVEN';
const MiddleSuffix = '_MIDDLE';
const FamilySuffix = '_FAMILY';
const SuffixSuffix = '_SUFFIX';

/**
* All supported suffixes (in documentation order).
*/
const Suffixes = [
self::HonorificSuffix,
self::GivenSuffix,
self::MiddleSuffix,
self::FamilySuffix,
self::SuffixSuffix,
];
}
71 changes: 71 additions & 0 deletions app/plugins/CoreEnroller/src/Lib/Util/MagicEnvDefaultUtilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* COmanage Registry Magic Env Default Utilities
*
* Utilities for deriving default values from environment variables where
* attribute values are composed from multiple components (eg: Name).
*
* 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.2.0
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*/

declare(strict_types = 1);

namespace CoreEnroller\Lib\Util;

use CoreEnroller\Lib\Enum\MagicEnvNameFieldsEnum;

class MagicEnvDefaultUtilities {
/**
* Return the default value for a Name component derived from a base env var name.
*
* No fallback values are invented: missing/empty env vars return null.
*
* @param string $baseEnvName Base env var name (eg: ENV_OIS_NAME)
* @param string $component One of: honorific|given|middle|family|suffix
* @return string|null
*/
public static function nameComponentFromEnv(string $baseEnvName, string $component): ?string {
$component = strtolower($component);

$suffix = match($component) {
'honorific' => MagicEnvNameFieldsEnum::HonorificSuffix,
'given' => MagicEnvNameFieldsEnum::GivenSuffix,
'middle' => MagicEnvNameFieldsEnum::MiddleSuffix,
'family' => MagicEnvNameFieldsEnum::FamilySuffix,
'suffix' => MagicEnvNameFieldsEnum::SuffixSuffix,
default => null
};

if($suffix === null || $baseEnvName === '') {
return null;
}

$v = getenv($baseEnvName . $suffix);

if($v === false || $v === '') {
return null;
}

return (string)$v;
}
}
4 changes: 4 additions & 0 deletions app/plugins/CoreEnroller/templates/element/field.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@
// Do we have a default value configured?
// Either a value or an Environmental Variable,
// Each default value is mutually exclusive to the rest. We do not have to worry about a conflict.
//
// NOTE: name/address/telephoneNumber are MVEAs and are rendered as grouped sub-fields.
// Their per-component defaults must be applied at the sub-field level, not here.
$options['default'] = match(true) {
isset($attr->default_value) => $attr->default_value,
isset($attr->default_value_env_name)
&& $attr->attribute !== 'name'
&& getenv($attr->default_value_env_name) !== false => getenv($attr->default_value_env_name),
isset($attr->default_value_datetime) => $attr->default_value_datetime,
default => ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
declare(strict_types = 1);

use \Cake\Utility\Inflector;
use CoreEnroller\Lib\Util\MagicEnvDefaultUtilities;

// $field: string
// $attr: object
Expand All @@ -53,12 +54,23 @@
// Do we have a default value configured?
// Either a value or an Environmental Variable,
// Each default value is mutually exclusive to the rest. We do not have to worry about a conflict.
//
// NOTE: For Name, default_value_env_name is treated as a base env var name.
// We derive per-component env vars (eg BASE_GIVEN). Missing/empty env vars yield no default.
$options['default'] = match(true) {
isset($attr->default_value) => $attr->default_value,

// XXX The $attr->default_value_env_name for the name attribute is tricky. Since the name has many values.
// Check the EnvSource plugin
isset($attr->default_value_env_name)
&& $attr->attribute === 'name'
&& MagicEnvDefaultUtilities::nameComponentFromEnv((string)$attr->default_value_env_name, (string)$field) !== null
=> MagicEnvDefaultUtilities::nameComponentFromEnv((string)$attr->default_value_env_name, (string)$field),

isset($attr->default_value_env_name)
&& $attr->attribute !== 'name'
&& getenv($attr->default_value_env_name) !== false => getenv($attr->default_value_env_name),

isset($attr->default_value_datetime) => $attr->default_value_datetime,
default => ''
};
Expand Down