package calendar import ( "github.com/gin-gonic/gin" "gorm.io/gorm" "kumoly.io/kumoly/app/auth" "kumoly.io/kumoly/app/errors" "kumoly.io/kumoly/app/history" "kumoly.io/kumoly/app/server" ) func ApiCalQuery(c *gin.Context) { id := c.Query("id") if id != "" { cal := &Calendar{} err := HasCalAccess(c, cal, id) if err != nil { panic(err) } server.OK(c, cal) } else { grp := c.Query("grp") cals := []Calendar{} cl, err := auth.GetContextClaims(c) if err != nil { panic(err) } var result *gorm.DB if grp != "" && auth.ACHas(c, auth.ADMIN, auth.SYSTEM, grp) { var grp_id uint db.Raw("select id from groups where name = ?", grp).Scan(&grp_id) if grp_id == 0 { panic(errors.ErrorNotFound) } result = db.Find(&cals, "`group_id` = ? ", grp_id) } else if !auth.ACHas(c, auth.ADMIN, auth.SYSTEM) { result = db. Find(&cals, "`group_id` in (?) or group_id = 0", db.Table("groups").Select("id").Where("name in ?", cl.Groups)) } else { result = db.Find(&cals) } if result.Error != nil { panic(result.Error) } server.OK(c, cals) } } func ApiCalNew(c *gin.Context) { cal := &Calendar{} if err := c.ShouldBindJSON(cal); err != nil { panic(err) } if cal.ID != "" { panic(errors.ErrorBadRequest) } if !auth.ACHas(c, auth.ADMIN, auth.SYSTEM, cal.GroupName) { panic(errors.ErrorForbidden) } if err := db.Create(cal).Error; err != nil { panic(err) } history.Send(history.Info(). Nm("Create"). Grp(cal.GroupName).Bd(cal). Iss(c.GetString(auth.GinUserKey)). Msg("Calendar created")) server.OK(c, cal) } func ApiCalUpdate(c *gin.Context) { cal := &Calendar{} if err := c.ShouldBindJSON(cal); err != nil { panic(err) } if cal.ID == "" { panic(errors.ErrorBadRequest) } if err := HasCalAccess(c, &Calendar{}, cal.ID); err != nil { panic(errors.ErrorForbidden) } if err := db.Save(cal).Error; err != nil { panic(err) } history.Send(history.Info(). Nm("Update"). Grp(cal.GroupName).Bd(cal). Iss(c.GetString(auth.GinUserKey)). Msg("Calendar Updated")) server.OK(c, cal) } func ApiCalDelete(c *gin.Context) { id := c.Query("id") if id == "" { panic(errors.ErrorBadRequest) } cal := &Calendar{} err := HasCalAccess(c, cal, id) if err != nil { panic(err) } err = db.Delete(&Calendar{}, "id = ?", id).Error if err != nil { panic(errors.NewError(404, err)) } history.Send(history.Info(). Nm("Delete"). Grp(cal.GroupName).Bd(cal). Iss(c.GetString(auth.GinUserKey)). Msg("Calendar Deleted")) server.OK(c, "ok") }