Skip to content

Commit 835d597

Browse files
committed
feat(core): 新用户注册时自动分配角色
新用户在注册时会被自动分配为普通用户角色,而第一个注册的用户会被分配为管理员角色。 - 在 `UserService` 的 `create_user` 方法中,添加了查询用户数量的逻辑。 - 根据用户数量,判断新用户的角色:如果用户数量为 0,则角色为 "admin";否则,角色为 "user"。 - 修改了 `users::ActiveModel` 中 `role` 字段的设置方式,使其根据上述逻辑动态设置用户角色。
1 parent f8d67be commit 835d597

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

crates/core/src/user.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,16 @@ impl UserService {
7979
let now = Utc::now();
8080
let user_id = Uuid::new_v4();
8181

82+
// First registered user becomes admin
83+
let user_count = users::Entity::find().count(db).await?;
84+
let role = if user_count == 0 { "admin" } else { "user" };
85+
8286
let user = users::ActiveModel {
8387
id: Set(user_id),
8488
username: Set(username.to_string()),
8589
email: Set(email.to_string()),
8690
password: Set(hashed),
87-
role: Set("user".to_string()),
91+
role: Set(role.to_string()),
8892
storage_quota: Set(10_737_418_240), // 10GB
8993
storage_used: Set(0),
9094
created_at: Set(now),

0 commit comments

Comments
 (0)