feat: add admin screens, permissions and navigation
Seven admin controllers in a "Studia BM" navigation group, guarded by jz.bm.access_courses, jz.bm.access_schedule and jz.bm.access_materials, with Polish and English labels. Date and time fields are checked by the models before a save and answered as 422 on the field.
This commit is contained in:
55
controllers/hooks.go
Normal file
55
controllers/hooks.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/cabana"
|
||||
"git.golem15.com/jakub/sm-bm-plugin/models"
|
||||
)
|
||||
|
||||
// Permission codes of the plugin's admin screens.
|
||||
const (
|
||||
PermCourses = "jz.bm.access_courses"
|
||||
PermSchedule = "jz.bm.access_schedule"
|
||||
PermMaterials = "jz.bm.access_materials"
|
||||
)
|
||||
|
||||
// temporalHooks checks a model's Date and DateTime columns before every admin
|
||||
// save and answers a bad value as a 422 on its field. lagoon.Validate has no
|
||||
// date rules, so the model checks them itself (models.TemporalValidator).
|
||||
type temporalHooks struct{}
|
||||
|
||||
func (temporalHooks) FormBeforeCreate(_ context.Context, model any) error {
|
||||
return checkTemporal(model)
|
||||
}
|
||||
|
||||
func (temporalHooks) FormBeforeUpdate(_ context.Context, model any) error {
|
||||
return checkTemporal(model)
|
||||
}
|
||||
|
||||
func checkTemporal(model any) error {
|
||||
v, ok := model.(models.TemporalValidator)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
msgs := v.TemporalErrors()
|
||||
if len(msgs) == 0 {
|
||||
return nil
|
||||
}
|
||||
details := make(map[string]any, len(msgs))
|
||||
for field, m := range msgs {
|
||||
details[field] = m
|
||||
}
|
||||
return &cabana.ValidationError{Details: details}
|
||||
}
|
||||
|
||||
// courseRelation is the belongsTo picker of a course_id column.
|
||||
func courseRelation(foreignKey string) cabana.FieldRelationContract {
|
||||
return cabana.FieldRelationContract{
|
||||
Field: "course",
|
||||
Kind: "belongsTo",
|
||||
NewRelated: func() any { return &models.Course{} },
|
||||
ForeignKey: foreignKey,
|
||||
LabelColumn: "name",
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user