1 parent
9dc861d560
commit
fd475cd88d
54 files changed
+987
-91
No files matched your search
@@ -17,7 +17,7 @@ import (
|
||||
|
||||
// ConfigStatus 返回配置管理状态,不返回任何密文、路径或 DSN。
|
||||
func ConfigStatus(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if err := authorizeCapability(ctx, access.CapabilityConfigRead, "", false); err != nil {
|
||||
if err := authorizeConfigOverview(ctx, false); err != nil {
|
||||
writeConfigAccessError(w, err)
|
||||
return
|
||||
}
|
||||
@@ -30,6 +30,10 @@ func ConfigStatus(ctx *appcontext.Context, w http.ResponseWriter, req *http.Requ
|
||||
return
|
||||
}
|
||||
status := manager.Status()
|
||||
if ctx.EffectiveRole() != access.RoleSuperAdmin {
|
||||
response.NewSuccessResponse(map[string]any{"available": true, "activeGeneration": status.ActiveGeneration}).FPrint(w)
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse(map[string]any{
|
||||
"available": true,
|
||||
"setupState": status.SetupState,
|
||||
@@ -52,7 +56,7 @@ func GetConfigSection(ctx *appcontext.Context, w http.ResponseWriter, req *http.
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := authorizeCapability(ctx, access.CapabilityConfigRead, "", false); err != nil {
|
||||
if err := authorizeConfigSection(ctx, section, false); err != nil {
|
||||
writeConfigAccessError(w, err)
|
||||
return
|
||||
}
|
||||
@@ -77,7 +81,7 @@ func ValidateConfig(ctx *appcontext.Context, w http.ResponseWriter, req *http.Re
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := authorizeCapability(ctx, access.CapabilityConfigWriteGlobal, "", false); err != nil {
|
||||
if err := authorizeConfigSection(ctx, section, true); err != nil {
|
||||
writeConfigAccessError(w, err)
|
||||
return
|
||||
}
|
||||
@@ -86,10 +90,10 @@ func ValidateConfig(ctx *appcontext.Context, w http.ResponseWriter, req *http.Re
|
||||
writeConfigError(w, errors.New("invalid configuration request"))
|
||||
return
|
||||
}
|
||||
result, err := manager.Validate(req.Context(), config.ValidateRequest{
|
||||
result, err := manager.ValidateAuthorized(req.Context(), config.ValidateRequest{
|
||||
Section: section, ExpectedGeneration: input.ExpectedGeneration,
|
||||
Payload: input.Payload, SecretChanges: input.SecretChanges,
|
||||
})
|
||||
}, configActor(ctx, req))
|
||||
if err != nil {
|
||||
writeConfigError(w, err)
|
||||
return
|
||||
@@ -103,7 +107,7 @@ func UpdateConfig(ctx *appcontext.Context, w http.ResponseWriter, req *http.Requ
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := authorizeCapability(ctx, access.CapabilityConfigWriteGlobal, "", false); err != nil {
|
||||
if err := authorizeConfigSection(ctx, section, true); err != nil {
|
||||
writeConfigAccessError(w, err)
|
||||
return
|
||||
}
|
||||
@@ -135,7 +139,7 @@ func ApplyConfig(ctx *appcontext.Context, w http.ResponseWriter, req *http.Reque
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := authorizeCapability(ctx, access.CapabilityConfigWriteGlobal, "", false); err != nil {
|
||||
if err := authorizeConfigSection(ctx, section, true); err != nil {
|
||||
writeConfigAccessError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -40,9 +40,9 @@ func TestConfigBackupReadDoesNotRequireRecentAuth(t *testing.T) {
|
||||
ctx := &appcontext.Context{UserID: 1, Role: role, RecentAuth: false}
|
||||
recorder := httptest.NewRecorder()
|
||||
ListConfigBackups(ctx, recorder, httptest.NewRequest(http.MethodGet, "/api/v1/config/backups", nil))
|
||||
if role == access.RoleUser {
|
||||
if role != access.RoleSuperAdmin {
|
||||
if recorder.Code != http.StatusForbidden {
|
||||
t.Fatalf("普通用户不应读取配置备份:%d", recorder.Code)
|
||||
t.Fatalf("非超级管理员不应读取配置备份:%d", recorder.Code)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -53,7 +53,7 @@ func TestConfigBackupReadDoesNotRequireRecentAuth(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if recorder.Code != http.StatusOK || payload.ErrorNo != 0 {
|
||||
t.Fatalf("管理员读取脱敏备份不应要求近期认证:%d", recorder.Code)
|
||||
t.Fatalf("超级管理员读取脱敏备份不应要求近期认证:%d", recorder.Code)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/dto/response"
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
appcontext "git.pcux.cn/xuans/Smail/utils/context"
|
||||
)
|
||||
|
||||
@@ -16,7 +15,7 @@ func GetPendingConfig(ctx *appcontext.Context, w http.ResponseWriter, req *http.
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := authorizeCapability(ctx, access.CapabilityConfigRead, "", false); err != nil {
|
||||
if err := authorizeConfigOverview(ctx, false); err != nil {
|
||||
writeConfigAccessError(w, err)
|
||||
return
|
||||
}
|
||||
@@ -38,7 +37,7 @@ func ManagePendingConfig(ctx *appcontext.Context, w http.ResponseWriter, req *ht
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := authorizeCapability(ctx, access.CapabilityConfigWriteGlobal, "", false); err != nil {
|
||||
if err := authorizeConfigOverview(ctx, true); err != nil {
|
||||
writeConfigAccessError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
appcontext "git.pcux.cn/xuans/Smail/utils/context"
|
||||
)
|
||||
|
||||
// 分区权限必须在接口和配置管理器两层检查,不能信任前端菜单。
|
||||
func authorizeConfigSection(ctx *appcontext.Context, section config.Section, write bool) error {
|
||||
capability := access.CapabilityConfigRead
|
||||
if write {
|
||||
capability = access.CapabilityConfigWriteGlobal
|
||||
}
|
||||
if section == config.SectionPlugins {
|
||||
capability = access.CapabilityPluginReadGlobal
|
||||
if write {
|
||||
capability = access.CapabilityPluginWriteGlobal
|
||||
}
|
||||
}
|
||||
return authorizeCapability(ctx, capability, "", false)
|
||||
}
|
||||
|
||||
func authorizeConfigOverview(ctx *appcontext.Context, write bool) error {
|
||||
if ctx != nil && ctx.EffectiveRole() == access.RoleAdmin {
|
||||
return authorizeConfigSection(ctx, config.SectionPlugins, write)
|
||||
}
|
||||
return authorizeConfigSection(ctx, config.SectionRoot, write)
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"git.pcux.cn/xuans/Smail/hooks/framework"
|
||||
"git.pcux.cn/xuans/Smail/i18n"
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/utils/async"
|
||||
"git.pcux.cn/xuans/Smail/utils/context"
|
||||
@@ -75,11 +76,12 @@ func Send(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
log.WithContext(ctx).Infof("发送邮件")
|
||||
|
||||
if reqData.From.Email != "" && !validation.IsAuthorizedSender(reqData.From.Email, ctx.UserAccount, ctx.IsAdmin, domains.Accepted) {
|
||||
allowLocalAlias := ctx.EffectiveRole() == access.RoleSuperAdmin
|
||||
if reqData.From.Email != "" && !validation.IsAuthorizedSender(reqData.From.Email, ctx.UserAccount, allowLocalAlias, domains.Accepted) {
|
||||
response.NewErrorResponse(response.ParamsError, "\u53d1\u4ef6\u4eba\u5730\u5740\u4e0d\u5141\u8bb8", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
if reqData.Sender.Email != "" && !validation.IsAuthorizedSender(reqData.Sender.Email, ctx.UserAccount, ctx.IsAdmin, domains.Accepted) {
|
||||
if reqData.Sender.Email != "" && !validation.IsAuthorizedSender(reqData.Sender.Email, ctx.UserAccount, allowLocalAlias, domains.Accepted) {
|
||||
response.NewErrorResponse(response.ParamsError, "\u53d1\u9001\u8005\u5730\u5740\u4e0d\u5141\u8bb8", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
@@ -194,7 +196,7 @@ func Send(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
log.WithContext(ctx).Debugf("插件执行--SendBefore End")
|
||||
|
||||
if e.From == nil || e.Sender == nil || !validation.IsAuthorizedSender(e.From.EmailAddress, ctx.UserAccount, ctx.IsAdmin, domains.Accepted) || !validation.IsAuthorizedSender(e.Sender.EmailAddress, ctx.UserAccount, ctx.IsAdmin, domains.Accepted) {
|
||||
if e.From == nil || e.Sender == nil || !validation.IsAuthorizedSender(e.From.EmailAddress, ctx.UserAccount, allowLocalAlias, domains.Accepted) || !validation.IsAuthorizedSender(e.Sender.EmailAddress, ctx.UserAccount, allowLocalAlias, domains.Accepted) {
|
||||
response.NewErrorResponse(response.ParamsError, "\u63d2\u4ef6\u4fee\u6539\u4e86\u672a\u6388\u6743\u7684\u53d1\u4ef6\u4eba", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/db"
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
"git.pcux.cn/xuans/Smail/services/logstore"
|
||||
appcontext "git.pcux.cn/xuans/Smail/utils/context"
|
||||
)
|
||||
|
||||
func authorizeLogs(ctx *appcontext.Context) error {
|
||||
if ctx == nil || ctx.UserID <= 0 {
|
||||
return &access.Denial{Code: access.CodeAccessDenied}
|
||||
}
|
||||
// 此授权只允许进入已实施数据范围过滤的日志接口,不影响旧的全局审计接口。
|
||||
return access.Authorize(access.Request{ActorRole: ctx.EffectiveRole(), Capability: access.CapabilityAuditRead, OwnResource: true})
|
||||
}
|
||||
|
||||
func runtimeLogActors(requestContext context.Context, ctx *appcontext.Context) (map[string]bool, error) {
|
||||
if ctx.EffectiveRole() == access.RoleSuperAdmin {
|
||||
return nil, nil
|
||||
}
|
||||
actors := map[string]bool{strconv.Itoa(ctx.UserID): true}
|
||||
if ctx.EffectiveRole() == access.RoleAdmin {
|
||||
if db.Instance == nil {
|
||||
return nil, logstore.ErrUnavailable
|
||||
}
|
||||
var users []models.User
|
||||
if err := db.Instance.Context(requestContext).Cols("id").Where(models.OrdinaryUserPredicate).Find(&users); err != nil {
|
||||
return nil, logstore.ErrUnavailable
|
||||
}
|
||||
for _, user := range users {
|
||||
if user.ID != ctx.UserID {
|
||||
actors[strconv.Itoa(user.ID)] = false
|
||||
}
|
||||
}
|
||||
}
|
||||
return actors, nil
|
||||
}
|
||||
@@ -28,7 +28,7 @@ func logQueryError(w http.ResponseWriter, err error) {
|
||||
// GetLogs 只读日志入口,授权在服务端检查,不依赖导航隐藏。
|
||||
func GetLogs(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
if err := authorizeCapability(ctx, access.CapabilityAuditRead, "", false); err != nil {
|
||||
if err := authorizeLogs(ctx); err != nil {
|
||||
writeConfigAccessError(w, err)
|
||||
return
|
||||
}
|
||||
@@ -48,6 +48,10 @@ func GetLogs(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request)
|
||||
bounded, cancel := context.WithTimeout(req.Context(), 10*time.Second)
|
||||
defer cancel()
|
||||
query := logstore.Query{Kind: values.Get("kind"), ActorID: values.Get("actorId"), Limit: limit + 1, Offset: offset}
|
||||
if ctx.EffectiveRole() != access.RoleSuperAdmin {
|
||||
query.ScopeActorID = strconv.Itoa(ctx.UserID)
|
||||
query.IncludeOrdinaryUsers = ctx.EffectiveRole() == access.RoleAdmin
|
||||
}
|
||||
for _, item := range []struct {
|
||||
name string
|
||||
target *time.Time
|
||||
@@ -78,6 +82,10 @@ func GetLogs(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request)
|
||||
}
|
||||
// 显式保留旧文件列表入口,新页面默认跨文件查询。
|
||||
if values.Get("listFiles") == "true" {
|
||||
if ctx.EffectiveRole() != access.RoleSuperAdmin {
|
||||
writeConfigAccessError(w, &access.Denial{Code: access.CodeAccessDenied})
|
||||
return
|
||||
}
|
||||
files, err := writer.Files()
|
||||
if err != nil {
|
||||
logQueryError(w, err)
|
||||
@@ -86,7 +94,12 @@ func GetLogs(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request)
|
||||
response.NewSuccessResponse(map[string]any{"files": files}).FPrint(w)
|
||||
return
|
||||
}
|
||||
page, err := writer.Query(bounded, runtimelog.Query{File: values.Get("file"), From: query.From, To: query.To, Offset: offset, Limit: limit})
|
||||
actors, err := runtimeLogActors(bounded, ctx)
|
||||
if err != nil {
|
||||
logQueryError(w, err)
|
||||
return
|
||||
}
|
||||
page, err := writer.Query(bounded, runtimelog.Query{File: values.Get("file"), From: query.From, To: query.To, Offset: offset, Limit: limit, AllowedActors: actors})
|
||||
if err != nil {
|
||||
logQueryError(w, err)
|
||||
return
|
||||
|
||||
@@ -3,6 +3,7 @@ package controllers
|
||||
import (
|
||||
"git.pcux.cn/xuans/Smail/dto/response"
|
||||
"git.pcux.cn/xuans/Smail/hooks"
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
"git.pcux.cn/xuans/Smail/utils/context"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
@@ -18,7 +19,7 @@ const (
|
||||
)
|
||||
|
||||
func GetPluginList(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if !ctx.IsAdmin {
|
||||
if err := authorizeCapability(ctx, access.CapabilityConfigRead, "", false); err != nil {
|
||||
response.NewErrorResponse(response.NoAccessPrivileges, "\u65e0\u6743\u8bbf\u95ee", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
@@ -31,7 +32,7 @@ func GetPluginList(ctx *context.Context, w http.ResponseWriter, req *http.Reques
|
||||
}
|
||||
|
||||
func SettingsHtml(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if !ctx.IsAdmin {
|
||||
if err := authorizeCapability(ctx, access.CapabilityConfigRead, "", false); err != nil {
|
||||
response.NewErrorResponse(response.NoAccessPrivileges, "\u65e0\u6743\u8bbf\u95ee", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/db"
|
||||
"git.pcux.cn/xuans/Smail/dto/response"
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
appcontext "git.pcux.cn/xuans/Smail/utils/context"
|
||||
"git.pcux.cn/xuans/Smail/utils/validation"
|
||||
)
|
||||
|
||||
// UpdateProfile 只接受本人显示名称,不允许传入目标账号、角色、禁用状态或密码。
|
||||
func UpdateProfile(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if ctx == nil || ctx.UserID <= 0 {
|
||||
response.NewErrorResponse(response.NoAccessPrivileges, "无权修改个人资料", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
if err := authorizeCapability(ctx, access.CapabilityProfileWriteOwn, "", true); err != nil {
|
||||
writeUserAuthorizationError(w, err, "")
|
||||
return
|
||||
}
|
||||
var input struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if err := decodeUserRequest(w, req, &input); err != nil {
|
||||
response.NewErrorResponse(response.ParamsError, "个人资料参数错误", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
name, err := validation.NormalizeDisplayName(input.Name)
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ParamsError, "请输入有效的显示名称", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
if db.Instance == nil {
|
||||
response.NewErrorResponse(response.ServerError, "个人资料保存失败", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
affected, err := db.Instance.Context(req.Context()).ID(ctx.UserID).Cols("name").Update(&models.User{Name: name})
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, "个人资料保存失败", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
// MySQL 重复保存相同名称时可能返回零行,必须区分无变化和账号已不存在。
|
||||
if affected == 0 {
|
||||
found, err := db.Instance.Context(req.Context()).ID(ctx.UserID).Exist(&models.User{})
|
||||
if err != nil || !found {
|
||||
response.NewErrorResponse(response.ServerError, "账号已不可用,请重新登录", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
}
|
||||
ctx.UserName = name
|
||||
response.NewSuccessResponse(map[string]any{"name": name}).FPrint(w)
|
||||
}
|
||||
@@ -60,7 +60,7 @@ func CreateUser(ctx *context.Context, w http.ResponseWriter, req *http.Request)
|
||||
if reqData.Role != "" {
|
||||
targetRole = access.Role(reqData.Role)
|
||||
}
|
||||
if err := authorizeCapability(ctx, access.CapabilityUserManage, targetRole, false); err != nil {
|
||||
if err := authorizeCapability(ctx, access.CapabilityUserCreate, targetRole, false); err != nil {
|
||||
response.NewErrorResponse(response.NoAccessPrivileges, "无权访问", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
@@ -170,7 +170,12 @@ func UserList(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
|
||||
var users []models.User
|
||||
totalNum, err := db.Instance.Table(&models.User{}).Limit(reqData.PageSize, offset).FindAndCount(&users)
|
||||
query := db.Instance.Table(&models.User{})
|
||||
defer query.Close()
|
||||
if ctx.EffectiveRole() == access.RoleAdmin {
|
||||
query.Where(models.OrdinaryUserPredicate)
|
||||
}
|
||||
totalNum, err := query.Asc("id").Limit(reqData.PageSize, offset).FindAndCount(&users)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("SQL Error: %+v", err)
|
||||
response.NewErrorResponse(response.ServerError, "查询失败", "").FPrint(w)
|
||||
|
||||
Reference in new issue
Block a user