goploy/cmd/server/api/role/handler.go

249 lines
8.1 KiB
Go
Raw Normal View History

2022-11-23 16:49:55 +08:00
// Copyright 2022 The Goploy Authors. All rights reserved.
// Use of this source code is governed by a GPLv3-style
// license that can be found in the LICENSE file.
2023-07-26 14:32:09 +08:00
package role
2022-11-23 16:49:55 +08:00
import (
2023-07-26 14:32:09 +08:00
"github.com/zhenorzz/goploy/cmd/server/api"
2022-11-23 16:49:55 +08:00
"github.com/zhenorzz/goploy/cmd/server/api/middleware"
"github.com/zhenorzz/goploy/config"
2023-06-20 11:02:02 +08:00
"github.com/zhenorzz/goploy/internal/model"
2022-11-23 16:49:55 +08:00
"github.com/zhenorzz/goploy/internal/server"
"github.com/zhenorzz/goploy/internal/server/response"
"net/http"
)
2023-07-26 14:32:09 +08:00
type Role api.API
2022-11-23 16:49:55 +08:00
func (r Role) Handler() []server.Route {
return []server.Route{
server.NewRoute("/role/getList", http.MethodGet, r.GetList).Permissions(config.ShowRolePage),
server.NewRoute("/role/getOption", http.MethodGet, r.GetOption),
server.NewRoute("/role/getPermissionList", http.MethodGet, r.GetPermissionList).Permissions(config.ShowRolePage),
server.NewRoute("/role/getPermissionBindings", http.MethodGet, r.GetPermissionBindings).Permissions(config.ShowRolePage),
server.NewRoute("/role/add", http.MethodPost, r.Add).Permissions(config.AddRole).LogFunc(middleware.AddOPLog),
server.NewRoute("/role/edit", http.MethodPut, r.Edit).Permissions(config.EditRole).LogFunc(middleware.AddOPLog),
server.NewRoute("/role/remove", http.MethodDelete, r.Remove).Permissions(config.DeleteRole).LogFunc(middleware.AddOPLog),
server.NewRoute("/role/changePermission", http.MethodPut, r.ChangePermission).Permissions(config.EditPermission).LogFunc(middleware.AddOPLog),
}
}
2023-07-26 14:32:09 +08:00
// GetList lists roles
// @Summary List roles
// @Tags Role
// @Produce json
// @Security ApiKeyHeader || ApiKeyQueryParam || NamespaceHeader || NamespaceQueryParam
// @Success 200 {object} response.JSON{data=role.GetList.RespData}
// @Router /role/getList [get]
2022-11-23 16:49:55 +08:00
func (Role) GetList(*server.Goploy) server.Response {
2023-06-20 11:02:02 +08:00
roleList, err := model.Role{}.GetList()
2022-11-23 16:49:55 +08:00
if err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
}
2023-07-26 14:32:09 +08:00
type RespData struct {
List model.Roles `json:"list"`
}
2022-11-23 16:49:55 +08:00
return response.JSON{
2023-07-26 14:32:09 +08:00
Data: RespData{List: roleList},
2022-11-23 16:49:55 +08:00
}
}
2023-07-26 14:32:09 +08:00
// GetOption lists all roles
// @Summary List all roles
// @Tags Role
// @Produce json
// @Security ApiKeyHeader || ApiKeyQueryParam || NamespaceHeader || NamespaceQueryParam
// @Success 200 {object} response.JSON{data=role.GetOption.RespData}
// @Router /role/getOption [get]
2022-11-23 16:49:55 +08:00
func (Role) GetOption(*server.Goploy) server.Response {
2023-06-20 11:02:02 +08:00
list, err := model.Role{}.GetAll()
2022-11-23 16:49:55 +08:00
if err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
}
2023-07-26 14:32:09 +08:00
type RespData struct {
2023-06-20 11:02:02 +08:00
List model.Roles `json:"list"`
2023-07-26 14:32:09 +08:00
}
return response.JSON{Data: RespData{list}}
2022-11-23 16:49:55 +08:00
}
2023-07-26 14:32:09 +08:00
// GetPermissionList lists permissions
// @Summary List permissions
// @Tags Role
// @Produce json
// @Security ApiKeyHeader || ApiKeyQueryParam || NamespaceHeader || NamespaceQueryParam
// @Success 200 {object} response.JSON{data=role.GetPermissionList.RespData}
// @Router /role/getPermissionList [get]
2022-11-23 16:49:55 +08:00
func (Role) GetPermissionList(*server.Goploy) server.Response {
2023-06-20 11:02:02 +08:00
list, err := model.Permission{}.GetList()
2022-11-23 16:49:55 +08:00
if err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
}
2023-07-26 14:32:09 +08:00
type RespData struct {
List model.Permissions `json:"list"`
}
2022-11-23 16:49:55 +08:00
return response.JSON{
2023-07-26 14:32:09 +08:00
Data: RespData{List: list},
2022-11-23 16:49:55 +08:00
}
}
2023-07-26 14:32:09 +08:00
// GetPermissionBindings lists role permissions
// @Summary List role permissions
// @Tags Role
// @Produce json
// @Security ApiKeyHeader || ApiKeyQueryParam || NamespaceHeader || NamespaceQueryParam
// @Success 200 {object} response.JSON{data=role.GetPermissionBindings.RespData}
// @Router /role/getPermissionBindings [get]
2022-11-23 16:49:55 +08:00
func (Role) GetPermissionBindings(gp *server.Goploy) server.Response {
type ReqData struct {
2023-07-26 14:32:09 +08:00
RoleID int64 `json:"roleId" validate:"required,gt=0"`
2022-11-23 16:49:55 +08:00
}
var reqData ReqData
2023-07-26 14:32:09 +08:00
if err := gp.Decode(&reqData); err != nil {
2022-11-23 16:49:55 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
}
2023-06-20 11:02:02 +08:00
list, err := model.RolePermission{RoleID: reqData.RoleID}.GetList()
2022-11-23 16:49:55 +08:00
if err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
}
2023-07-26 14:32:09 +08:00
type RespData struct {
List model.RolePermissions `json:"list"`
}
2022-11-23 16:49:55 +08:00
return response.JSON{
2023-07-26 14:32:09 +08:00
Data: RespData{List: list},
2022-11-23 16:49:55 +08:00
}
}
2023-07-26 14:32:09 +08:00
// Add adds a role
// @Summary Add a role
// @Tags Role
// @Produce json
// @Security ApiKeyHeader || ApiKeyQueryParam || NamespaceHeader || NamespaceQueryParam
// @Param request body role.Add.ReqData true "body params"
// @Success 200 {object} response.JSON{data=role.Add.RespData}
// @Router /role/add [post]
2022-11-23 16:49:55 +08:00
func (Role) Add(gp *server.Goploy) server.Response {
type ReqData struct {
Name string `json:"name" validate:"required"`
Description string `json:"description" validate:"max=255"`
}
var reqData ReqData
2023-07-26 14:32:09 +08:00
if err := gp.Decode(&reqData); err != nil {
2022-11-23 16:49:55 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
}
2023-06-20 11:02:02 +08:00
id, err := model.Role{Name: reqData.Name, Description: reqData.Description}.AddRow()
2022-11-23 16:49:55 +08:00
if err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
}
2023-07-26 14:32:09 +08:00
type RespData struct {
ID int64 `json:"id"`
}
2022-11-23 16:49:55 +08:00
return response.JSON{
2023-07-26 14:32:09 +08:00
Data: RespData{ID: id},
2022-11-23 16:49:55 +08:00
}
}
2023-07-26 14:32:09 +08:00
// Edit edits the role
// @Summary Edit the role
// @Tags Role
// @Produce json
// @Security ApiKeyHeader || ApiKeyQueryParam || NamespaceHeader || NamespaceQueryParam
// @Param request body role.Edit.ReqData true "body params"
// @Success 200 {object} response.JSON
// @Router /role/edit [put]
2022-11-23 16:49:55 +08:00
func (Role) Edit(gp *server.Goploy) server.Response {
type ReqData struct {
2023-07-26 14:32:09 +08:00
ID int64 `json:"id" validate:"required,gt=0"`
2022-11-23 16:49:55 +08:00
Name string `json:"name" validate:"required"`
Description string `json:"description" validate:"max=255"`
}
var reqData ReqData
2023-07-26 14:32:09 +08:00
if err := gp.Decode(&reqData); err != nil {
2022-11-23 16:49:55 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
}
2023-06-20 11:02:02 +08:00
err := model.Role{ID: reqData.ID, Name: reqData.Name, Description: reqData.Description}.EditRow()
2022-11-23 16:49:55 +08:00
if err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
}
return response.JSON{}
}
2023-07-26 14:32:09 +08:00
// Remove removes the role
// @Summary Remove the role
// @Tags Role
// @Produce json
// @Security ApiKeyHeader || ApiKeyQueryParam || NamespaceHeader || NamespaceQueryParam
// @Param request body role.Remove.ReqData true "body params"
// @Success 200 {object} response.JSON
// @Router /role/remove [delete]
2022-11-23 16:49:55 +08:00
func (Role) Remove(gp *server.Goploy) server.Response {
type ReqData struct {
2023-07-26 14:32:09 +08:00
ID int64 `json:"id" validate:"required,gt=0"`
2022-11-23 16:49:55 +08:00
}
var reqData ReqData
2023-07-26 14:32:09 +08:00
if err := gp.Decode(&reqData); err != nil {
2022-11-23 16:49:55 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
}
2023-06-20 11:02:02 +08:00
namespaceUser, err := (model.NamespaceUser{RoleID: reqData.ID}).GetDataByRoleID()
2022-11-23 16:49:55 +08:00
if err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
}
if namespaceUser.ID > 0 {
return response.JSON{Code: response.Error, Message: "The role has binding user"}
}
2023-06-20 11:02:02 +08:00
if err := (model.Role{ID: reqData.ID}).DeleteRow(); err != nil {
2022-11-23 16:49:55 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
}
return response.JSON{}
}
2023-07-26 14:32:09 +08:00
// ChangePermission changes the role permissions
// @Summary Change the role permissions
// @Tags Role
// @Produce json
// @Security ApiKeyHeader || ApiKeyQueryParam || NamespaceHeader || NamespaceQueryParam
// @Param request body role.ChangePermission.ReqData true "body params"
// @Success 200 {object} response.JSON
// @Router /role/changePermission [put]
2022-11-23 16:49:55 +08:00
func (Role) ChangePermission(gp *server.Goploy) server.Response {
type ReqData struct {
PermissionIDs []int64 `json:"permissionIds" validate:"required"`
2023-07-26 14:32:09 +08:00
RoleID int64 `json:"roleId" validate:"required,gt=0"`
2022-11-23 16:49:55 +08:00
}
var reqData ReqData
2023-07-26 14:32:09 +08:00
if err := gp.Decode(&reqData); err != nil {
2022-11-23 16:49:55 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
}
2023-06-20 11:02:02 +08:00
if err := (model.RolePermission{RoleID: reqData.RoleID}).DeleteByRoleID(); err != nil {
2022-11-23 16:49:55 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
}
2023-06-20 11:02:02 +08:00
rolePermissionsModel := model.RolePermissions{}
2022-11-23 16:49:55 +08:00
for _, PermissionID := range reqData.PermissionIDs {
2023-06-20 11:02:02 +08:00
rolePermissionModel := model.RolePermission{
2022-11-23 16:49:55 +08:00
PermissionID: PermissionID,
RoleID: reqData.RoleID,
}
rolePermissionsModel = append(rolePermissionsModel, rolePermissionModel)
}
if err := rolePermissionsModel.AddMany(); err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
}
return response.JSON{}
}