app/calendar/util.go

62 lines
1.4 KiB
Go

package calendar
import (
"math/rand"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"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
}
func HasEventAccess(c *gin.Context, e *Event, cid string) error {
err := db.First(e, "id = ?", cid).Error
if err != nil {
return errors.NewError(404, err)
}
if e.EventGroup.GroupName == "" {
return nil
}
if !auth.ACHas(c, auth.ADMIN, auth.SYSTEM, e.EventGroup.GroupName) {
return errors.ErrorForbidden
}
return nil
}
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
}
tx.Exec(`update events set group_id = (
select id from groups where name = ?
) where calendar_id = ?`, to, cal_id)
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
}