feat: 实现账号设备分组与命令日志
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package boot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.asio.asia/TapRemly/server/internal/auth"
|
||||
"git.asio.asia/TapRemly/server/internal/store"
|
||||
)
|
||||
|
||||
// Admin 在还没有管理员时,用给定账号创建一个。
|
||||
// password 为空则生成密码,并通过 generated 返回,调用方只应打印一次。
|
||||
func Admin(ctx context.Context, st *store.Store, username, password string) (created bool, generated string, err error) {
|
||||
if username == "" {
|
||||
return false, "", nil
|
||||
}
|
||||
has, err := st.HasAdmin(ctx)
|
||||
if err != nil {
|
||||
return false, "", err
|
||||
}
|
||||
if has {
|
||||
return false, "", nil
|
||||
}
|
||||
if password == "" {
|
||||
password, err = auth.GeneratePassword(20)
|
||||
if err != nil {
|
||||
return false, "", err
|
||||
}
|
||||
generated = password
|
||||
}
|
||||
if err := auth.ValidatePassword(password); err != nil {
|
||||
return false, "", fmt.Errorf("bootstrap admin password is too short")
|
||||
}
|
||||
hash, err := auth.HashPassword(password)
|
||||
if err != nil {
|
||||
return false, "", err
|
||||
}
|
||||
created, err = st.CreateAdmin(ctx, username, hash)
|
||||
if err != nil {
|
||||
return false, "", err
|
||||
}
|
||||
if !created {
|
||||
return false, "", nil
|
||||
}
|
||||
return true, generated, nil
|
||||
}
|
||||
Reference in New Issue
Block a user