001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.permissions;
020
021import com.plotsquared.core.configuration.Settings;
022import org.checkerframework.checker.index.qual.NonNegative;
023import org.checkerframework.checker.nullness.qual.NonNull;
024import org.checkerframework.checker.nullness.qual.Nullable;
025
026/**
027 * Any object which can hold permissions
028 */
029public interface PermissionHolder {
030
031    /**
032     * Check if the owner of the profile has a given (global) permission
033     *
034     * @param permission Permission
035     * @return {@code true} if the owner has the given permission, else {@code false}
036     */
037    default boolean hasPermission(final @NonNull String permission) {
038        return hasPermission(null, permission);
039    }
040
041    /**
042     * Check if the owner of the profile has a given (global) permission
043     *
044     * @param permission Permission
045     * @return {@code true} if the owner has the given permission, else {@code false}
046     */
047    default boolean hasPermission(final @NonNull Permission permission) {
048        return hasPermission(permission.toString());
049    }
050
051    /**
052     * Check if the owner of the profile has a given (global) keyed permission. Checks both {@code permission.key}
053     * and {@code permission.*}
054     *
055     * @param permission Permission
056     * @param key        Permission "key"
057     * @return {@code true} if the owner has the given permission, else {@code false}
058     * @since 6.0.10
059     */
060    default boolean hasKeyedPermission(
061            final @NonNull String permission,
062            final @NonNull String key
063    ) {
064        return hasKeyedPermission(null, permission, key);
065    }
066
067    /**
068     * Check the highest permission a PlotPlayer has within a specified range.<br>
069     * - Excessively high values will lag<br>
070     * - The default range that is checked is {@link Settings.Limit#MAX_PLOTS}<br>
071     *
072     * @param stub  The permission stub to check e.g. for `plots.plot.#` the stub is `plots.plot`
073     * @param range The range to check
074     * @return The highest permission they have within that range
075     */
076    @NonNegative
077    default int hasPermissionRange(
078            final @NonNull Permission stub,
079            @NonNegative final int range
080    ) {
081        return hasPermissionRange(stub.toString(), range);
082    }
083
084    /**
085     * Check the highest permission a PlotPlayer has within a specified range.<br>
086     * - Excessively high values will lag<br>
087     * - The default range that is checked is {@link Settings.Limit#MAX_PLOTS}<br>
088     *
089     * @param stub  The permission stub to check e.g. for `plots.plot.#` the stub is `plots.plot`
090     * @param range The range to check
091     * @return The highest permission they have within that range
092     */
093    @NonNegative
094    default int hasPermissionRange(
095            final @NonNull String stub,
096            @NonNegative final int range
097    ) {
098        if (hasPermission(Permission.PERMISSION_ADMIN.toString())) {
099            return Integer.MAX_VALUE;
100        }
101        String[] nodes = stub.split("\\.");
102        StringBuilder builder = new StringBuilder();
103        // Wildcard check from less specific permission to more specific permission
104        for (int i = 0; i < (nodes.length - 1); i++) {
105            builder.append(nodes[i]).append(".");
106            if (!stub.equals(builder + Permission.PERMISSION_STAR.toString())) {
107                if (hasPermission(builder + Permission.PERMISSION_STAR.toString())) {
108                    return Integer.MAX_VALUE;
109                }
110            }
111        }
112        // Wildcard check for the full permission
113        if (hasPermission(stub + ".*")) {
114            return Integer.MAX_VALUE;
115        }
116        for (int i = range; i > 0; i--) {
117            if (hasPermission(stub + "." + i)) {
118                return i;
119            }
120        }
121        return 0;
122    }
123
124    /**
125     * Checks if the owner of the profile has a permission, and optionally send the no permission message if applicable.
126     *
127     * @param permission Permission
128     * @param notify     If to notify the permission holder
129     * @return {@code true} if the owner has the given permission, else {@code false}
130     */
131    default boolean hasPermission(@NonNull Permission permission, boolean notify) {
132        return hasPermission(permission.toString(), notify);
133    }
134
135    /**
136     * Checks if the owner of the profile has a permission, and optionally send the no permission message if applicable.
137     *
138     * @param permission Permission
139     * @param notify     If to notify the permission holder
140     * @return {@code true} if the owner has the given permission, else {@code false}
141     */
142    boolean hasPermission(@NonNull String permission, boolean notify);
143
144    /**
145     * Check if the owner of the profile has a given permission
146     *
147     * @param world      World name
148     * @param permission Permission
149     * @return {@code true} if the owner has the given permission, else {@code false}
150     */
151    boolean hasPermission(@Nullable String world, @NonNull String permission);
152
153    /**
154     * Check if the owner of the profile has a given keyed permission. Checks both {@code permission.key}
155     * and {@code permission.*}
156     *
157     * @param world      World name
158     * @param permission Permission
159     * @param key        Permission "key"
160     * @return {@code true} if the owner has the given permission, else {@code false}
161     * @since 6.0.10
162     */
163    boolean hasKeyedPermission(@Nullable String world, @NonNull String permission, @NonNull String key);
164
165}