2019-03-21 05:46:49 +00:00
|
|
|
import { AccessControlFormData } from '../../../../portainer/components/accessControlForm/porAccessControlFormModel';
|
|
|
|
|
2018-02-01 12:27:52 +00:00
|
|
|
angular.module('portainer.docker')
|
2017-09-19 15:10:15 +00:00
|
|
|
.controller('CreateSecretController', ['$scope', '$state', 'Notifications', 'SecretService', 'LabelHelper', 'Authentication', 'ResourceControlService', 'FormValidator',
|
|
|
|
function ($scope, $state, Notifications, SecretService, LabelHelper, Authentication, ResourceControlService, FormValidator) {
|
|
|
|
|
2017-05-27 07:23:49 +00:00
|
|
|
$scope.formValues = {
|
|
|
|
Name: '',
|
|
|
|
Data: '',
|
|
|
|
Labels: [],
|
2017-09-19 15:10:15 +00:00
|
|
|
encodeSecret: true,
|
|
|
|
AccessControlData: new AccessControlFormData()
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.state = {
|
2017-11-12 19:27:28 +00:00
|
|
|
formValidationError: '',
|
2017-11-12 21:39:12 +00:00
|
|
|
actionInProgress: false
|
2017-05-27 07:23:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.addLabel = function() {
|
2017-07-10 07:33:09 +00:00
|
|
|
$scope.formValues.Labels.push({ key: '', value: ''});
|
2017-05-27 07:23:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removeLabel = function(index) {
|
|
|
|
$scope.formValues.Labels.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
function prepareLabelsConfig(config) {
|
2017-07-10 07:33:09 +00:00
|
|
|
config.Labels = LabelHelper.fromKeyValueToLabelHash($scope.formValues.Labels);
|
2017-05-27 07:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function prepareSecretData(config) {
|
|
|
|
if ($scope.formValues.encodeSecret) {
|
|
|
|
config.Data = btoa(unescape(encodeURIComponent($scope.formValues.Data)));
|
|
|
|
} else {
|
|
|
|
config.Data = $scope.formValues.Data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepareConfiguration() {
|
|
|
|
var config = {};
|
|
|
|
config.Name = $scope.formValues.Name;
|
|
|
|
prepareSecretData(config);
|
|
|
|
prepareLabelsConfig(config);
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2017-09-19 15:10:15 +00:00
|
|
|
function validateForm(accessControlData, isAdmin) {
|
|
|
|
$scope.state.formValidationError = '';
|
|
|
|
var error = '';
|
|
|
|
error = FormValidator.validateAccessControl(accessControlData, isAdmin);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
$scope.state.formValidationError = error;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.create = function () {
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
const accessControlData = $scope.formValues.AccessControlData;
|
|
|
|
const userDetails = Authentication.getUserDetails();
|
|
|
|
const isAdmin = Authentication.isAdmin();
|
2017-09-19 15:10:15 +00:00
|
|
|
|
|
|
|
if (!validateForm(accessControlData, isAdmin)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-12 21:39:12 +00:00
|
|
|
$scope.state.actionInProgress = true;
|
2017-09-19 15:10:15 +00:00
|
|
|
var secretConfiguration = prepareConfiguration();
|
|
|
|
SecretService.create(secretConfiguration)
|
2017-05-27 07:23:49 +00:00
|
|
|
.then(function success(data) {
|
2019-11-12 23:41:42 +00:00
|
|
|
const userId = userDetails.ID;
|
|
|
|
const resourceControl = data.Portainer.ResourceControl;
|
|
|
|
return ResourceControlService.applyResourceControl(userId, accessControlData, resourceControl);
|
2017-09-19 15:10:15 +00:00
|
|
|
})
|
|
|
|
.then(function success() {
|
2017-05-27 07:23:49 +00:00
|
|
|
Notifications.success('Secret successfully created');
|
2018-02-01 12:27:52 +00:00
|
|
|
$state.go('docker.secrets', {}, {reload: true});
|
2017-05-27 07:23:49 +00:00
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to create secret');
|
|
|
|
})
|
|
|
|
.finally(function final() {
|
2017-11-12 21:39:12 +00:00
|
|
|
$scope.state.actionInProgress = false;
|
2017-05-27 07:23:49 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}]);
|