master
Evan Chen 2021-12-25 17:48:33 +08:00
parent df63ab8d5e
commit 5c8b5c20d6
6 changed files with 90 additions and 46 deletions

View File

@ -27,17 +27,22 @@ func ApiEventQuery(c *gin.Context) {
} }
var result *gorm.DB var result *gorm.DB
if grp != "" && auth.ACHas(c, auth.ADMIN, auth.SYSTEM, grp) { if grp != "" {
var grp_id uint if !auth.ACHas(c, auth.ADMIN, auth.SYSTEM, grp) {
db.Raw("select id from groups where name = ?", grp).Scan(&grp_id) panic(errors.ErrorForbidden)
if grp_id == 0 {
panic(errors.ErrorNotFound)
} }
result = db.Find(&events, "`group_id` = ? ", grp_id) result = db.Find(&events, `event_group_id in (
select id from event_groups where group_id = (
select id from groups where name = ?
)
)`, grp)
} else if !auth.ACHas(c, auth.ADMIN, auth.SYSTEM) { } else if !auth.ACHas(c, auth.ADMIN, auth.SYSTEM) {
result = db. result = db.Find(&events, `event_group_id in (
Find(&events, "`group_id` in (?) or group_id = 0", select eg.id from event_groups eg where eg.group_id in (
db.Table("groups").Select("id").Where("name in ?", cl.Groups)) select g.id from groups g where g.name in ?
)
)`, cl.Groups)
} else { } else {
result = db.Find(&events) result = db.Find(&events)
} }
@ -60,15 +65,28 @@ func ApiEventNew(c *gin.Context) {
if e.Start.IsZero() || e.End.IsZero() || e.Start.Before(e.End) { if e.Start.IsZero() || e.End.IsZero() || e.Start.Before(e.End) {
panic(ErrorInvalidTime) panic(ErrorInvalidTime)
} }
if !auth.ACHas(c, auth.ADMIN, auth.SYSTEM, e.GroupName) { cal := 0
if db.Raw("select count(id) from calendars where id = ?", e.CalendarID).
Scan(&cal); cal != 1 {
panic(errors.ErrorNotFound)
}
if !auth.ACHas(c, auth.ADMIN, auth.SYSTEM, e.EventGroup.GroupName) {
panic(errors.ErrorForbidden) panic(errors.ErrorForbidden)
} }
if err := db.Create(e).Error; err != nil { if e.EventGroup.Name == "" {
e.EventGroup.Name = e.Name
}
if err := db.Transaction(func(tx *gorm.DB) error {
if err := tx.Create(e).Error; err != nil {
return err
}
return nil
}); err != nil {
panic(err) panic(err)
} }
history.Send(history.Info(). history.Send(history.Info().
Nm("Create"). Nm("Create").
Grp(e.GroupName).Bd(e). Grp(e.EventGroup.GroupName).Bd(e).
Iss(c.GetString(auth.GinUserKey)). Iss(c.GetString(auth.GinUserKey)).
Msg("Event created")) Msg("Event created"))
server.OK(c, e) server.OK(c, e)
@ -90,7 +108,7 @@ func ApiEventUpdate(c *gin.Context) {
} }
history.Send(history.Info(). history.Send(history.Info().
Nm("Update"). Nm("Update").
Grp(e.GroupName).Bd(e). Grp(e.EventGroup.GroupName).Bd(e).
Iss(c.GetString(auth.GinUserKey)). Iss(c.GetString(auth.GinUserKey)).
Msg("Event Updated")) Msg("Event Updated"))
server.OK(c, e) server.OK(c, e)
@ -107,13 +125,16 @@ func ApiEventDelete(c *gin.Context) {
panic(err) panic(err)
} }
err = db.Delete(&Calendar{}, "id = ?", id).Error err = db.Delete(e, "id = ?", id).Error
if err != nil { if err != nil {
panic(errors.NewError(404, err)) panic(errors.NewError(404, err))
} }
db.Exec(`delete from event_groups where id = ?
and 0 = (select count(id) from events where event_group_id = ?)`,
e.EventGroupID, e.EventGroupID)
history.Send(history.Info(). history.Send(history.Info().
Nm("Delete"). Nm("Delete").
Grp(e.GroupName).Bd(e). Grp(e.EventGroup.GroupName).Bd(e).
Iss(c.GetString(auth.GinUserKey)). Iss(c.GetString(auth.GinUserKey)).
Msg("Event Deleted")) Msg("Event Deleted"))
server.OK(c, "ok") server.OK(c, "ok")

View File

@ -14,6 +14,11 @@ var l zerolog.Logger
var db *gorm.DB var db *gorm.DB
var color []string = []string{
"red", "pink", "purple", "deep-purple", "indigo", "blue", "light-blue", "cyan", "teal", "green",
"light-green", "lime", "yellow", "amber", "orange", "deep-orange", "blue-grey",
}
type Calendar struct { type Calendar struct {
ID string `gorm:"primaryKey"` ID string `gorm:"primaryKey"`
@ -63,5 +68,6 @@ func (c *Calendar) AfterFind(tx *gorm.DB) (err error) {
db.Raw("select name from groups where id = ?", c.GroupID).Scan(&name) db.Raw("select name from groups where id = ?", c.GroupID).Scan(&name)
c.GroupName = name c.GroupName = name
} }
return return
} }

