title: Using user id with other model classes
body: <p>I am using the play-authenticate plugin in my play-framework project. I want to be able to use the user ID (of the user currently logged in) from the User.java model class of the plugin. </p>
  
  <pre><code>    @Id
      public Long id;
  </code></pre>
  
  <p>I want to do this so that when users are creating entries in a separate model class I can store the user who has created these entries. Is there functionality in place to access this information or would I need to write an additional method in the User class to return the active user?</p>
  
  <pre><code>package controllers;
  
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import models.*;
  import models.User;
  import play.Routes;
  import play.data.Form;
  import play.mvc.*;
  import play.mvc.Http.Response;
  import play.mvc.Http.Session;
  import providers.MyUsernamePasswordAuthProvider;
  import providers.MyUsernamePasswordAuthProvider.MyLogin;
  import providers.MyUsernamePasswordAuthProvider.MySignup;
  import play.data.*;
  import views.html.*;
  import play.*;
  import be.objectify.deadbolt.actions.Restrict;
  import com.feth.play.module.pa.PlayAuthenticate;
  import com.feth.play.module.pa.providers.password.UsernamePasswordAuthProvider;
  
  public class Application extends Controller {
  
      /*Part of the Play-Authenticate authentication plugin for the Play Framework.*/
      public static final String FLASH_MESSAGE_KEY = "message";
      public static final String FLASH_ERROR_KEY = "error";
      public static final String USER_ROLE = "user";
  
  
  
  
      public static User getLocalUser(final Session session) {
          final User localUser = User.findByAuthUserIdentity(PlayAuthenticate
                  .getUser(session));
          return localUser;
      }
  
      /*Source: https://github.com/joscha/play-authenticate*/
      @Restrict(Application.USER_ROLE)
      public static Result restricted() {
          final User localUser = getLocalUser(session());
          return ok(journeyManagement.render(localUser));
      }
  
      /*Source: https://github.com/joscha/play-authenticate*/
      @Restrict(Application.USER_ROLE)
      public static Result profile() {
          final User localUser = getLocalUser(session());
          return ok(profile.render(localUser));
      }
  
      /*Source: https://github.com/joscha/play-authenticate*/
      public static Result login() {
          return ok(login.render(MyUsernamePasswordAuthProvider.LOGIN_FORM));
      }
  
      /*Source: https://github.com/joscha/play-authenticate*/
      public static Result doLogin() {
          com.feth.play.module.pa.controllers.Authenticate.noCache(response());
          final Form&lt;MyLogin&gt; filledForm = MyUsernamePasswordAuthProvider.LOGIN_FORM
                  .bindFromRequest();
          if (filledForm.hasErrors()) {
              // User did not fill everything properly
              return badRequest(login.render(filledForm));
          } else {
              // Everything was filled
              return UsernamePasswordAuthProvider.handleLogin(ctx());
          }
      }
  
      /*Source: https://github.com/joscha/play-authenticate*/
      public static Result signup() {
          return ok(signup.render(MyUsernamePasswordAuthProvider.SIGNUP_FORM));
      }
  
      /*Source: https://github.com/joscha/play-authenticate*/
      public static Result jsRoutes() {
          return ok(
                  Routes.javascriptRouter("jsRoutes",
                          controllers.routes.javascript.Signup.forgotPassword()))
                  .as("text/javascript");
      }
  
      /*Source: https://github.com/joscha/play-authenticate*/
      public static Result doSignup() {
          com.feth.play.module.pa.controllers.Authenticate.noCache(response());
          final Form&lt;MySignup&gt; filledForm = MyUsernamePasswordAuthProvider.SIGNUP_FORM
                  .bindFromRequest();
          if (filledForm.hasErrors()) {
              // User did not fill everything properly
              return badRequest(signup.render(filledForm));
          } else {
              // Everything was filled
              // do something with your part of the form before handling the user
              // signup
              return UsernamePasswordAuthProvider.handleSignup(ctx());
          }
      }
  
      /*Source: https://github.com/joscha/play-authenticate*/
      public static String formatTimestamp(final long t) {
          return new SimpleDateFormat("yyyy-dd-MM HH:mm:ss").format(new Date(t));
      }
  
  }
  </code></pre>
  
  <p>Update: </p>
  
  <pre><code>package models;
  
  import play.mvc.Http.Session;
  import controllers.*;
  import java.util.*;
  import play.db.ebean.*;
  import play.data.validation.Constraints.*;
  import javax.persistence.*;
  import play.data.format.*;
  import com.avaje.ebean.*;
  import java.text.*;
  
  
  
  
  @Entity
  public class Journey extends Model { 
  
  
      public SimpleDateFormat simpleTimeFormat = new SimpleDateFormat("hh:mm");
      public SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yy");
  
  
      @Id
      public Long id;
  
      @Required
      public String start_loc;
  
      @Required
      public String end_loc;
  
      @Required
      public String participant_type;
  
      @Required
      public String date = simpleDateFormat.format(new Date());
  
      @Required
      public String time = simpleTimeFormat.format(new Date());
  
      @ManyToOne
      public User createUser;
  
      @ManyToOne
      public User modifyUser;
  
      public static Finder&lt;Long,Journey&gt; find = new Finder(
          Long.class, Journey.class
          );
  
      public static List&lt;Journey&gt; all() {
          return find.all();
      }
  
      public static void create(Journey journey) {
          journey.save();
      }
  
      public static void delete(Long id) {
          find.ref(id).delete();
      }
  
      public static List&lt;Journey&gt; searchByAddress(String address) {
          return find.where().ilike("start_loc", "%"+address+"%").findList();
      }
  
      public void save() {
          User logged = Application.getLocalUser(session());
          if (logged != null) {
              this.createUser = logged;
              this.modifyUser = logged;
          }
          super.save();
      }
  
      public void update(Object o) {
           User logged = Application.getLocalUser(session());
          if (logged != null) {
              this.modifyUser = logged;
          }
          super.update(o);
      }
  }
  </code></pre>