统一 frontend、backend、test、test-log 和 out 目录,更新构建、部署及测试入口;纳入配置管理兼容与首次安全引导修复;排除本机配置、密钥和测试产物。
This commit is contained in:
1 parent
28cd9d3406
commit
f60d3b0880
603 files changed
+2351
-660
No files matched your search
@@ -0,0 +1,67 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
)
|
||||
|
||||
const userSessionKey = "user"
|
||||
|
||||
type UserIdentity struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Account string `json:"account"`
|
||||
Role access.Role `json:"role"`
|
||||
IsAdmin int `json:"is_admin"`
|
||||
SessionVersion int `json:"session_version"`
|
||||
SecurityVersion int `json:"security_version"`
|
||||
MustChangePassword int `json:"must_change_password"`
|
||||
}
|
||||
|
||||
func PutUser(ctx context.Context, user *models.User) error {
|
||||
if Instance == nil || user == nil || user.ID <= 0 {
|
||||
return errors.New("session user is invalid")
|
||||
}
|
||||
role := user.EffectiveRole()
|
||||
encoded, err := json.Marshal(UserIdentity{
|
||||
ID: user.ID,
|
||||
Name: user.Name,
|
||||
Account: user.Account,
|
||||
Role: role,
|
||||
IsAdmin: user.IsAdmin,
|
||||
SessionVersion: user.SessionVersion,
|
||||
SecurityVersion: user.SecurityVersion,
|
||||
MustChangePassword: user.MustChangePassword,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
Instance.Put(ctx, userSessionKey, string(encoded))
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetUser(ctx context.Context) (UserIdentity, error) {
|
||||
var identity UserIdentity
|
||||
if Instance == nil {
|
||||
return identity, errors.New("session manager is unavailable")
|
||||
}
|
||||
encoded := Instance.GetString(ctx, userSessionKey)
|
||||
if encoded == "" {
|
||||
return identity, nil
|
||||
}
|
||||
if err := json.Unmarshal([]byte(encoded), &identity); err != nil {
|
||||
return UserIdentity{}, err
|
||||
}
|
||||
if !identity.Role.Valid() {
|
||||
if identity.IsAdmin == 1 {
|
||||
identity.Role = access.RoleSuperAdmin
|
||||
} else {
|
||||
identity.Role = access.RoleUser
|
||||
}
|
||||
}
|
||||
return identity, nil
|
||||
}
|
||||
Reference in new issue
Block a user