gpt4 book ai didi

java - 将用户 ID 与其他模型类一起使用

转载 作者:行者123 更新时间:2023-12-01 14:48:39 26 4
gpt4 key购买 nike

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

    @Id
public Long id;

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

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

最佳答案

您不需要手动编写它,例如,如果您有一个 Book.java 模型,您可以为示例 updatedBy 添加字段来识别谁是上次编辑记录时,您还可以重写 Model 的 方法 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
}

关于java - 将用户 ID 与其他模型类一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15118528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com