Skip to content

Commit

Permalink
feat-UserInfoController-项目启动时会判断是否存在管理员账号,不存在就自动创建
Browse files Browse the repository at this point in the history
  • Loading branch information
aruis committed Oct 8, 2024
1 parent 4a84af6 commit 2522cbe
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.ximatai.muyun.database.IDatabaseOperations;
import net.ximatai.muyun.database.builder.Column;
import net.ximatai.muyun.database.builder.TableWrapper;
import net.ximatai.muyun.database.exception.MyDatabaseException;
import net.ximatai.muyun.model.PageResult;
import net.ximatai.muyun.test.testcontainers.PostgresTestResource;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -210,9 +209,9 @@ void testDelete() {
.then()
.statusCode(200);

assertThrows(MyDatabaseException.class, () -> {
databaseOperations.row("select * from %s where id = :id ".formatted(tableName), Map.of("id", id));
});
assertNull(
databaseOperations.row("select * from %s where id = :id ".formatted(tableName), Map.of("id", id))
);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.ximatai.muyun.ability.ISortAbility;
import net.ximatai.muyun.core.exception.QueryException;
import net.ximatai.muyun.database.builder.TableBase;
import net.ximatai.muyun.database.exception.MyDatabaseException;
import net.ximatai.muyun.database.tool.DateTool;
import net.ximatai.muyun.model.PageResult;
import net.ximatai.muyun.model.QueryItem;
Expand Down Expand Up @@ -98,6 +99,11 @@ referenceTableTempName, getMainTable(), info.getRelationColumn(),
@Operation(summary = "查看指定的数据")
default Map<String, ?> view(@PathParam("id") String id) {
Map<String, Object> row = getDB().row(getSelectOneRowSql(), Map.of("id", id));

if (row == null) {
throw new MyDatabaseException(null, MyDatabaseException.Type.DATA_NOT_FOUND);
}

if (this instanceof ISecurityAbility securityAbility) {
securityAbility.decrypt(row);
securityAbility.checkSign(row);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,10 @@ public <T> T insert(String sql, Map<String, ?> params, String pk, Class<T> idTyp

@Override
public Map<String, Object> row(String sql, Map<String, ?> params) {
var row = getJdbi().withHandle(handle -> handle.createQuery(sql)
return getJdbi().withHandle(handle -> handle.createQuery(sql)
.bindMap(params)
.map(new MyPgMapMapper())
.findOne().orElse(null));

if (row == null) {
throw new MyDatabaseException(null, MyDatabaseException.Type.DATA_NOT_FOUND);
}

return row;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
import net.ximatai.muyun.ability.ISoftDeleteAbility;
import net.ximatai.muyun.ability.curd.std.IQueryAbility;
import net.ximatai.muyun.base.BaseBusinessTable;
import net.ximatai.muyun.core.MuYunConfig;
import net.ximatai.muyun.core.exception.MyException;
import net.ximatai.muyun.database.builder.Column;
import net.ximatai.muyun.database.builder.TableWrapper;
import net.ximatai.muyun.database.exception.MyDatabaseException;
import net.ximatai.muyun.model.QueryItem;
import net.ximatai.muyun.model.ReferenceInfo;
import net.ximatai.muyun.platform.ScaffoldForPlatform;
Expand Down Expand Up @@ -50,6 +52,9 @@ public class UserInfoController extends ScaffoldForPlatform implements IReferabl
@Inject
IAuthorizationService authorizationService;

@Inject
MuYunConfig config;

@Override
public String getMainTable() {
return "auth_userinfo";
Expand Down Expand Up @@ -82,6 +87,22 @@ protected void afterInit() {
new Dict("1", "男"),
new Dict("2", "女")
), false);

String superUserId = config.superUserId();

try {
this.view(superUserId);
} catch (MyDatabaseException e) {
this.create(Map.of(
"id", superUserId,
"v_name", "超级管理员"
));
this.setUser(superUserId, Map.of(
"v_username", "admin",
"v_password", "admin@bsy",
"v_password2", "admin@bsy"
));
}
}

@Override
Expand Down

0 comments on commit 2522cbe

Please sign in to comment.