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

com.memority.citadel.shared.api.im.RightGrant Maven / Gradle / Ivy

Go to download

This artifact provides the API classes that are necessary to implement general configuration Rules on the Memority IM platform.

There is a newer version: 3.43.1
Show newest version
/*
 * Copyright (c) 2016-2023 Memority. All Rights Reserved.
 *
 * This file is part of Memority Citadel API , a Memority project.
 *
 * This file is released under the Memority Public Artifacts End-User License Agreement,
 * see 
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 */
package com.memority.citadel.shared.api.im;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.NonNull;

import com.memority.citadel.shared.api.XmlConstants;

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.*;

import static java.util.stream.Collectors.joining;

@Data
@EqualsAndHashCode(of = {"name", "target", "privilege", "computed"})
@JsonInclude(JsonInclude.Include.NON_NULL)
@XmlRootElement(name = XmlConstants.NAME_ELEMENT_RIGHT_GRANT)
@XmlType(name = XmlConstants.NAME_TYPE_RIGHT_GRANT)
@XmlAccessorType(XmlAccessType.FIELD)
public class RightGrant implements Comparable {

    @JsonView(JsonViews.Jdbc.MObjectSearchQuery.class)
    private final String name;

    /**
     * An optional resource on which the Right is granted.
     */
    @XmlElement(name = "target")
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @JsonView(JsonViews.Jdbc.MObjectSearchQuery.class)
    private final String target;

    /**
     * An optional opaque string modulating the granted Right.
* This is more intended for business fine-grained restrictions (e.g. visibility restrictions) than coarse access * control. */ @XmlElement @JsonInclude(JsonInclude.Include.NON_EMPTY) @JsonView(JsonViews.Jdbc.MObjectSearchQuery.class) private final String privilege; @XmlElement(name = "dims") @XmlJavaTypeAdapter(RightGrantDimensionsAdapter.class) @JsonInclude(JsonInclude.Include.NON_EMPTY) @JsonProperty("dims") private final Map dimensions = new HashMap<>(); @XmlAttribute(name = "computed") @JsonProperty("computed") private final boolean computed; @SuppressWarnings("unused") //JAXB private RightGrant() { this(null, null, null, null, false); } public RightGrant(String name, String target, String privilege, Map dimensions) { this(name, target, privilege, dimensions, false); } @JsonCreator public RightGrant(@JsonProperty("name") String name, @JsonProperty("target") String target, @JsonProperty("privilege") String privilege, @JsonProperty("dims") Map dimensions, @JsonProperty("computed") boolean computed) { this.name = name; this.privilege = privilege; this.computed = computed; this.target = target; if (dimensions != null) { this.dimensions.putAll(dimensions); } } @Override public int compareTo(@NonNull RightGrant o) { return Comparator.comparing(RightGrant::getName).compare(this, o); } public String getName() { return name; } public Map getDimensions() { return Collections.unmodifiableMap(dimensions); } public Object getDimensionValue(String name) { Object raw = this.dimensions.get(name); if (raw == null) { return null; } if (raw instanceof List) { if (((List) raw).isEmpty()) { return null; } else { return ((List) raw).get(0); } } return raw; } public List getDimensionValues(String name) { Object raw = this.dimensions.get(name); if (raw == null) { return Collections.emptyList(); } if (raw instanceof List) { return Collections.unmodifiableList((List) raw); } else { return Collections.singletonList(raw); } } /** * @return true if it was granted through a Role Assignment, false otherwise. Computed * Right Grants cannot be added or removed by API. */ public boolean isComputed() { return computed; } /** * Same as {@link #of(String, boolean)} of(rightSpec, false)} * * @param rightSpec the serialized {@code RightGrant} representation * @return a new {@code RightGrant} */ public static RightGrant of(String rightSpec) { return of(rightSpec, false); } /** * The {@code rightSpec} argument can be formatted as follows: *
    *
  • {@code }, such as {@code "admin"}
  • *
  • {@code :,}, such as {@code "admin:employee,partner"}
  • *
  • {@code :,}, such as {@code ":employee,partner"}
  • *
* @param rightSpec the serialized {@code RightGrant} representation * @param computed whether the instantiated {@code RightGrant} is computed * @return a new {@code RightGrant} */ public static RightGrant of(String rightSpec, boolean computed) { List specParts = Arrays.asList(rightSpec.split(":")); // Handle right name String name = null; if (specParts.size() > 0 && StringUtils.isNotEmpty(specParts.get(0))) { name = specParts.get(0); } // Handle right target String target = null; if (specParts.size() > 1 && StringUtils.isNotEmpty(specParts.get(1))) { target = specParts.get(1); } //the remainder is the privilege String privilege = null; if (specParts.size() > 2) { privilege = specParts.stream() .skip(2) .collect(joining(":")); } return new RightGrant(name, target, privilege, Collections.emptyMap(), computed); } public RightGrant dimension(String name, Object value) { Map newDimensions = new HashMap<>(this.dimensions); newDimensions.put(name, value); return new RightGrant(this.name, this.target, this.privilege, newDimensions, computed); } public RightGrant dimensions(Map dimensions) { return new RightGrant(this.name, this.target, this.privilege, dimensions, computed); } public RightGrant target(String target) { return new RightGrant(this.name, target, privilege, dimensions, computed); } public RightGrant privilege(String privilege) { return new RightGrant(this.name, this.target, privilege, this.dimensions, computed); } /** * Internal Use only - Jackson JSON views, for various purposes */ public interface JsonViews { interface Jdbc { /** * View used to prepare arguments of complex search (SELECT) SQL queries. */ interface MObjectSearchQuery { } } } }