FFFF Add organization roles by ggomarighetti · Pull Request #50606 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public UmaPermissionRepresentation toRepresentation(Policy policy, Authorization

if (role.isClientRole()) {
representation.addClientRole(ClientModel.class.cast(role.getContainer()).getClientId(),role.getName());
} else {
} else if (role.isType(RoleModel.Type.REALM)) {
representation.addRole(role.getName());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ private boolean isGranted(RealmModel realm, AuthorizationProvider authorizationP
RoleModel role = realm.getRoleById(roleDefinition.getId());

if (role != null) {
if (role.isType(RoleModel.Type.ORGANIZATION)) {
if (Boolean.TRUE.equals(roleDefinition.isRequired())) {
return false;
}
continue;
}

boolean isFetchRoles = policyRep.isFetchRoles() != null && policyRep.isFetchRoles();
boolean hasRole = hasRole(identity, role, realm, authorizationProvider, isFetchRoles);

Expand All @@ -103,11 +110,14 @@ private boolean hasRole(Identity identity, RoleModel role, RealmModel realm, Aut
return subject != null && subject.hasRole(role);
}
String roleName = role.getName();
if (role.isClientRole()) {
ClientModel clientModel = realm.getClientById(role.getContainerId());
return identity.hasClientRole(clientModel.getClientId(), roleName);
}
return identity.hasRealmRole(roleName);
return switch (role.getType()) {
case CLIENT -> {
ClientModel clientModel = realm.getClientById(role.getContainerId());
yield identity.hasClientRole(clientModel.getClientId(), roleName);
}
case REALM -> identity.hasRealmRole(roleName);
case ORGANIZATION -> false;
Comment thread
pedroigor marked this conversation as resolved.
Resolved
};
Comment on lines +113 to +120
}

private UserModel getSubject(Identity identity, RealmModel realm, AuthorizationProvider authorizationProvider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
public class RolePolicyProviderFactory implements PolicyProviderFactory<RolePolicyRepresentation> {

public static final String ID = "role";
static final String ORGANIZATION_ROLES_NOT_SUPPORTED = "Organization roles cannot be used in role policies";
private RolePolicyProvider provider = new RolePolicyProvider(this::toRepresentation);

@Override
Expand Down Expand Up @@ -96,22 +97,18 @@ public Class<RolePolicyRepresentation> getRepresentationType() {

@Override
public void onCreate(Policy policy, RolePolicyRepresentation representation, AuthorizationProvider authorization) {
updateRoles(policy, representation, authorization);
updatePolicy(policy, representation, authorization);
}

@Override
public void onUpdate(Policy policy, RolePolicyRepresentation representation, AuthorizationProvider authorization) {
updateRoles(policy, representation, authorization);
updatePolicy(policy, representation, authorization);
}

@Override
public void onImport(Policy policy, PolicyRepresentation representation, AuthorizationProvider authorization) {
updateRoles(policy, authorization, getRoles(representation.getConfig().get("roles"), authorization.getRealm()));
String fetchRoles = representation.getConfig().get("fetchRoles");

if (StringUtil.isNotBlank(fetchRoles)) {
policy.putConfig("fetchRoles", fetchRoles);
}
updatePolicy(policy, authorization, getRoles(representation.getConfig().get("roles"), authorization.getRealm()),
representation.getConfig().get("fetchRoles"));
}

@Override
Expand All @@ -124,7 +121,7 @@ public void onExport(Policy policy, PolicyRepresentation representation, Authori

if (role.isClientRole()) {
roleDefinition.setId(ClientModel.class.cast(role.getContainer()).getClientId() + "/" + role.getName());
} else {
} else if (role.isType(RoleModel.Type.REALM)) {
roleDefinition.setId(role.getName());
}
}
Expand All @@ -144,14 +141,13 @@ public void onExport(Policy policy, PolicyRepresentation representation, Authori
representation.setConfig(config);
}

private void updateRoles(Policy policy, RolePolicyRepresentation representation, AuthorizationProvider authorization) {
if (representation.isFetchRoles() != null) {
policy.putConfig("fetchRoles", String.valueOf(representation.isFetchRoles()));
}
updateRoles(policy, authorization, representation.getRoles());
private void updatePolicy(Policy policy, RolePolicyRepresentation representation, AuthorizationProvider authorization) {
updatePolicy(policy, authorization, representation.getRoles(),
representation.isFetchRoles() == null ? null : String.valueOf(representation.isFetchRoles()));
}

private void updateRoles(Policy policy, AuthorizationProvider authorization, Set<RolePolicyRepresentation.RoleDefinition> roles) {
private void updatePolicy(Policy policy, AuthorizationProvider authorization,
Set<RolePolicyRepresentation.RoleDefinition> roles, String fetchRoles) {
Set<RolePolicyRepresentation.RoleDefinition> updatedRoles = new HashSet<>();
Set<String> processedRoles = new HashSet<>();
if (roles != null) {
Expand All @@ -162,19 +158,29 @@ private void updateRoles(Policy policy, AuthorizationProvider authorization, Set
continue;
}

if (role.isType(RoleModel.Type.ORGANIZATION)) {
throw new PolicyValidationException(ORGANIZATION_ROLES_NOT_SUPPORTED);
}

if (!processedRoles.add(role.getId())) {
throw new PolicyValidationException("Role can't be specified multiple times - " + role.getName());
}
definition.setId(role.getId());
updatedRoles.add(definition);
updatedRoles.add(new RoleDefinition(role.getId(), definition.isRequired()));
}
}

Map<String, String> config = new HashMap<>(policy.getConfig());
try {
policy.putConfig("roles", JsonSerialization.writeValueAsString(updatedRoles));
config.put("roles", JsonSerialization.writeValueAsString(updatedRoles));
} catch (IOException cause) {
throw new RuntimeException("Failed to serialize roles", cause);
}

if (StringUtil.isNotBlank(fetchRoles)) {
config.put("fetchRoles", fetchRoles);
}

policy.setConfig(config);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class GroupRepresentation {
protected Map<String, List<String>> attributes;
protected List<String> realmRoles;
protected Map<String, List<String>> clientRoles;
protected Map<String, List<String>> organizationRoles;

private Map<String, Boolean> access;

Expand Down Expand Up @@ -111,6 +112,13 @@ public void setClientRoles(Map<String, List<String>> clientRoles) {
this.clientRoles = clientRoles;
}

public Map<String, List<String>> getOrganizationRoles() {
return organizationRoles;
}

public void setOrganizationRoles(Map<String, List<String>> organizationRoles) {
this.organizationRoles = organizationRoles;
}

public Map<String, List<String>> getAttributes() {
return attributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
public class MappingsRepresentation {
protected List<RoleRepresentation> realmMappings;
protected Map<String, ClientMappingsRepresentation> clientMappings;
protected Map<String, List<RoleRepresentation>> organizationMappings;

public List<RoleRepresentation> getRealmMappings() {
return realmMappings;
Expand All @@ -43,4 +44,12 @@ public Map<String, ClientMappingsRepresentation> getClientMappings() {
public void setClientMappings(Map<String, ClientMappingsRepresentation> clientMappings) {
this.clientMappings = clientMappings;
}

public Map<String, List<RoleRepresentation>> getOrganizationMappings() {
return organizationMappings;
}

public void setOrganizationMappings(Map<String, List<RoleRepresentation>> organizationMappings) {
this.organizationMappings = organizationMappings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@

package org.keycloak.representations.idm;

import java.util.ArrayList;
import java.util.List;

public class MemberRepresentation extends UserRepresentation {

private MembershipType membershipType;
private List<String> organizationRoles;

public MemberRepresentation() {
super();
Expand All @@ -36,4 +40,19 @@ public MembershipType getMembershipType() {
public void setMembershipType(MembershipType membershipType) {
this.membershipType = membershipType;
}

public List<String> getOrganizationRoles() {
return organizationRoles;
}

public void setOrganizationRoles(List<String> organizationRoles) {
this.organizationRoles = organizationRoles;
}

public void addOrganizationRole(String roleName) {
if (organizationRoles == null) {
organizationRoles = new ArrayList<>();
}
organizationRoles.add(roleName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ public class OrganizationRepresentation {
private String redirectUrl;
private Map<String, List<String>> attributes;
private Set<OrganizationDomainRepresentation> domains;
private RoleRepresentation defaultRole;
private List<RoleRepresentation> roles;
private List<MemberRepresentation> members;
private List<IdentityProviderRepresentation> identityProviders;
private List<GroupRepresentation> groups;
private Map<String, Boolean> access;

public String getId() {
return id;
Expand Down Expand Up @@ -129,6 +132,29 @@ public void removeDomain(OrganizationDomainRepresentation domain) {
getDomains().remove(domain);
}

public RoleRepresentation getDefaultRole() {
return defaultRole;
}

public void setDefaultRole(RoleRepresentation defaultRole) {
this.defaultRole = defaultRole;
}

public List<RoleRepresentation> getRoles() {
return roles;
}

public void setRoles(List<RoleRepresentation> roles) {
this.roles = roles;
}

public void addRole(RoleRepresentation role) {
if (roles == null) {
roles = new ArrayList<>();
}
roles.add(role);
}

public List<MemberRepresentation> getMembers() {
return members;
}
Expand Down Expand Up @@ -174,6 +200,14 @@ public void addGroup(GroupRepresentation group) {
groups.add(group);
}

public Map<String, Boolean> getAccess() {
return access;
}

public void setAccess(Map<String, Boolean> access) {
this.access = access;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ public class RoleRepresentation {
private Boolean clientRole;
private String containerId;
protected Map<String, List<String>> attributes;
protected Map<String, Boolean> access;

public static class Composites {
protected Set<String> realm;
protected Map<String, Li 10BC8 st<String>> client;
protected Set<String> organization;
@Deprecated
protected Map<String, List<String>> application;

Expand All @@ -62,6 +64,14 @@ public void setClient(Map<String, List<String>> client) {
this.client = client;
}

public Set<String> getOrganization() {
return organization;
}

public void setOrganization(Set<String> organization) {
this.organization = organization;
}

@Deprecated
public Map<String, List<String>> getApplication() {
return application;
Expand Down Expand Up @@ -151,6 +161,14 @@ public void setAttributes(Map<String, List<String>> attributes) {
this.attributes = attributes;
}

public Map<String, Boolean> getAccess() {
return access;
}

public void setAccess(Map<String, Boolean> access) {
this.access = access;
}

public RoleRepresentation singleAttribute(String name, String value) {
if (attributes == null) {
attributes = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ include::organizations/managing-organization.adoc[leveloffset=+2]
include::organizations/managing-domains.adoc[leveloffset=+2]
include::organizations/managing-attributes.adoc[leveloffset=+2]
include::organizations/managing-members.adoc[leveloffset=+2]
include::organizations/managing-roles.adoc[leveloffset=+2]
include::organizations/managing-groups.adoc[leveloffset=+2]
include::organizations/managing-identity-providers.adoc[leveloffset=+2]
include::organizations/authenticating-members.adoc[leveloffset=+2]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,9 @@ Group attributes are stored separately from user attributes. To include group at

== Assigning roles to groups

Organization groups support realm and client role mappings, just like realm groups. This lets you define role-based access that applies to all members of a group.
Organization groups support realm, client, and organization role mappings. This lets you define access that applies to all members of a group. Organization role mappings are limited to regular roles from the same organization. Every member inherits the default organization role through the hidden internal root group, so the default role cannot be assigned to a visible group.

=== Assigning realm and client roles

.Procedure

Expand All @@ -311,7 +313,19 @@ Roles assigned to organization groups are automatically included in `realm_acces

To also include these roles within the `organization` claim (organized per organization), enable the *Add group role mappings* option on the *Organization Group Membership* mapper. See <<oidc-token-with-roles,Token structure with roles>> and <<saml-assertion-with-roles,SAML assertion structure with roles>> for details.

NOTE: Assigning roles to organization groups requires the `manage-organizations` realm admin role (or equivalent Fine-Grained Admin Permission on the organization) and the `map-role` permission on the specific role being assigned.
=== Assigning organization roles

.Procedure

. Select an organization group.
. Click the *Role mapping* tab.
. Click *Assign role* and select *Organization roles*.
. Select one or more roles from the organization.
. Click *Assign*.

Members inherit these roles from the selected group and its parent groups. The roles appear in the `organization` claim when the *Organization Role Membership* mapper is enabled. The role picker only lists regular roles from the current organization that the administrator is allowed to map; it does not list the default role.

NOTE: Assigning organization roles to an organization group requires permission to manage the organization and to map each selected role.

== Important distinctions

Expand All @@ -329,7 +343,7 @@ NOTE: Assigning roles to organization groups requires the `manage-organizations`
|Fully isolated—same paths can exist across organizations

|Role assignment
|Supported—realm and client roles can be assigned to organization groups via the Admin API and Admin Console
|Supported—realm, client, and regular roles from the same organization can be assigned via the Admin API and Admin Console

|Authorization policies
|Cannot be used in authorization policies—use realm groups instead
Expand Down
Loading
Loading
0