- controller golem15.user.organisations behind golem15.users.access_users - list and form (name, slug preset from the name, description, avatar) - members relation manager: link sets and unlink clears users.organisation_id - deleting an organisation releases its members first - Organisation gains Fillable, Rules, MorphName, AttachRelations, BeforeValidate and the Members relation; models.Slugify - navigation side item organisations - TestAdminOrganisations, TestAdminOrganisationMembers, TestSlugify
68 lines
2.3 KiB
Go
68 lines
2.3 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/lagoon/attach"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Organisation is a row of golem15_user_organisations, the PHP Organisation
|
|
// model: a named group of users (users.organisation_id) that other plugins
|
|
// hang organisation-level data on. The table is shared with those plugins
|
|
// and gets no column from the admin screen.
|
|
type Organisation struct {
|
|
ID uint `gorm:"column:id;primaryKey"`
|
|
Slug string `gorm:"column:slug"`
|
|
Name string `gorm:"column:name"`
|
|
Description *string `gorm:"column:description"`
|
|
CreatedAt time.Time `gorm:"column:created_at"`
|
|
UpdatedAt time.Time `gorm:"column:updated_at"`
|
|
// Members is the PHP hasMany members relation: the users whose
|
|
// organisation_id is this organisation. It is not a column. It is loaded
|
|
// only on request, written only by the admin members manager (which sets
|
|
// and clears users.organisation_id), and never serialized.
|
|
Members []User `gorm:"foreignKey:OrganisationID;references:ID" json:"-"`
|
|
}
|
|
|
|
func (Organisation) TableName() string { return "golem15_user_organisations" }
|
|
|
|
// MorphName is the PHP class string stored in system_files.attachment_type.
|
|
func (Organisation) MorphName() string { return `Golem15\User\Models\Organisation` }
|
|
|
|
// OrganisationAvatarField is the attachOne relation name of the
|
|
// organisation's picture.
|
|
const OrganisationAvatarField = "avatar"
|
|
|
|
// AttachRelations declares the public attachOne avatar (PHP $attachOne).
|
|
func (Organisation) AttachRelations() []attach.Relation {
|
|
return []attach.Relation{{Name: OrganisationAvatarField, Public: true}}
|
|
}
|
|
|
|
// Fillable is the PHP $fillable list.
|
|
func (Organisation) Fillable() []string {
|
|
return []string{"name", "slug", "description"}
|
|
}
|
|
|
|
// Rules is the PHP rule set without alpha_dash on slug, which the rule
|
|
// engine does not know: the admin controller checks the slug's characters.
|
|
func (Organisation) Rules() map[string]string {
|
|
return map[string]string{
|
|
"name": "required|max:255",
|
|
"slug": "required|unique:golem15_user_organisations",
|
|
"description": "nullable|max:5000",
|
|
}
|
|
}
|
|
|
|
// BeforeValidate fills an empty slug from the name, the PHP beforeValidate.
|
|
func (o *Organisation) BeforeValidate(_ *gorm.DB) error {
|
|
if o.Slug == "" && o.Name != "" {
|
|
o.Slug = Slugify(o.Name)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func init() {
|
|
Register(Organisation{})
|
|
}
|