diff --git a/app/plugins/EnvSource/src/Model/Table/EnvSourceCollectorsTable.php b/app/plugins/EnvSource/src/Model/Table/EnvSourceCollectorsTable.php index 4977abdc8..873bc9bde 100644 --- a/app/plugins/EnvSource/src/Model/Table/EnvSourceCollectorsTable.php +++ b/app/plugins/EnvSource/src/Model/Table/EnvSourceCollectorsTable.php @@ -253,15 +253,7 @@ public function parse(\EnvSource\Model\Entity\EnvSource $envSource): array { // The look aside file is for debugging purposes. If the file is specified but not found, // we throw an error to prevent unintended configurations. - $src = parse_ini_file($envSource->lookaside_file); - - if(!$src) { - throw new \InvalidArgumentException(__d('env_source', 'error.lookaside_file', [$envSource->lookaside_file])); - } - // Put the values in the environment - foreach($src as $k => $v) { - putenv("$k=$v"); - } + return $this->loadFromLookasideFile($envSource->lookaside_file, $envSource); } // We walk through our configuration and only copy the variables that were configured @@ -281,6 +273,40 @@ public function parse(\EnvSource\Model\Entity\EnvSource $envSource): array { return $ret; } + /** + * Load environment variables from a lookaside file based on the given configuration. + * + * @param string $filename Path to the lookaside file + * @param \EnvSource\Model\Entity\EnvSource $envSource EnvSource configuration entity + * @return array Array of environment variables and their parsed values + * @throws InvalidArgumentException + *@since COmanage Registry v5.1.0 + */ + public function loadFromLookasideFile(string $filename, \EnvSource\Model\Entity\EnvSource $envSource): array { + $src = parse_ini_file($filename); + $ret = []; + + if(!$src) { + throw new \InvalidArgumentException(__d('env_source', 'error.lookaside_file', [$filename])); + } + + // We walk through our configuration and only copy the variables that were configured + foreach($envSource->getVisible() as $field) { + // We only want the fields starting env_ (except env_source_id, which is changelog metadata) + + if(strncmp($field, "env_", 4)==0 && $field != "env_source_id" + && !empty($envSource->$field) // This field is configured with an env var name + && isset($src[$envSource->$field]) // This env var is populated + ) { + // Note we're using the EnvSource field name (eg: env_name_given) as the key + // and not the configured variable name (which might be something like SHIB_FIRST_NAME) + $ret[$field] = $src[$envSource->$field]; + } + } + + return $ret; + } + /** * Insert or update a Petition Env Identity. * diff --git a/app/plugins/EnvSource/src/Model/Table/EnvSourcesTable.php b/app/plugins/EnvSource/src/Model/Table/EnvSourcesTable.php index 64383a669..48e62f736 100644 --- a/app/plugins/EnvSource/src/Model/Table/EnvSourcesTable.php +++ b/app/plugins/EnvSource/src/Model/Table/EnvSourcesTable.php @@ -204,7 +204,7 @@ protected function resultToEntityData( \EnvSource\Model\Entity\EnvSource $EnvSource, array $result ): array { - // We don't need most of the $EnvSource configuation since EnvSourceCollector::parse + // We don't need most of the $EnvSource configuration since EnvSourceCollector::parse // already mapped the variable names for us. We do need to know the sp_mode for parsing // multiple values, and also we need the types. @@ -218,7 +218,7 @@ protected function resultToEntityData( $role = [ // We only support one role per record 'role_key' => '1', - 'affiliation' => $this->DefaultAffiliationTypes->getTypeLabel($EnvSource->name_type_id) + 'affiliation' => $this->DefaultAffiliationTypes->getTypeLabel($EnvSource->default_affiliation_type_id) ]; foreach([