001/* 002 * Stallion Core: A Modern Web Framework 003 * 004 * Copyright (C) 2015 - 2016 Stallion Software LLC. 005 * 006 * This program is free software: you can redistribute it and/or modify it under the terms of the 007 * GNU General Public License as published by the Free Software Foundation, either version 2 of 008 * the License, or (at your option) any later version. This program is distributed in the hope that 009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 011 * License for more details. You should have received a copy of the GNU General Public License 012 * along with this program. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>. 013 * 014 * 015 * 016 */ 017 018package io.stallion.plugins.javascript; 019 020import io.stallion.Context; 021import io.stallion.dataAccess.DataAccessRegistry; 022import io.stallion.dataAccess.ModelController; 023import io.stallion.exceptions.UsageException; 024 025import java.util.Collection; 026import java.util.Map; 027import java.util.Set; 028import java.util.function.BiConsumer; 029import java.util.function.BiFunction; 030import java.util.function.Function; 031 032 033public class SandboxedDataAccess implements Map<String, ModelController> { 034 035 private Sandbox sandbox; 036 037 public SandboxedDataAccess(Sandbox sandbox) { 038 this.sandbox = sandbox; 039 } 040 041 @Override 042 public int size() { 043 return DataAccessRegistry.instance().size(); 044 } 045 046 @Override 047 public boolean isEmpty() { 048 return false; 049 } 050 051 @Override 052 public boolean containsKey(Object key) { 053 return DataAccessRegistry.instance().containsKey(key); 054 } 055 056 @Override 057 public boolean containsValue(Object value) { 058 return false; 059 } 060 061 @Override 062 public ModelController get(Object key) { 063 if (sandbox.isCanWriteAllData()) { 064 return DataAccessRegistry.instance().get(key); 065 } 066 if (key.equals("users") && sandbox.getUsers().isCanWriteDb()) { 067 return Context.getUserController(); 068 } 069 if (key.equals("users") && sandbox.getUsers().isCanReadDb()) { 070 return Context.getUserController().getReadonlyWrapper(); 071 } 072 if (sandbox.getWhitelist().getWriteBuckets().contains(key)) { 073 return DataAccessRegistry.instance().get(key); 074 } 075 if (sandbox.isCanReadAllData()) { 076 return DataAccessRegistry.instance().get(key).getReadonlyWrapper(); 077 } 078 if (sandbox.getWhitelist().getReadBuckets().contains(key)) { 079 return DataAccessRegistry.instance().get(key).getReadonlyWrapper(); 080 } 081 throw new UsageException("You do not have access to the data bucket " + key); 082 } 083 084 @Override 085 public ModelController put(String key, ModelController value) { 086 return null; 087 } 088 089 @Override 090 public ModelController remove(Object key) { 091 return null; 092 } 093 094 @Override 095 public void putAll(Map<? extends String, ? extends ModelController> m) { 096 097 } 098 099 @Override 100 public void clear() { 101 102 } 103 104 @Override 105 public Set<String> keySet() { 106 return null; 107 } 108 109 @Override 110 public Collection<ModelController> values() { 111 return null; 112 } 113 114 @Override 115 public Set<Entry<String, ModelController>> entrySet() { 116 return null; 117 } 118 119 @Override 120 public ModelController getOrDefault(Object key, ModelController defaultValue) { 121 return null; 122 } 123 124 @Override 125 public void forEach(BiConsumer<? super String, ? super ModelController> action) { 126 127 } 128 129 @Override 130 public void replaceAll(BiFunction<? super String, ? super ModelController, ? extends ModelController> function) { 131 132 } 133 134 @Override 135 public ModelController putIfAbsent(String key, ModelController value) { 136 return null; 137 } 138 139 @Override 140 public boolean remove(Object key, Object value) { 141 return false; 142 } 143 144 @Override 145 public boolean replace(String key, ModelController oldValue, ModelController newValue) { 146 return false; 147 } 148 149 @Override 150 public ModelController replace(String key, ModelController value) { 151 return null; 152 } 153 154 @Override 155 public ModelController computeIfAbsent(String key, Function<? super String, ? extends ModelController> mappingFunction) { 156 return null; 157 } 158 159 @Override 160 public ModelController computeIfPresent(String key, BiFunction<? super String, ? super ModelController, ? extends ModelController> remappingFunction) { 161 return null; 162 } 163 164 @Override 165 public ModelController compute(String key, BiFunction<? super String, ? super ModelController, ? extends ModelController> remappingFunction) { 166 return null; 167 } 168 169 @Override 170 public ModelController merge(String key, ModelController value, BiFunction<? super ModelController, ? super ModelController, ? extends ModelController> remappingFunction) { 171 return null; 172 } 173}