Skip to content

Commit

Permalink
Implement User Login and Logout Support in Doecean Framework with Mon…
Browse files Browse the repository at this point in the history
…goDB (close #836)
  • Loading branch information
goodjava committed Apr 29, 2024
1 parent 25d0904 commit 0a58e95
Show file tree
Hide file tree
Showing 16 changed files with 439 additions and 63 deletions.
87 changes: 50 additions & 37 deletions jcommon/docean-plugin/docean-plugin-mongodb/pom.xml
Original file line number Diff line number Diff line change
@@ -1,39 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>run.mone</groupId>
<artifactId>docean-plugin</artifactId>
<version>1.6.0-jdk21-SNAPSHOT</version>
</parent>
<artifactId>docean-plugin-mongodb</artifactId>
<version>1.5.0-jdk21-SNAPSHOT</version>
<dependencies>

<dependency>
<groupId>run.mone</groupId>
<artifactId>docean-plugin-config</artifactId>
</dependency>

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.8.0</version>
</dependency>

<dependency>
<groupId>run.mone</groupId>
<artifactId>catPlugin</artifactId>
<version>1.6.0-jdk21-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>dev.morphia.morphia</groupId>
<artifactId>morphia-core</artifactId>
<version>2.4.13</version>
</dependency>


</dependencies>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>run.mone</groupId>
<artifactId>docean-plugin</artifactId>
<version>1.6.0-jdk21-SNAPSHOT</version>
</parent>
<artifactId>docean-plugin-mongodb</artifactId>
<version>1.5.0-jdk21-SNAPSHOT</version>
<dependencies>

<dependency>
<groupId>run.mone</groupId>
<artifactId>docean-plugin-config</artifactId>
</dependency>

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.8.0</version>
</dependency>

<dependency>
<groupId>run.mone</groupId>
<artifactId>catPlugin</artifactId>
<version>1.6.0-jdk21-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>dev.morphia.morphia</groupId>
<artifactId>morphia-core</artifactId>
<version>2.4.13</version>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.4.2.Final</version>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.2.Final</version>
</dependency>


</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* 用户
*
* @author mone
* @author [email protected]
*/
@Data
@Builder
Expand Down Expand Up @@ -55,6 +55,8 @@ public class User implements MongoBo{
//版本(用于乐观锁)
private int version;

private String token;

public User(String username, String password) {
this.username = username;
this.password = password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public MongodbController(Class<T> clazz) {
//查询一条记录
//{"name":"$eq","field":"name","value":"bbb"}
@Auth
@RequestMapping(path = "/one", method = "get")
@RequestMapping(path = "/one")
public T one(Filter filter) {
return this.datastore.find(this.clazz).filter(filter).first();
}
Expand All @@ -50,7 +50,6 @@ public T getById(@RequestParam("id") String id) {
}



@RequestMapping(path = "/getByIdAndUid", method = "get")
public T getByIdAndUid(@ModelAttribute("user") User user, @RequestParam("id") String id) {
return datastore.find(this.clazz).filter(Filters.and(Filters.eq("id", id), Filters.eq("uid", user.getId()))).first();
Expand Down Expand Up @@ -126,10 +125,8 @@ public boolean delete(T t) {
}

//删除
@Auth(role = "user")
@RequestMapping(path = "/delete")
public boolean deleteWithUid(T t) {
User user = getCurrentUser();
@RequestMapping(path = "/deleteWithUid")
public boolean deleteWithUid(@ModelAttribute("user") User user, T t) {
t.setUid(user.getUid());
this.datastore.delete(t);
return true;
Expand All @@ -146,9 +143,8 @@ public boolean update(T t) {

//更新
@Auth(role = "user")
@RequestMapping(path = "/update")
public boolean updateWithUid(T t) {
User user = getCurrentUser();
@RequestMapping(path = "/updateWithUid")
public boolean updateWithUid(@ModelAttribute("user") User user, T t) {
t.setUtime(System.currentTimeMillis());
t.setUid(user.getUid());
this.datastore.merge(t);
Expand All @@ -168,9 +164,8 @@ public boolean add(T t) {
}

@Auth(role = "user")
@RequestMapping(path = "/add")
public boolean addWithUid(T t) {
User user = getCurrentUser();
@RequestMapping(path = "/addWithUid")
public boolean addWithUid(@ModelAttribute("user") User user, T t) {
long now = System.currentTimeMillis();
t.setState(0);
t.setUid(user.getUid());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package run.mone.controller;

import com.xiaomi.youpin.docean.anno.Controller;
import com.xiaomi.youpin.docean.anno.ModelAttribute;
import com.xiaomi.youpin.docean.anno.RequestMapping;
import com.xiaomi.youpin.docean.anno.RequestParam;
import com.xiaomi.youpin.docean.mvc.ContextHolder;
import com.xiaomi.youpin.docean.mvc.MvcContext;
import run.mone.bo.User;
import run.mone.mapper.UserMapper;
import run.mone.service.UserService;
import run.mone.vo.UserVo;

import javax.annotation.Resource;

/**
* 用户管理控制器
*
* @author [email protected]
*/
@Controller
@RequestMapping(path = "/user")
public class UserController extends MongodbController<User> {

@Resource
private UserService userService;

public UserController() {
super(User.class);
}

/**
* 根据用户名和密码查找用户
*
* @param username 用户名
* @param password 密码
* @return 用户对象
*/
@RequestMapping(path = "/findByUsernameAndPassword", method = "get")
public UserVo findUserByUsernameAndPassword(@RequestParam("username") String username, @RequestParam("password") String password) {
User user = userService.findUserByUsernameAndPassword(username, password);
return UserMapper.INSTANCE.userToUserVo(user);
}

/**
* 更新用户密码
*
* @param userId 用户ID
* @param newPassword 新密码
* @return 是否更新成功
*/
@RequestMapping(path = "/updatePassword")
public boolean updatePassword(String userId, String newPassword) {
return userService.updatePassword(userId, newPassword);
}

/**
* 更新用户个人信息
*
* @param user 新的个人信息
* @return 是否更新成功
*/
@RequestMapping(path = "/updateProfile")
public boolean updateProfile(@ModelAttribute("user") User user, User updateUser) {
updateUser.setId(user.getId());
return userService.updateProfile(updateUser);
}


/**
* 处理用户注册请求的方法。
* 调用userService中的registerUser方法,并返回注册结果。
*
* @param user 用户对象,包含用户注册信息。
* @return 返回注册操作的成功与否。
*/
@RequestMapping(path = "/register")
public boolean registerUser(User user) {
return userService.registerUser(user);
}

@RequestMapping(path = "/getByUserId")
public UserVo getByUserId(@RequestParam("userId") String userId) {
User user = userService.findById(userId);
return UserMapper.INSTANCE.userToUserVo(user);
}

/**
* 该方法用于获取当前登录用户的信息
* 通过@RequestMapping注解映射请求路径为"/getLoginUser"
* 使用@ModelAttribute注解将请求参数绑定到User对象
* 返回绑定了请求参数的User对象
*/
@RequestMapping(path = "/getLoginUser")
public User getLoginUser(@ModelAttribute("user") User user) {
return user;
}

@RequestMapping(path = "/login")
public String login(User userReq) {
MvcContext context = ContextHolder.getContext().get();
User user = userService.findUserByUsernameAndPassword(userReq.getUsername(), userReq.getPassword());
if (null == user) {
return "error";
}
user.setPassword("");
context.session().setAttribute("user", user);
return "ok";
}

@RequestMapping(path = "/logout")
public String logout(MvcContext context) {
context.session().removeAttribute("user");
return "ok";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package run.mone.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import run.mone.bo.User;
import run.mone.vo.UserVo;

/**
* @author [email protected]
* @date 2024/4/23 16:59
*/
@Mapper
public interface UserMapper {

UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

UserVo userToUserVo(User user);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package run.mone.service;

import com.xiaomi.youpin.docean.anno.Service;
import dev.morphia.query.filters.Filter;
import dev.morphia.query.filters.Filters;
import run.mone.bo.User;

/**
* @author [email protected]
* @date 2024/4/18 22:49
*/
@Service
public class UserService extends MongoService<User> {


public UserService() {
super(User.class);
}

/**
* 根据用户名和密码查找用户
*/
public User findUserByUsernameAndPassword(String username, String password) {
Filter filter = Filters.and(
Filters.eq("username", username),
Filters.eq("password", password)
);
return findFirst(filter);
}

/**
* 更新用户密码
*/
public boolean updatePassword(String userId, String newPassword) {
User user = findById(userId);
if (user != null) {
user.setPassword(newPassword);
return update(user);
}
return false;
}

/**
* 更新用户个人信息
*/
public boolean updateProfile(User user) {
return update(user);
}


/**
* 注册用户
* <p>
* 该方法用于注册新用户。首先会根据用户名查找是否已存在相同的用户,如果存在则返回 false 表示注册失败。
* 如果不存在相同用户,则保存新用户并返回 true 表示注册成功。
*
* @param user 待注册的用户对象
* @return 注册是否成功, 成功返回 true,失败返回 false
*/
public boolean registerUser(User user) {
User existingUser = findFirst(Filters.eq("username", user.getUsername()));
if (existingUser != null) {
return false; // 用户名已存在
}
return save(user);
}
}
Loading

0 comments on commit 0a58e95

Please sign in to comment.