Fix: setting type and admin permission

pull/24/head
zijiren233 1 year ago
parent 6378b89a91
commit 8a52618fed

@ -5,6 +5,7 @@ import (
"github.com/synctv-org/synctv/internal/db"
"github.com/synctv-org/synctv/internal/model"
"github.com/synctv-org/synctv/internal/settings"
)
type User struct {
@ -12,6 +13,14 @@ type User struct {
}
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))...)
}
@ -27,7 +36,7 @@ func (u *User) IsRoot() bool {
}
func (u *User) IsAdmin() bool {
return u.Role >= model.RoleAdmin
return u.Role == model.RoleAdmin || u.IsRoot()
}
func (u *User) IsBanned() bool {

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

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

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

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

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

Loading…
Cancel
Save