1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-31 03:09:44 +02:00

fix(template): add name validation for template name EE-1806 (#5823)

* add name validation for tempalte name
This commit is contained in:
Richard Wei 2021-10-07 13:02:56 +13:00 committed by GitHub
parent 84e57cebc9
commit 661931d8b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 49 additions and 5 deletions

View file

@ -31,3 +31,4 @@ angular
export const PORTAINER_FADEOUT = 1500; export const PORTAINER_FADEOUT = 1500;
export const STACK_NAME_VALIDATION_REGEX = '^[-_a-z0-9]+$'; export const STACK_NAME_VALIDATION_REGEX = '^[-_a-z0-9]+$';
export const TEMPLATE_NAME_VALIDATION_REGEX = '^[-_a-z0-9]+$';

View file

@ -5,7 +5,27 @@
Title Title
</label> </label>
<div class="col-sm-9 col-lg-10"> <div class="col-sm-9 col-lg-10">
<input type="text" class="form-control" ng-model="$ctrl.formValues.Title" id="template_title" name="template_title" placeholder="e.g. mytemplate" auto-focus required /> <input
type="text"
class="form-control"
ng-model="$ctrl.formValues.Title"
ng-pattern="$ctrl.nameRegex"
id="template_title"
name="template_title"
placeholder="e.g. mytemplate"
auto-focus
required
/>
</div>
</div>
<div class="form-group" ng-show="commonCustomTemplateForm.template_title.$invalid">
<div class="col-sm-12 small text-warning">
<div ng-messages="commonCustomTemplateForm.template_title.$error">
<p ng-message="pattern">
<i class="fa fa-exclamation-triangle" aria-hidden="true"></i>
<span>This field must consist of lower case alphanumeric characters, '_' or '-' (e.g. 'my-name', or 'abc-123').</span>
</p>
</div>
</div> </div>
</div> </div>
<div class="form-group" ng-show="commonCustomTemplateForm.template_title.$invalid"> <div class="form-group" ng-show="commonCustomTemplateForm.template_title.$invalid">

View file

@ -7,5 +7,6 @@ angular.module('portainer.app').component('customTemplateCommonFields', {
formValues: '=', formValues: '=',
showPlatformField: '<', showPlatformField: '<',
showTypeField: '<', showTypeField: '<',
nameRegex: '<',
}, },
}); });

View file

