2
0
mirror of https://github.com/VinylDNS/vinyldns synced 2025-08-23 02:27:09 +00:00
vinyldns/modules/portal/public/lib/controllers/controller.manageZones.js

394 lines
13 KiB
JavaScript
Raw Normal View History

2018-07-27 10:18:29 -04:00
/*
* Copyright 2018 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
angular.module('controller.manageZones', [])
.controller('ManageZonesController', function ($scope, $timeout, $log, recordsService, zonesService, groupsService,
profileService, utilityService) {
2019-08-21 14:26:51 -04:00
groupsService.getGroupsStored()
2018-07-27 10:18:29 -04:00
.then(function (results) {
$scope.myGroups = results.groups;
})
.catch(function (error){
handleError(error, 'getMyGroup:get-groups-failure');
});
zonesService.getBackendIds().then(function (results) {
if (results.data) {
$scope.backendIds = results.data;
}
});
2018-07-27 10:18:29 -04:00
/**
* Zone scope data initial setup
*/
$scope.alerts = [];
$scope.zoneInfo = {};
2022-07-22 10:14:58 +05:30
$scope.zoneChanges={};
$scope.allAclRules = [];
2018-07-27 10:18:29 -04:00
$scope.updateZoneInfo = {};
$scope.manageZoneState = {
UPDATE: 0,
CONFIRM_UPDATE: 1
};
$scope.allGroups = [];
2018-07-27 10:18:29 -04:00
$scope.keyAlgorithms = ['HMAC-MD5', 'HMAC-SHA1', 'HMAC-SHA224', 'HMAC-SHA256', 'HMAC-SHA384', 'HMAC-SHA512'];
2018-07-27 10:18:29 -04:00
/**
* Acl scope data initial setup
*/
$scope.aclTypes = ['User', 'Group'];
$scope.aclAccessLevels = ['Read', 'Write', 'Delete', 'No Access'];
$scope.currentAclRule = {};
$scope.currentAclRuleIndex = {};
$scope.aclRules = [];
$scope.aclModalState = {
CREATE: 0,
UPDATE: 1,
CONFIRM_UPDATE: 2,
CONFIRM_DELETE: 3
};
$scope.aclModalParams = {
readOnly: {
class: '',
readOnly: true
},
editable: {
class: 'acl-edit',
readOnly: false
}
};
$scope.aclRecordTypes = ['A', 'AAAA', 'CNAME', 'DS', 'MX', 'NS', 'PTR', 'SRV', 'NAPTR', 'SSHFP', 'TXT'];
2018-07-27 10:18:29 -04:00
/**
* Zone modal control functions
*/
$scope.clickUpdateZone = function() {
$scope.currentManageZoneState = $scope.manageZoneState.CONFIRM_UPDATE;
};
$scope.cancelUpdateZone = function() {
$scope.currentManageZoneState = $scope.manageZoneState.UPDATE;
};
$scope.confirmDeleteZone = function() {
$("#delete_zone_connection_modal").modal("show");
};
$scope.submitDeleteZone = function() {
zonesService.delZone($scope.zoneInfo.id)
.then(function (response) {
$("#delete_zone_connection_modal").modal("hide");
var msg = response.statusText + " (HTTP "+response.status+"): " + response.data.changeType + " zone '" + response.data.zone.name + "'";
$scope.alerts.push({type: "success", content: msg});
$timeout(function(){
location.href = "/zones";
}, 2000);
})
.catch(function (error) {
$("#delete_zone_connection_modal").modal("hide");
$scope.zoneError = true;
handleError(error, 'zonesService::sendZone-failure');
});
};
2018-07-27 10:18:29 -04:00
/**
* Acl modal control functions
*/
$scope.clickCreateAclRule = function() {
$scope.currentAclRule = {
priority: 'User',
accessLevel: 'Read'
};
$scope.aclModal = {
action: $scope.aclModalState.CREATE,
title: 'Create ACL Rule',
details: $scope.aclModalParams.editable
};
$scope.addAclRuleForm.$setPristine();
$('#acl_modal').modal('show');
};
$scope.clickDeleteAclRule = function(index) {
$scope.currentAclRuleIndex = index;
$scope.currentAclRule = $scope.aclRules[index];
$scope.aclModal = {
action: $scope.aclModalState.CONFIRM_DELETE,
title: 'Delete ACL Rule',
details: $scope.aclModalParams.readOnly
};
$('#acl_modal').modal('show');
};
$scope.clickUpdateAclRule = function(index) {
$scope.currentAclRuleIndex = index;
$scope.currentAclRule = angular.copy($scope.aclRules[index]);
$scope.aclModal = {
action: $scope.aclModalState.UPDATE,
title: 'Update ACL Rule',
details: $scope.aclModalParams.editable
};
$('#acl_modal').modal('show');
};
$scope.confirmUpdateAclRule = function (bool) {
if (bool) {
$scope.aclModal.action = $scope.aclModalState.CONFIRM_UPDATE;
} else {
$scope.aclModal.action = $scope.aclModalState.UPDATE;
}
};
$scope.closeAclModal = function() {
$scope.addAclRuleForm.$setPristine();
};
$scope.clearForm = function() {
$scope.currentAclRule = {
priority: 'User',
accessLevel: 'Read'
};
$scope.addAclRuleForm.$setPristine();
};
/**
* Zone form submission functions
*/
$scope.submitUpdateZone = function () {
var zone = angular.copy($scope.updateZoneInfo);
zone = zonesService.normalizeZoneDates(zone);
zone = zonesService.setConnectionKeys(zone);
zone = zonesService.checkBackendId(zone);
zone = zonesService.checkSharedStatus(zone);
2018-07-27 10:18:29 -04:00
$scope.currentManageZoneState = $scope.manageZoneState.UPDATE;
$scope.updateZone(zone, 'Zone Update');
};
$scope.submitDeleteAclRule = function() {
var newZone = angular.copy($scope.zoneInfo);
newZone = zonesService.normalizeZoneDates(newZone);
newZone.acl.rules.splice($scope.currentAclRuleIndex, 1);
$scope.updateZone(newZone, 'ACL Rule Delete');
$("#acl_modal").modal('hide');
};
$scope.submitAclRule = function(type) {
if ($scope.addAclRuleForm.$valid) {
$("#acl_modal").modal('hide');
if ($scope.currentAclRule.priority == 'User') {
profileService.getUserDataByUsername($scope.currentAclRule.userName)
.then(function (profile) {
$log.log('profileService::getUserDataByUsername-success');
$scope.currentAclRule.userId = profile.data.id;
$scope.postUserLookup(type);
})
.catch(function (error){
handleError(error, 'profileService::getUserDataByUsername-failure');
});
} else {
$scope.postUserLookup(type);
}
}
};
$scope.postUserLookup = function(type) {
var newRule = zonesService.toVinylAclRule($scope.currentAclRule);
var newZone = angular.copy($scope.zoneInfo);
newZone = zonesService.normalizeZoneDates(newZone);
if (type == 'Update') {
newZone.acl.rules[$scope.currentAclRuleIndex] = newRule;
$scope.updateZone(newZone, 'ACL Rule Update');
} else if (type == 'Create') {
newZone.acl.rules.push(newRule);
$scope.updateZone(newZone, 'ACL Rule Create');
}
$scope.addAclRuleForm.$setPristine();
};
/**
* Form helpers
*/
$scope.objectsDiffer = function(left, right) {
var l = $scope.normalizeZone(left);
var r = $scope.normalizeZone(right);
return !angular.equals(l, r);
};
$scope.normalizeZone = function(zone) {
var vinyldnsZone = angular.copy(zone);
delete vinyldnsZone.adminGroupName;
delete vinyldnsZone.hiddenKey;
delete vinyldnsZone.hiddenTransferKey;
return vinyldnsZone;
};
$scope.clearUpdateConnection = function() {
delete $scope.updateZoneInfo.connection;
$scope.updateZoneInfo.hiddenKey = '';
};
$scope.clearUpdateTransferConnection = function() {
delete $scope.updateZoneInfo.transferConnection;
$scope.updateZoneInfo.hiddenTransferKey = '';
};
function handleError(error, type) {
var alert = utilityService.failure(error, type);
$scope.alerts.push(alert);
$scope.processing = false;
}
function showSuccess(requestType, response) {
var msg = requestType + " " + response.statusText + " (HTTP "+response.status+"): ";
msg += $scope.zoneInfo.name + ' updated';
$scope.alerts.push({type: "success", content: msg});
$timeout($scope.refreshZone(), 2000);
}
/**
* Global data-updating functions
*/
$scope.refreshZone = function() {
function success(response) {
$log.log('recordsService::getZone-success');
$scope.zoneInfo = response.data.zone;
$scope.updateZoneInfo = angular.copy($scope.zoneInfo);
$scope.updateZoneInfo.hiddenKey = '';
$scope.updateZoneInfo.hiddenTransferKey = '';
$scope.currentManageZoneState = $scope.manageZoneState.UPDATE;
$scope.refreshAclRuleDisplay();
}
return recordsService
.getZone($scope.zoneId)
.then(success)
.catch(function (error){
handleError(error, 'recordsService::getZone-failure');
});
};
2022-07-22 10:14:58 +05:30
$scope.refreshZoneChange = function() {
function success(response) {
$log.log('zonesService::getZoneChanges-success');
updateZoneChangeDisplay(response.data.zoneChanges);
}
return zonesService
.getZoneChanges($scope.zoneId)
.then(success)
.catch(function (error) {
handleError(error, 'zonesService::getZoneChanges-failure');
});
};
$scope.refreshAclRule = function (index) {
for (var length = 0; length < $scope.allZonesChange[index].zone.acl.rules.length; length++) {
$scope.allAclRules.push($scope.allZonesChange[index].zone.acl.rules[length]);
getAclUser($scope.allZonesChange[index].zone.acl.rules[length].userId, length, index);
getAclGroup($scope.allZonesChange[index].zone.acl.rules[length].groupId, length, index);
}
}
function updateZoneChangeDisplay (zoneChange) {
$scope.allZonesChange = zoneChange;
for (var length = 0; length < zoneChange.length; length++) {
getZoneGroup(zoneChange[length].zone.adminGroupId, length);
getZoneUser(zoneChange[length].userId, length);
}
};
function getZoneGroup(groupId, length) {
function success(response) {
$log.log('groupsService::getZoneGroup-success');
$scope.allZonesChange[length].zone.adminGroupName = response.data.name;
}
return groupsService
.getGroup(groupId)
.then(success)
.catch(function (error) {
handleError(error, 'groupsService::getZoneGroup-failure');
});
}
function getZoneUser(userId, length) {
function success(response) {
$log.log('profileService::getZoneUserDataById-success');
$scope.allZonesChange[length].userName = response.data.userName;
}
return profileService
.getUserDataById(userId)
.then(success)
.catch(function (error) {
handleError(error, 'profileService::getZoneUserDataById-failure');
});
};
function getAclGroup(groupId, length, index) {
function success(response) {
$log.log('groupsService::getAclGroup-success',length);
$scope.allZonesChange[index].zone.acl.rules[length].groupName = response.data.name;
}
return groupsService
.getGroup(groupId)
.then(success)
.catch(function (error) {
handleError(error, 'groupsService::getAclGroup-failure');
});
}
function getAclUser(userId, length, index) {
function success(response) {
$log.log('profileService::getAclUserDataById-success',userId, length, index);
$scope.allZonesChange[index].zone.acl.rules[length].userName = response.data.userName;
}
return profileService
.getUserDataById(userId)
.then(success)
.catch(function (error) {
handleError(error, 'profileService::getAclUserDataById-failure');
});
};
2018-07-27 10:18:29 -04:00
$scope.refreshAclRuleDisplay = function() {
$scope.aclRules = [];
angular.forEach($scope.zoneInfo.acl.rules, function (rule) {
$scope.aclRules.push(zonesService.toDisplayAclRule(rule));
});
};
/**
* Service interaction functions
*/
$scope.updateZone = function(zone, message) {
return zonesService
.updateZone($scope.zoneId, zone)
.then(function(response){showSuccess(message, response)})
.catch(function (error){
$timeout($scope.refreshZone(), 1000);
handleError(error, 'zonesService::updateZone-failure');
});
};
$timeout($scope.refreshZone, 0);
});