// Path: app/actions/CurrentUser.java
// public class CurrentUser extends Action.Simple
// {
//     @Override
//     public Result call(Http.Context ctx) throws Throwable
//     {
//         accessUser(ctx);
//         return delegate.call(ctx);
//     }
// 
//     private static User accessUser(Http.Context ctx)
//     {
//         User user = (User)ctx.args.get("user");
//         if (user == null)
//         {
//             String userName = ctx.session().get("userName");
//             if (!StringUtils.isEmpty(userName))
//             {
//                 user = User.findByUserName(userName);
//                 if (user != null)
//                 {
//                     ctx.args.put("user",
//                                  user);
//                 }
//             }
//         }
// 
//         return user;
//     }
// 
//     public static User currentUser()
//     {
//         return currentUser(Http.Context.current());
//     }
// 
//     public static User currentUser(Http.Context context)
//     {
//         return accessUser(context);
//     }
// }
// 
// Path: app/controllers/Application.java
// @With(CurrentUser.class)
// public class Application extends AbstractController
// {
//     public static Result index()
//     {
//         final List<Module> mostRecentModules = Module.findMostRecent(5);
//         final List<Module> highestRatedModules = Collections.emptyList(); // best way to use the rating algorithm for the db call?  pre-calculate before storing?
//         final List<FeaturedModule> featuredModules = FeaturedModule.getAll();
//         final List<PlayVersion> playVersions = PlayVersion.getAll();
// 
//         return ok(index.render(currentUser(),
//                                mostRecentModules,
//                                highestRatedModules,
//                                featuredModules,
//                                playVersions,
//                                Module.count()));
//     }
// 
//     public static Result login()
//     {
//         return ok(login.render(form(Login.class)));
//     }
// 
//     public static Result authenticate()
//     {
//         Form<Login> loginForm = form(Login.class).bindFromRequest();
//         Result result;
//         if (loginForm.hasErrors())
//         {
//             result = badRequest(login.render(loginForm));
//         }
//         else
//         {
//             session("userName", loginForm.get().userName);
//             result = redirect(routes.Application.index());
//         }
// 
//         return result;
//     }
// 
//     public static Result register()
//     {
//         return ok(register.render(form(Register.class)));
//     }
// 
//     public static Result createAccount()
//     {
//         Form<Register> registerForm = form(Register.class).bindFromRequest();
//         Result result;
//         if (registerForm.hasErrors())
//         {
//             result = badRequest(register.render(registerForm));
//         }
//         else
//         {
//             Register register = registerForm.get();
//             new UserServices().createUser(register.userName, register.displayName, register.password);
//             session("userName", register.userName);
//             result = redirect(routes.Application.index());
//         }
//         return result;
//     }
// 
//     public static Result logout()
//     {
//         session().clear();
//         return redirect(routes.Application.index());
//     }
// 
//     /**
//      * Returns the sitemap of the application
//      */
//     public static Result sitemap()
//     {
//         List<Sitemap> list = SitemapServices.generateSitemap(request());
//         return ok(sitemap.render(list)).as("application/xml");
//     }
// }


import actions.CurrentUser;
import be.objectify.deadbolt.DeadboltHandler;
import be.objectify.deadbolt.DynamicResourceHandler;
import be.objectify.deadbolt.models.RoleHolder;
import controllers.Application;
import controllers.routes;
import play.core.Router;
import play.mvc.Http;
import play.mvc.Result;
import play.mvc.Results;

/*
 * Copyright 2012 The Play! Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package security;


/**
 * @author Steve Chaloner
 */
public class RepoDeadboltHandler extends Results implements DeadboltHandler
{
    @Override
    public Result beforeRoleCheck(Http.Context context)
    {
        return null;
    }

    @Override
    public RoleHolder getRoleHolder(Http.Context context)
    {
        return CurrentUser.currentUser(context);
    }

