fathom.authz.DomainPermission Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 fathom.authz;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import fathom.utils.Util;
import java.util.Set;
/**
* Provides a base Permission class from which type-safe/domain-specific subclasses may extend. Can be used
* as a base class for JPA/Hibernate persisted permissions that wish to store the parts of the permission string
* in separate columns (e.g. 'domain', 'actions' and 'targets' columns), which can be used in querying
* strategies.
*
* This class was adapted from Apache Shiro.
*/
public class DomainPermission extends Permission {
private String domain;
private Set actions;
private Set targets;
private static final long serialVersionUID = 1l;
/**
* Creates a domain permission with *all* actions for *all* targets;
*/
public DomainPermission() {
this.domain = getDomain(getClass());
setParts(getDomain(getClass()));
}
public DomainPermission(String actions) {
domain = getDomain(getClass());
this.actions = Util.splitToSet(actions, SUBPART_DIVIDER_TOKEN);
encodeParts(domain, actions, null);
}
public DomainPermission(String actions, String targets) {
this.domain = getDomain(getClass());
this.actions = Util.splitToSet(actions, SUBPART_DIVIDER_TOKEN);
this.targets = Util.splitToSet(targets, SUBPART_DIVIDER_TOKEN);
encodeParts(this.domain, actions, targets);
}
protected DomainPermission(Set actions, Set targets) {
this.domain = getDomain(getClass());
setParts(domain, actions, targets);
}
private void encodeParts(String domain, String actions, String targets) {
if (Strings.isNullOrEmpty(domain)) {
throw new IllegalArgumentException("domain argument cannot be null or empty.");
}
StringBuilder sb = new StringBuilder(domain);
if (Strings.isNullOrEmpty(actions)) {
if (!Strings.isNullOrEmpty(targets)) {
sb.append(PART_DIVIDER_TOKEN).append(WILDCARD_TOKEN);
}
} else {
sb.append(PART_DIVIDER_TOKEN).append(actions);
}
if (!Strings.isNullOrEmpty(targets)) {
sb.append(PART_DIVIDER_TOKEN).append(targets);
}
setParts(sb.toString());
}
protected void setParts(String domain, Set actions, Set targets) {
String actionsString = Joiner.on(SUBPART_DIVIDER_TOKEN).join(actions);
String targetsString = Joiner.on(SUBPART_DIVIDER_TOKEN).join(targets);
encodeParts(domain, actionsString, targetsString);
this.domain = domain;
this.actions = actions;
this.targets = targets;
}
protected String getDomain(Class clazz) {
String domain = clazz.getSimpleName().toLowerCase();
//strip any trailing 'permission' text from the name (as all subclasses should have been named):
int index = domain.lastIndexOf("permission");
if (index != -1) {
domain = domain.substring(0, index);
}
return domain;
}
public String getDomain() {
return domain;
}
protected void setDomain(String domain) {
if (this.domain != null && this.domain.equals(domain)) {
return;
}
this.domain = domain;
setParts(domain, actions, targets);
}
public Set getActions() {
return actions;
}
protected void setActions(Set actions) {
if (this.actions != null && this.actions.equals(actions)) {
return;
}
this.actions = actions;
setParts(domain, actions, targets);
}
public Set getTargets() {
return targets;
}
protected void setTargets(Set targets) {
this.targets = targets;
if (this.targets != null && this.targets.equals(targets)) {
return;
}
this.targets = targets;
setParts(domain, actions, targets);
}
}