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

93 lines
2.0 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/user"
)
func canFactory(
rejectLogger func(*hyphae.Hypha, *user.User, string),
action string,
dispatcher func(*hyphae.Hypha, *user.User) (string, string),
noRightsMsg string,
notExistsMsg string,
2021-03-06 09:44:20 +00:00
careAboutExistence bool,
2021-02-20 14:03:54 +00:00
) func(*user.User, *hyphae.Hypha) (error, string) {
return func(u *user.User, h *hyphae.Hypha) (error, string) {
if !u.CanProceed(action) {
rejectLogger(h, u, "no rights")
return errors.New(noRightsMsg), "Not enough rights"
}
2021-03-06 09:44:20 +00:00
if careAboutExistence && !h.Exists {
2021-02-20 14:03:54 +00:00
rejectLogger(h, u, "does not exist")
return errors.New(notExistsMsg), "Does not exist"
}
if dispatcher == nil {
return nil, ""
}
errmsg, errtitle := dispatcher(h, u)
if errtitle == "" {
return nil, ""
}
return errors.New(errmsg), errtitle
}
}
var (
CanDelete = canFactory(
rejectDeleteLog,
"delete-confirm",
nil,
"Not enough rights to delete, you must be a moderator",
"Cannot delete this hypha because it does not exist",
true,
)
CanRename = canFactory(
rejectRenameLog,
"rename-confirm",
nil,
"Not enough rights to rename, you must be a trusted editor",
"Cannot rename this hypha because it does not exist",
true,
)
CanUnattach = canFactory(
rejectUnattachLog,
"unattach-confirm",
func(h *hyphae.Hypha, u *user.User) (errmsg, errtitle string) {
if h.BinaryPath == "" {
rejectUnattachLog(h, u, "no amnt")
return "Cannot unattach this hypha because it has no attachment", "No attachment"
}
return "", ""
},
"Not enough rights to unattach, you must be a trusted editor",
"Cannot unattach this hypha because it does not exist",
true,
)
CanEdit = canFactory(
rejectEditLog,
"upload-text",
nil,
"You must be an editor to edit a hypha",
"You cannot edit a hypha that does not exist",
false,
)
CanAttach = canFactory(
rejectAttachLog,
"upload-binary",
nil,
"You must be an editor to attach a hypha",
"You cannot attach a hypha that does not exist",
false,
)
)