PageRenderTime 102ms CodeModel.GetById 28ms app.highlight 52ms RepoModel.GetById 1ms app.codeStats 0ms

/worldedit-bukkit/src/main/java/com/sk89q/wepif/FlatFilePermissionsResolver.java

https://gitlab.com/Skull3x/WorldEdit
Java | 248 lines | 178 code | 50 blank | 20 comment | 40 complexity | 06ede97e16c1421af0500f0d84a4dd25 MD5 | raw file
  1/*
  2 * WorldEdit, a Minecraft world manipulation toolkit
  3 * Copyright (C) sk89q <http://www.sk89q.com>
  4 * Copyright (C) WorldEdit team and contributors
  5 *
  6 * This program is free software: you can redistribute it and/or modify it
  7 * under the terms of the GNU Lesser General Public License as published by the
  8 * Free Software Foundation, either version 3 of the License, or
  9 * (at your option) any later version.
 10 *
 11 * This program is distributed in the hope that it will be useful, but WITHOUT
 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 14 * for more details.
 15 *
 16 * You should have received a copy of the GNU Lesser General Public License
 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 18 */
 19
 20package com.sk89q.wepif;
 21
 22import com.sk89q.util.yaml.YAMLProcessor;
 23import org.bukkit.OfflinePlayer;
 24import org.bukkit.Server;
 25
 26import java.io.BufferedReader;
 27import java.io.File;
 28import java.io.FileReader;
 29import java.io.IOException;
 30import java.util.Arrays;
 31import java.util.HashMap;
 32import java.util.HashSet;
 33import java.util.Map;
 34import java.util.Set;
 35import java.util.logging.Level;
 36import java.util.logging.Logger;
 37
 38public class FlatFilePermissionsResolver implements PermissionsResolver {
 39
 40    private static final Logger log = Logger.getLogger(FlatFilePermissionsResolver.class.getCanonicalName());
 41
 42    private Map<String, Set<String>> userPermissionsCache;
 43    private Set<String> defaultPermissionsCache;
 44    private Map<String, Set<String>> userGroups;
 45
 46    private final File groupFile;
 47    private final File userFile;
 48
 49    public static PermissionsResolver factory(Server server, YAMLProcessor config) {
 50        File groups = new File("perms_groups.txt");
 51        File users = new File("perms_users.txt");
 52
 53        if (!groups.exists() || !users.exists()) {
 54            return null;
 55        }
 56
 57        return new FlatFilePermissionsResolver(groups, users);
 58    }
 59
 60    public FlatFilePermissionsResolver() {
 61        this(new File("perms_groups.txt"), new File("perms_users.txt"));
 62    }
 63
 64    public FlatFilePermissionsResolver(File groupFile, File userFile) {
 65        this.groupFile = groupFile;
 66        this.userFile = userFile;
 67    }
 68
 69    @Deprecated
 70    public static boolean filesExists() {
 71        return (new File("perms_groups.txt")).exists() && (new File("perms_users.txt")).exists();
 72    }
 73
 74    public Map<String, Set<String>> loadGroupPermissions() {
 75        Map<String, Set<String>> userGroupPermissions = new HashMap<String, Set<String>>();
 76
 77        BufferedReader buff = null;
 78
 79        try {
 80            FileReader input = new FileReader(this.groupFile);
 81            buff = new BufferedReader(input);
 82
 83            String line;
 84            while ((line = buff.readLine()) != null) {
 85                line = line.trim();
 86
 87                // Blank line
 88                if (line.isEmpty()) {
 89                    continue;
 90                } else if (line.charAt(0) == ';' || line.charAt(0) == '#') {
 91                    continue;
 92                }
 93
 94                String[] parts = line.split(":");
 95
 96                String key = parts[0];
 97
 98                if (parts.length > 1) {
 99                    String[] perms = parts[1].split(",");
100
101                    Set<String> groupPerms = new HashSet<String>(Arrays.asList(perms));
102                    userGroupPermissions.put(key, groupPerms);
103                }
104            }
105        } catch (IOException e) {
106            log.log(Level.WARNING, "Failed to load permissions", e);
107        } finally {
108            try {
109                if (buff != null) {
110                    buff.close();
111                }
112            } catch (IOException ignored) {
113            }
114        }
115
116        return userGroupPermissions;
117    }
118
119    @Override
120    public void load() {
121        userGroups = new HashMap<String, Set<String>>();
122        userPermissionsCache = new HashMap<String, Set<String>>();
123        defaultPermissionsCache = new HashSet<String>();
124
125        Map<String, Set<String>> userGroupPermissions = loadGroupPermissions();
126
127        if (userGroupPermissions.containsKey("default")) {
128            defaultPermissionsCache = userGroupPermissions.get("default");
129        }
130
131        BufferedReader buff =  null;
132
133        try {
134            FileReader input = new FileReader(this.userFile);
135            buff = new BufferedReader(input);
136
137            String line;
138            while ((line = buff.readLine()) != null) {
139                Set<String> permsCache = new HashSet<String>();
140
141                line = line.trim();
142
143                // Blank line
144                if (line.isEmpty()) {
145                    continue;
146                } else if (line.charAt(0) == ';' || line.charAt(0) == '#') {
147                    continue;
148                }
149
150                String[] parts = line.split(":");
151
152                String key = parts[0];
153
154                if (parts.length > 1) {
155                    String[] groups = (parts[1] + ",default").split(",");
156                    String[] perms = parts.length > 2 ? parts[2].split(",") : new String[0];
157
158                    permsCache.addAll(Arrays.asList(perms));
159
160                    for (String group : groups) {
161                        Set<String> groupPerms = userGroupPermissions.get(group);
162                        if (groupPerms != null) {
163                            permsCache.addAll(groupPerms);
164                        }
165                    }
166
167                    userPermissionsCache.put(key.toLowerCase(), permsCache);
168                    userGroups.put(key.toLowerCase(), new HashSet<String>(Arrays.asList(groups)));
169                }
170            }
171        } catch (IOException e) {
172            log.log(Level.WARNING, "Failed to load permissions", e);
173        } finally {
174            try {
175                if (buff != null) {
176                    buff.close();
177                }
178            } catch (IOException ignored) {
179            }
180        }
181    }
182
183    @Override
184    public boolean hasPermission(String player, String permission) {
185        int dotPos = permission.lastIndexOf(".");
186        if (dotPos > -1) {
187            if (hasPermission(player, permission.substring(0, dotPos))) {
188                return true;
189            }
190        }
191
192        Set<String> perms = userPermissionsCache.get(player.toLowerCase());
193        if (perms == null) {
194            return defaultPermissionsCache.contains(permission)
195                    || defaultPermissionsCache.contains("*");
196        }
197
198        return perms.contains("*") || perms.contains(permission);
199    }
200
201    @Override
202    public boolean hasPermission(String worldName, String player, String permission) {
203        return hasPermission(player, "worlds." + worldName + "." + permission)
204                || hasPermission(player, permission);
205    }
206
207    @Override
208    public boolean inGroup(String player, String group) {
209        Set<String> groups = userGroups.get(player.toLowerCase());
210        return groups != null && groups.contains(group);
211    }
212
213    @Override
214    public String[] getGroups(String player) {
215        Set<String> groups = userGroups.get(player.toLowerCase());
216        if (groups == null) {
217            return new String[0];
218        }
219
220        return groups.toArray(new String[groups.size()]);
221    }
222
223    @Override
224    public boolean hasPermission(OfflinePlayer player, String permission) {
225        return hasPermission(player.getName(), permission);
226    }
227
228    @Override
229    public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
230        return hasPermission(worldName, player.getName(), permission);
231    }
232
233    @Override
234    public boolean inGroup(OfflinePlayer player, String group) {
235        return inGroup(player.getName(), group);
236    }
237
238    @Override
239    public String[] getGroups(OfflinePlayer player) {
240        return getGroups(player.getName());
241    }
242
243    @Override
244    public String getDetectionMessage() {
245        return "perms_groups.txt and perms_users.txt detected! Using flat file permissions.";
246    }
247
248}