app/calendar/util.go

64 lines
1.5 KiB
Go
Raw Normal View History

2021-12-24 08:56:16 +00:00
package calendar
import (
2021-12-25 09:48:33 +00:00
"math/rand"
2021-12-24 08:56:16 +00:00
"github.com/gin-gonic/gin"
2021-12-24 14:31:29 +00:00
"gorm.io/gorm"
2021-12-24 08:56:16 +00:00
"kumoly.io/kumoly/app/auth"
"kumoly.io/kumoly/app/errors"
)
func HasCalAccess(c *gin.Context, cal *Calendar, cid string) error {
err := db.First(cal, "id = ?", cid).Error
if err != nil {
return errors.NewError(404, err)
}
if cal.GroupName == "" {
return nil
}
if !auth.ACHas(c, auth.ADMIN, auth.SYSTEM, cal.GroupName) {
return errors.ErrorForbidden
}
return nil
}
2021-12-26 08:19:18 +00:00
func HasEventAccess(c *gin.Context, e *Event, eid string) error {
err := db.First(e, "id = ?", eid).Error
2021-12-24 08:56:16 +00:00
if err != nil {
return errors.NewError(404, err)
}
2021-12-25 09:48:33 +00:00
if e.EventGroup.GroupName == "" {
2021-12-24 08:56:16 +00:00
return nil
}
2021-12-25 09:48:33 +00:00
if !auth.ACHas(c, auth.ADMIN, auth.SYSTEM, e.EventGroup.GroupName) {
2021-12-24 08:56:16 +00:00
return errors.ErrorForbidden
}
return nil
}
2021-12-24 14:31:29 +00:00
2021-12-24 16:56:16 +00:00
func ChangeCalGroup(tx *gorm.DB, cal_id, to string) error {
affected := tx.Exec(`update calendars set group_id = (
select id from groups where name = ?
) where id = ?`, to, cal_id).RowsAffected
if affected == 0 {
return errors.ErrorNotFound
}
2021-12-26 07:42:57 +00:00
tx.Exec(`update event_groups set group_id = (
2021-12-24 16:56:16 +00:00
select id from groups where name = ?
2021-12-26 07:42:57 +00:00
) where id in (
select event_group_id from events where calendar_id = ?
)`, to, cal_id)
2021-12-24 16:56:16 +00:00
return nil
2021-12-24 14:31:29 +00:00
}
2021-12-25 09:48:33 +00:00
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 {
2021-12-26 08:19:18 +00:00
return rand.Intn(len(Color))
2021-12-25 09:48:33 +00:00
}
return eg.Color + 1
}