Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Turns Deadbolt2 into a Play2.0 plugin and fix hot-reloading in dev mode #2

Merged
merged 1 commit into from Mar 30, 2012
Jump to file or symbol
Failed to load files and symbols.
+150 −79
Split
@@ -15,75 +15,34 @@
*/
package be.objectify.deadbolt;
-import be.objectify.deadbolt.models.RoleHolder;
-import be.objectify.deadbolt.utils.RequestUtils;
-import play.Configuration;
-import play.Logger;
-import play.Play;
-import play.cache.Cache;
-import play.mvc.Http;
-
import java.util.List;
import java.util.concurrent.Callable;
import java.util.regex.Pattern;
+import play.Logger;
+import play.cache.Cache;
+import play.mvc.Http;
+import be.objectify.deadbolt.models.RoleHolder;
+import be.objectify.deadbolt.utils.PluginUtils;
+import be.objectify.deadbolt.utils.RequestUtils;
+
/**
- * Provides the entry point for view-level annotations. Also loads and stores the global {@link DeadboltHandler} given
- * in application.conf.
+ * Provides the entry point for view-level annotations.
*
* @author Steve Chaloner (steve@objectify.be)
*/
public class Deadbolt
{
- public static final String DEADBOLT_HANDLER_KEY = "deadbolt.handler";
-
- public static final String CACHE_USER = "deadbolt.cache-user";
-
- public static boolean CACHE_USER_PER_REQUEST = false;
-
- public static final DeadboltHandler DEADBOLT_HANDLER;
-
- static
- {
- Configuration configuration = Play.application().configuration();
-
- if (!configuration.keys().contains(DEADBOLT_HANDLER_KEY))
- {
- throw configuration.reportError(DEADBOLT_HANDLER_KEY,
- "A Deadbolt handler must be defined",
- null);
- }
-
- String deadboltHandlerName = null;
- try
- {
-
- deadboltHandlerName = configuration.getString(DEADBOLT_HANDLER_KEY);
- DEADBOLT_HANDLER = (DeadboltHandler)Class.forName(deadboltHandlerName).newInstance();
- }
- catch (Exception e)
- {
- throw configuration.reportError(DEADBOLT_HANDLER_KEY,
- "Error creating Deadbolt handler: " + deadboltHandlerName,
- e);
- }
-
- if (configuration.keys().contains(CACHE_USER))
- {
- CACHE_USER_PER_REQUEST = configuration.getBoolean(CACHE_USER);
- }
- }
-
/**
* Used for restrict tags in the template.
*
* @param roles a list of String arrays. Within an array, the roles are ANDed. The arrays in the list are OR'd.
* @return true if the view can be accessed, otherwise false
*/
- public static boolean viewRestrict(List<String[]> roles)
+ public static boolean viewRestrict(List<String[]> roles) throws Throwable
{
boolean roleOk = false;
- RoleHolder roleHolder = RequestUtils.getRoleHolder(DEADBOLT_HANDLER,
+ RoleHolder roleHolder = RequestUtils.getRoleHolder(PluginUtils.getHandler(),
Http.Context.current());
for (int i = 0; !roleOk && i < roles.size(); i++)
{
@@ -102,9 +61,9 @@ public static boolean viewRestrict(List<String[]> roles)
* @return true if the view can be accessed, otherwise false
*/
public static boolean viewDynamic(String name,
- String meta)
+ String meta) throws Throwable
{
- DynamicResourceHandler resourceHandler = DEADBOLT_HANDLER.getDynamicResourceHandler(Http.Context.current());
+ DynamicResourceHandler resourceHandler = PluginUtils.getHandler().getDynamicResourceHandler(Http.Context.current());
boolean allowed = false;
if (resourceHandler == null)
{
@@ -114,7 +73,7 @@ public static boolean viewDynamic(String name,
{
if (resourceHandler.isAllowed(name,
meta,
- DEADBOLT_HANDLER,
+ PluginUtils.getHandler(),
Http.Context.current()))
{
allowed = true;
@@ -129,9 +88,9 @@ public static boolean viewDynamic(String name,
*
* @return true if the view can be accessed, otherwise false
*/
- public static boolean viewRoleHolderPresent()
+ public static boolean viewRoleHolderPresent() throws Throwable
{
- RoleHolder roleHolder = DEADBOLT_HANDLER.getRoleHolder(Http.Context.current());
+ RoleHolder roleHolder = PluginUtils.getHandler().getRoleHolder(Http.Context.current());
boolean allowed = false;
if (roleHolder != null)
@@ -150,7 +109,7 @@ public static boolean viewPattern(String value,
switch (patternType)
{
case REGEX:
- allowed = DeadboltAnalyzer.checkRegexPattern(DEADBOLT_HANDLER.getRoleHolder(Http.Context.current()),
+ allowed = DeadboltAnalyzer.checkRegexPattern(PluginUtils.getHandler().getRoleHolder(Http.Context.current()),
getPattern(value));
break;
case TREE:
@@ -0,0 +1,75 @@
+package be.objectify.deadbolt;
+
+import play.Application;
+import play.Configuration;
+import play.Plugin;
+
+/**
+ * A play plugin that provides authorization mechanism for defining access rights
+ * to certain controller methods or parts of a view using a simple AND/OR/NOT syntax.
+ *
+ */
+public class DeadboltPlugin extends Plugin {
+
+ public static final String DEADBOLT_HANDLER_KEY = "deadbolt.handler";
+
+ public static final String CACHE_USER = "deadbolt.cache-user";
+
+ private boolean cacheUserPerRequestEnabled = false;
+ private DeadboltHandler deadboltHandler;
+
+ private final Application application;
+
+ public DeadboltPlugin(Application application) {
+ this.application = application;
+ }
+
+ /**
+ * Reads the configuration file and initialize the {@link DeadboltHandler}
+ */
+ @Override
+ public void onStart() {
+ Configuration configuration = application.configuration();
+ if (!configuration.keys().contains(DEADBOLT_HANDLER_KEY))
+ {
+ throw configuration.reportError(DEADBOLT_HANDLER_KEY,
+ "A Deadbolt handler must be defined",
+ null);
+ }
+
+ String deadboltHandlerName = null;
+ try
+ {
+ deadboltHandlerName = configuration.getString(DEADBOLT_HANDLER_KEY);
+ deadboltHandler = (DeadboltHandler) Class.forName(deadboltHandlerName, true, application.classloader()).newInstance();
+ }
+ catch (Exception e)
+ {
+ throw configuration.reportError(DEADBOLT_HANDLER_KEY,
+ "Error creating Deadbolt handler: " + deadboltHandlerName,
+ e);
+ }
+
+ if (configuration.keys().contains(CACHE_USER))
+ {
+ cacheUserPerRequestEnabled = configuration.getBoolean(CACHE_USER);
+ }
+ }
+
+ /**
+ * Getter for the cache-user configuration option
+ * @return boolean cache-user value
+ */
+ public boolean isCacheUserPerRequestEnabled() {
+ return cacheUserPerRequestEnabled;
+ }
+
+ /**
+ * Getter for the registered Deadbolt Handler
+ * @return DeadboltHandler registered Deadbolt handler
+ */
+ public DeadboltHandler getDeadboltHandler() {
+ return deadboltHandler;
+ }
+
+}
@@ -19,6 +19,7 @@
import be.objectify.deadbolt.DeadboltAnalyzer;
import be.objectify.deadbolt.DeadboltHandler;
import be.objectify.deadbolt.models.RoleHolder;
+import be.objectify.deadbolt.utils.PluginUtils;
import be.objectify.deadbolt.utils.RequestUtils;
import play.Configuration;
import play.Logger;
@@ -48,7 +49,7 @@
* @param <C> the actual class of the DeadboltHandler
* @return an instance of DeadboltHandler.
*/
- protected <C extends DeadboltHandler> DeadboltHandler getDeadboltHandler(Class<C> deadboltHandlerClass)
+ protected <C extends DeadboltHandler> DeadboltHandler getDeadboltHandler(Class<C> deadboltHandlerClass) throws Throwable
{
DeadboltHandler deadboltHandler;
if (deadboltHandlerClass != null
@@ -66,7 +67,7 @@
}
else
{
- deadboltHandler = Deadbolt.DEADBOLT_HANDLER;
+ deadboltHandler = PluginUtils.getHandler();
}
return deadboltHandler;
}
@@ -0,0 +1,30 @@
+package be.objectify.deadbolt.utils;
+
+import static play.api.Play.unsafeApplication;
+import static play.libs.Scala.orNull;
+import play.Logger;
+import be.objectify.deadbolt.DeadboltHandler;
+import be.objectify.deadbolt.DeadboltPlugin;
+
+public class PluginUtils
+{
+ public static DeadboltPlugin getPlugin() throws Exception
+ {
+ DeadboltPlugin plugin = orNull(unsafeApplication().plugin(DeadboltPlugin.class));
+ if (plugin == null) {
+ throw new Exception("Deadbolt plugin was not registered or disabled (check you conf/play.plugins file)");
+ }
+ return plugin;
+ }
+
+ public static boolean isUserCacheEnabled() throws Exception
+ {
+ DeadboltPlugin p = getPlugin();
+ return (p != null ? p.isCacheUserPerRequestEnabled() : false);
+ }
+
+ public static DeadboltHandler getHandler() throws Exception {
+ DeadboltPlugin p = getPlugin();
+ return (p != null ? p.getDeadboltHandler() : null);
+ }
+}
@@ -15,10 +15,11 @@
*/
package be.objectify.deadbolt.utils;
-import be.objectify.deadbolt.Deadbolt;
+import play.Logger;
+import play.mvc.Http;
import be.objectify.deadbolt.DeadboltHandler;
+import be.objectify.deadbolt.DeadboltPlugin;
import be.objectify.deadbolt.models.RoleHolder;
-import play.mvc.Http;
/**
* @author Steve Chaloner (steve@objectify.be)
@@ -33,26 +34,30 @@ private RequestUtils()
public static final RoleHolder getRoleHolder(DeadboltHandler deadboltHandler,
Http.Context ctx)
{
- Object cachedUser = ctx.args.get(Deadbolt.CACHE_USER);
- RoleHolder roleHolder;
- if (Deadbolt.CACHE_USER_PER_REQUEST)
- {
- if (cachedUser != null)
- {
- roleHolder = (RoleHolder)cachedUser;
- }
- else
- {
- roleHolder = deadboltHandler.getRoleHolder(ctx);
- ctx.args.put(Deadbolt.CACHE_USER,
- roleHolder);
- }
+ Object cachedUser = ctx.args.get(DeadboltPlugin.CACHE_USER);
+ RoleHolder roleHolder = null;
+ try {
+ if (PluginUtils.isUserCacheEnabled())
+ {
+ if (cachedUser != null)
+ {
+ roleHolder = (RoleHolder)cachedUser;
+ }
+ else
+ {
+ roleHolder = deadboltHandler.getRoleHolder(ctx);
+ ctx.args.put(DeadboltPlugin.CACHE_USER,
+ roleHolder);
+ }
+ }
+ else
+ {
+ roleHolder = deadboltHandler.getRoleHolder(ctx);
+ }
}
- else
- {
- roleHolder = deadboltHandler.getRoleHolder(ctx);
+ catch (Exception e) {
+ Logger.error(e.getMessage());
}
-
return roleHolder;
}
}
@@ -0,0 +1 @@
+10000:be.objectify.deadbolt.DeadboltPlugin