1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-13 05:50:27 +00:00
mycorrhiza/shroom/can.go

68 lines
1.5 KiB
Go
Raw Normal View History

2021-02-20 14:03:54 +00:00
package shroom
import (
"errors"
"github.com/bouncepaw/mycorrhiza/hyphae"
"github.com/bouncepaw/mycorrhiza/l18n"
2021-02-20 14:03:54 +00:00
"github.com/bouncepaw/mycorrhiza/user"
)
2022-02-04 17:04:39 +00:00
// TODO: get rid of this abomination
2021-02-20 14:03:54 +00:00
func canFactory(
2022-02-19 08:26:38 +00:00
rejectLogger func(hyphae.Hypha, *user.User, string),
2021-02-20 14:03:54 +00:00
action string,
2022-02-19 08:26:38 +00:00
dispatcher func(hyphae.Hypha, *user.User, *l18n.Localizer) (string, string),
2021-02-20 14:03:54 +00:00
noRightsMsg string,
notExistsMsg string,
mustExist bool,
) func(*user.User, hyphae.Hypha, *l18n.Localizer) error {
return func(u *user.User, h hyphae.Hypha, lc *l18n.Localizer) error {
2021-02-20 14:03:54 +00:00
if !u.CanProceed(action) {
rejectLogger(h, u, "no rights")
return errors.New(noRightsMsg)
2021-02-20 14:03:54 +00:00
}
if mustExist {
switch h.(type) {
case *hyphae.EmptyHypha:
rejectLogger(h, u, "does not exist")
return errors.New(notExistsMsg)
}
2021-02-20 14:03:54 +00:00
}
if dispatcher == nil {
return nil
2021-02-20 14:03:54 +00:00
}
errmsg, errtitle := dispatcher(h, u, lc)
2021-02-20 14:03:54 +00:00
if errtitle == "" {
return nil
2021-02-20 14:03:54 +00:00
}
return errors.New(errmsg)
2021-02-20 14:03:54 +00:00
}
}
// CanDelete and etc are hyphae operation checkers based on user rights and hyphae existence.
2021-02-20 14:03:54 +00:00
var (
CanEdit = canFactory(
rejectEditLog,
"upload-text",
nil,
"ui.act_norights_edit",
2021-02-20 14:03:54 +00:00
"You cannot edit a hypha that does not exist",
false,
)
CanAttach = canFactory(
2022-02-26 07:33:09 +00:00
rejectUploadMediaLog,
2021-02-20 14:03:54 +00:00
"upload-binary",
nil,
2022-02-26 07:33:09 +00:00
"ui.act_norights_media",
2021-02-20 14:03:54 +00:00
"You cannot attach a hypha that does not exist",
false,
)
)
/* I've left 'not exists' messages for edit and attach out of translation as they are not used -- chekoopa */