2022-12-21 16:55:16 +08:00
|
|
|
package controller_admin
|
2022-09-27 17:59:05 +08:00
|
|
|
|
|
|
|
import (
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/handler"
|
2022-11-29 15:10:57 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/middleware"
|
2023-04-03 11:17:38 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/reason"
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/schema"
|
2022-12-21 16:55:16 +08:00
|
|
|
"github.com/answerdev/answer/internal/service/user_admin"
|
2023-04-03 11:17:38 +08:00
|
|
|
"github.com/answerdev/answer/plugin"
|
2022-09-27 17:59:05 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
2023-04-03 11:17:38 +08:00
|
|
|
"github.com/segmentfault/pacman/errors"
|
2022-09-27 17:59:05 +08:00
|
|
|
)
|
|
|
|
|
2022-12-21 16:55:16 +08:00
|
|
|
// UserAdminController user controller
|
|
|
|
type UserAdminController struct {
|
|
|
|
userService *user_admin.UserAdminService
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
2022-12-21 16:55:16 +08:00
|
|
|
// NewUserAdminController new controller
|
|
|
|
func NewUserAdminController(userService *user_admin.UserAdminService) *UserAdminController {
|
|
|
|
return &UserAdminController{userService: userService}
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateUserStatus update user
|
|
|
|
// @Summary update user
|
|
|
|
// @Description update user
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Tags admin
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param data body schema.UpdateUserStatusReq true "user"
|
|
|
|
// @Success 200 {object} handler.RespBody
|
|
|
|
// @Router /answer/admin/api/user/status [put]
|
2022-12-21 16:55:16 +08:00
|
|
|
func (uc *UserAdminController) UpdateUserStatus(ctx *gin.Context) {
|
2023-04-17 18:04:28 +08:00
|
|
|
if u, ok := plugin.GetUserCenter(); ok && u.Description().UserStatusAgentEnabled {
|
2023-04-03 11:17:38 +08:00
|
|
|
handler.HandleResponse(ctx, errors.Forbidden(reason.ForbiddenError), nil)
|
|
|
|
return
|
|
|
|
}
|
2022-09-27 17:59:05 +08:00
|
|
|
req := &schema.UpdateUserStatusReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-23 11:44:05 +08:00
|
|
|
req.LoginUserID = middleware.GetLoginUserIDFromContext(ctx)
|
|
|
|
|
2022-09-27 17:59:05 +08:00
|
|
|
err := uc.userService.UpdateUserStatus(ctx, req)
|
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
}
|
|
|
|
|
2022-11-29 15:10:57 +08:00
|
|
|
// UpdateUserRole update user role
|
|
|
|
// @Summary update user role
|
|
|
|
// @Description update user role
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Tags admin
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param data body schema.UpdateUserRoleReq true "user"
|
|
|
|
// @Success 200 {object} handler.RespBody
|
|
|
|
// @Router /answer/admin/api/user/role [put]
|
2022-12-21 16:55:16 +08:00
|
|
|
func (uc *UserAdminController) UpdateUserRole(ctx *gin.Context) {
|
2022-11-29 15:10:57 +08:00
|
|
|
req := &schema.UpdateUserRoleReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.LoginUserID = middleware.GetLoginUserIDFromContext(ctx)
|
|
|
|
|
|
|
|
err := uc.userService.UpdateUserRole(ctx, req)
|
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
}
|
|
|
|
|
2022-12-07 10:26:02 +08:00
|
|
|
// AddUser add user
|
|
|
|
// @Summary add user
|
|
|
|
// @Description add user
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Tags admin
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param data body schema.AddUserReq true "user"
|
|
|
|
// @Success 200 {object} handler.RespBody
|
|
|
|
// @Router /answer/admin/api/user [post]
|
2022-12-21 16:55:16 +08:00
|
|
|
func (uc *UserAdminController) AddUser(ctx *gin.Context) {
|
2022-12-07 10:26:02 +08:00
|
|
|
req := &schema.AddUserReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.LoginUserID = middleware.GetLoginUserIDFromContext(ctx)
|
|
|
|
|
|
|
|
err := uc.userService.AddUser(ctx, req)
|
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
}
|
|
|
|
|
2023-08-14 12:13:28 +08:00
|
|
|
// AddUsers add users
|
|
|
|
// @Summary add users
|
|
|
|
// @Description add users
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Tags admin
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2023-08-14 16:42:41 +08:00
|
|
|
// @Param data body schema.AddUsersReq true "user"
|
2023-08-14 12:13:28 +08:00
|
|
|
// @Success 200 {object} handler.RespBody
|
|
|
|
// @Router /answer/admin/api/users [post]
|
|
|
|
func (uc *UserAdminController) AddUsers(ctx *gin.Context) {
|
|
|
|
req := &schema.AddUsersReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-14 15:46:13 +08:00
|
|
|
resp, err := uc.userService.AddUsers(ctx, req)
|
|
|
|
handler.HandleResponse(ctx, err, resp)
|
2023-08-14 12:13:28 +08:00
|
|
|
}
|
|
|
|
|
2022-12-07 10:26:02 +08:00
|
|
|
// UpdateUserPassword update user password
|
|
|
|
// @Summary update user password
|
|
|
|
// @Description update user password
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Tags admin
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param data body schema.UpdateUserPasswordReq true "user"
|
|
|
|
// @Success 200 {object} handler.RespBody
|
|
|
|
// @Router /answer/admin/api/user/password [put]
|
2022-12-21 16:55:16 +08:00
|
|
|
func (uc *UserAdminController) UpdateUserPassword(ctx *gin.Context) {
|
2022-12-07 10:26:02 +08:00
|
|
|
req := &schema.UpdateUserPasswordReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.LoginUserID = middleware.GetLoginUserIDFromContext(ctx)
|
|
|
|
|
|
|
|
err := uc.userService.UpdateUserPassword(ctx, req)
|
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
}
|
|
|
|
|
2022-09-27 17:59:05 +08:00
|
|
|
// GetUserPage get user page
|
|
|
|
// @Summary get user page
|
|
|
|
// @Description get user page
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Tags admin
|
|
|
|
// @Produce json
|
|
|
|
// @Param page query int false "page size"
|
|
|
|
// @Param page_size query int false "page size"
|
2022-11-01 09:12:12 +08:00
|
|
|
// @Param query query string false "search query: email, username or id:[id]"
|
2022-11-29 15:10:57 +08:00
|
|
|
// @Param staff query bool false "staff user"
|
2022-11-01 09:12:12 +08:00
|
|
|
// @Param status query string false "user status" Enums(suspended, deleted, inactive)
|
2022-09-27 17:59:05 +08:00
|
|
|
// @Success 200 {object} handler.RespBody{data=pager.PageModel{records=[]schema.GetUserPageResp}}
|
|
|
|
// @Router /answer/admin/api/users/page [get]
|
2022-12-21 16:55:16 +08:00
|
|
|
func (uc *UserAdminController) GetUserPage(ctx *gin.Context) {
|
2022-09-27 17:59:05 +08:00
|
|
|
req := &schema.GetUserPageReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := uc.userService.GetUserPage(ctx, req)
|
|
|
|
handler.HandleResponse(ctx, err, resp)
|
|
|
|
}
|
2023-07-10 18:00:16 +08:00
|
|
|
|
|
|
|
// GetUserActivation get user activation
|
|
|
|
// @Summary get user activation
|
|
|
|
// @Description get user activation
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Tags admin
|
|
|
|
// @Produce json
|
|
|
|
// @Param user_id query string true "user id"
|
|
|
|
// @Success 200 {object} handler.RespBody{data=schema.GetUserActivationResp}
|
2023-07-11 15:15:59 +08:00
|
|
|
// @Router /answer/admin/api/user/activation [get]
|
2023-07-10 18:00:16 +08:00
|
|
|
func (uc *UserAdminController) GetUserActivation(ctx *gin.Context) {
|
|
|
|
req := &schema.GetUserActivationReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := uc.userService.GetUserActivation(ctx, req)
|
|
|
|
handler.HandleResponse(ctx, err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendUserActivation send user activation
|
|
|
|
// @Summary send user activation
|
|
|
|
// @Description send user activation
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Tags admin
|
|
|
|
// @Produce json
|
|
|
|
// @Param data body schema.SendUserActivationReq true "SendUserActivationReq"
|
|
|
|
// @Success 200 {object} handler.RespBody
|
|
|
|
// @Router /answer/admin/api/users/activation [post]
|
|
|
|
func (uc *UserAdminController) SendUserActivation(ctx *gin.Context) {
|
|
|
|
req := &schema.SendUserActivationReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := uc.userService.SendUserActivation(ctx, req)
|
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
}
|