From e8a04b7ce1fdb31febca7894229247207369bc85 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Mon, 20 Feb 2023 17:00:44 +0100 Subject: [PATCH] [bugfix] Fix account roles (#1542) * Change account role from string to object * Update tests * small fixes + swagger docs --------- Co-authored-by: zowhoey <11893985+zowhoey@users.noreply.github.com> --- docs/api/swagger.yaml | 20 ++--- internal/api/client/admin/reportsget_test.go | 64 ++++++++++++---- .../api/client/instance/instancepatch_test.go | 24 ++++-- internal/api/model/account.go | 21 +++-- internal/api/model/admin.go | 2 +- internal/typeutils/internaltofrontend.go | 20 +++-- internal/typeutils/internaltofrontend_test.go | 76 ++++++++++++++----- web/source/settings/index.js | 2 +- web/source/settings/user/profile.js | 2 +- web/template/profile.tmpl | 2 +- 10 files changed, 160 insertions(+), 73 deletions(-) diff --git a/docs/api/swagger.yaml b/docs/api/swagger.yaml index 4a4d1af1..ce3eecbe 100644 --- a/docs/api/swagger.yaml +++ b/docs/api/swagger.yaml @@ -256,12 +256,7 @@ definitions: type: string x-go-name: Note role: - description: |- - Role of the account on this instance. - Omitted for remote accounts. - example: user - type: string - x-go-name: Role + $ref: '#/definitions/accountRole' source: $ref: '#/definitions/Source' statuses_count: @@ -346,6 +341,15 @@ definitions: type: object x-go-name: Relationship x-go-package: github.com/superseriousbusiness/gotosocial/internal/api/model + accountRole: + properties: + name: + type: string + x-go-name: Name + title: AccountRole models the role of an account. + type: object + x-go-name: AccountRole + x-go-package: github.com/superseriousbusiness/gotosocial/internal/api/model adminAccountInfo: properties: account: @@ -423,9 +427,7 @@ definitions: type: string x-go-name: Locale role: - description: The current role of the account. - type: string - x-go-name: Role + $ref: '#/definitions/accountRole' silenced: description: Whether the account is currently silenced type: boolean diff --git a/internal/api/client/admin/reportsget_test.go b/internal/api/client/admin/reportsget_test.go index f3572001..706dcbc4 100644 --- a/internal/api/client/admin/reportsget_test.go +++ b/internal/api/client/admin/reportsget_test.go @@ -156,7 +156,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() { "ips": [], "locale": "", "invite_request": null, - "role": "user", + "role": { + "name": "user" + }, "confirmed": false, "approved": false, "disabled": false, @@ -195,7 +197,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() { "ips": [], "locale": "en", "invite_request": "", - "role": "user", + "role": { + "name": "user" + }, "confirmed": true, "approved": true, "disabled": false, @@ -222,7 +226,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() { "last_status_at": "2021-10-20T10:40:37.000Z", "emojis": [], "fields": [], - "role": "user" + "role": { + "name": "user" + } }, "created_by_application_id": "01F8MGY43H3N2C8EWPR2FPYEXG" }, @@ -236,7 +242,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() { "ips": [], "locale": "en", "invite_request": "", - "role": "admin", + "role": { + "name": "admin" + }, "confirmed": true, "approved": true, "disabled": false, @@ -264,7 +272,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() { "emojis": [], "fields": [], "enable_rss": true, - "role": "admin" + "role": { + "name": "admin" + } }, "created_by_application_id": "01F8MGXQRHYF5QPMTMXP78QC2F" }, @@ -278,7 +288,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() { "ips": [], "locale": "en", "invite_request": "", - "role": "admin", + "role": { + "name": "admin" + }, "confirmed": true, "approved": true, "disabled": false, @@ -306,7 +318,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() { "emojis": [], "fields": [], "enable_rss": true, - "role": "admin" + "role": { + "name": "admin" + } }, "created_by_application_id": "01F8MGXQRHYF5QPMTMXP78QC2F" }, @@ -333,7 +347,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() { "ips": [], "locale": "en", "invite_request": "", - "role": "user", + "role": { + "name": "user" + }, "confirmed": true, "approved": true, "disabled": false, @@ -360,7 +376,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() { "last_status_at": "2021-10-20T10:40:37.000Z", "emojis": [], "fields": [], - "role": "user" + "role": { + "name": "user" + } }, "created_by_application_id": "01F8MGY43H3N2C8EWPR2FPYEXG" }, @@ -374,7 +392,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() { "ips": [], "locale": "", "invite_request": null, - "role": "user", + "role": { + "name": "user" + }, "confirmed": false, "approved": false, "disabled": false, @@ -528,7 +548,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet2() { "ips": [], "locale": "en", "invite_request": "", - "role": "user", + "role": { + "name": "user" + }, "confirmed": true, "approved": true, "disabled": false, @@ -555,7 +577,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet2() { "last_status_at": "2021-10-20T10:40:37.000Z", "emojis": [], "fields": [], - "role": "user" + "role": { + "name": "user" + } }, "created_by_application_id": "01F8MGY43H3N2C8EWPR2FPYEXG" }, @@ -569,7 +593,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet2() { "ips": [], "locale": "", "invite_request": null, - "role": "user", + "role": { + "name": "user" + }, "confirmed": false, "approved": false, "disabled": false, @@ -723,7 +749,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet3() { "ips": [], "locale": "en", "invite_request": "", - "role": "user", + "role": { + "name": "user" + }, "confirmed": true, "approved": true, "disabled": false, @@ -750,7 +778,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet3() { "last_status_at": "2021-10-20T10:40:37.000Z", "emojis": [], "fields": [], - "role": "user" + "role": { + "name": "user" + } }, "created_by_application_id": "01F8MGY43H3N2C8EWPR2FPYEXG" }, @@ -764,7 +794,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet3() { "ips": [], "locale": "", "invite_request": null, - "role": "user", + "role": { + "name": "user" + }, "confirmed": false, "approved": false, "disabled": false, diff --git a/internal/api/client/instance/instancepatch_test.go b/internal/api/client/instance/instancepatch_test.go index 9233bc2b..0a508083 100644 --- a/internal/api/client/instance/instancepatch_test.go +++ b/internal/api/client/instance/instancepatch_test.go @@ -151,7 +151,9 @@ func (suite *InstancePatchTestSuite) TestInstancePatch1() { "emojis": [], "fields": [], "enable_rss": true, - "role": "admin" + "role": { + "name": "admin" + } }, "max_toot_chars": 5000 }`, dst.String()) @@ -247,7 +249,9 @@ func (suite *InstancePatchTestSuite) TestInstancePatch2() { "emojis": [], "fields": [], "enable_rss": true, - "role": "admin" + "role": { + "name": "admin" + } }, "max_toot_chars": 5000 }`, dst.String()) @@ -343,7 +347,9 @@ func (suite *InstancePatchTestSuite) TestInstancePatch3() { "emojis": [], "fields": [], "enable_rss": true, - "role": "admin" + "role": { + "name": "admin" + } }, "max_toot_chars": 5000 }`, dst.String()) @@ -490,7 +496,9 @@ func (suite *InstancePatchTestSuite) TestInstancePatch6() { "emojis": [], "fields": [], "enable_rss": true, - "role": "admin" + "role": { + "name": "admin" + } }, "max_toot_chars": 5000 }`, dst.String()) @@ -609,7 +617,9 @@ func (suite *InstancePatchTestSuite) TestInstancePatch8() { "emojis": [], "fields": [], "enable_rss": true, - "role": "admin" + "role": { + "name": "admin" + } }, "max_toot_chars": 5000 }`, dst.String()) @@ -740,7 +750,9 @@ func (suite *InstancePatchTestSuite) TestInstancePatch9() { "emojis": [], "fields": [], "enable_rss": true, - "role": "admin" + "role": { + "name": "admin" + } }, "max_toot_chars": 5000 }`, dst.String()) diff --git a/internal/api/model/account.go b/internal/api/model/account.go index ddfe6cc3..238def59 100644 --- a/internal/api/model/account.go +++ b/internal/api/model/account.go @@ -96,8 +96,7 @@ type Account struct { EnableRSS bool `json:"enable_rss,omitempty"` // Role of the account on this instance. // Omitted for remote accounts. - // example: user - Role AccountRole `json:"role,omitempty"` + Role *AccountRole `json:"role,omitempty"` } // AccountCreateRequest models account creation parameters. @@ -215,13 +214,19 @@ type AccountDeleteRequest struct { // AccountRole models the role of an account. // -// swagger:enum accountRole +// swagger:model accountRole +type AccountRole struct { + Name AccountRoleName `json:"name"` +} + +// AccountRoleName represent the name of the role of an account. +// // swagger:type string -type AccountRole string +type AccountRoleName string const ( - AccountRoleUser AccountRole = "user" // Standard user - AccountRoleModerator AccountRole = "moderator" // Moderator privileges - AccountRoleAdmin AccountRole = "admin" // Instance admin - AccountRoleUnknown AccountRole = "" // We don't know / remote account + AccountRoleUser AccountRoleName = "user" // Standard user + AccountRoleModerator AccountRoleName = "moderator" // Moderator privileges + AccountRoleAdmin AccountRoleName = "admin" // Instance admin + AccountRoleUnknown AccountRoleName = "" // We don't know / remote account ) diff --git a/internal/api/model/admin.go b/internal/api/model/admin.go index df688694..cc550377 100644 --- a/internal/api/model/admin.go +++ b/internal/api/model/admin.go @@ -56,7 +56,7 @@ type AdminAccountInfo struct { // example: Pleaaaaaaaaaaaaaaase!! InviteRequest *string `json:"invite_request"` // The current role of the account. - Role string `json:"role"` + Role AccountRole `json:"role"` // Whether the account has confirmed their email address. Confirmed bool `json:"confirmed"` // Whether the account is currently approved. diff --git a/internal/typeutils/internaltofrontend.go b/internal/typeutils/internaltofrontend.go index 6e6bc381..40837ad6 100644 --- a/internal/typeutils/internaltofrontend.go +++ b/internal/typeutils/internaltofrontend.go @@ -167,10 +167,8 @@ func (c *converter) AccountToAPIAccountPublic(ctx context.Context, a *gtsmodel.A log.Errorf(ctx, "error converting account emojis: %v", err) } - var ( - acct string - role = apimodel.AccountRoleUnknown - ) + var acct string + var role *apimodel.AccountRole if a.Domain != "" { // this is a remote user @@ -185,11 +183,11 @@ func (c *converter) AccountToAPIAccountPublic(ctx context.Context, a *gtsmodel.A switch { case *user.Admin: - role = apimodel.AccountRoleAdmin + role = &apimodel.AccountRole{Name: apimodel.AccountRoleAdmin} case *user.Moderator: - role = apimodel.AccountRoleModerator + role = &apimodel.AccountRole{Name: apimodel.AccountRoleModerator} default: - role = apimodel.AccountRoleUser + role = &apimodel.AccountRole{Name: apimodel.AccountRoleUser} } } @@ -270,7 +268,7 @@ func (c *converter) AccountToAdminAPIAccount(ctx context.Context, a *gtsmodel.Ac disabled bool silenced bool suspended bool - role apimodel.AccountRole = apimodel.AccountRoleUser // assume user by default + role = apimodel.AccountRole{Name: apimodel.AccountRoleUser} // assume user by default createdByApplicationID string ) @@ -296,9 +294,9 @@ func (c *converter) AccountToAdminAPIAccount(ctx context.Context, a *gtsmodel.Ac locale = user.Locale inviteRequest = &user.Account.Reason if *user.Admin { - role = apimodel.AccountRoleAdmin + role.Name = apimodel.AccountRoleAdmin } else if *user.Moderator { - role = apimodel.AccountRoleModerator + role.Name = apimodel.AccountRoleModerator } confirmed = !user.ConfirmedAt.IsZero() approved = *user.Approved @@ -323,7 +321,7 @@ func (c *converter) AccountToAdminAPIAccount(ctx context.Context, a *gtsmodel.Ac IPs: []interface{}{}, // not implemented, Locale: locale, InviteRequest: inviteRequest, - Role: string(role), + Role: role, Confirmed: confirmed, Approved: approved, Disabled: disabled, diff --git a/internal/typeutils/internaltofrontend_test.go b/internal/typeutils/internaltofrontend_test.go index 5599e4e8..f82fd8cb 100644 --- a/internal/typeutils/internaltofrontend_test.go +++ b/internal/typeutils/internaltofrontend_test.go @@ -63,7 +63,9 @@ func (suite *InternalToFrontendTestSuite) TestAccountToFrontend() { "emojis": [], "fields": [], "enable_rss": true, - "role": "user" + "role": { + "name": "user" + } }`, string(b)) } @@ -109,7 +111,9 @@ func (suite *InternalToFrontendTestSuite) TestAccountToFrontendWithEmojiStruct() ], "fields": [], "enable_rss": true, - "role": "user" + "role": { + "name": "user" + } }`, string(b)) } @@ -155,7 +159,9 @@ func (suite *InternalToFrontendTestSuite) TestAccountToFrontendWithEmojiIDs() { ], "fields": [], "enable_rss": true, - "role": "user" + "role": { + "name": "user" + } }`, string(b)) } @@ -198,7 +204,9 @@ func (suite *InternalToFrontendTestSuite) TestAccountToFrontendSensitive() { "follow_requests_count": 0 }, "enable_rss": true, - "role": "user" + "role": { + "name": "user" + } }`, string(b)) } @@ -258,7 +266,9 @@ func (suite *InternalToFrontendTestSuite) TestStatusToFrontend() { "emojis": [], "fields": [], "enable_rss": true, - "role": "admin" + "role": { + "name": "admin" + } }, "media_attachments": [ { @@ -371,7 +381,9 @@ func (suite *InternalToFrontendTestSuite) TestStatusToFrontendUnknownLanguage() "emojis": [], "fields": [], "enable_rss": true, - "role": "admin" + "role": { + "name": "admin" + } }, "media_attachments": [ { @@ -553,7 +565,9 @@ func (suite *InternalToFrontendTestSuite) TestInstanceV1ToFrontend() { "emojis": [], "fields": [], "enable_rss": true, - "role": "admin" + "role": { + "name": "admin" + } }, "max_toot_chars": 5000 }`, string(b)) @@ -660,7 +674,9 @@ func (suite *InternalToFrontendTestSuite) TestInstanceV2ToFrontend() { "emojis": [], "fields": [], "enable_rss": true, - "role": "admin" + "role": { + "name": "admin" + } } }, "rules": [] @@ -811,7 +827,9 @@ func (suite *InternalToFrontendTestSuite) TestReportToFrontend2() { "last_status_at": "2021-10-20T10:40:37.000Z", "emojis": [], "fields": [], - "role": "user" + "role": { + "name": "user" + } } }`, string(b)) } @@ -843,7 +861,9 @@ func (suite *InternalToFrontendTestSuite) TestAdminReportToFrontend1() { "ips": [], "locale": "", "invite_request": null, - "role": "user", + "role": { + "name": "user" + }, "confirmed": false, "approved": false, "disabled": false, @@ -882,7 +902,9 @@ func (suite *InternalToFrontendTestSuite) TestAdminReportToFrontend1() { "ips": [], "locale": "en", "invite_request": "", - "role": "user", + "role": { + "name": "user" + }, "confirmed": true, "approved": true, "disabled": false, @@ -909,7 +931,9 @@ func (suite *InternalToFrontendTestSuite) TestAdminReportToFrontend1() { "last_status_at": "2021-10-20T10:40:37.000Z", "emojis": [], "fields": [], - "role": "user" + "role": { + "name": "user" + } }, "created_by_application_id": "01F8MGY43H3N2C8EWPR2FPYEXG" }, @@ -923,7 +947,9 @@ func (suite *InternalToFrontendTestSuite) TestAdminReportToFrontend1() { "ips": [], "locale": "en", "invite_request": "", - "role": "admin", + "role": { + "name": "admin" + }, "confirmed": true, "approved": true, "disabled": false, @@ -951,7 +977,9 @@ func (suite *InternalToFrontendTestSuite) TestAdminReportToFrontend1() { "emojis": [], "fields": [], "enable_rss": true, - "role": "admin" + "role": { + "name": "admin" + } }, "created_by_application_id": "01F8MGXQRHYF5QPMTMXP78QC2F" }, @@ -965,7 +993,9 @@ func (suite *InternalToFrontendTestSuite) TestAdminReportToFrontend1() { "ips": [], "locale": "en", "invite_request": "", - "role": "admin", + "role": { + "name": "admin" + }, "confirmed": true, "approved": true, "disabled": false, @@ -993,7 +1023,9 @@ func (suite *InternalToFrontendTestSuite) TestAdminReportToFrontend1() { "emojis": [], "fields": [], "enable_rss": true, - "role": "admin" + "role": { + "name": "admin" + } }, "created_by_application_id": "01F8MGXQRHYF5QPMTMXP78QC2F" }, @@ -1030,7 +1062,9 @@ func (suite *InternalToFrontendTestSuite) TestAdminReportToFrontend2() { "ips": [], "locale": "en", "invite_request": "", - "role": "user", + "role": { + "name": "user" + }, "confirmed": true, "approved": true, "disabled": false, @@ -1057,7 +1091,9 @@ func (suite *InternalToFrontendTestSuite) TestAdminReportToFrontend2() { "last_status_at": "2021-10-20T10:40:37.000Z", "emojis": [], "fields": [], - "role": "user" + "role": { + "name": "user" + } }, "created_by_application_id": "01F8MGY43H3N2C8EWPR2FPYEXG" }, @@ -1071,7 +1107,9 @@ func (suite *InternalToFrontendTestSuite) TestAdminReportToFrontend2() { "ips": [], "locale": "", "invite_request": null, - "role": "user", + "role": { + "name": "user" + }, "confirmed": false, "approved": false, "disabled": false, diff --git a/web/source/settings/index.js b/web/source/settings/index.js index 812fff6b..1be4f4a3 100644 --- a/web/source/settings/index.js +++ b/web/source/settings/index.js @@ -55,7 +55,7 @@ const nav = { const { sidebar, panelRouter } = require("./lib/get-views")(nav); function App({ account }) { - const isAdmin = account.role == "admin"; + const isAdmin = account.role.name == "admin"; const [logoutQuery] = query.useLogoutMutation(); return ( diff --git a/web/source/settings/user/profile.js b/web/source/settings/user/profile.js index b2d2d25b..c48ee093 100644 --- a/web/source/settings/user/profile.js +++ b/web/source/settings/user/profile.js @@ -90,7 +90,7 @@ function UserProfileForm({ data: profile }) { header={form.header.previewValue ?? profile.header} display_name={form.displayName.value ?? profile.username} username={profile.username} - role={profile.role} + role={profile.role.name} />
diff --git a/web/template/profile.tmpl b/web/template/profile.tmpl index e484bd61..d584676a 100644 --- a/web/template/profile.tmpl +++ b/web/template/profile.tmpl @@ -34,7 +34,7 @@
@{{ .account.Username }}@{{ .instance.AccountDomain }}
{{- /* Only render account role if 1. it's present and 2. it's not equal to the standard 'user' role */ -}} - {{ if and (.account.Role) (ne .account.Role "user") }}
{{ .account.Role }}
{{ end }} + {{ if and (.account.Role) (ne .account.Role.Name "user") }}
{{ .account.Role.Name }}
{{ end }}