View File

@ -5,7 +5,6 @@ import (
"github.com/rs/xid" "github.com/rs/xid"
"gorm.io/gorm" "gorm.io/gorm"
"kumoly.io/kumoly/app/auth"
"kumoly.io/kumoly/app/errors" "kumoly.io/kumoly/app/errors"
) )
@ -15,36 +14,22 @@ type Event struct {
Start time.Time Start time.Time
End time.Time End time.Time
Name string `gorm:"not null"` Name string `gorm:"not null"`
Hosts string
Description string
Location string
// ntu Color string `gorm:"-"`
Class string
CourseID string IsExt bool
Semester string
CalendarID string CalendarID string
EventGroup EventGroup `json:"Detial"`
EventGroupID string EventGroupID string
Group auth.Group `json:"-"`
GroupName string `gorm:"-" json:"Group"`
GroupID uint `json:"-"`
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
} }
func (e *Event) BeforeSave(tx *gorm.DB) (err error) { func (e *Event) BeforeSave(tx *gorm.DB) (err error) {
if e.GroupName != "" { if e.CalendarID == "" {
var grp_id uint return errors.ErrorBadRequest
db.Raw("select id from groups where name = ?", e.GroupName).Scan(&grp_id)
if grp_id == 0 {
return errors.ErrorNotFound
}
e.GroupID = grp_id
} else {
e.GroupID = 0
} }
return return
} }
@ -53,14 +38,16 @@ func (e *Event) BeforeCreate(tx *gorm.DB) (err error) {
if e.ID == "" { if e.ID == "" {
e.ID = xid.New().String() e.ID = xid.New().String()
} }
e.IsExt = false
return return
} }
func (e *Event) AfterFind(tx *gorm.DB) (err error) { func (e *Event) AfterFind(tx *gorm.DB) (err error) {
if e.GroupID != 0 { eg := EventGroup{}
var name string if err := tx.First(&eg, "id = ?", e.EventGroupID).Error; err != nil {
db.Raw("select name from groups where id = ?", e.GroupID).Scan(&name) return err
e.GroupName = name
} }
e.EventGroup = eg
e.Color = color[e.EventGroup.Color%len(color)]
return return
} }

View File

@ -15,7 +15,19 @@ type EventGroup struct {
Name string Name string
Rrule string Rrule string
Events []Event // event header
Hosts string
Description string
Location string
// ntu
Class string
CourseID string
Semester string
// UI
Color int
// event end
Group auth.Group `json:"-"` Group auth.Group `json:"-"`
GroupName string `gorm:"-" json:"Group"` GroupName string `gorm:"-" json:"Group"`
@ -36,6 +48,8 @@ func (eg *EventGroup) BeforeSave(tx *gorm.DB) (err error) {
} else { } else {
eg.GroupID = 0 eg.GroupID = 0
} }
eg.Color = GetNewColor(eg.GroupName)
return return
} }
func (eg *EventGroup) BeforeCreate(tx *gorm.DB) (err error) { func (eg *EventGroup) BeforeCreate(tx *gorm.DB) (err error) {

View File

@ -1,6 +1,9 @@
package calendar package calendar
import ( import (
"math/rand"
"time"
"kumoly.io/kumoly/app/server" "kumoly.io/kumoly/app/server"
"kumoly.io/kumoly/app/store" "kumoly.io/kumoly/app/store"
"kumoly.io/kumoly/app/util" "kumoly.io/kumoly/app/util"
@ -16,6 +19,7 @@ func (srv Service) Del() {}
func (srv Service) Health() error { return nil } func (srv Service) Health() error { return nil }
func (srv Service) Init() error { func (srv Service) Init() error {
rand.Seed(time.Now().UnixNano())
db = store.DB db = store.DB
l = util.Klog.With().Str("mod", "auth").Logger() l = util.Klog.With().Str("mod", "auth").Logger()

View File

@ -1,6 +1,8 @@
package calendar package calendar
import ( import (
"math/rand"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gorm.io/gorm" "gorm.io/gorm"
"kumoly.io/kumoly/app/auth" "kumoly.io/kumoly/app/auth"
@ -26,10 +28,10 @@ func HasEventAccess(c *gin.Context, e *Event, cid string) error {
if err != nil { if err != nil {
return errors.NewError(404, err) return errors.NewError(404, err)
} }
if e.GroupName == "" { if e.EventGroup.GroupName == "" {
return nil return nil
} }
if !auth.ACHas(c, auth.ADMIN, auth.SYSTEM, e.GroupName) { if !auth.ACHas(c, auth.ADMIN, auth.SYSTEM, e.EventGroup.GroupName) {
return errors.ErrorForbidden return errors.ErrorForbidden
} }
return nil return nil
@ -47,3 +49,13 @@ func ChangeCalGroup(tx *gorm.DB, cal_id, to string) error {
) where calendar_id = ?`, to, cal_id) ) where calendar_id = ?`, to, cal_id)
return nil return nil
} }
func GetNewColor(GroupName string) int {
eg := &EventGroup{}
if err := db.Select("color").
Where("group_id = (select id from groups where name = ? )", GroupName).
Last(eg).Error; err != nil {
return rand.Intn(len(color))
}
return eg.Color + 1
}