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.requests.SandboxedRequest;
022import io.stallion.requests.SandboxedResponse;
023import io.stallion.requests.Site;
024import io.stallion.templating.TemplateRenderer;
025import io.stallion.users.EmptyUser;
026import io.stallion.users.IUser;
027
028import java.util.Map;
029
030import static io.stallion.Context.*;
031
032
033public class SandboxedContext {
034
035    private JsPluginSettings pluginSettings;
036    private SandboxedDataAccess dataAccess;
037    private String pluginFolder;
038    private Sandbox sandbox;
039    private Site site;
040
041
042    public SandboxedContext(String pluginFolder, Sandbox sandbox, JsPluginSettings pluginSettings) {
043        this.pluginFolder = pluginFolder;
044        this.pluginSettings = pluginSettings;
045        this.dataAccess = new SandboxedDataAccess(sandbox);
046        this.sandbox = sandbox;
047        this.site = new Site();
048
049        this.site
050                .setName(settings().getSiteName())
051                .setTitle(settings().getDefaultTitle())
052                .setUrl(settings().getSiteUrl());
053    }
054
055    public String renderTemplate(String template, Map<String, Object> context) {
056        return TemplateRenderer.instance().renderSandboxedTemplate(sandbox, template, context);
057    }
058
059
060    public JsPluginSettings getPluginSettings() {
061        return pluginSettings;
062    }
063
064    public IUser getUser() {
065        if (sandbox.getUsers().isCanAccess()) {
066            return Context.getUser();
067        } else {
068            return new EmptyUser();
069        }
070    }
071
072    public SandboxedRequest getRequest() {
073        return Context.getRequest().getSandboxedRequest(sandbox);
074    }
075
076    public SandboxedDataAccess getDal() {
077        return dataAccess;
078    }
079    public SandboxedDataAccess getDataAccess() {
080        return dataAccess;
081    }
082
083    public SandboxedResponse getResponse() {
084        return Context.getResponse().getSandboxedResponse();
085    }
086
087    public String getPluginFolder() {
088        return pluginFolder;
089    }
090
091    public SandboxedContext setPluginFolder(String pluginFolder) {
092        this.pluginFolder = pluginFolder;
093        return this;
094    }
095
096    public Site getSite() {
097        return site;
098    }
099
100    public SandboxedContext setSite(Site site) {
101        this.site = site;
102        return this;
103    }
104}