fix: clear a stale organisation_role when a user's organisation changes in the admin

The members relation manager and the organisation field of the Users form
declare cabana ClearOnChange on organisation_role: adding or removing a
member, and a form save that moves a user to another organisation or to
none, clear the role unless the same save writes it. Needs a SummerCMS
release with ClearOnChange.
This commit is contained in:
Jakub Zych
2026-10-22 03:33:12 +02:00
parent c3712dc0d4
commit aa8396916e
5 changed files with 68 additions and 22 deletions

View File

@@ -23,8 +23,9 @@ const membersRelation = "members"
// manager. The screen and every write need golem15.users.access_users, as in
// PHP.
//
// The members manager sets and clears users.organisation_id and nothing
// else. It never writes users_groups.
// The members manager sets and clears users.organisation_id, and clears
// users.organisation_role with it (a role belongs to one organisation). It
// never writes users_groups.
type organisationsAdminController struct {
app appAccess
}
@@ -73,12 +74,15 @@ func (c organisationsAdminController) requireSlugFormat(ctx context.Context, mod
// AdminRelationContracts declares the members relation manager: the users
// whose organisation_id is the organisation. Linking sets the column and
// unlinking clears it; the column is nullable, so a user is never deleted.
// Both also clear organisation_role (ClearOnChange), so a role never moves
// to another organisation or outlives the membership.
func (organisationsAdminController) AdminRelationContracts() []cabana.RelationContract {
return []cabana.RelationContract{{
Name: membersRelation,
Kind: cabana.RelationHasMany,
NewRelated: func() any { return &models.User{} },
ForeignKey: "organisation_id",
Name: membersRelation,
Kind: cabana.RelationHasMany,
NewRelated: func() any { return &models.User{} },
ForeignKey: "organisation_id",
ClearOnChange: []string{"organisation_role"},
Columns: map[string]string{
"name": "name",
"email": "email",