portainer/app/docker/views/secrets/create/createSecretController.js

88 lines
2.5 KiB
JavaScript
Raw Normal View History

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) {
$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: '',
actionInProgress: false
};
$scope.addLabel = function() {
$scope.formValues.Labels.push({ key: '', value: ''});
};
$scope.removeLabel = function(index) {
$scope.formValues.Labels.splice(index, 1);
};
function prepareLabelsConfig(config) {
config.Labels = LabelHelper.fromKeyValueToLabelHash($scope.formValues.Labels);
}
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 () {
var accessControlData = $scope.formValues.AccessControlData;
var userDetails = Authentication.getUserDetails();
var isAdmin = userDetails.role === 1;
2017-09-19 15:10:15 +00:00
if (!validateForm(accessControlData, isAdmin)) {
return;
}
$scope.state.actionInProgress = true;
2017-09-19 15:10:15 +00:00
var secretConfiguration = prepareConfiguration();
SecretService.create(secretConfiguration)
.then(function success(data) {
2017-09-19 15:10:15 +00:00
var secretIdentifier = data.ID;
var userId = userDetails.ID;
return ResourceControlService.applyResourceControl('secret', secretIdentifier, userId, accessControlData, []);
})
.then(function success() {
Notifications.success('Secret successfully created');
$state.go('docker.secrets', {}, {reload: true});
})
.catch(function error(err) {
Notifications.error('Failure', err, 'Unable to create secret');
})
.finally(function final() {
$scope.state.actionInProgress = false;
});
};
}]);