1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 11:46:16 +00:00
mycorrhiza/shroom/can.go

104 lines
2.3 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"
)
func canFactory(
rejectLogger func(hyphae.Hypher, *user.User, string),
2021-02-20 14:03:54 +00:00
action string,
dispatcher func(hyphae.Hypher, *user.User, *l18n.Localizer) (string, string),
2021-02-20 14:03:54 +00:00
noRightsMsg string,
notExistsMsg string,
mustExist bool,
) func(*user.User, hyphae.Hypher, *l18n.Localizer) (string, error) {
return func(u *user.User, h hyphae.Hypher, lc *l18n.Localizer) (string, error) {
2021-02-20 14:03:54 +00:00
if !u.CanProceed(action) {
rejectLogger(h, u, "no rights")
return lc.Get("ui.act_no_rights"), errors.New(lc.Get(noRightsMsg))
2021-02-20 14:03:54 +00:00
}
if mustExist {
switch h.(type) {
case *hyphae.EmptyHypha:
rejectLogger(h, u, "does not exist")
return lc.Get("ui.act_notexist"), errors.New(lc.Get(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 errtitle, 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 (
CanDelete = canFactory(
rejectDeleteLog,
"delete-confirm",
nil,
"ui.act_norights_delete",
"ui.act_notexist_delete",
2021-02-20 14:03:54 +00:00
true,
)
CanRename = canFactory(
rejectRenameLog,
"rename-confirm",
nil,
"ui.act_norights_rename",
"ui.act_notexist_rename",
2021-02-20 14:03:54 +00:00
true,
)
CanUnattach = canFactory(
rejectUnattachLog,
"unattach-confirm",
func(h hyphae.Hypher, u *user.User, lc *l18n.Localizer) (errmsg, errtitle string) {
switch h := h.(type) {
case *hyphae.EmptyHypha:
case *hyphae.MediaHypha:
if h.Kind() != hyphae.HyphaMedia {
rejectUnattachLog(h, u, "no amnt")
return lc.Get("ui.act_noattachment_tip"), lc.Get("ui.act_noattachment")
}
2021-02-20 14:03:54 +00:00
}
return "", ""
},
"ui.act_norights_unattach",
"ui.act_notexist_unattach",
2021-02-20 14:03:54 +00:00
true,
)
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(
rejectAttachLog,
"upload-binary",
nil,
"ui.act_norights_attach",
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 */