Skip to content

Commit

Permalink
[KEYCLOAK-5806] - Adding table of selected policies
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroigor committed Dec 16, 2017
1 parent 3ee760e commit e211885
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
package org.keycloak.testsuite.console.page.clients.authorization.policy;

import static org.keycloak.testsuite.util.UIUtils.performOperationWithPageReload;
import static org.openqa.selenium.By.tagName;

import java.util.List;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.jboss.arquillian.graphene.page.Page;
import org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation;
Expand All @@ -34,6 +39,7 @@
import org.keycloak.testsuite.console.page.fragment.ModalDialog;
import org.keycloak.testsuite.console.page.fragment.MultipleStringSelect2;
import org.keycloak.testsuite.page.Form;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.Select;
Expand All @@ -56,14 +62,11 @@ public class AggregatePolicyForm extends Form {
private WebElement deleteButton;

@FindBy(id = "s2id_policies")
private MultipleStringSelect2 policySelect;
private PolicySelect policySelect;

@FindBy(xpath = "//div[@class='modal-dialog']")
protected ModalDialog modalDialog;

@FindBy(id = "create-policy-btn")
private WebElement createPolicyBtn;

@FindBy(id = "create-policy")
private Select createPolicySelect;

Expand Down Expand Up @@ -141,7 +144,6 @@ public AggregatePolicyRepresentation toRepresentation() {
}

public void createPolicy(AbstractPolicyRepresentation expected) {
createPolicyBtn.click();
performOperationWithPageReload(() -> createPolicySelect.selectByValue(expected.getType()));

if ("role".equals(expected.getType())) {
Expand All @@ -160,4 +162,35 @@ public void createPolicy(AbstractPolicyRepresentation expected) {
groupPolicy.form().populate((GroupPolicyRepresentation) expected, true);
}
}

public class PolicySelect extends MultipleStringSelect2 {

@Override
protected List<WebElement> getSelectedElements() {
return getRoot().findElements(By.xpath("(//table[@id='selected-policies'])/tbody/tr")).stream()
.filter(webElement -> webElement.findElements(tagName("td")).size() > 1)
.collect(Collectors.toList());
}

@Override
protected BiFunction<WebElement, String, Boolean> deselect() {
return (webElement, name) -> {
List<WebElement> tds = webElement.findElements(tagName("td"));

if (!tds.get(0).getText().isEmpty()) {
if (tds.get(0).getText().equals(name)) {
tds.get(2).click();
return true;
}
}

return false;
};
}

@Override
protected Function<WebElement, String> representation() {
return webElement -> webElement.findElements(tagName("td")).get(0).getText();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,7 @@ authz-resources=Resources
authz-scope=Scope
authz-authz-scopes=Authorization Scopes
authz-policies=Policies
authz-policy=Policy
authz-permissions=Permissions
authz-users=Users in Role
authz-evaluate=Evaluate
Expand Down Expand Up @@ -1189,7 +1190,8 @@ authz-policy-decision-strategy.tooltip=The decision strategy dictates how the po
authz-policy-decision-strategy-affirmative=Affirmative
authz-policy-decision-strategy-unanimous=Unanimous
authz-policy-decision-strategy-consensus=Consensus
authz-select-a-policy=Select a policy
authz-select-a-policy=Select existing policy
authz-no-policies-assigned=No policies assigned.

# Authz Role Policy Detail
authz-add-role-policy=Add Role Policy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,48 @@ module.service("PolicyController", function($http, $route, $location, ResourceSe
$location.url("/realms/" + realm.realm + "/clients/" + client.id + "/authz/resource-server/policy/" + policyType.type + "/create?new_policy=true");
}

$scope.detailPolicy = function(policy) {
policyState.state = $scope.policy;
if ($scope.selectedPolicies) {
policyState.state.selectedPolicies = $scope.selectedPolicies;
}
var previousUrl = window.location.href.substring(window.location.href.indexOf('/realms'));

if (previousUrl.indexOf('back=true') == -1) {
previousUrl = previousUrl + '?back=true';
}
policyState.state.previousUrl = previousUrl;
$location.url("/realms/" + realm.realm + "/clients/" + client.id + "/authz/resource-server/policy/" + policy.type + "/" + policy.id + "?new_policy=true");
}

$scope.removePolicy = function(list, policy) {
for (i = 0; i < angular.copy(list).length; i++) {
if (policy.id == list[i].id) {
list.splice(i, 1);
}
}
}

$scope.selectPolicy = function(policy) {
if (!policy || !policy.id) {
return;
}

if (!$scope.selectedPolicies) {
$scope.selectedPolicies = [];
}

$scope.selectedPolicy = null;

for (i = 0; i < $scope.selectedPolicies.length; i++) {
if ($scope.selectedPolicies[i].id == policy.id) {
return;
}
}

$scope.selectedPolicies.push(policy);
}

$scope.createNewPolicy = function() {
$scope.showNewPolicy = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}">{{client.clientId}}</a></li>
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}/authz/resource-server">{{:: 'authz-authorization' | translate}}</a></li>
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}/authz/resource-server/policy">{{:: 'authz-policies' | translate}}</a></li>
<li data-ng-show="create && policyState.state.name != null">{{policyState.state.name}}</li>
<li data-ng-show="create && policyState.state.name == null">{{policyState.previousPage.name}}</li>
<li data-ng-show="create && policyState.state.name != null && historyBackOnSaveOrCancel">{{policyState.state.name}}</li>
<li data-ng-show="create && policyState.state.name == null && historyBackOnSaveOrCancel">{{policyState.previousPage.name}}</li>
<li data-ng-show="create">{{:: 'authz-add-aggregated-policy' | translate}}</li>
<li data-ng-hide="create">{{:: 'authz-aggregated' | translate}}</li>
<li data-ng-hide="create">{{originalPolicy.name}}</li>
Expand Down Expand Up @@ -35,32 +35,49 @@ <h1 data-ng-hide="create">{{originalPolicy.name|capitalize}}<i class="pficon pfi
<div class="form-group clearfix">
<label class="col-md-2 control-label" for="policies">{{:: 'authz-policy-apply-policy' | translate}} <span class="required">*</span></label>
<div class="col-sm-6">
<input type="hidden" ui-select2="policiesUiSelect" id="policies" data-ng-model="selectedPolicies" data-placeholder="{{:: 'authz-select-a-policy' | translate}}..." multiple data-ng-required="!selectedPolicies || selectedPolicies.length == 0" />
<input type="hidden" ui-select2="policiesUiSelect" id="policies" data-ng-change="selectPolicy(selectedPolicy);" data-ng-model="selectedPolicy" data-placeholder="{{:: 'authz-select-a-policy' | translate}}..." data-ng-required="!selectedPolicies || selectedPolicies.length == 0"/>
<p/>
<table class="table table-striped table-bordered" id="selected-policies">
<thead>
<tr>
<th class="kc-table-actions" colspan="3">
<div class="form-inline">
<div class="form-group">
</div>
<div class="pull-right">
<select id="create-policy" class="form-control" ng-model="policyType"
ng-options="p.name for p in policyProviders track by p.type"
data-ng-change="addPolicy(policyType);">
<option value="" disabled selected>{{:: 'authz-create-policy' | translate}}...</option>
</select>
</div>
</div>
</th>
</tr>
<tr data-ng-hide="!selectedPolicies || selectedPolicies.length == 0">
<th>{{:: 'name' | translate}}</th>
<th>{{:: 'description' | translate}}</th>
<th>{{:: 'actions' | translate}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="policy in selectedPolicies">
<td><a href="" data-ng-click="detailPolicy(policy)">{{policy.name}}</a></td>
<td>{{policy.description}}</td>
<td class="kc-action-cell" ng-click="removePolicy(selectedPolicies, policy);">
{{:: 'remove' | translate}}
</td>
</tr>
<tr data-ng-show="!selectedPolicies || selectedPolicies.length == 0">
<td class="text-muted" colspan="3">{{:: 'authz-no-policies-assigned' | translate}}</td>
</tr>
</tbody>
</table>
</div>
<kc-tooltip>{{:: 'authz-policy-apply-policy.tooltip' | translate}}</kc-tooltip>
</div>
<div class="form-group clearfix" data-ng-hide="historyBackOnSaveOrCancel">
<label class="col-md-2 control-label" for="policies"></label>
<div class="col-sm-6">
<div class="input-group">
<div class="input-group-btn" data-ng-hide="showNewPolicy == true">
<button data-ng-click="createNewPolicy()" id="create-policy-btn" class="btn btn-primary">{{:: 'authz-create-policy' | translate}}</button>
</div>
<select id="create-policy" ng-model="policyType" class="form-control"
ng-options="p.name for p in policyProviders track by p.type"
data-ng-change="addPolicy(policyType);"
data-ng-show="showNewPolicy == true">
<option value="" disabled selected>{{:: 'authz-create-policy' | translate}}...</option>
</select>
<div class="input-group-btn" data-ng-show="showNewPolicy == true">
<button data-ng-click="cancelCreateNewPolicy()" id="cancel-create-policy-btn" class="btn btn-primary">{{:: 'cancel' | translate}}</button>
</div>
</div>
</div>
</div>
<div class="form-group clearfix">
<label class="col-md-2 control-label" for="policy.decisionStrategy">{{:: 'authz-policy-decision-strategy' | translate}}</label>

<div class="col-sm-2">
<select class="form-control" id="policy.decisionStrategy"
data-ng-model="policy.decisionStrategy"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}">{{client.clientId}}</a></li>
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}/authz/resource-server">{{:: 'authz-authorization' | translate}}</a></li>
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}/authz/resource-server/policy">{{:: 'authz-policies' | translate}}</a></li>
<li data-ng-show="create && policyState.state.name != null">{{policyState.state.name}}</li>
<li data-ng-show="create && policyState.state.name == null">{{policyState.previousPage.name}}</li>
<li data-ng-show="create && policyState.state.name != null && historyBackOnSaveOrCancel">{{policyState.state.name}}</li>
<li data-ng-show="create && policyState.state.name == null && historyBackOnSaveOrCancel">{{policyState.previousPage.name}}</li>
<li data-ng-show="create">{{:: 'authz-add-client-policy' | translate}}</li>
<li data-ng-hide="create">{{:: 'client' | translate}}</li>
<li data-ng-hide="create">{{originalPolicy.name}}</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}">{{client.clientId}}</a></li>
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}/authz/resource-server">{{:: 'authz-authorization' | translate}}</a></li>
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}/authz/resource-server/policy">{{:: 'authz-policies' | translate}}</a></li>
<li data-ng-show="create && policyState.state.name != null">{{policyState.state.name}}</li>
<li data-ng-show="create && policyState.state.name == null">{{policyState.previousPage.name}}</li>
<li data-ng-show="create && policyState.state.name != null && historyBackOnSaveOrCancel">{{policyState.state.name}}</li>
<li data-ng-show="create && policyState.state.name == null && historyBackOnSaveOrCancel">{{policyState.previousPage.name}}</li>
<li data-ng-show="create">{{:: 'authz-add-drools-policy' | translate}}</li>
<li data-ng-hide="create">Rules</li>
<li data-ng-hide="create">{{originalPolicy.name}}</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}">{{client.clientId}}</a></li>
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}/authz/resource-server">{{:: 'authz-authorization' | translate}}</a></li>
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}/authz/resource-server/policy">{{:: 'authz-policies' | translate}}</a></li>
<li data-ng-show="create && policyState.state.name != null">{{policyState.state.name}}</li>
<li data-ng-show="create && policyState.state.name == null">{{policyState.previousPage.name}}</li>
<li data-ng-show="create && policyState.state.name != null && historyBackOnSaveOrCancel">{{policyState.state.name}}</li>
<li data-ng-show="create && policyState.state.name == null && historyBackOnSaveOrCancel">{{policyState.previousPage.name}}</li>
<li data-ng-show="create">{{:: 'authz-add-group-policy' | translate}}</li>
<li data-ng-hide="create">{{:: 'groups' | translate}}</li>
<li data-ng-hide="create">{{originalPolicy.name}}</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}">{{client.clientId}}</a></li>
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}/authz/resource-server">{{:: 'authz-authorization' | translate}}</a></li>
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}/authz/resource-server/policy">{{:: 'authz-policies' | translate}}</a></li>
<li data-ng-show="create && policyState.state.name != null">{{policyState.state.name}}</li>
<li data-ng-show="create && policyState.state.name == null">{{policyState.previousPage.name}}</li>
<li data-ng-show="create && policyState.state.name != null && historyBackOnSaveOrCancel">{{policyState.state.name}}</li>
<li data-ng-show="create && policyState.state.name == null && historyBackOnSaveOrCancel">{{policyState.previousPage.name}}</li>
<li data-ng-show="create">{{:: 'authz-add-js-policy' | translate}}</li>
<li data-ng-hide="create">JavaScript</li>
<li data-ng-hide="create">{{originalPolicy.name}}</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}">{{client.clientId}}</a></li>
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}/authz/resource-server">{{:: 'authz-authorization' | translate}}</a></li>
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}/authz/resource-server/policy">{{:: 'authz-policies' | translate}}</a></li>
<li data-ng-show="create && policyState.state.name != null">{{policyState.state.name}}</li>
<li data-ng-show="create && policyState.state.name == null">{{:: policyState.previousPage.name | translate}}</li>
<li data-ng-show="create && policyState.state.name != null && historyBackOnSaveOrCancel">{{policyState.state.name}}</li>
<li data-ng-show="create && policyState.state.name == null && historyBackOnSaveOrCancel">{{:: policyState.previousPage.name | translate}}</li>
<li data-ng-show="create">{{:: 'authz-add-role-policy' | translate}}</li>
<li data-ng-hide="create">{{:: 'roles' | translate}}</li>
<li data-ng-hide="create">{{originalPolicy.name}}</li>
Expand Down Expand Up @@ -159,9 +159,10 @@ <h1 data-ng-hide="create">{{originalPolicy.name|capitalize}}<i class="pficon pfi
<div class="form-group" data-ng-show="access.manageAuthorization">
<div class="col-md-10 col-md-offset-2">
<button kc-save data-ng-disabled="!changed">{{:: 'save' | translate}}</button>
<button kc-reset data-ng-disabled="!changed">{{:: 'cancel' | translate}}</button>
<button kc-reset data-ng-disabled="!changed && !historyBackOnSaveOrCancel">{{:: 'cancel' | translate}}</button>
</div>
</div>
{{policyState.page.previous}}
</form>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}">{{client.clientId}}</a></li>
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}/authz/resource-server">{{:: 'authz-authorization' | translate}}</a></li>
<li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}/authz/resource-server/policy">{{:: 'authz-policies' | translate}}</a></li>
<li data-ng-show="create && policyState.state.name != null">{{policyState.state.name}}</li>
<li data-ng-show="create && policyState.state.name == null">{{policyState.previousPage.name}}</li>
<li data-ng-show="create && policyState.state.name != null && historyBackOnSaveOrCancel">{{policyState.state.name}}</li>
<li data-ng-show="create && policyState.state.name == null && historyBackOnSaveOrCancel">{{policyState.previousPage.name}}</li>
<li data-ng-show="create">{{:: 'authz-add-time-policy' | translate}}</li>
<li data-ng-hide="create">{{:: 'time' | translate}}</li>
<li data-ng-hide="create">{{originalPolicy.name}}</li>
Expand Down
Loading

0 comments on commit e211885

Please sign in to comment.