# HG changeset patch # User Sascha L. Teichmann # Date 1632304923 -7200 # Node ID f0c668bc4082e6e2250ac977bfea53c92cfa2d52 # Parent a30b6c6541e056364ccc2cfbf005f2c485ba3967 Moved active login/nologin to triggers. Allow /user PATCH endpoint to modify the field. diff -r a30b6c6541e0 -r f0c668bc4082 pkg/controllers/user.go --- a/pkg/controllers/user.go Tue Sep 21 22:06:43 2021 +0200 +++ b/pkg/controllers/user.go Wed Sep 22 12:02:03 2021 +0200 @@ -330,6 +330,9 @@ if patch.Reports != nil && priv { update("report_reciever", *patch.Reports) } + if patch.Active != nil && priv { + update("active", *patch.Active) + } if patch.Extent != nil { updateBox("map_extent", patch.Extent) } diff -r a30b6c6541e0 -r f0c668bc4082 pkg/models/user.go --- a/pkg/models/user.go Tue Sep 21 22:06:43 2021 +0200 +++ b/pkg/models/user.go Wed Sep 22 12:02:03 2021 +0200 @@ -59,6 +59,7 @@ Email *Email `json:"email,omitempty"` Country *Country `json:"country,omitempty"` Reports *bool `json:"reports,omitempty"` + Active *bool `json:"active,omitempty"` Extent *BoundingBox `json:"extent,omitempty"` } diff -r a30b6c6541e0 -r f0c668bc4082 schema/manage_users.sql --- a/schema/manage_users.sql Tue Sep 21 22:06:43 2021 +0200 +++ b/schema/manage_users.sql Wed Sep 22 12:02:03 2021 +0200 @@ -82,8 +82,18 @@ END IF; INSERT INTO internal.user_profiles ( - username, country, map_extent, email_address) - VALUES (NEW.username, NEW.country, NEW.map_extent, NEW.email_address); + username, country, map_extent, email_address, + report_reciever, active) + VALUES (NEW.username, NEW.country, NEW.map_extent, NEW.email_address, + NEW.report_reciever, NEW.active); + + IF NEW.active THEN + EXECUTE format( + 'ALTER ROLE %I LOGIN', NEW.username); + ELSE + EXECUTE format( + 'ALTER ROLE %I NOLOGIN', NEW.username); + END IF; -- Do not leak new password NEW.pw = ''; @@ -152,8 +162,10 @@ END IF; UPDATE internal.user_profiles p - SET (username, country, map_extent, email_address) - = (NEW.username, NEW.country, NEW.map_extent, NEW.email_address) + SET (username, country, map_extent, email_address, + report_reciever, active) + = (NEW.username, NEW.country, NEW.map_extent, NEW.email_address, + NEW.report_reciever, NEW.active) WHERE p.username = cur_username; IF NEW.rolname <> OLD.rolname @@ -172,6 +184,16 @@ internal.check_password(NEW.pw)); END IF; + IF NEW.active <> OLD.active THEN + IF NEW.active THEN + EXECUTE format( + 'ALTER ROLE %I LOGIN', cur_username); + ELSE + EXECUTE format( + 'ALTER ROLE %I NOLOGIN', cur_username); + END IF; + END IF; + -- Do not leak new password NEW.pw = ''; RETURN NEW; @@ -260,10 +282,6 @@ ('nomail@example.com', false, false) WHERE username = rolename; - -- Do not allow to login any more. - EXECUTE format( - 'ALTER ROLE %I NOLOGIN', rolename); - RETURN 2; END; $$