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) { 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({ PolicyController.onInit({
getPolicyType : function() { getPolicyType : function() {
return "resource"; if (ResourceServerScope == null) {
return "resource";
} else {
return "scope";
}
}, },
isPermission : function() { isPermission : function() {
@ -911,6 +927,49 @@ module.controller('ResourceServerPolicyResourceDetailCtrl', function($scope, $ro
}, },
onInit : function() { 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 = { $scope.resourcesUiSelect = {
minimumInputLength: 1, minimumInputLength: 1,
delay: 500, 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() { $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; $scope.selectedResource = null;
} else { } else {
$scope.policy.resourceType = null; $scope.policy.resourceType = null;
} }
if (ResourceServerScope != null) {
$scope.selectedScopes = [];
$scope.changed = true;
}
} }
}, },
onInitUpdate : function(policy) { onInitUpdate : function(policy) {
if (!policy.resourceType) { if (!policy.resourceType) {
$scope.selectedResource = {};
ResourceServerPermission.resources({ ResourceServerPermission.resources({
realm: $route.current.params.realm, realm: $route.current.params.realm,
client: client.id, client: client.id,
id: policy.id id: policy.id
}, function (resources) { }, function (resources) {
resources[0].text = resources[0].name; $scope.resourceScopes = [];
$scope.selectedResource = resources[0]; if (resources.length > 0) {
var copy = angular.copy($scope.selectedResource); resources[0].text = resources[0].name;
$scope.$watch('selectedResource', function() { $scope.selectedResource = resources[0];
if (!angular.equals($scope.selectedResource, copy)) { 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; $scope.changed = true;
} }
}, true); }, true);
}); });
} else {
$scope.applyToResourceTypeFlag = true;
} }
ResourceServerPermission.associatedPolicies({ ResourceServerPermission.associatedPolicies({
@ -1020,363 +1140,21 @@ module.controller('ResourceServerPolicyResourceDetailCtrl', function($scope, $ro
}, },
onUpdate : function() { onUpdate : function() {
if ($scope.selectedResource && $scope.selectedResource._id) { if ($scope.selectedResource != null && $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) {
$scope.policy.resources = [$scope.selectedResource._id]; $scope.policy.resources = [$scope.selectedResource._id];
} else { } else {
$scope.policy.resources = []; $scope.policy.resources = [];
} }
var scopes = []; if (ResourceServerScope != null) {
var scopes = [];
for (i = 0; i < $scope.selectedScopes.length; i++) { for (i = 0; i < $scope.selectedScopes.length; i++) {
if ($scope.selectedScopes[i].id) {
scopes.push($scope.selectedScopes[i].id); scopes.push($scope.selectedScopes[i].id);
} else {
scopes.push($scope.selectedScopes[i]);
} }
$scope.policy.scopes = scopes;
} }
$scope.policy.scopes = scopes;
var policies = []; var policies = [];
if ($scope.selectedPolicies) { if ($scope.selectedPolicies) {
for (i = 0; i < $scope.selectedPolicies.length; i++) { for (i = 0; i < $scope.selectedPolicies.length; i++) {
policies.push($scope.selectedPolicies[i].id); policies.push($scope.selectedPolicies[i].id);
@ -1388,43 +1166,27 @@ module.controller('ResourceServerPolicyScopeDetailCtrl', function($scope, $route
}, },
onInitCreate : function(newPolicy) { onInitCreate : function(newPolicy) {
policyViewState.state.previousPage.name = 'authz-add-scope-permission'; if (ResourceServerScope == null) {
var scopeId = $location.search()['scpid']; policyViewState.state.previousPage.name = 'authz-add-resource-permission';
} else {
if (scopeId) { policyViewState.state.previousPage.name = 'authz-add-scope-permission';
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);
});
} }
}, },
onCreate : function() { onCreate : function() {
if ($scope.selectedResource != null) { if ($scope.selectedResource != null && $scope.selectedResource._id) {
$scope.policy.resources = [$scope.selectedResource._id]; $scope.policy.resources = [$scope.selectedResource._id];
} }
var scopes = []; if (ResourceServerScope != null) {
var scopes = [];
for (i = 0; i < $scope.selectedScopes.length; i++) { for (i = 0; i < $scope.selectedScopes.length; i++) {
if ($scope.selectedScopes[i].id) {
scopes.push($scope.selectedScopes[i].id); scopes.push($scope.selectedScopes[i].id);
} else {
scopes.push($scope.selectedScopes[i]);
} }
$scope.policy.scopes = scopes;
} }
$scope.policy.scopes = scopes;
var policies = []; var policies = [];
if ($scope.selectedPolicies) { if ($scope.selectedPolicies) {
for (i = 0; i < $scope.selectedPolicies.length; i++) { for (i = 0; i < $scope.selectedPolicies.length; i++) {
policies.push($scope.selectedPolicies[i].id); policies.push($scope.selectedPolicies[i].id);
@ -1436,21 +1198,28 @@ module.controller('ResourceServerPolicyScopeDetailCtrl', function($scope, $route
}, },
onSaveState : function(policy) { 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.selectedResource = $scope.selectedResource;
policyViewState.state.resourceScopes = $scope.resourceScopes;
policyViewState.state.applyToResourceTypeFlag = $scope.applyToResourceTypeFlag; policyViewState.state.applyToResourceTypeFlag = $scope.applyToResourceTypeFlag;
}, },
onRestoreState : function(policy) { 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.selectedResource = policyViewState.state.selectedResource;
$scope.resourceScopes = policyViewState.state.resourceScopes;
$scope.applyToResourceTypeFlag = policyViewState.state.applyToResourceTypeFlag; $scope.applyToResourceTypeFlag = policyViewState.state.applyToResourceTypeFlag;
policy.resourceType = policyViewState.state.policy.resourceType; policy.resourceType = policyViewState.state.policy.resourceType;
} }
}, realm, client, $scope); }, realm, client, $scope);
});
};
module.controller('ResourceServerPolicyUserDetailCtrl', function($scope, $route, realm, client, PolicyController, User) { module.controller('ResourceServerPolicyUserDetailCtrl', function($scope, $route, realm, client, PolicyController, User) {
PolicyController.onInit({ PolicyController.onInit({

View file

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