Skip to content

Commit

Permalink
feat-用户管理、账户管理
Browse files Browse the repository at this point in the history
  • Loading branch information
aruis committed Sep 27, 2024
1 parent cc51ea5 commit 8e55b46
Show file tree
Hide file tree
Showing 9 changed files with 455 additions and 14 deletions.
145 changes: 145 additions & 0 deletions my-boot/src/test/java/net/ximatai/muyun/test/plaform/TestUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package net.ximatai.muyun.test.plaform;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.common.mapper.TypeRef;
import net.ximatai.muyun.platform.PlatformConst;
import net.ximatai.muyun.test.testcontainers.PostgresTestResource;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.*;

@QuarkusTest
@QuarkusTestResource(value = PostgresTestResource.class, restrictToAnnotatedClass = true)
public class TestUser {
String base = PlatformConst.BASE_PATH;

@Test
void test() {
// 新增人员信息
String id = given()
.contentType("application/json")
.body(Map.of(
"v_name", "测试",
"dict_user_gender", "0"
))
.when()
.post("%s/userinfo/create".formatted(base))
.then()
.statusCode(200)
.extract()
.asString();

Map row = given()
.get("%s/userinfo/view/%s".formatted(base, id))
.then()
.statusCode(200)
.extract()
.as(new TypeRef<>() {

});

assertEquals("测试", row.get("v_name"));
assertFalse((Boolean) row.get("b_user"));

// 设置用户
given()
.contentType("application/json")
.body(Map.of(
"v_username", "test",
"v_password", "pw",
"v_password2", "pw"
))
.when()
.post("%s/userinfo/setUser/%s".formatted(base, id))
.then()
.statusCode(200)
.extract()
.asString();

Map row2 = given()
.get("%s/userinfo/view/%s".formatted(base, id))
.then()
.statusCode(200)
.extract()
.as(new TypeRef<>() {

});

assertTrue((Boolean) row2.get("b_user"));

// 登录
Map loginUser = given()
.contentType("application/json")
.body(Map.of(
"username", "test",
"password", "pw"
))
.when()
.post("/sso/login")
.then()
.statusCode(200)
.extract()
.as(new TypeRef<>() {

});

assertEquals("test", loginUser.get("v_username_at_auth_user"));
assertEquals("测试", loginUser.get("v_name"));

// 停用用户
given()
.get("%s/userinfo/disableUser/%s".formatted(base, id))
.then()
.statusCode(200);

given()
.contentType("application/json")
.body(Map.of(
"username", "test",
"password", "pw"
))
.when()
.post("/sso/login")
.then()
.statusCode(500);

// 启用用户
given()
.get("%s/userinfo/enableUser/%s".formatted(base, id))
.then()
.statusCode(200);

given()
.contentType("application/json")
.body(Map.of(
"username", "test",
"password", "pw"
))
.when()
.post("/sso/login")
.then()
.statusCode(200);

// 删除用户
given()
.get("%s/userinfo/delete/%s".formatted(base, id))
.then()
.statusCode(200);

given()
.contentType("application/json")
.body(Map.of(
"username", "test",
"password", "pw"
))
.when()
.post("/sso/login")
.then()
.statusCode(500);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ default PageResult view(@Parameter(description = "页码") @QueryParam("page") I
return ISelectAbility.super.view(page, size, noPage, sort, queryBody, queryItemList());
}

default PageResult query(Map<String, Object> queryBody) {
return ISelectAbility.super.view(null, null, true, null, queryBody, queryItemList());
}

}
3 changes: 3 additions & 0 deletions my-core/src/main/java/net/ximatai/muyun/model/QueryItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class QueryItem {

private Object defaultValue; //ui

private QueryItem() {
}

public static QueryItem of(String column) {
QueryItem item = new QueryItem();
item.column = column;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jakarta.inject.Provider;
import jakarta.ws.rs.Path;
import net.ximatai.muyun.ability.IChildAbility;
import net.ximatai.muyun.ability.IReferableAbility;
import net.ximatai.muyun.ability.IReferenceAbility;
import net.ximatai.muyun.ability.ITreeAbility;
import net.ximatai.muyun.ability.curd.std.IDataCheckAbility;
Expand All @@ -21,7 +22,7 @@
import static net.ximatai.muyun.platform.PlatformConst.BASE_PATH;

@Path(BASE_PATH + "/department")
public class DepartmentController extends ScaffoldForPlatform implements ITreeAbility, IChildAbility, IReferenceAbility, IDataCheckAbility {
public class DepartmentController extends ScaffoldForPlatform implements ITreeAbility, IChildAbility, IReferableAbility, IReferenceAbility, IDataCheckAbility {

@Inject
BaseBusinessTable base;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import net.ximatai.muyun.ability.IReferableAbility;
import net.ximatai.muyun.ability.IReferenceAbility;
import net.ximatai.muyun.ability.ITreeAbility;
import net.ximatai.muyun.ability.curd.std.IQueryAbility;
import net.ximatai.muyun.base.BaseBusinessTable;
import net.ximatai.muyun.core.database.MyTableWrapper;
import net.ximatai.muyun.database.builder.Column;
import net.ximatai.muyun.database.builder.TableWrapper;
import net.ximatai.muyun.model.ChildTableInfo;
import net.ximatai.muyun.model.DataChangeChannel;
import net.ximatai.muyun.model.QueryItem;
import net.ximatai.muyun.model.ReferenceInfo;
import net.ximatai.muyun.platform.ScaffoldForPlatform;
import net.ximatai.muyun.platform.model.Dict;
Expand All @@ -23,7 +25,7 @@
import static net.ximatai.muyun.platform.PlatformConst.BASE_PATH;

@Path(BASE_PATH + "/organization")
public class OrganizationController extends ScaffoldForPlatform implements ITreeAbility, IChildrenAbility, IReferableAbility, IReferenceAbility, IDataBroadcastAbility {
public class OrganizationController extends ScaffoldForPlatform implements ITreeAbility, IChildrenAbility, IReferableAbility, IReferenceAbility, IDataBroadcastAbility, IQueryAbility {

@Inject
BaseBusinessTable base;
Expand All @@ -40,10 +42,10 @@ public class OrganizationController extends ScaffoldForPlatform implements ITree
@Override
protected void afterInit() {
dictCategoryController.putDictCategory(
new DictCategory("org_type", "platform_dir", "机构类型", 0).setDictList(List.of(
new DictCategory("org_type", "platform_dir", "机构类型", 0).setDictList(
new Dict("jituan", "集团公司"),
new Dict("erjigongsi", "二级公司")
)), false);
), false);
}

@Override
Expand Down Expand Up @@ -83,4 +85,13 @@ public List<ReferenceInfo> getReferenceList() {
public DataChangeChannel getDataChangeChannel() {
return new DataChangeChannel(this);
}

@Override
public List<QueryItem> queryItemList() {
return List.of(
QueryItem.of("id"),
QueryItem.of("v_name").setSymbolType(QueryItem.SymbolType.LIKE),
QueryItem.of("v_code").setSymbolType(QueryItem.SymbolType.LIKE)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package net.ximatai.muyun.platform.controller;

import jakarta.inject.Inject;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import net.ximatai.muyun.core.exception.MyException;
import net.ximatai.muyun.model.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Objects;

@Path("/sso")
public class SsoController {

private final Logger logger = LoggerFactory.getLogger(SsoController.class);

@Inject
UserController userController;

@Inject
UserInfoController userInfoController;

@POST
@Path("/login")
public Map login(Map body) {
String username = (String) body.get("username");
String password = (String) body.get("password");

Objects.requireNonNull(username);
Objects.requireNonNull(password);

PageResult pageResult = userController.query(Map.of("v_username", username));

if (pageResult.getSize() == 0) {
logger.error("不存在的用户信息进行登录:{}", username);
throw new MyException("用户名或密码错误");
}

Map userInDB = (Map) pageResult.getList().getFirst();

if (password.equals(userInDB.get("v_password").toString())) {
if ((boolean) userInDB.get("b_enabled")) {
Map<String, ?> user = userInfoController.view((String) userInDB.get("id"));
return user;
} else {
logger.error("用户已停用,用户名:{}", username);
throw new MyException("用户名或密码错误");
}
} else {
logger.error("用户密码验证失败,用户名:{}", username);
throw new MyException("用户名或密码错误");
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package net.ximatai.muyun.platform.controller;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import net.ximatai.muyun.ability.IReferableAbility;
import net.ximatai.muyun.ability.ISecurityAbility;
import net.ximatai.muyun.ability.curd.std.IQueryAbility;
import net.ximatai.muyun.core.database.MyTableWrapper;
import net.ximatai.muyun.core.security.AbstractEncryptor;
import net.ximatai.muyun.core.security.SMEncryptor;
import net.ximatai.muyun.database.builder.Column;
import net.ximatai.muyun.database.builder.TableWrapper;
import net.ximatai.muyun.model.QueryItem;
import net.ximatai.muyun.platform.ScaffoldForPlatform;

import java.util.List;

@ApplicationScoped
public class UserController extends ScaffoldForPlatform implements IQueryAbility, ISecurityAbility, IReferableAbility {

@Override
public String getMainTable() {
return "auth_user";
}

@Inject
SMEncryptor smEncryptor;

@Override
public TableWrapper getTableWrapper() {
return new MyTableWrapper(this)
.setPrimaryKey(Column.ID_POSTGRES)
.addColumn("v_username")
.addColumn("v_password")
.addColumn("t_create")
.addColumn("t_update")
.addColumn("t_last_login")
.addColumn("t_this_login")
.addColumn(Column.of("b_enabled").setDefaultValue(true))
.addIndex("v_username", true);
}

@Override
public List<QueryItem> queryItemList() {
return List.of(
QueryItem.of("v_username"),
QueryItem.of("b_enabled")
);
}

@Override
public List<String> getColumnsForSigning() {
return List.of();
}

@Override
public List<String> getColumnsForEncryption() {
return List.of("v_password");
}

@Override
public AbstractEncryptor getAEncryptor() {
return smEncryptor;
}

@Override
public String getLabelColumn() {
return "v_username";
}
}
Loading

0 comments on commit 8e55b46

Please sign in to comment.