package controllers;

import java.util.ArrayList;
import java.util.Date;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.SqlQuery;
import com.avaje.ebean.SqlRow;

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

import models.User;

import play.Logger;
import play.mvc.Http.Context;
import play.mvc.Http.Session;
import play.mvc.Result;
import play.mvc.Security;

public class Secured extends Security.Authenticator {

	@Override
	public String getUsername(final Context ctx) {
        return getUsername(ctx, true);
	}

    public String getUsername(final Context ctx, boolean allow_ip) {
        Logger.debug("Secured::getUsername " + ctx + ", " + allow_ip);
        final AuthUser u = PlayAuthenticate.getUser(ctx.session());

        if (u != null) {
            User the_user = User.findByAuthUserIdentity(u);
            if (the_user != null) {
				// If a user is logged in already, check the session timeout.
				// If there is no timeout, or we can't parse it, or it's too old,
				// don't count the user as being logged in.
				Session sess = ctx.session();
				if (sess.get("timeout") != null) {
					try
					{
						long timeout = Long.parseLong(sess.get("timeout"));
						if (System.currentTimeMillis() - timeout < 1000 * 60 * 30) {
							sess.put("timeout", "" + System.currentTimeMillis());
							return the_user.email;
						}
					}
					catch (NumberFormatException e) {
					}
				}
			}
        }

        // If we don't have a logged-in user, try going by IP address.
        if (allow_ip) {
            String sql = "select ip from allowed_ips where ip like :ip";
            SqlQuery sqlQuery = Ebean.createSqlQuery(sql);
            String address = Application.getRemoteIp();
            sqlQuery.setParameter("ip", address);

            // execute the query returning a List of MapBean objects
            SqlRow result = sqlQuery.findUnique();

            if (result != null) {
                return address;
            }
        }

        return null;
    }

	@Override
	public Result onUnauthorized(final Context ctx) {
        PlayAuthenticate.storeOriginalUrl(ctx);
		return redirect(routes.Public.index());
	}
}

@@ -161,17 +161,16 @@
 SqlRow;%0A
-%0A
 import c
@@ -253,24 +253,52 @@
 .AuthUser;%0A%0A
+import models.Organization;%0A
 import model
@@ -1661,16 +1661,44 @@
 like :ip
+ and organization_id=:org_id
 %22;%0A     
@@ -1856,16 +1856,90 @@
 ddress);
+%0A            sqlQuery.setParameter(%22org_id%22, Organization.getByHost().id);
 %0A%0A      

Check allowed_ips based on organization