All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kryshchuk.imcollector.auth.deadbolt.AbstractDeadboltHandler Maven / Gradle / Ivy

The newest version!
/*
 * imcollector Authentication
 * Copyright (C) 2013  Yuriy Kryshchuk
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package com.kryshchuk.imcollector.auth.deadbolt;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feth.play.module.pa.PlayAuthenticate;
import com.feth.play.module.pa.user.AuthUserIdentity;

import play.libs.F;
import play.libs.F.Promise;
import play.mvc.Result;
import play.mvc.Results;
import play.mvc.Http.Context;
import be.objectify.deadbolt.core.models.Subject;
import be.objectify.deadbolt.java.DeadboltHandler;
import be.objectify.deadbolt.java.DynamicResourceHandler;

/**
 * @author yura
 * @since 1.0
 */
public abstract class AbstractDeadboltHandler extends Results implements DeadboltHandler {

  protected final Logger logger = LoggerFactory.getLogger(getClass());

  @Override
  public DynamicResourceHandler getDynamicResourceHandler(final Context ctx) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Promise onAuthFailure(final Context context, final String content) {
    // if the user has a cookie with a valid user and the local user has
    // been deactivated/deleted in between, it is possible that this gets
    // shown. You might want to consider to sign the user out in this case.
    return F.Promise.promise(() -> forbidden("Forbidden"));
  }

  @Override
  public Promise beforeAuthCheck(final Context context) {
    if (PlayAuthenticate.isLoggedIn(context.session())) {
      // user is logged in
      return F.Promise.pure(null);
    } else {
      logger.trace("User is not logged in");

      // call this if you want to redirect your visitor to the page that
      // was requested before sending him to the login page
      // if you don't call this, the user will get redirected to the page
      // defined by your resolver
      final String originalUrl = PlayAuthenticate.storeOriginalUrl(context);

      context.flash().put("error", "You need to log in first, to view '" + originalUrl + "'");
      return F.Promise.promise(() -> redirect(PlayAuthenticate.getResolver().login()));
    }
  }

  @Override
  public Promise getSubject(final Context context) {
    final AuthUserIdentity u = PlayAuthenticate.getUser(context);
    if (u == null) {
      return F.Promise.pure(null);
    } else {
      // Caching might be a good idea here
      logger.info("Need to find user by identity {}", u);
      return F.Promise.promise(() -> getSubject(u));
    }
  }

  protected abstract Subject getSubject(AuthUserIdentity identity);

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy