Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Ensure required custom fields specified on create issue
Fixes #23577
  • Loading branch information
vboctor committed Nov 10, 2017
1 parent 0e071b6 commit 03db938
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
5 changes: 5 additions & 0 deletions api/soap/mc_issue_api.php
Expand Up @@ -816,6 +816,11 @@ function mc_issue_add( $p_username, $p_password, $p_issue ) {

log_event( LOG_WEBSERVICE, 'created new issue id \'' . $t_issue_id . '\'' );

$t_cf_result = mci_project_custom_fields_validate( $t_project_id, $p_issue['custom_fields'] );
if( ApiObjectFactory::isFault( $t_cf_result ) ) {
return $t_cf_result;
}

$t_set_custom_field_error = mci_issue_set_custom_fields( $t_issue_id, $p_issue['custom_fields'], false );
if( $t_set_custom_field_error != null ) {
return $t_set_custom_field_error;
Expand Down
74 changes: 74 additions & 0 deletions api/soap/mc_project_api.php
Expand Up @@ -633,6 +633,80 @@ function mc_project_get_custom_fields( $p_username, $p_password, $p_project_id )
return mci_project_get_custom_fields( $p_project_id );
}

/**
* Validate custom fields before creating/updating issues.
*
* @param integer $p_project_id The project id.
* @param array $p_custom_fields The custom fields, may be not set.
* return bool|SoapFault|RestFault true or error.
*/
function mci_project_custom_fields_validate( $p_project_id, &$p_custom_fields ) {
# Load custom field definitions
$t_related_custom_field_ids = custom_field_get_linked_ids( $p_project_id );
$t_custom_field_defs = array();
foreach( $t_related_custom_field_ids as $t_custom_field_id ) {
$t_def = custom_field_get_definition( $t_custom_field_id );
$t_custom_field_defs[$t_custom_field_id] = $t_def;
}

$t_custom_field_values = array();
if( isset( $p_custom_fields ) ) {
foreach( $p_custom_fields as $t_custom_field ) {
if( !isset( $t_custom_field['value'] ) ) {
$t_error = 'Custom field has no value specified.';
return ApiObjectFactory::faultBadRequest( $t_error );
}

if( !isset( $t_custom_field['field'] ) ) {
$t_error = 'Custom field with no specified id or name.';
return ApiObjectFactory::faultBadRequest( $t_error );
}

if( isset( $t_custom_field['field']['id'] ) ) {
$t_def = $t_custom_field_defs[(int)$t_custom_field['field']['id']];
$t_custom_field_values[$t_def['name']] = $t_custom_field['value'];
continue;
}

if( isset( $t_custom_field['field']['name'] ) ) {
$t_custom_field_values[$t_custom_field['field']['name']] = $t_custom_field['value'];
continue;
}

$t_error = 'Custom field with no specified id or name.';
return ApiObjectFactory::faultBadRequest( $t_error );
}
}

# Validate the custom fields before adding the bug.
foreach( $t_related_custom_field_ids as $t_custom_field_id ) {
# Skip custom fields that user doesn't have access to write.
if( !custom_field_has_write_access_to_project( $t_custom_field_id, $p_project_id ) ) {
continue;
}

$t_def = $t_custom_field_defs[$t_custom_field_id];
$t_name = custom_field_get_field( $t_custom_field_id, 'name' );

# Produce an error if the field is required but wasn't posted
if( $t_def['require_report'] ) {
if( !isset( $t_custom_field_values[$t_name] ) ||
is_blank( $t_custom_field_values[$t_name] ) ) {
$t_error = "Mandatory field '$t_name' is missing.";
return ApiObjectFactory::faultBadRequest( $t_error );
}
}

if( isset( $t_custom_field_values[$t_name] ) &&
!custom_field_validate( $t_custom_field_id, $t_custom_field_values[$t_name] ) ) {
$t_error = "Invalid custom field '$t_name' value.";
return ApiObjectFactory::faultBadRequest( $t_error );
}
}

return true;
}

/**
* Get the custom fields that belong to the specified project.
*
Expand Down

0 comments on commit 03db938

Please sign in to comment.