    @Override
    public Result onAccessFailure(Http.Context context,
                                  String s)
    {
        Http.Context.current.set(context);

// Path: app/actions/CurrentUser.java
// public class CurrentUser extends Action.Simple
// {
//     @Override
//     public Result call(Http.Context ctx) throws Throwable
//     {
//         accessUser(ctx);
//         return delegate.call(ctx);
//     }
// 
//     private static User accessUser(Http.Context ctx)
//     {
//         User user = (User)ctx.args.get("user");
//         if (user == null)
//         {
//             String userName = ctx.session().get("userName");
//             if (!StringUtils.isEmpty(userName))
//             {
//                 user = User.findByUserName(userName);
//                 if (user != null)
//                 {
//                     ctx.args.put("user",
//                                  user);
//                 }
//             }
//         }
// 
//         return user;
//     }
// 
//     public static User currentUser()
//     {
//         return currentUser(Http.Context.current());
//     }
// 
//     public static User currentUser(Http.Context context)
//     {
//         return accessUser(context);
//     }
// }
// 
// Path: app/controllers/Application.java
// @With(CurrentUser.class)
// public class Application extends AbstractController
// {
//     public static Result index()
//     {
//         final List<Module> mostRecentModules = Module.findMostRecent(5);
//         final List<Module> highestRatedModules = Collections.emptyList(); // best way to use the rating algorithm for the db call?  pre-calculate before storing?
//         final List<FeaturedModule> featuredModules = FeaturedModule.getAll();
//         final List<PlayVersion> playVersions = PlayVersion.getAll();
// 
//         return ok(index.render(currentUser(),
//                                mostRecentModules,
//                                highestRatedModules,
//                                featuredModules,
//                                playVersions,
//                                Module.count()));
//     }
// 
//     public static Result login()
//     {
//         return ok(login.render(form(Login.class)));
//     }
// 
//     public static Result authenticate()
//     {
//         Form<Login> loginForm = form(Login.class).bindFromRequest();
//         Result result;
//         if (loginForm.hasErrors())
//         {
//             result = badRequest(login.render(loginForm));
//         }
//         else
//         {
//             session("userName", loginForm.get().userName);
//             result = redirect(routes.Application.index());
//         }
// 
//         return result;
//     }
// 
//     public static Result register()
//     {
//         return ok(register.render(form(Register.class)));
//     }
// 
//     public static Result createAccount()
//     {
//         Form<Register> registerForm = form(Register.class).bindFromRequest();
//         Result result;
//         if (registerForm.hasErrors())
//         {
//             result = badRequest(register.render(registerForm));
//         }
//         else
//         {
//             Register register = registerForm.get();
//             new UserServices().createUser(register.userName, register.displayName, register.password);
//             session("userName", register.userName);
//             result = redirect(routes.Application.index());
//         }
//         return result;
//     }
// 
//     public static Result logout()
//     {
//         session().clear();
//         return redirect(routes.Application.index());
//     }
// 
//     /**
//      * Returns the sitemap of the application
//      */
//     public static Result sitemap()
//     {
//         List<Sitemap> list = SitemapServices.generateSitemap(request());
//         return ok(sitemap.render(list)).as("application/xml");
//     }
// }


// Path: app/security/RepoDeadboltHandler.java
import actions.CurrentUser;
import be.objectify.deadbolt.DeadboltHandler;
import be.objectify.deadbolt.DynamicResourceHandler;
import be.objectify.deadbolt.models.RoleHolder;
import controllers.Application;
import controllers.routes;
import play.core.Router;
import play.mvc.Http;
import play.mvc.Result;
import play.mvc.Results;

/*
 * Copyright 2012 The Play! Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package security;


/**
 * @author Steve Chaloner
 */
public class RepoDeadboltHandler extends Results implements DeadboltHandler
{
    @Override
    public Result beforeRoleCheck(Http.Context context)
    {
        return null;
    }

    @Override
    public RoleHolder getRoleHolder(Http.Context context)
    {
        return CurrentUser.currentUser(context);
    }

    @Override
    public Result onAccessFailure(Http.Context context,
                                  String s)
    {
        Http.Context.current.set(context);

        return redirect(routes.Application.index());
