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 com.moandjiezana.toml.Toml;
021import io.stallion.services.Log;
022import io.stallion.settings.Settings;
023
024import java.io.File;
025import java.util.ArrayList;
026import java.util.List;
027
028import static io.stallion.utils.Literals.*;
029import static io.stallion.Context.*;
030
031
032public class Sandbox {
033
034    public static Sandbox allPermissions() {
035        return new Sandbox()
036                .setCanReadAllData(true)
037                .setCanWriteAllData(true)
038                .setUsers(new Users().setCanAccess(true).setCanWriteDb(true))
039                .setWhitelist(new Whitelist())
040                ;
041    }
042
043    public static Sandbox forPlugin(String plugin) {
044        return fromPath(Settings.instance().getTargetFolder() + "/plugins/" + plugin + "/sandbox.toml");
045    }
046
047    public static Sandbox fromPath(String path) {
048        return fromFile(new File(path));
049    }
050    public static Sandbox fromFile(File file) {
051        Log.info("Look for sandbox.toml for file {0}", file.getAbsolutePath());
052        if (!file.isFile()) {
053            return null;
054        }
055        Log.info("Loadding sandbox.toml for file {0}", file.getAbsolutePath());
056        Toml boxToml = new Toml().read(file);
057        Sandbox box = boxToml.to(Sandbox.class);
058        Toml users = boxToml.getTable("users");
059        if (emptyInstance(users)) {
060            box.setUsers(new Users());
061        } else {
062            box.setUsers(users.to(Users.class));
063        }
064        Toml whitelist = boxToml.getTable("whitelist");
065        if (emptyInstance(whitelist)) {
066            box.setWhitelist(new Whitelist());
067        } else {
068            box.setWhitelist(whitelist.to(Whitelist.class));
069        }
070        return box;
071    }
072
073    private boolean canWriteAllData = false;
074    private boolean canReadAllData = false;
075    private Users users;
076    private Whitelist whitelist;
077
078
079    public boolean isCanReadAllData() {
080        return canReadAllData;
081    }
082
083    public Sandbox setCanReadAllData(boolean canReadAllData) {
084        this.canReadAllData = canReadAllData;
085        return this;
086    }
087
088    public boolean isCanWriteAllData() {
089        return canWriteAllData;
090    }
091
092    public Sandbox setCanWriteAllData(boolean canWriteAllData) {
093        this.canWriteAllData = canWriteAllData;
094        return this;
095    }
096
097    public Users getUsers() {
098        return users;
099    }
100
101    public Sandbox setUsers(Users users) {
102        this.users = users;
103        return this;
104    }
105
106    public Whitelist getWhitelist() {
107        return whitelist;
108    }
109
110    public Sandbox setWhitelist(Whitelist whitelist) {
111        this.whitelist = whitelist;
112        return this;
113    }
114
115    public static class Whitelist {
116        private List<String> cookies = list();
117        private List<String> headers = list();
118        private List<String> classes = list();
119        private List<String> readBuckets = list();
120        private List<String> writeBuckets = list();
121
122        public List<String> getCookies() {
123            return cookies;
124        }
125
126        public Whitelist setCookies(List<String> cookies) {
127            this.cookies = cookies;
128            return this;
129        }
130
131        public List<String> getHeaders() {
132            return headers;
133        }
134
135        public Whitelist setHeaders(List<String> headers) {
136            this.headers = headers;
137            return this;
138        }
139
140        public List<String> getClasses() {
141            return classes;
142        }
143
144        public Whitelist setClasses(List<String> classes) {
145            this.classes = classes;
146            return this;
147        }
148
149        public List<String> getReadBuckets() {
150            return readBuckets;
151        }
152
153        public Whitelist setReadBuckets(List<String> readBuckets) {
154            this.readBuckets = readBuckets;
155            return this;
156        }
157
158        public List<String> getWriteBuckets() {
159            return writeBuckets;
160        }
161
162        public Whitelist setWriteBuckets(List<String> writeBuckets) {
163            this.writeBuckets = writeBuckets;
164            return this;
165        }
166    }
167
168
169    public static class Users {
170        private boolean canAccess = false;
171        private boolean canReadDb = false;
172        private boolean canWriteDb = false;
173
174        public boolean isCanAccess() {
175            return canAccess;
176        }
177
178        public Users setCanAccess(boolean canAccess) {
179            this.canAccess = canAccess;
180            return this;
181        }
182
183        public boolean isCanReadDb() {
184            return canReadDb;
185        }
186
187        public Users setCanReadDb(boolean canReadDb) {
188            this.canReadDb = canReadDb;
189            return this;
190        }
191
192        public boolean isCanWriteDb() {
193            return canWriteDb;
194        }
195
196        public Users setCanWriteDb(boolean canWriteDb) {
197            this.canWriteDb = canWriteDb;
198            return this;
199        }
200    }
201}