mirror of https://github.com/synctv-org/synctv
Feat: root and admin role api
parent
66ba37ac59
commit
eb66cbe655
@ -0,0 +1,48 @@
|
||||
package root
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/synctv-org/synctv/internal/bootstrap"
|
||||
"github.com/synctv-org/synctv/internal/db"
|
||||
)
|
||||
|
||||
var AddCmd = &cobra.Command{
|
||||
Use: "add",
|
||||
Short: "add root by user id",
|
||||
Long: `add root by user id`,
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add(
|
||||
bootstrap.InitDiscardLog,
|
||||
bootstrap.InitConfig,
|
||||
bootstrap.InitDatabase,
|
||||
).Run()
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
return errors.New("missing user id")
|
||||
}
|
||||
id, err := strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid user id: %s", args[0])
|
||||
}
|
||||
u, err := db.GetUserByID(uint(id))
|
||||
if err != nil {
|
||||
fmt.Printf("get user failed: %s", err)
|
||||
return nil
|
||||
}
|
||||
if err := db.AddRoot(u); err != nil {
|
||||
fmt.Printf("add root failed: %s", err)
|
||||
return nil
|
||||
}
|
||||
fmt.Printf("add root success: %s\n", u.Username)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCmd.AddCommand(AddCmd)
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package root
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/synctv-org/synctv/internal/bootstrap"
|
||||
"github.com/synctv-org/synctv/internal/db"
|
||||
)
|
||||
|
||||
var RemoveCmd = &cobra.Command{
|
||||
Use: "remove",
|
||||
Short: "remove",
|
||||
Long: `remove root`,
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add(
|
||||
bootstrap.InitDiscardLog,
|
||||
bootstrap.InitConfig,
|
||||
bootstrap.InitDatabase,
|
||||
).Run()
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
return errors.New("missing user id")
|
||||
}
|
||||
id, err := strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid user id: %s", args[0])
|
||||
}
|
||||
u, err := db.GetUserByID(uint(id))
|
||||
if err != nil {
|
||||
fmt.Printf("get user failed: %s", err)
|
||||
return nil
|
||||
}
|
||||
if err := db.RemoveRoot(u); err != nil {
|
||||
fmt.Printf("remove root failed: %s", err)
|
||||
return nil
|
||||
}
|
||||
fmt.Printf("remove root success: %s\n", u.Username)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCmd.AddCommand(RemoveCmd)
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package root
|
||||
|
||||
import "github.com/spf13/cobra"
|
||||
|
||||
var RootCmd = &cobra.Command{
|
||||
Use: "root",
|
||||
Short: "root",
|
||||
Long: `you must first shut down the server, otherwise the changes will not take effect.`,
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package root
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/synctv-org/synctv/internal/bootstrap"
|
||||
"github.com/synctv-org/synctv/internal/db"
|
||||
)
|
||||
|
||||
var ShowCmd = &cobra.Command{
|
||||
Use: "show",
|
||||
Short: "show root",
|
||||
Long: `show root`,
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
return bootstrap.New(bootstrap.WithContext(cmd.Context())).Add(
|
||||
bootstrap.InitDiscardLog,
|
||||
bootstrap.InitConfig,
|
||||
bootstrap.InitDatabase,
|
||||
).Run()
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
roots := db.GetRoots()
|
||||
for _, root := range roots {
|
||||
fmt.Printf("id: %d\tusername: %s\n", root.ID, root.Username)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCmd.AddCommand(ShowCmd)
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/synctv-org/synctv/internal/db"
|
||||
dbModel "github.com/synctv-org/synctv/internal/model"
|
||||
"github.com/synctv-org/synctv/internal/op"
|
||||
"github.com/synctv-org/synctv/server/model"
|
||||
)
|
||||
|
||||
func Admins(ctx *gin.Context) {
|
||||
// user := ctx.MustGet("user").(*op.User)
|
||||
|
||||
u := db.GetAdmins()
|
||||
us := make([]model.UserInfoResp, len(u))
|
||||
for i, v := range u {
|
||||
us[i] = model.UserInfoResp{
|
||||
ID: v.ID,
|
||||
Username: v.Username,
|
||||
Role: v.Role,
|
||||
CreatedAt: v.CreatedAt.UnixMilli(),
|
||||
}
|
||||
}
|
||||
ctx.JSON(http.StatusOK, model.NewApiDataResp(us))
|
||||
}
|
||||
|
||||
func AddAdmin(ctx *gin.Context) {
|
||||
user := ctx.MustGet("user").(*op.User)
|
||||
|
||||
req := model.IdReq{}
|
||||
if err := model.Decode(ctx, &req); err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
|
||||
return
|
||||
}
|
||||
|
||||
if req.Id == user.ID {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("cannot add yourself"))
|
||||
return
|
||||
}
|
||||
u, err := op.GetUserById(req.Id)
|
||||
if err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorStringResp("user not found"))
|
||||
return
|
||||
}
|
||||
if u.Role >= dbModel.RoleAdmin {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("user is already admin"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := u.SetRole(dbModel.RoleAdmin); err != nil {
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func DeleteAdmin(ctx *gin.Context) {
|
||||
user := ctx.MustGet("user").(*op.User)
|
||||
|
||||
req := model.IdReq{}
|
||||
if err := model.Decode(ctx, &req); err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
|
||||
return
|
||||
}
|
||||
|
||||
if req.Id == user.ID {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("cannot remove yourself"))
|
||||
return
|
||||
}
|
||||
u, err := op.GetUserById(req.Id)
|
||||
if err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorStringResp("user not found"))
|
||||
return
|
||||
}
|
||||
if u.Role == dbModel.RoleRoot {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("cannot remove root"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := u.SetRole(dbModel.RoleUser); err != nil {
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
Loading…
Reference in New Issue