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-10-01 17:12:16 +00:00
|
|
|
) func(*user.User, *hyphae.Hypha) (string, error) {
|
|
|
|
return func(u *user.User, h *hyphae.Hypha) (string, error) {
|
2021-02-20 14:03:54 +00:00
|
|
|
if !u.CanProceed(action) {
|
|
|
|
rejectLogger(h, u, "no rights")
|
2021-10-01 17:12:16 +00:00
|
|
|
return "Not enough rights", errors.New(noRightsMsg)
|
2021-02-20 14:03:54 +00:00
|
|
|
}
|
|
|
|
|
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")
|
2021-10-01 17:12:16 +00:00
|
|
|
return "Does not exist", errors.New(notExistsMsg)
|
2021-02-20 14:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if dispatcher == nil {
|
2021-10-01 17:12:16 +00:00
|
|
|
return "", nil
|
2021-02-20 14:03:54 +00:00
|
|
|
}
|
|
|
|
errmsg, errtitle := dispatcher(h, u)
|
|
|
|
if errtitle == "" {
|
2021-10-01 17:12:16 +00:00
|
|
|
return "", nil
|
2021-02-20 14:03:54 +00:00
|
|
|
}
|
2021-10-01 17:12:16 +00:00
|
|
|
return errtitle, errors.New(errmsg)
|
2021-02-20 14:03:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 17:12:16 +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,
|
|
|
|
"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,
|
|
|
|
)
|
|
|
|
)
|