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; 019 020import io.stallion.boot.AppContextLoader; 021import io.stallion.dataAccess.DataAccessRegistry; 022import io.stallion.dataAccess.db.DB; 023import io.stallion.plugins.PluginRegistry; 024import io.stallion.requests.*; 025import io.stallion.restfulEndpoints.RestEndpointBase; 026import io.stallion.settings.Settings; 027import io.stallion.templating.TemplateRenderer; 028import io.stallion.users.*; 029 030import static io.stallion.utils.Literals.empty; 031import static io.stallion.utils.Literals.emptyInstance; 032 033/** 034 * A static helper class providing short-cut access to the most common services and data objects running 035 * in the current application and request context. 036 * 037 * The static methods from this class are imported by default into all new source code files. 038 * 039 */ 040public class Context { 041 private static final ThreadLocal<StRequest> _request = new ThreadLocal<StRequest>(); 042 private static final ThreadLocal<StResponse> _response = new ThreadLocal<StResponse>(); 043 044 045 public static AppContextLoader app() { 046 return AppContextLoader.instance(); 047 } 048 049 public static DB db() { 050 return DB.instance(); 051 } 052 public static DB getDb() { 053 return DB.instance(); 054 } 055 056 public static UserController getUserController() { 057 return UserController.instance(); 058 } 059 public static UserController userController() { 060 return UserController.instance(); 061 } 062 063 public static TemplateRenderer templateRenderer() { 064 return TemplateRenderer.instance(); 065 } 066 public static TemplateRenderer getTemplateRenderer() { 067 return TemplateRenderer.instance(); 068 } 069 070 public static PluginRegistry pluginRegistry() { 071 return PluginRegistry.instance(); 072 } 073 074 public static PluginRegistry getPluginRegistry() { 075 return PluginRegistry.instance(); 076 } 077 078 public static DataAccessRegistry dal() { return DataAccessRegistry.instance(); } 079 public static DataAccessRegistry getDal() { return DataAccessRegistry.instance(); } 080 081 public static DataAccessRegistry dataAccess() { return DataAccessRegistry.instance(); } 082 public static DataAccessRegistry getDataAccess() { return DataAccessRegistry.instance(); } 083 084 /** 085 * Alias for getRequest(); 086 * @return 087 */ 088 public static IRequest request() { 089 if (_request.get() == null) { 090 return new EmptyRequest(); 091 } 092 return (StRequest)_request.get(); 093 } 094 095 /** 096 * The current request, as stashed on a thread local variable. Returns a new EmptyRequest() object 097 * if called from outside a web request 098 * @return 099 */ 100 public static IRequest getRequest() { 101 return request(); 102 } 103 104 /** 105 * Alias for getResponse(); 106 * @return 107 */ 108 public static StResponse response() { 109 if (_response.get() == null) { 110 return new EmptyResponse(); 111 } 112 return (StResponse)_response.get(); 113 114 } 115 116 /** 117 * The current response, as stashed on a thread local variable. Returns a new EmptyResponse() object 118 * if called from outside a web request 119 * @return 120 */ 121 public static StResponse getResponse() { 122 return response(); 123 } 124 125 /** 126 * Determines if the currently active user, based on the current active request, is allowed 127 * to access the passed in endpoint. 128 * 129 * For a scoped, endpoint request, it will check the scope. 130 * 131 * For all other requests, it will check to see if the endpoint requires a mininum role, and 132 * if the user has that role. 133 * 134 * @param endpoint 135 * @return 136 */ 137 public static boolean currentUserCanAccessEndpoint(RestEndpointBase endpoint) { 138 // Scoped request 139 if (request().isScoped()) { 140 // Endpoint not scoped, deny to scoped requests 141 if (!endpoint.isScoped()) { 142 return false; 143 } 144 // Endpoint has scope that the current user does not, deny 145 if (!"any".equals(endpoint.getScope()) && request().getScopes().contains(endpoint.getScope())) { 146 return false; 147 } 148 } 149 if (!emptyInstance(endpoint.getRole())) { 150 // User does not have role, deny 151 if (!getUser().isInRole(endpoint.getRole())) { 152 return false; 153 } 154 } 155 return true; 156 } 157 158 @Deprecated 159 public static AppContextLoader getApp() { 160 return app(); 161 } 162 163 public static void setRequest(StRequest request) { 164 _request.set(request); 165 } 166 167 public static void setResponse(StResponse response) { 168 _response.set(response); 169 } 170 171 public static Settings settings() { 172 return getSettings(); 173 } 174 175 public static Settings getSettings() { 176 if (Settings.isNull()) { 177 return new Settings(); 178 } else { 179 return Settings.instance(); 180 } 181 } 182 183 184 185 public static void resetThreadContext() { 186 setUser(new EmptyUser()); 187 setOrg(new EmptyOrg()); 188 } 189 190 public static String getValetEmail() { 191 return _request.get().getValetEmail(); 192 } 193 194 public static Long getValetUserId() { 195 return _request.get().getValetUserId(); 196 } 197 198 public static void setValet(Long valetUserId, String valetEmail) { 199 _request.get().setValetUserId(valetUserId); 200 _request.get().setValetEmail(valetEmail); 201 202 } 203 204 public static void setUser(IUser user) { 205 _request.get().setUser(user); 206 } 207 208 public static void setOrg(IOrg org) { 209 _request.get().setOrg(org); 210 } 211 212 public static IUser getUser() { 213 return request().getUser(); 214 } 215 216 public static IOrg getOrg() { 217 return request().getOrg(); 218 } 219 220 221 222} 223