app/calendar/util.go

52 lines
1.2 KiB
Go
Raw Normal View History

2021-12-24 08:56:16 +00:00
package calendar
import (
"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
}
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.GroupName == "" {
return nil
}
if !auth.ACHas(c, auth.ADMIN, auth.SYSTEM, e.GroupName) {
return errors.ErrorForbidden
}
return nil
}
2021-12-24 14:31:29 +00:00
func ChangeCalGroup(cal_id, to string) error {
return db.Transaction(func(tx *gorm.DB) error {
2021-12-24 14:45:28 +00:00
affected := tx.Exec(`update calendars set group_id = (
2021-12-24 14:31:29 +00:00
select id from groups where name = ?
2021-12-24 14:45:28 +00:00
) where id = ?`, to, cal_id).RowsAffected
2021-12-24 14:31:29 +00:00
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
})
}