Skip to content

Commit

Permalink
Update Web UI to use command for config add, clone, and edit
Browse files Browse the repository at this point in the history
Fixes #32258
  • Loading branch information
vboctor committed Apr 9, 2023
1 parent da8d235 commit e7dcd29
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 48 deletions.
68 changes: 21 additions & 47 deletions adm_config_set.php
Expand Up @@ -35,14 +35,9 @@
*/

require_once( 'core.php' );
require_api( 'access_api.php' );
require_api( 'config_api.php' );
require_api( 'constant_inc.php' );
require_api( 'error_api.php' );
require_api( 'form_api.php' );
require_api( 'gpc_api.php' );
require_api( 'print_api.php' );
require_api( 'project_api.php' );
require_api( 'utility_api.php' );

form_security_validate( 'adm_config_set' );
Expand All @@ -57,38 +52,6 @@
$f_original_config_option = gpc_get_string( 'original_config_option' );
$f_edit_action = gpc_get_string( 'action' );


if( is_blank( $f_config_option ) ) {
error_parameters( 'config_option' );
trigger_error( ERROR_EMPTY_FIELD, ERROR );
}

access_ensure_global_level( config_get( 'set_configuration_threshold' ) );

if( $f_project_id != ALL_PROJECTS ) {
project_ensure_exists( $f_project_id );
}

# make sure that configuration option specified is a valid one.
$t_not_found_value = '***CONFIG OPTION NOT FOUND***';
if( config_get( $f_config_option, $t_not_found_value ) === $t_not_found_value ) {
error_parameters( $f_config_option );
trigger_error( ERROR_CONFIG_OPT_NOT_FOUND, ERROR );
}

# make sure that configuration option specified can be stored in the database
if( !config_can_set_in_database( $f_config_option ) ) {
error_parameters( $f_config_option );
trigger_error( ERROR_CONFIG_OPT_CANT_BE_SET_IN_DB, ERROR );
}

if( !config_can_delete( $f_config_option ) ) {
error_parameters( $f_config_option );
# @TODO define an error code for values that can't be set in DB, nor config_inc
trigger_error( ERROR_CONFIG_OPT_CANT_BE_SET_IN_DB, ERROR );
}


# For 'default', behavior is based on the global variable's type
# If value is empty, process as per default to ensure proper typecast
if( $f_type == CONFIG_TYPE_DEFAULT || empty( $f_value ) ) {
Expand Down Expand Up @@ -134,16 +97,27 @@
}
}

if( MANAGE_CONFIG_ACTION_EDIT === $f_edit_action ){
# EDIT action doesn't keep original if key values are different.
if ( $f_original_config_option !== $f_config_option
|| $f_original_user_id !== $f_user_id
|| $f_original_project_id !== $f_project_id ){
config_delete( $f_original_config_option, $f_original_user_id, $f_original_project_id );
}
}

config_set( $f_config_option, $t_value, $f_user_id, $f_project_id );
$t_data = array(
'payload' => array(
'user' => array( 'id' => $f_user_id ),
'project' => array( 'id' => $f_project_id ),
'configs' => array(
array(
'option' => $f_config_option,
'value' => $t_value,
),
),
),
'options' => array(
'edit_action' => $f_edit_action,
'original_user_id' => $f_original_user_id,
'original_project_id' => $f_original_project_id,
'original_option' => $f_original_config_option,
)
);

$t_command = new ConfigsSetCommand( $t_data );
$t_command->execute();

form_security_purge( 'adm_config_set' );

Expand Down
43 changes: 42 additions & 1 deletion core/commands/ConfigsSetCommand.php
Expand Up @@ -16,6 +16,8 @@

use Mantis\Exceptions\ClientException;

require_once( dirname( __FILE__ ) . '/../../api/soap/mc_api.php' );

/**
* A command that sets config options.
*
Expand Down Expand Up @@ -113,6 +115,12 @@ function validate() {
continue;
}

# make sure that configuration option specified is a valid one.
$t_not_found_value = '***CONFIG OPTION NOT FOUND***';
if( config_get( $t_name, $t_not_found_value ) === $t_not_found_value ) {
continue;
}

# these are config options that are stored in the database, but can't be deleted
# or modified. For example, database_version (schema version).
if( !config_can_delete( $t_name ) ) {
Expand All @@ -138,6 +146,23 @@ function validate() {

$this->options[] = $t_config;
}

# This mode is only for web UI and it will always have a single config option
if( MANAGE_CONFIG_ACTION_EDIT === $this->option( 'edit_action', MANAGE_CONFIG_ACTION_CREATE ) ) {
$t_original_option = $this->option( 'original_option', '' );
$t_original_user_id = (int)$this->option( 'original_user_id', '' );
$t_original_project_id = (int)$this->option( 'original_project_id', '' );

if( count( $this->options ) != 1 ||
is_blank( $t_original_option ) ||
is_blank( $t_original_user_id ) ||
is_blank( $t_original_project_id ) ) {
throw new ClientException(
'Invalid parameters for edit action',
ERROR_INVALID_FIELD_VALUE,
array( 'edit_action' ) );
}
}
}

/**
Expand All @@ -146,7 +171,23 @@ function validate() {
* @returns void
*/
protected function process() {
foreach( $this->options as $t_option ) {
# The edit case is internal only to web UI and it will always have a single config option
if( MANAGE_CONFIG_ACTION_EDIT === $this->option( 'edit_action', MANAGE_CONFIG_ACTION_CREATE ) ) {
$t_original_option = $this->option( 'original_option' );
$t_original_user_id = (int)$this->option( 'original_user_id' );
$t_original_project_id = (int)$this->option( 'original_project_id' );

$t_option = $this->options[0];

# EDIT action doesn't keep original if key values are different.
if ( $t_original_option !== $t_option['option']
|| $t_original_user_id !== $this->user_id
|| $t_original_project_id !== $this->project_id ) {
config_delete( $t_original_option, $t_original_user_id, $t_original_project_id );
}
}

foreach( $this->options as $t_option ) {
if( is_null( $t_option['value'] ) ) {
config_delete( $t_option['option'], $this->user_id, $this->project_id );
} else {
Expand Down

0 comments on commit e7dcd29

Please sign in to comment.