From 6219ca31821ec9cb6a1433a343ee10c7694d9149 Mon Sep 17 00:00:00 2001 From: Victor Boctor Date: Sun, 30 Sep 2018 14:19:37 -0700 Subject: [PATCH] Support new tags when creating new issues Fixes #24774 --- bug_report.php | 2 +- core/commands/IssueAddCommand.php | 21 ++++++++++++++++++++- core/tag_api.php | 24 ++++++++++++++++++++++-- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/bug_report.php b/bug_report.php index 362f163409..1707575209 100644 --- a/bug_report.php +++ b/bug_report.php @@ -106,7 +106,7 @@ if( !empty( $t_tags ) ) { $t_issue['tags'] = array(); foreach( $t_tags as $t_tag ) { - $t_issue['tags'][] = array( 'id' => $t_tag['id'] ); + $t_issue['tags'][] = array( 'id' => $t_tag['id'], 'name' => $t_tag['name'] ); } } diff --git a/core/commands/IssueAddCommand.php b/core/commands/IssueAddCommand.php index 76d1b3db58..11f795f880 100644 --- a/core/commands/IssueAddCommand.php +++ b/core/commands/IssueAddCommand.php @@ -217,6 +217,16 @@ function validate() { } } + if( isset( $t_issue['tags'] ) && is_array( $t_issue['tags'] ) && !tag_can_create( $this->user_id ) ) { + foreach( $t_issue['tags'] as $t_tag ) { + if( $t_tag['id'] === -1 ) { + throw new ClientException( + sprintf( "User '%d' can't create tag '%s'.", $this->user_id, $t_new_tag ), + ERROR_TAG_NOT_FOUND ); + } + } + } + $t_category = isset( $t_issue['category'] ) ? $t_issue['category'] : null; $t_category_id = mci_get_category_id( $t_category, $t_project_id ); @@ -332,7 +342,16 @@ protected function process() { # Add Tags if( isset( $t_issue['tags'] ) && is_array( $t_issue['tags'] ) ) { - mci_tag_set_for_issue( $t_issue_id, $t_issue['tags'], $this->user_id ); + $t_tags = array(); + foreach( $t_issue['tags'] as $t_tag ) { + if( $t_tag['id'] === -1 ) { + $t_tag['id'] = tag_create( $t_tag['name'], $this->user_id ); + } + + $t_tags[] = $t_tag; + } + + mci_tag_set_for_issue( $t_issue_id, $t_tags, $this->user_id ); } # Handle the file upload diff --git a/core/tag_api.php b/core/tag_api.php index 010ee74879..da85042eb1 100644 --- a/core/tag_api.php +++ b/core/tag_api.php @@ -327,7 +327,7 @@ function tag_attach_many( $p_bug_id, $p_tag_string, $p_tag_id = 0 ) { access_ensure_bug_level( config_get( 'tag_attach_threshold' ), $p_bug_id ); $t_tags = tag_parse_string( $p_tag_string ); - $t_can_create = access_has_global_level( config_get( 'tag_create_threshold' ) ); + $t_can_create = tag_can_create(); $t_tags_create = array(); $t_tags_attach = array(); @@ -529,6 +529,26 @@ function tag_get_field( $p_tag_id, $p_field_name ) { } } +/** + * Can the specified user create a tag? + * + * @param integer $p_user_id The id of the user to check access rights for. + * @return bool true: can create, false: otherwise. + */ +function tag_can_create( $p_user_id = null ) { + return access_has_global_level( config_get( 'tag_create_threshold' ), $p_user_id ); +} + +/** + * Ensure specified user can create tags. + * + * @param integer $p_user_id The id of the user to check access rights for. + * @return void + */ +function tag_ensure_can_create( $p_user_id = null ) { + access_ensure_global_level( config_get( 'tag_create_threshold' ), $p_user_id ); +} + /** * Create a tag with the given name, creator, and description. * Defaults to the currently logged in user, and a blank description. @@ -538,7 +558,7 @@ function tag_get_field( $p_tag_id, $p_field_name ) { * @return int Tag ID */ function tag_create( $p_name, $p_user_id = null, $p_description = '' ) { - access_ensure_global_level( config_get( 'tag_create_threshold' ) ); + tag_ensure_can_create( $p_user_id ); tag_ensure_name_is_valid( $p_name ); tag_ensure_unique( $p_name );