feat(auth): 添加用户存储服务实现
- 实现了基于内存的用户存储服务 - 支持通过ID和名称查找用户 - 注册了用户存储服务的单例模式 - 添加了基础用户数据初始化逻辑
This commit is contained in:
36
32-platform/sportsstore/admin/auth/user_store.go
Normal file
36
32-platform/sportsstore/admin/auth/user_store.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"platform/authorization/identity"
|
||||
"platform/services"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func RegisterUserStoreService() {
|
||||
err := services.AddSingleton(func() identity.UserStore {
|
||||
return &userStore{}
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
var users = map[int]identity.User{
|
||||
1: identity.NewBasicUser(1, "Alice", "Administrator"),
|
||||
}
|
||||
|
||||
type userStore struct{}
|
||||
|
||||
func (store *userStore) GetUserById(id int) (identity.User, bool) {
|
||||
user, found := users[id]
|
||||
return user, found
|
||||
}
|
||||
|
||||
func (store *userStore) GetUserByName(name string) (identity.User, bool) {
|
||||
for _, user := range users {
|
||||
if strings.EqualFold(user.GetDisplayName(), name) {
|
||||
return user, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
Reference in New Issue
Block a user