Java tutorial
package com.kimbrelk.privileges; /* Copyright (c) 2014 Karson Kimbrel Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import com.kimbrelk.privileges.PrivilegeGroup.BasicPrivGroup; import com.kimbrelk.privileges.util.interfaces.IJson; import java.util.ArrayList; import org.json.JSONArray; import org.json.JSONObject; /** * The Inherited Privilege System */ public abstract class Privileges implements IJson { public final static int JSON_VERSION = 1; public final static int ERROR_ALLOWANCE_WILDCARD_WITHOUT_KNOWN_PRIVS = 1; public final static int ERROR_USER_WITHOUT_GROUP = 2; public final static int ERROR_JSON_UNKNOWN_VERSION = 3; public final static int ERROR_JSON_INVALID_USER = 4; public final static int ERROR_JSON_INVALID_GROUP = 5; private ArrayList<String> registeredPrivileges; private ArrayList<PrivilegeGroup> groups; private ArrayList<User> users; public Privileges() { registeredPrivileges = null; groups = new ArrayList<PrivilegeGroup>(); users = new ArrayList<User>(); } public Privileges(JSONObject json) throws Exception { IJson.Result result = fromJSON(json, -1); if (result.hasFailed()) { if (result == Result.OUTDATED_APP) { throw new Exception("Application is outdated, unable to load privlages from JSON."); } else { throw new Exception("Unable to load privlages from JSON."); } } } /** * Adds a new privilege group the the privilege system * @param group New privilege group */ public final void addGroup(PrivilegeGroup group) { groups.add(group); } /** * Adds a new user to the privilege system * @param user New user */ public final void addUser(User user) { users.add(user); } public final IJson.Result fromJSON(JSONObject json, int version) { try { version = json.getInt("version"); if (version > JSON_VERSION) return IJson.Result.OUTDATED_APP; groups = new ArrayList<PrivilegeGroup>(); JSONArray jsonGroups = json.optJSONArray("groups"); if (jsonGroups != null) { int len = jsonGroups.length(); for (int a = 0; a < len; a++) { try { groups.add(new PrivilegeGroup(jsonGroups.getJSONObject(a), version)); } catch (Exception e) { onError(ERROR_JSON_INVALID_GROUP); } } } users = new ArrayList<User>(); JSONArray jsonUsers = json.optJSONArray("users"); if (jsonUsers != null) { int len = jsonUsers.length(); for (int a = 0; a < len; a++) { try { users.add(new User(jsonUsers.getJSONObject(a), version)); } catch (Exception e) { onError(ERROR_JSON_INVALID_USER); } } } if (version < JSON_VERSION) return IJson.Result.OUTDATED_FORMAT; return IJson.Result.SUCCESS; } catch (Exception e) { e.printStackTrace(); return IJson.Result.FAILURE; } } /** * @return A list of privileges that are known to exist. */ public final ArrayList<String> getRegisteredPrivileges() { return registeredPrivileges; } public final PrivilegeGroup getGroup(BasicPrivGroup basicGroup) { for (PrivilegeGroup group : groups) { if (group.getBasicGroup() == basicGroup) { return group; } } return null; } /** * Retrieves the specified group. * @param groupName The name of the group * @return The specified group. Null if the group is not found. */ public final PrivilegeGroup getGroup(String groupName) { return (PrivilegeGroup) getPrivilegeHolder(groupName, groups); } /** * Gets all of the applicable privileges from the specified PrivilegeGroup * @param group The PrivilegeGroup * @return An ArrayList of privileges */ public final ArrayList<String> getGroupPrivileges(PrivilegeGroup group) { ArrayList<String> ret = null; if (group == null) return new ArrayList<String>(); PrivilegeGroup superGroup = getGroup(group.getSuperGroup()); if (superGroup != null) ret = getGroupPrivileges(superGroup); if (ret == null) ret = new ArrayList<String>(); processPrivileges(ret, group); return ret; } /** * @return An ArrayList of all registered privilege groups */ public final ArrayList<PrivilegeGroup> getGroups() { return groups; } /** * Helper function for grabbing a User or a PrivilegeGroup * @param name The PrivilegeHolder name * @param privilegeHolders An ArrayList of PrivilegeHolders * @return The specified PrivilegeHolder or null if one is not found */ @SuppressWarnings({ "rawtypes", "unchecked" }) private final static PrivilegeHolder getPrivilegeHolder(String name, ArrayList privilegeHolders) { if (name == null || privilegeHolders == null) { return null; } for (PrivilegeHolder privilegeHolder : (ArrayList<PrivilegeHolder>) privilegeHolders) { if (privilegeHolder.getName().equalsIgnoreCase(name)) { return privilegeHolder; } } return null; } /** * Retrieves the specified user. * @param userName The name of the user * @return The specified user. Null if the user is not found. */ public final User getUser(String userName) { return (User) getPrivilegeHolder(userName, users); } /** * Gets an ArrayList of the specified User's privileges * @param user Specified User * @return An ArrayList of the specified User's privileges, * will be empty if the user is null or non existant. */ public final ArrayList<String> getUserPrivileges(User user) { ArrayList<String> ret = null; if (user != null) ret = getGroupPrivileges(getGroup(user.getGroupName())); if (ret == null) { onError(ERROR_USER_WITHOUT_GROUP); ret = new ArrayList<String>(); } processPrivileges(ret, user); return ret; } /** * @return An ArrayList of all registered users */ public final ArrayList<User> getUsers() { return users; } /** * This event is dispatched when an error occurs * @param error Error code */ public abstract void onError(int error); /** * Will process (add and removed) privileges appropriately as based on the PrivilegeHolder's allowances and denials * @param privs An ArrayList of already allowed privileges * @param entity The PrivilegeHolder to process denials and allowances from */ private final void processPrivileges(ArrayList<String> privs, PrivilegeHolder entity) { if (entity == null || privs == null) return; // Denials ArrayList<String> denials = entity.getDenials(); for (String denial : denials) { if (denial.equals("*")) { privs.clear(); break; } privs.remove(denial); } // Allowances ArrayList<String> allowances = entity.getAllowances(); for (String allowance : allowances) { if (allowance.equals("*")) { ArrayList<String> t = getRegisteredPrivileges(); if (t != null) { privs.clear(); privs.addAll(t); return; } else { onError(ERROR_ALLOWANCE_WILDCARD_WITHOUT_KNOWN_PRIVS); } } else { privs.add(allowance); } } } /** * Sets the list of privileges that are known to exist. * This is required in order to use wildcards with allowances. * @param registeredPrivileges A list of privileges that are known to exist */ public final void setRegisteredPrivileges(ArrayList<String> registeredPrivileges) { this.registeredPrivileges = registeredPrivileges; } public final JSONObject toJSON() { try { JSONObject json = new JSONObject(); json.put("version", JSON_VERSION); JSONArray jsonGroups = new JSONArray(); for (PrivilegeGroup group : groups) { try { jsonGroups.put(group.toJSON()); } catch (Exception e) { onError(ERROR_JSON_INVALID_GROUP); } } JSONArray jsonUsers = new JSONArray(); for (User user : users) { try { jsonUsers.put(user.toJSON()); } catch (Exception e) { onError(ERROR_JSON_INVALID_GROUP); } } json.put("groups", jsonGroups); json.put("users", jsonUsers); return json; } catch (Exception e) { e.printStackTrace(); return null; } } }