Fix: setting type and admin permission

pull/24/head
zijiren233 2 years ago
parent 6378b89a91
commit 8a52618fed

@ -5,6 +5,7 @@ import (
"github.com/synctv-org/synctv/internal/db" "github.com/synctv-org/synctv/internal/db"
"github.com/synctv-org/synctv/internal/model" "github.com/synctv-org/synctv/internal/model"
"github.com/synctv-org/synctv/internal/settings"
) )
type User struct { type User struct {
@ -12,6 +13,14 @@ type User struct {
} }
func (u *User) CreateRoom(name, password string, conf ...db.CreateRoomConfig) (*model.Room, error) { func (u *User) CreateRoom(name, password string, conf ...db.CreateRoomConfig) (*model.Room, error) {
if u.IsBanned() {
return nil, errors.New("user banned")
}
if !u.IsAdmin() && settings.CreateRoomNeedReview.Get() {
conf = append(conf, db.WithStatus(model.RoomStatusPending))
} else {
conf = append(conf, db.WithStatus(model.RoomStatusActive))
}
return db.CreateRoom(name, password, append(conf, db.WithCreator(&u.User))...) return db.CreateRoom(name, password, append(conf, db.WithCreator(&u.User))...)
} }
@ -27,7 +36,7 @@ func (u *User) IsRoot() bool {
} }
func (u *User) IsAdmin() bool { func (u *User) IsAdmin() bool {
return u.Role >= model.RoleAdmin return u.Role == model.RoleAdmin || u.IsRoot()
} }
func (u *User) IsBanned() bool { func (u *User) IsBanned() bool {

@ -28,8 +28,9 @@ type Bool struct {
func NewBool(name string, value bool, group model.SettingGroup) *Bool { func NewBool(name string, value bool, group model.SettingGroup) *Bool {
b := &Bool{ b := &Bool{
setting: setting{ setting: setting{
name: name, name: name,
group: group, group: group,
settingType: model.SettingTypeBool,
}, },
defaultValue: value, defaultValue: value,
value: value, value: value,
@ -47,14 +48,7 @@ func (b *Bool) Init(value string) error {
} }
func (b *Bool) Parse(value string) (bool, error) { func (b *Bool) Parse(value string) (bool, error) {
switch value { return strconv.ParseBool(value)
case "1":
return true, nil
case "0":
return false, nil
default:
return strconv.ParseBool(value)
}
} }
func (b *Bool) Stringify(value bool) string { func (b *Bool) Stringify(value bool) string {

@ -28,8 +28,9 @@ type Float64 struct {
func NewFloat64(name string, value float64, group model.SettingGroup) *Float64 { func NewFloat64(name string, value float64, group model.SettingGroup) *Float64 {
f := &Float64{ f := &Float64{
setting: setting{ setting: setting{
name: name, name: name,
group: group, group: group,
settingType: model.SettingTypeFloat64,
}, },
defaultValue: value, defaultValue: value,
value: value, value: value,

@ -138,8 +138,9 @@ func initAndFixSettings(i ...Setting) error {
} }
type setting struct { type setting struct {
name string name string
group model.SettingGroup settingType model.SettingType
group model.SettingGroup
} }
func (d *setting) Name() string { func (d *setting) Name() string {
@ -147,7 +148,7 @@ func (d *setting) Name() string {
} }
func (d *setting) Type() model.SettingType { func (d *setting) Type() model.SettingType {
return model.SettingTypeString return d.settingType
} }
func (d *setting) Group() model.SettingGroup { func (d *setting) Group() model.SettingGroup {

@ -27,8 +27,9 @@ type String struct {
func NewString(name string, value string, group model.SettingGroup) *String { func NewString(name string, value string, group model.SettingGroup) *String {
s := &String{ s := &String{
setting: setting{ setting: setting{
name: name, name: name,
group: group, group: group,
settingType: model.SettingTypeString,
}, },
defaultValue: value, defaultValue: value,
value: value, value: value,

@ -7,7 +7,6 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/synctv-org/synctv/internal/db" "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/internal/op"
"github.com/synctv-org/synctv/internal/settings" "github.com/synctv-org/synctv/internal/settings"
"github.com/synctv-org/synctv/server/middlewares" "github.com/synctv-org/synctv/server/middlewares"
@ -42,15 +41,7 @@ func CreateRoom(ctx *gin.Context) {
return return
} }
var ( r, err := user.CreateRoom(req.RoomName, req.Password, db.WithSetting(req.Setting))
r *dbModel.Room
err error
)
if settings.CreateRoomNeedReview.Get() {
r, err = user.CreateRoom(req.RoomName, req.Password, db.WithSetting(req.Setting), db.WithStatus(dbModel.RoomStatusPending))
} else {
r, err = user.CreateRoom(req.RoomName, req.Password, db.WithSetting(req.Setting), db.WithStatus(dbModel.RoomStatusActive))
}
if err != nil { if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err)) ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
return return

Loading…
Cancel
Save