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

97 lines
2.2 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.Hypha, *user.User, string),
action string,
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) (string, error) {
return func(u *user.User, h *hyphae.Hypha, 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 && !h.Exists {
2021-02-20 14:03:54 +00:00
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.Hypha, u *user.User, lc *l18n.Localizer) (errmsg, errtitle string) {
2021-02-20 14:03:54 +00:00
if h.BinaryPath == "" {
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 */