2021-02-20 14:03:54 +00:00
|
|
|
package shroom
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/hyphae"
|
2021-10-10 03:51:34 +00:00
|
|
|
"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,
|
2021-10-27 05:43:36 +00:00
|
|
|
mustExist bool,
|
2022-02-19 16:42:32 +00:00
|
|
|
) 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")
|
2022-02-19 16:42:32 +00:00
|
|
|
return errors.New(noRightsMsg)
|
2021-02-20 14:03:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-04 14:56:28 +00:00
|
|
|
if mustExist {
|
|
|
|
switch h.(type) {
|
|
|
|
case *hyphae.EmptyHypha:
|
|
|
|
rejectLogger(h, u, "does not exist")
|
2022-02-19 16:42:32 +00:00
|
|
|
return errors.New(notExistsMsg)
|
2022-02-04 14:56:28 +00:00
|
|
|
}
|
2021-02-20 14:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if dispatcher == nil {
|
2022-02-19 16:42:32 +00:00
|
|
|
return nil
|
2021-02-20 14:03:54 +00:00
|
|
|
}
|
2021-10-10 03:51:34 +00:00
|
|
|
errmsg, errtitle := dispatcher(h, u, lc)
|
2021-02-20 14:03:54 +00:00
|
|
|
if errtitle == "" {
|
2022-02-19 16:42:32 +00:00
|
|
|
return nil
|
2021-02-20 14:03:54 +00:00
|
|
|
}
|
2022-02-19 16:42:32 +00:00
|
|
|
return 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 (
|
|
|
|
CanEdit = canFactory(
|
|
|
|
rejectEditLog,
|
|
|
|
"upload-text",
|
|
|
|
nil,
|
2021-10-10 03:51:34 +00:00
|
|
|
"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,
|
|
|
|
)
|
|
|
|
)
|
2021-10-10 03:51:34 +00:00
|
|
|
|
|
|
|
/* I've left 'not exists' messages for edit and attach out of translation as they are not used -- chekoopa */
|