0

我在我的 play-framework 项目中使用 play-authenticate 插件。我希望能够使用插件的 User.java 模型类中的用户 ID(当前登录的用户)。

    @Id
    public Long id;

我想这样做,以便当用户在单独的模型类中创建条目时,我可以存储创建这些条目的用户。是否有访问此信息的功能,或者我是否需要在 User 类中编写一个附加方法来返回活动用户?

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<MyLogin> 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<MySignup> 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));
    }

}

更新:

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<Long,Journey> find = new Finder(
        Long.class, Journey.class
        );

    public static List<Journey> 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<Journey> 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);
    }
}
4

1 回答 1

1

您不需要手动编写它,例如,如果您有一个Book.java模型,您可以为示例添加字段updatedBy以识别上次编辑记录的人,此外您可以覆盖Model's方法save()update(Object o)确保这些字段将是总是更新而无需额外的努力。

@Entity
public class Book extends Model {

    @Id
    public Integer id;

    @ManyToOne
    public User createUser;

    @ManyToOne
    public User modifyUser;

    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);
    }

    // other fields/methods
}
于 2013-02-27T19:40:09.230 回答