79614b2d07
- Updated multiple repository and service files to replace imports from "github.com/mlogclub/simple/web/params" with "cs-agent/internal/pkg/httpx/params". - Adjusted context handling in auth_service and ws_service to use gin.Context instead of iris.Context. - Ensured consistent usage of HTTP status responses across websocket handlers.
200 lines
5.8 KiB
Go
200 lines
5.8 KiB
Go
package dashboard
|
|
|
|
import (
|
|
"cs-agent/internal/builders"
|
|
"cs-agent/internal/pkg/constants"
|
|
"cs-agent/internal/pkg/dto/request"
|
|
"cs-agent/internal/pkg/dto/response"
|
|
"cs-agent/internal/pkg/enums"
|
|
"cs-agent/internal/services"
|
|
|
|
"cs-agent/internal/pkg/httpx/params"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/mlogclub/simple/web"
|
|
)
|
|
|
|
type UserController struct {
|
|
Ctx *gin.Context
|
|
}
|
|
|
|
func (c *UserController) AnyList() *web.JsonResult {
|
|
if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionUserView); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
|
|
cnd := params.NewPagedSqlCnd(c.Ctx,
|
|
params.QueryFilter{ParamName: "status"},
|
|
params.QueryFilter{ParamName: "username", Op: params.Like},
|
|
params.QueryFilter{ParamName: "nickname", Op: params.Like},
|
|
).Desc("id")
|
|
cnd.Where("status <> ?", enums.StatusDeleted)
|
|
list, paging := services.UserService.FindPageByCnd(cnd)
|
|
results := builders.BuildUserList(list, builders.UserBuildOptions{
|
|
Roles: true,
|
|
Permissions: false,
|
|
})
|
|
return web.JsonData(&web.PageResult{Results: results, Page: paging})
|
|
}
|
|
|
|
func (c *UserController) AnyList_all() *web.JsonResult {
|
|
if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionUserView); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
|
|
cnd := params.NewSqlCnd(c.Ctx,
|
|
params.QueryFilter{ParamName: "status"},
|
|
params.QueryFilter{ParamName: "username", Op: params.Like},
|
|
params.QueryFilter{ParamName: "nickname", Op: params.Like},
|
|
).Desc("id")
|
|
cnd.Where("status <> ?", enums.StatusDeleted)
|
|
|
|
list := services.UserService.Find(cnd)
|
|
results := builders.BuildUserList(list, builders.UserBuildOptions{
|
|
Roles: true,
|
|
Permissions: false,
|
|
})
|
|
return web.JsonData(results)
|
|
}
|
|
|
|
func (c *UserController) GetBy(id int64) *web.JsonResult {
|
|
if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionUserView); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
|
|
item := services.UserService.Get(id)
|
|
if item == nil {
|
|
return web.JsonErrorMsg("用户不存在")
|
|
}
|
|
return web.JsonData(builders.BuildUserResponse(item, builders.UserBuildOptions{
|
|
Roles: true,
|
|
Permissions: true,
|
|
}))
|
|
}
|
|
|
|
func (c *UserController) PostCreate() *web.JsonResult {
|
|
operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionUserCreate)
|
|
if err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
|
|
req := request.CreateUserRequest{}
|
|
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
user, generatedPassword, err := services.UserService.CreateUser(req, operator)
|
|
if err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
return web.JsonData(&response.CreateUserResultResponse{
|
|
User: builders.BuildUserResponse(user, builders.UserBuildOptions{Roles: true, Permissions: true}),
|
|
Password: generatedPassword,
|
|
})
|
|
}
|
|
|
|
func (c *UserController) PostUpdate() *web.JsonResult {
|
|
operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionUserUpdate)
|
|
if err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
|
|
req := request.UpdateUserRequest{}
|
|
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
if err := services.UserService.UpdateUser(req, operator); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
return web.JsonSuccess()
|
|
}
|
|
|
|
func (c *UserController) PostDelete() *web.JsonResult {
|
|
operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionUserDelete)
|
|
if err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
|
|
req := request.DeleteUserRequest{}
|
|
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
if err := services.UserService.DeleteUser(req.ID, operator); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
return web.JsonSuccess()
|
|
}
|
|
|
|
func (c *UserController) PostUpdate_status() *web.JsonResult {
|
|
operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionUserUpdate)
|
|
if err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
|
|
req := request.UpdateUserStatusRequest{}
|
|
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
if err := services.UserService.UpdateStatus(req.ID, req.Status, operator); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
return web.JsonSuccess()
|
|
}
|
|
|
|
func (c *UserController) PostReset_password() *web.JsonResult {
|
|
operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionUserUpdate)
|
|
if err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
|
|
var req struct {
|
|
UserID int64 `json:"userId"`
|
|
}
|
|
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
password, err := services.UserService.ResetPassword(req.UserID, operator)
|
|
if err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
return web.JsonData(map[string]any{
|
|
"password": password,
|
|
})
|
|
}
|
|
|
|
func (c *UserController) PostChange_password() *web.JsonResult {
|
|
principal := services.AuthService.GetAuthPrincipal(c.Ctx)
|
|
if principal == nil {
|
|
if _, err := services.AuthService.Authenticate(c.Ctx); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
principal = services.AuthService.GetAuthPrincipal(c.Ctx)
|
|
}
|
|
if principal == nil {
|
|
return web.JsonErrorMsg("未登录或登录已过期")
|
|
}
|
|
|
|
req := request.ChangePasswordRequest{}
|
|
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
if err := services.UserService.ChangeOwnPassword(req.Password, principal); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
return web.JsonSuccess()
|
|
}
|
|
|
|
func (c *UserController) PostAssign_role() *web.JsonResult {
|
|
operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionUserAssignRole)
|
|
if err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
|
|
req := request.AssignRoleRequest{}
|
|
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
if err := services.UserService.AssignRoles(req.UserID, req.RoleIDs, operator); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
return web.JsonSuccess()
|
|
}
|