
org.craftercms.commons.security.permissions.PermissionBase Maven / Gradle / Ivy
/*
* Copyright (C) 2007-2014 Crafter Software Corporation.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.craftercms.commons.security.permissions;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
/**
* Base for {@link org.craftercms.commons.security.permissions.Permission} that uses a set of allowed actions to
* check
*/
public abstract class PermissionBase implements Permission {
public static final String ANY_ACTION = "*";
protected Set allowedActions;
@Override
public boolean isAllowed(String action) {
return CollectionUtils.isNotEmpty(allowedActions) &&
(allowedActions.contains(ANY_ACTION) || allowedActions.contains(action));
}
public Set getAllowedActions() {
return allowedActions;
}
public void setAllowedActions(Set allowedActions) {
this.allowedActions = allowedActions;
}
public PermissionBase allowAny() {
allow(ANY_ACTION);
return this;
}
public PermissionBase allow(String action) {
if (allowedActions == null) {
allowedActions = new HashSet<>();
}
allowedActions.add(action);
return this;
}
public PermissionBase allow(String... actions) {
if (ArrayUtils.isNotEmpty(actions)) {
for (String action : actions) {
allow(action);
}
}
return this;
}
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"allowedActions=" + allowedActions +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PermissionBase that = (PermissionBase)o;
if (allowedActions != null? !allowedActions.equals(that.allowedActions): that.allowedActions != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
return allowedActions != null? allowedActions.hashCode(): 0;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy