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

com.marklogic.mgmt.api.security.Role Maven / Gradle / Ivy

Go to download

Java client for the MarkLogic REST Management API and for deploying applications to MarkLogic

There is a newer version: 5.0.0
Show newest version
/*
 * Copyright (c) 2023 MarkLogic Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.marklogic.mgmt.api.security;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.marklogic.mgmt.api.API;
import com.marklogic.mgmt.api.Resource;
import com.marklogic.mgmt.resource.ResourceManager;
import com.marklogic.mgmt.resource.security.RoleManager;

import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@XmlRootElement(name = "role-properties")
@XmlAccessorType(XmlAccessType.FIELD)
public class Role extends Resource {

	@XmlElement(name = "role-name")
	private String roleName;

	private String description;
	private String compartment;

	@XmlElementWrapper(name = "external-names")
	@XmlElement(name = "external-name")
	private List externalName;

	@XmlElementWrapper(name = "roles")
	private List role;

	@XmlElementWrapper(name = "permissions")
	private List permission;

	@XmlElementWrapper(name = "privileges")
	private List privilege;

	@XmlElementWrapper(name = "collections")
	private List collection;

	// This does not yet have an XmlElementWrapper on it as CapabilityQuery does not yet support XML marshalling;
	// unclear how to use e.g. XmlAnyElement on the query portion of it
	private List capabilityQuery;

	public Role() {
	}

	public Role(String roleName) {
		this(null, roleName);
	}

	public Role(API api, String roleName) {
		super(api);
		this.roleName = roleName;
	}

	public boolean hasPermissionsOrRoles() {
		return (role != null && !role.isEmpty()) || (permission != null && !permission.isEmpty());
	}

	@JsonIgnore
	public List getDependentRoleNames() {
		List names = new ArrayList<>();
		if (role != null) {
			names.addAll(role);
		}
		if (permission != null) {
			permission.forEach(p -> {
				if (!names.contains(p.getRoleName())) {
					names.add(p.getRoleName());
				}
			});
		}
		return names;
	}

	public void clearPermissionsAndRoles() {
		if (role != null) {
			role.clear();
			role = null;
		}
		if (permission != null) {
			permission.clear();
			permission = null;
		}
	}

	public boolean hasPermissionWithOwnRoleName() {
		return hasPermissionWithRoleName(this.roleName);
	}

	public boolean hasPermissionWithRoleName(String someRoleName) {
		if (permission != null && someRoleName != null) {
			for (Permission perm : permission) {
				if (someRoleName.equals(perm.getRoleName())) {
					return true;
				}
			}
		}
		return false;
	}

	public void addExternalName(String name) {
		if (externalName == null) {
			externalName = new ArrayList<>();
		}
		externalName.add(name);
	}

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

	public void addPermission(Permission p) {
		if (permission == null) {
			permission = new ArrayList<>();
		}
		permission.add(p);
	}

	public void addPrivilege(RolePrivilege priv) {
		if (privilege == null) {
			privilege = new ArrayList<>();
		}
		privilege.add(priv);
	}

	public void addCollection(String c) {
		if (collection == null) {
			collection = new ArrayList<>();
		}
		collection.add(c);
	}

	@Override
	protected ResourceManager getResourceManager() {
		return new RoleManager(getClient());
	}

	@Override
	protected String getResourceId() {
		return roleName;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getCompartment() {
		return compartment;
	}

	public void setCompartment(String compartment) {
		this.compartment = compartment;
	}

	public List getExternalName() {
		return externalName;
	}

	public void setExternalName(List externalName) {
		this.externalName = externalName;
	}

	public List getRole() {
		return role;
	}

	public void setRole(List role) {
		this.role = role;
	}

	public List getPermission() {
		return permission;
	}

	public void setPermission(List permission) {
		this.permission = permission;
	}

	public List getPrivilege() {
		return privilege;
	}

	public void setPrivilege(List privilege) {
		this.privilege = privilege;
	}

	public List getCollection() {
		return collection;
	}

	public void setCollection(List collection) {
		this.collection = collection;
	}

	public List getCapabilityQuery() {
		return capabilityQuery;
	}

	public void setCapabilityQuery(List capabilityQuery) {
		this.capabilityQuery = capabilityQuery;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy