All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cloud.security.ams.api.PrincipalImpl Maven / Gradle / Ivy

Go to download

Client Library for integrating Jakarta EE applications with SAP Authorization Management Service (AMS)

The newest version!
/************************************************************************
* © 2019-2023 SAP SE or an SAP affiliate company. All rights reserved. *
************************************************************************/
package com.sap.cloud.security.ams.api;

import com.sap.cloud.security.ams.dcl.client.pdp.Attributes;

import java.util.*;

/**
 * Internal implementation of {@link Principal} interface.
 */
class PrincipalImpl implements Principal {

	private String appTid;
	private String id;
	private String scimId;
	private List groups = Collections.emptyList();
	private String email;
	private final Set policies = new HashSet<>();
	private Attributes attributes;
	private PolicyAssignments policyAssignments;

	PrincipalImpl(String appTid, String id, Collection policies) {
		this.appTid = appTid;
		this.id = id;
		this.policies.addAll(policies);
		this.policyAssignments = PolicyAssignmentBuilder.createEmpty();
	}

	PrincipalImpl() {
		this(null, null, Collections.emptyList());
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null) {
			return false;
		}
		if (this.getClass() != obj.getClass()) {
			return false;
		}
		Principal principal = (Principal) obj;
		return Objects.equals(getAppTid(), principal.getAppTid()) &&
				Objects.equals(getId(), principal.getId()) &&
				Objects.equals(getAttributes(), principal.getAttributes());
	}

	@Override
	public int hashCode() {
		return Objects.hash(getAppTid(), getId(), policies);
	}

	@Override
	public String toString() {
		return "Principal {" +
				"app_tid='" + appTid + "', " +
				"id='" + id + "', " +
				"policies=" + policies + '}';
	}

	@Override
	public String getAppTid() {
		return appTid;
	}

	@Override
	public String getZoneId() {
		return getAppTid();
	}

	@Override
	public String getId() {
		return id;
	}

	@Override
	public boolean hasPolicies() {
		return !policies.isEmpty();
	}

	@Override
	public String getEmail() {
		return email;
	}

	void setEmail(String email) {
		this.email = email;
	}

	@Override
	public List getGroups() {
		return groups;
	}

	public String getScimId() {
		return scimId;
	}

	public void setScimId(String scimId) {
		this.scimId = scimId;
	}

	void setGroups(List groups) {
		this.groups = groups;
	}

	List getToPolicyMapping() {
		List principalToPolicies = new ArrayList<>();
		principalToPolicies.add(getAppTid());
		principalToPolicies.add(getScimId() != null ? getScimId() : getId());
		return principalToPolicies;
	}

	void addPolicy(String policy) {
		this.policies.add(policy);
	}

	void addPolicies(Collection policies) {
		this.policies.addAll(policies);
	}

	void setAppTid(String appTid) {
		this.appTid = appTid;
	}

	void setId(String id) {
		this.id = id;
	}

	boolean hasAppTid() {
		return appTid != null && !appTid.trim().isEmpty();
	}

	void setAttributes(Attributes attributes) {
		this.attributes = attributes;
	}

	@Override
	public Attributes getAttributes() {
		return attributes;
	}

	/**
	 * Returns a list of Authorization Management Service policies.
	 *
	 * @return the policies
	 */
	public Collection getPolicies() {
		return new HashSet<>(policies);
	}

	@Override
	public void setPolicyAssignments(PolicyAssignments policyAssignments) {
		if (Objects.nonNull(policyAssignments)) {
			this.policyAssignments = policyAssignments;
		}
	}

	public PolicyAssignments getPolicyAssignments() {
		return this.policyAssignments;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy