Refactored code for ResourceServerPolicyResourceDetailCtrl and ResourceServerPolicyScopeDetailCtrl with common method and related issues fixing

This commit is contained in:
Niket Kapadia 2022-03-23 12:19:51 +05:30 committed by Pedro Igor
parent 100827977e
commit 96f5883ee0
2 changed files with 160 additions and 402 deletions

View file

@ -901,9 +901,25 @@ module.controller('ResourceServerPermissionCtrl', function($scope, $http, $route
});
module.controller('ResourceServerPolicyResourceDetailCtrl', function($scope, $route, $location, realm, client, PolicyController, ResourceServerPermission, ResourceServerResource, policyViewState) {
// call common handler method with ResourceServerScope as null.
ResourceServerPolicyCommonHandler($scope, $route, $location, realm, client, PolicyController, ResourceServerPermission, ResourceServerResource, null, policyViewState);
});
module.controller('ResourceServerPolicyScopeDetailCtrl', function($scope, $route, $location, realm, client, PolicyController, ResourceServerPermission, ResourceServerResource, ResourceServerScope, policyViewState) {
ResourceServerPolicyCommonHandler($scope, $route, $location, realm, client, PolicyController, ResourceServerPermission, ResourceServerResource, ResourceServerScope, policyViewState);
});
function ResourceServerPolicyCommonHandler($scope, $route, $location, realm, client, PolicyController, ResourceServerPermission, ResourceServerResource, ResourceServerScope, policyViewState) {
// if ResourceServerScope will be avaialble, scope handling will happen else ignored.
PolicyController.onInit({
getPolicyType : function() {
return "resource";
if (ResourceServerScope == null) {
return "resource";
} else {
return "scope";
}
},
isPermission : function() {
@ -911,6 +927,49 @@ module.controller('ResourceServerPolicyResourceDetailCtrl', function($scope, $ro
},
onInit : function() {
if (ResourceServerScope != null) {
$scope.scopesUiSelect = {
minimumInputLength: 1,
delay: 500,
allowClear: true,
query: function (query) {
if ($scope.selectedResource && $scope.selectedResource._id) {
// if resource is selected, provide result based on resourceScopes
var filteredScopes = $scope.resourceScopes.filter(
resourceScope => resourceScope.name.toLowerCase().includes(
query.term.trim().toLowerCase())
);
var data = {results: []};
data.results = filteredScopes;
query.callback(data);
} else {
// if no resource is selected, all the scopes are allowed.
var data = {results: []};
if ('' == query.term.trim()) {
query.callback(data);
return;
}
$scope.query = {
realm: realm.realm,
client : client.id,
name: query.term.trim(),
deep: false,
max : 20,
first : 0
};
ResourceServerScope.query($scope.query, function(response) {
data.results = response;
query.callback(data);
});
}
},
formatResult: function(object, container, query) {
object.text = object.name;
return object.name;
}
};
}
$scope.resourcesUiSelect = {
minimumInputLength: 1,
delay: 500,
@ -970,34 +1029,95 @@ module.controller('ResourceServerPolicyResourceDetailCtrl', function($scope, $ro
}
};
if (ResourceServerScope != null) {
$scope.selectResource = function() {
$scope.selectedScopes = [];
if ($scope.selectedResource) {
ResourceServerResource.scopes({
realm: $route.current.params.realm,
client: client.id,
rsrid: $scope.selectedResource._id
}, function (data) {
$scope.resourceScopes = data;
});
}
}
}
$scope.applyToResourceType = function() {
if ($scope.applyToResourceTypeFlag) {
// if previously apply to resource type flag is selected,
// assume that it will be disabled now and accordingly,
// set values for selectedResource or resourceType
const prevApplyToResourceTypeFlag = $scope.applyToResourceTypeFlag;
if (!prevApplyToResourceTypeFlag) {
$scope.selectedResource = null;
} else {
$scope.policy.resourceType = null;
}
if (ResourceServerScope != null) {
$scope.selectedScopes = [];
$scope.changed = true;
}
}
},
onInitUpdate : function(policy) {
if (!policy.resourceType) {
$scope.selectedResource = {};
ResourceServerPermission.resources({
realm: $route.current.params.realm,
client: client.id,
id: policy.id
}, function (resources) {
resources[0].text = resources[0].name;
$scope.selectedResource = resources[0];
var copy = angular.copy($scope.selectedResource);
$scope.$watch('selectedResource', function() {
if (!angular.equals($scope.selectedResource, copy)) {
$scope.resourceScopes = [];
if (resources.length > 0) {
resources[0].text = resources[0].name;
$scope.selectedResource = resources[0];
if (ResourceServerScope != null) {
ResourceServerResource.scopes({
realm: $route.current.params.realm,
client: client.id,
rsrid: resources[0]._id
}, function (data) {
$scope.resourceScopes = data;
});
}
} else {
$scope.selectedResource = null;
}
});
$scope.applyToResourceTypeFlag = false;
} else {
$scope.selectedResource = null;
$scope.resourceScopes = [];
$scope.applyToResourceTypeFlag = true;
}
var copy = angular.copy($scope.selectedResource);
$scope.$watch('selectedResource', function() {
if (!angular.equals($scope.selectedResource, copy)) {
$scope.changed = true;
}
}, true);
if (ResourceServerScope != null) {
ResourceServerPermission.scopes({
realm : $route.current.params.realm,
client : client.id,
id : policy.id
}, function(scopes) {
$scope.selectedScopes = [];
for (i = 0; i < scopes.length; i++) {
scopes[i].text = scopes[i].name;
$scope.selectedScopes.push(scopes[i]);
}
var copy = angular.copy($scope.selectedScopes);
$scope.$watch('selectedScopes', function() {
if (!angular.equals($scope.selectedScopes, copy)) {
$scope.changed = true;
}
}, true);
});
} else {
$scope.applyToResourceTypeFlag = true;
}
ResourceServerPermission.associatedPolicies({
@ -1020,363 +1140,21 @@ module.controller('ResourceServerPolicyResourceDetailCtrl', function($scope, $ro
},
onUpdate : function() {
if ($scope.selectedResource && $scope.selectedResource._id) {
$scope.policy.resources = [];
$scope.policy.resources.push($scope.selectedResource._id);
} else {
$scope.policy.resources = [];
}
var policies = [];
for (i = 0; i < $scope.selectedPolicies.length; i++) {
policies.push($scope.selectedPolicies[i].id);
}
$scope.policy.policies = policies;
delete $scope.policy.config;
},
onInitCreate : function(newPolicy) {
policyViewState.state.previousPage.name = 'authz-add-resource-permission';
$scope.selectedResource = null;
var copy = angular.copy($scope.selectedResource);
$scope.$watch('selectedResource', function() {
if (!angular.equals($scope.selectedResource, copy)) {
$scope.changed = true;
}
}, true);
$scope.selectedPolicies = null;
var copy = angular.copy($scope.selectedPolicies);
$scope.$watch('selectedPolicies', function() {
if (!angular.equals($scope.selectedPolicies, copy)) {
$scope.changed = true;
}
}, true);
var resourceId = $location.search()['rsrid'];
if (resourceId) {
ResourceServerResource.get({
realm : $route.current.params.realm,
client : client.id,
rsrid : resourceId
}, function(data) {
data.text = data.name;
$scope.selectedResource = data;
});
}
},
onCreate : function() {
if ($scope.selectedResource && $scope.selectedResource._id) {
$scope.policy.resources = [];
$scope.policy.resources.push($scope.selectedResource._id);
} else {
delete $scope.policy.resources
}
var policies = [];
if ($scope.selectedPolicies) {
for (i = 0; i < $scope.selectedPolicies.length; i++) {
policies.push($scope.selectedPolicies[i].id);
}
}
$scope.policy.policies = policies;
delete $scope.policy.config;
},
onSaveState : function(policy) {
policyViewState.state.selectedResource = $scope.selectedResource;
policyViewState.state.applyToResourceTypeFlag = $scope.applyToResourceTypeFlag;
},
onRestoreState : function(policy) {
$scope.selectedResource = policyViewState.state.selectedResource;
$scope.applyToResourceTypeFlag = policyViewState.state.applyToResourceTypeFlag;
policy.resourceType = policyViewState.state.policy.resourceType;
}
}, realm, client, $scope);
});
module.controller('ResourceServerPolicyScopeDetailCtrl', function($scope, $route, $location, realm, client, PolicyController, ResourceServerPolicy, ResourceServerResource, ResourceServerScope, policyViewState) {
PolicyController.onInit({
getPolicyType : function() {
return "scope";
},
isPermission : function() {
return true;
},
onInit : function() {
$scope.scopesUiSelect = {
minimumInputLength: 1,
delay: 500,
allowClear: true,
query: function (query) {
var data = {results: []};
if ('' == query.term.trim()) {
query.callback(data);
return;
}
$scope.query = {
realm: realm.realm,
client : client.id,
name: query.term.trim(),
deep: false,
max : 20,
first : 0
};
ResourceServerScope.query($scope.query, function(response) {
data.results = response;
query.callback(data);
});
},
formatResult: function(object, container, query) {
object.text = object.name;
return object.name;
}
};
$scope.resourcesUiSelect = {
minimumInputLength: 1,
delay: 500,
allowClear: true,
id: function(resource){ return resource._id; },
query: function (query) {
var data = {results: []};
if ('' == query.term.trim()) {
query.callback(data);
return;
}
$scope.query = {
realm: realm.realm,
client : client.id,
name: query.term.trim(),
deep: false,
max : 20,
first : 0
};
ResourceServerResource.query($scope.query, function(response) {
data.results = response;
query.callback(data);
});
},
formatResult: function(object, container, query) {
object.text = object.name;
return object.name;
}
};
$scope.policiesUiSelect = {
minimumInputLength: 1,
delay: 500,
allowClear: true,
query: function (query) {
var data = {results: []};
if ('' == query.term.trim()) {
query.callback(data);
return;
}
$scope.query = {
realm: realm.realm,
client : client.id,
permission: false,
name: query.term.trim(),
max : 20,
first : 0
};
ResourceServerPolicy.query($scope.query, function(response) {
data.results = response;
query.callback(data);
});
},
formatResult: function(object, container, query) {
object.text = object.name;
return object.name;
}
};
$scope.selectResource = function() {
$scope.selectedScopes = null;
if ($scope.selectedResource) {
ResourceServerResource.scopes({
realm: $route.current.params.realm,
client: client.id,
rsrid: $scope.selectedResource._id
}, function (data) {
$scope.resourceScopes = data;
});
}
}
$scope.applyToResourceType = function() {
if ($scope.applyToResourceTypeFlag) {
$scope.selectedResource = null;
} else {
$scope.policy.resourceType = null;
}
$scope.selectedScopes = [];
$scope.changed = true;
}
},
onInitUpdate : function(policy) {
if (!policy.resourceType) {
ResourceServerPolicy.resources({
realm : $route.current.params.realm,
client : client.id,
id : policy.id
}, function(resources) {
if (resources.length > 0) {
for (i = 0; i < resources.length; i++) {
ResourceServerResource.get({
realm: $route.current.params.realm,
client: client.id,
rsrid: resources[0]._id,
}, function (resource) {
ResourceServerResource.query({
realm: $route.current.params.realm,
client: client.id,
_id: resource._id,
deep: false
}, function (resource) {
resource[0].text = resource[0].name;
$scope.selectedResource = resource[0];
var copy = angular.copy($scope.selectedResource);
$scope.$watch('selectedResource', function() {
if (!angular.equals($scope.selectedResource, copy)) {
$scope.changed = true;
}
}, true);
ResourceServerResource.scopes({
realm: $route.current.params.realm,
client: client.id,
rsrid: resource[0]._id
}, function (scopes) {
$scope.resourceScopes = scopes;
});
});
});
}
ResourceServerPolicy.scopes({
realm : $route.current.params.realm,
client : client.id,
id : policy.id
}, function(scopes) {
$scope.selectedScopes = [];
for (i = 0; i < scopes.length; i++) {
scopes[i].text = scopes[i].name;
$scope.selectedScopes.push(scopes[i].id);
}
var copy = angular.copy($scope.selectedScopes);
$scope.$watch('selectedScopes', function() {
if (!angular.equals($scope.selectedScopes, copy)) {
$scope.changed = true;
}
}, true);
});
} else {
$scope.selectedResource = null;
var copy = angular.copy($scope.selectedResource);
$scope.$watch('selectedResource', function() {
if (!angular.equals($scope.selectedResource, copy)) {
$scope.changed = true;
}
}, true);
ResourceServerPolicy.scopes({
realm : $route.current.params.realm,
client : client.id,
id : policy.id
}, function(scopes) {
$scope.selectedScopes = [];
for (i = 0; i < scopes.length; i++) {
scopes[i].text = scopes[i].name;
$scope.selectedScopes.push(scopes[i]);
}
var copy = angular.copy($scope.selectedScopes);
$scope.$watch('selectedScopes', function() {
if (!angular.equals($scope.selectedScopes, copy)) {
$scope.changed = true;
}
}, true);
});
}
});
} else {
$scope.selectedResource = null;
var copy = angular.copy($scope.selectedResource);
$scope.$watch('selectedResource', function() {
if (!angular.equals($scope.selectedResource, copy)) {
$scope.changed = true;
}
}, true);
ResourceServerPolicy.scopes({
realm : $route.current.params.realm,
client : client.id,
id : policy.id
}, function(scopes) {
$scope.selectedScopes = [];
for (i = 0; i < scopes.length; i++) {
scopes[i].text = scopes[i].name;
$scope.selectedScopes.push(scopes[i]);
}
var copy = angular.copy($scope.selectedScopes);
$scope.$watch('selectedScopes', function() {
if (!angular.equals($scope.selectedScopes, copy)) {
$scope.changed = true;
}
}, true);
});
$scope.applyToResourceTypeFlag = true;
}
ResourceServerPolicy.associatedPolicies({
realm : $route.current.params.realm,
client : client.id,
id : policy.id
}, function(policies) {
$scope.selectedPolicies = [];
for (i = 0; i < policies.length; i++) {
policies[i].text = policies[i].name;
$scope.selectedPolicies.push(policies[i]);
}
var copy = angular.copy($scope.selectedPolicies);
$scope.$watch('selectedPolicies', function() {
if (!angular.equals($scope.selectedPolicies, copy)) {
$scope.changed = true;
}
}, true);
});
},
onUpdate : function() {
if ($scope.selectedResource != null) {
if ($scope.selectedResource != null && $scope.selectedResource._id) {
$scope.policy.resources = [$scope.selectedResource._id];
} else {
$scope.policy.resources = [];
}
var scopes = [];
for (i = 0; i < $scope.selectedScopes.length; i++) {
if ($scope.selectedScopes[i].id) {
if (ResourceServerScope != null) {
var scopes = [];
for (i = 0; i < $scope.selectedScopes.length; i++) {
scopes.push($scope.selectedScopes[i].id);
} else {
scopes.push($scope.selectedScopes[i]);
}
$scope.policy.scopes = scopes;
}
$scope.policy.scopes = scopes;
var policies = [];
if ($scope.selectedPolicies) {
for (i = 0; i < $scope.selectedPolicies.length; i++) {
policies.push($scope.selectedPolicies[i].id);
@ -1388,43 +1166,27 @@ module.controller('ResourceServerPolicyScopeDetailCtrl', function($scope, $route
},
onInitCreate : function(newPolicy) {
policyViewState.state.previousPage.name = 'authz-add-scope-permission';
var scopeId = $location.search()['scpid'];
if (scopeId) {
ResourceServerScope.get({
realm: $route.current.params.realm,
client: client.id,
id: scopeId,
}, function (data) {
data.text = data.name;
if (!$scope.policy.scopes) {
$scope.selectedScopes = [];
}
$scope.selectedScopes.push(data);
});
if (ResourceServerScope == null) {
policyViewState.state.previousPage.name = 'authz-add-resource-permission';
} else {
policyViewState.state.previousPage.name = 'authz-add-scope-permission';
}
},
onCreate : function() {
if ($scope.selectedResource != null) {
if ($scope.selectedResource != null && $scope.selectedResource._id) {
$scope.policy.resources = [$scope.selectedResource._id];
}
var scopes = [];
for (i = 0; i < $scope.selectedScopes.length; i++) {
if ($scope.selectedScopes[i].id) {
if (ResourceServerScope != null) {
var scopes = [];
for (i = 0; i < $scope.selectedScopes.length; i++) {
scopes.push($scope.selectedScopes[i].id);
} else {
scopes.push($scope.selectedScopes[i]);
}
$scope.policy.scopes = scopes;
}
$scope.policy.scopes = scopes;
var policies = [];
if ($scope.selectedPolicies) {
for (i = 0; i < $scope.selectedPolicies.length; i++) {
policies.push($scope.selectedPolicies[i].id);
@ -1436,21 +1198,28 @@ module.controller('ResourceServerPolicyScopeDetailCtrl', function($scope, $route
},
onSaveState : function(policy) {
policyViewState.state.selectedScopes = $scope.selectedScopes;
if (ResourceServerScope != null) {
policyViewState.state.selectedScopes = $scope.selectedScopes;
policyViewState.state.resourceScopes = $scope.resourceScopes;
}
policyViewState.state.selectedResource = $scope.selectedResource;
policyViewState.state.resourceScopes = $scope.resourceScopes;
policyViewState.state.applyToResourceTypeFlag = $scope.applyToResourceTypeFlag;
},
onRestoreState : function(policy) {
$scope.selectedScopes = policyViewState.state.selectedScopes;
if (ResourceServerScope != null) {
$scope.selectedScopes = policyViewState.state.selectedScopes;
$scope.resourceScopes = policyViewState.state.resourceScopes;
}
$scope.selectedResource = policyViewState.state.selectedResource;
$scope.resourceScopes = policyViewState.state.resourceScopes;
$scope.applyToResourceTypeFlag = policyViewState.state.applyToResourceTypeFlag;
policy.resourceType = policyViewState.state.policy.resourceType;
}
}, realm, client, $scope);
});
};
module.controller('ResourceServerPolicyUserDetailCtrl', function($scope, $route, realm, client, PolicyController, User) {
PolicyController.onInit({

View file

@ -50,24 +50,13 @@
<kc-tooltip>{{:: 'authz-permission-resource-type.tooltip' | translate}}</kc-tooltip>
</div>
<div class="form-group clearfix" data-ng-show="selectedResource">
<div class="form-group clearfix">
<label class="col-md-2 control-label" for="resourceScopes">{{:: 'authz-scopes' | translate}} <span class="required">*</span></label>
<div class="col-md-6">
<select ui-select2 id="resourceScopes"
data-ng-model="selectedScopes"
data-placeholder="{{:: 'authz-any-scope' | translate}}..." multiple
data-ng-required="selectedResource != null">
<option ng-repeat="scope in resourceScopes" value="{{scope.id}}">{{scope.name}}</option>
</select>
</div>
<kc-tooltip>{{:: 'authz-permission-scope-scope.tooltip' | translate}}</kc-tooltip>
</div>
<div class="form-group clearfix" data-ng-show="!selectedResource">
<label class="col-md-2 control-label" for="scopes">{{:: 'authz-scopes' | translate}} <span class="required">*</span></label>
<div class="col-md-6">
<input type="hidden" ui-select2="scopesUiSelect" id="scopes" data-ng-model="selectedScopes" data-placeholder="{{:: 'authz-any-scope' | translate}}..." multiple data-ng-required="selectedResource == null" />
<input type="hidden" ui-select2="scopesUiSelect" id="scopes" data-ng-model="selectedScopes"
data-placeholder="{{:: 'authz-any-scope' | translate}}..." multiple data-ng-required="true" />
</div>
<!-- type="hidden" -->
<kc-tooltip>{{:: 'authz-permission-scope-scope.tooltip' | translate}}</kc-tooltip>
</div>
<div class="form-group clearfix">