@ -8,6 +8,7 @@ angular.module('portainer.app').component('stackFromTemplateForm', {
state: '=', state: '=',
createTemplate: '<', createTemplate: '<',
unselectTemplate: '<', unselectTemplate: '<',
nameRegex: '<',
}, },
transclude: { transclude: {
advanced: '?advancedForm', advanced: '?advancedForm',

View file

@ -2,7 +2,7 @@
<rd-widget> <rd-widget>
<rd-widget-custom-header icon="$ctrl.template.Logo" title-text="$ctrl.template.Title"></rd-widget-custom-header> <rd-widget-custom-header icon="$ctrl.template.Logo" title-text="$ctrl.template.Title"></rd-widget-custom-header>
<rd-widget-body classes="padding"> <rd-widget-body classes="padding">
<form class="form-horizontal"> <form class="form-horizontal" name="stackTemplateForm">
<!-- description --> <!-- description -->
<div ng-if="$ctrl.template.Note"> <div ng-if="$ctrl.template.Note">
<div class="col-sm-12 form-section-title"> <div class="col-sm-12 form-section-title">
@ -20,9 +20,19 @@
</div> </div>
<!-- name-input --> <!-- name-input -->
<div class="form-group"> <div class="form-group">
<label for="container_name" class="col-sm-2 control-label text-left">Name</label> <label for="template_name" class="col-sm-2 control-label text-left">Name</label>
<div class="col-sm-10"> <div class="col-sm-10">
<input type="text" name="container_name" class="form-control" ng-model="$ctrl.formValues.name" placeholder="e.g. myStack" required /> <input type="text" name="template_name" class="form-control" ng-model="$ctrl.formValues.name" ng-pattern="$ctrl.nameRegex" placeholder="e.g. myStack" required />
</div>
</div>
<div class="form-group" ng-show="stackTemplateForm.template_name.$invalid">
<div class="col-sm-12 small text-warning">
<div ng-messages="stackTemplateForm.template_name.$error">
<p ng-message="pattern">
<i class="fa fa-exclamation-triangle" aria-hidden="true"></i>
<span>This field must consist of lower case alphanumeric characters, '_' or '-' (e.g. 'my-name', or 'abc-123').</span>
</p>
</div>
</div> </div>
</div> </div>
<!-- !name-input --> <!-- !name-input -->

View file

@ -8,7 +8,12 @@
<rd-widget> <rd-widget>
<rd-widget-body> <rd-widget-body>
<form class="form-horizontal" name="customTemplateForm"> <form class="form-horizontal" name="customTemplateForm">
<custom-template-common-fields form-values="$ctrl.formValues" show-platform-field="true" show-type-field="true"></custom-template-common-fields> <custom-template-common-fields
form-values="$ctrl.formValues"
show-platform-field="true"
show-type-field="true"
name-regex="$ctrl.state.templateNameRegex"
></custom-template-common-fields>
<!-- build-method --> <!-- build-method -->
<div ng-if="!$ctrl.state.fromStack"> <div ng-if="!$ctrl.state.fromStack">

View file

@ -1,5 +1,6 @@
import _ from 'lodash'; import _ from 'lodash';
import { AccessControlFormData } from 'Portainer/components/accessControlForm/porAccessControlFormModel'; import { AccessControlFormData } from 'Portainer/components/accessControlForm/porAccessControlFormModel';
import { TEMPLATE_NAME_VALIDATION_REGEX } from '@/constants';
class CreateCustomTemplateViewController { class CreateCustomTemplateViewController {
/* @ngInject */ /* @ngInject */
@ -43,7 +44,9 @@ class CreateCustomTemplateViewController {
fromStack: false, fromStack: false,
loading: true, loading: true,
isEditorDirty: false, isEditorDirty: false,
templateNameRegex: TEMPLATE_NAME_VALIDATION_REGEX,
}; };
this.templates = []; this.templates = [];
this.createCustomTemplate = this.createCustomTemplate.bind(this); this.createCustomTemplate = this.createCustomTemplate.bind(this);

View file

@ -11,6 +11,7 @@
ng-if="$ctrl.state.selectedTemplate" ng-if="$ctrl.state.selectedTemplate"
template="$ctrl.state.selectedTemplate" template="$ctrl.state.selectedTemplate"
form-values="$ctrl.formValues" form-values="$ctrl.formValues"
name-regex="$ctrl.state.templateNameRegex"
state="$ctrl.state" state="$ctrl.state"
create-template="$ctrl.createStack" create-template="$ctrl.createStack"
unselect-template="$ctrl.unselectTemplate" unselect-template="$ctrl.unselectTemplate"

View file

@ -1,5 +1,6 @@
import _ from 'lodash-es'; import _ from 'lodash-es';
import { AccessControlFormData } from 'Portainer/components/accessControlForm/porAccessControlFormModel'; import { AccessControlFormData } from 'Portainer/components/accessControlForm/porAccessControlFormModel';
import { TEMPLATE_NAME_VALIDATION_REGEX } from '@/constants';
class CustomTemplatesViewController { class CustomTemplatesViewController {
/* @ngInject */ /* @ngInject */
@ -44,6 +45,7 @@ class CustomTemplatesViewController {
actionInProgress: false, actionInProgress: false,
isEditorVisible: false, isEditorVisible: false,
deployable: false, deployable: false,
templateNameRegex: TEMPLATE_NAME_VALIDATION_REGEX,
}; };
this.currentUser = { this.currentUser = {