mirror of https://github.com/synctv-org/synctv
Feat: cmd user manager
parent
0082218c90
commit
d3abf4bc45
@ -0,0 +1,48 @@
|
||||
package admin
|
||||
|
||||
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 admin by user id",
|
||||
Long: `add admin 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.AddAdmin(u); err != nil {
|
||||
fmt.Printf("add admin failed: %s", err)
|
||||
return nil
|
||||
}
|
||||
fmt.Printf("add admin success: %s\n", u.Username)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
AdminCmd.AddCommand(AddCmd)
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var AdminCmd = &cobra.Command{
|
||||
Use: "admin",
|
||||
Short: "admin",
|
||||
Long: `you must first shut down the server, otherwise the changes will not take effect.`,
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package admin
|
||||
|
||||
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 admin`,
|
||||
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.RemoveAdmin(u); err != nil {
|
||||
fmt.Printf("remove admin failed: %s", err)
|
||||
return nil
|
||||
}
|
||||
fmt.Printf("remove admin success: %s\n", u.Username)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
AdminCmd.AddCommand(RemoveCmd)
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package admin
|
||||
|
||||
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 admin",
|
||||
Long: `show admin`,
|
||||
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 {
|
||||
admins := db.GetAdmins()
|
||||
for _, admin := range admins {
|
||||
fmt.Printf("id: %d\tusername: %s\n", admin.ID, admin.Username)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
AdminCmd.AddCommand(ShowCmd)
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/synctv-org/synctv/internal/bootstrap"
|
||||
"github.com/synctv-org/synctv/internal/db"
|
||||
)
|
||||
|
||||
var BanCmd = &cobra.Command{
|
||||
Use: "ban",
|
||||
Short: "ban user with user id",
|
||||
Long: "ban user with 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\n", err)
|
||||
return nil
|
||||
}
|
||||
err = db.BanUser(u)
|
||||
if err != nil {
|
||||
fmt.Printf("ban user failed: %s\n", err)
|
||||
return nil
|
||||
}
|
||||
fmt.Printf("ban user success: %s\n", u.Username)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
UserCmd.AddCommand(BanCmd)
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/synctv-org/synctv/internal/bootstrap"
|
||||
"github.com/synctv-org/synctv/internal/db"
|
||||
)
|
||||
|
||||
var DeleteCmd = &cobra.Command{
|
||||
Use: "delete",
|
||||
Short: "delete",
|
||||
Long: `delete user`,
|
||||
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.LoadAndDeleteUserByID(uint(id))
|
||||
if err != nil {
|
||||
fmt.Printf("delete user failed: %s\n", err)
|
||||
return nil
|
||||
}
|
||||
fmt.Printf("delete user success: %s\n", u.Username)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
UserCmd.AddCommand(DeleteCmd)
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/synctv-org/synctv/internal/bootstrap"
|
||||
"github.com/synctv-org/synctv/internal/db"
|
||||
)
|
||||
|
||||
var SearchCmd = &cobra.Command{
|
||||
Use: "search",
|
||||
Short: "search user by id or username",
|
||||
Long: `search user by id or username`,
|
||||
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 or username")
|
||||
}
|
||||
us, err := db.GetUserByIDOrUsernameLike(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(us) == 0 {
|
||||
fmt.Println("user not found")
|
||||
return nil
|
||||
}
|
||||
for _, u := range us {
|
||||
fmt.Printf("id: %d\tusername: %s\tcreated_at: %s\trole: %s\n", u.ID, u.Username, u.CreatedAt, u.Role)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
UserCmd.AddCommand(SearchCmd)
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/synctv-org/synctv/internal/bootstrap"
|
||||
"github.com/synctv-org/synctv/internal/db"
|
||||
)
|
||||
|
||||
var UnbanCmd = &cobra.Command{
|
||||
Use: "unban",
|
||||
Short: "unban user with user id",
|
||||
Long: "unban user with 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\n", err)
|
||||
return nil
|
||||
}
|
||||
err = db.UnbanUser(u)
|
||||
if err != nil {
|
||||
fmt.Printf("unban user failed: %s", err)
|
||||
return nil
|
||||
}
|
||||
fmt.Printf("unban user success: %s\n", u.Username)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
UserCmd.AddCommand(UnbanCmd)
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package user
|
||||
|
||||
import "github.com/spf13/cobra"
|
||||
|
||||
var UserCmd = &cobra.Command{
|
||||
Use: "user",
|
||||
Short: "user",
|
||||
Long: `you must first shut down the server, otherwise the changes will not take effect.`,
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/synctv-org/synctv/internal/model"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
func GetSettingItems() ([]*model.SettingItem, error) {
|
||||
var items []*model.SettingItem
|
||||
err := db.Find(&items).Error
|
||||
return items, err
|
||||
}
|
||||
|
||||
func GetSettingItemByName(name string) (*model.SettingItem, error) {
|
||||
var item model.SettingItem
|
||||
err := db.Where("name = ?", name).First(&item).Error
|
||||
return &item, err
|
||||
}
|
||||
|
||||
func SaveSettingItem(item *model.SettingItem) error {
|
||||
return db.Clauses(clause.OnConflict{
|
||||
UpdateAll: true,
|
||||
}).Save(item).Error
|
||||
}
|
||||
|
||||
func DeleteSettingItem(item *model.SettingItem) error {
|
||||
return db.Delete(item).Error
|
||||
}
|
||||
|
||||
func DeleteSettingItemByName(name string) error {
|
||||
return db.Where("name = ?", name).Delete(&model.SettingItem{}).Error
|
||||
}
|
||||
|
||||
func GetSettingItemValue(name string) (string, error) {
|
||||
var value string
|
||||
err := db.Model(&model.SettingItem{}).Where("name = ?", name).Select("value").First(&value).Error
|
||||
return value, err
|
||||
}
|
||||
|
||||
func SetSettingItemValue(name, value string) error {
|
||||
return db.Model(&model.SettingItem{}).Where("name = ?", name).Update("value", value).Error
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package model
|
||||
|
||||
type SettingItem struct {
|
||||
Name string `gorm:"primaryKey"`
|
||||
Value string
|
||||
}
|
Loading…
Reference in New Issue