From 141c58f5a6353a3e9173ababd1532819ba2ee284 Mon Sep 17 00:00:00 2001 From: Lanre Adelowo Date: Sun, 3 Mar 2019 23:57:24 +0100 Subject: [PATCH] add isAdmin to user model (#6231) update vendor and add tests fix swagger --- Gopkg.lock | 4 ++-- models/user.go | 1 + models/user_test.go | 17 +++++++++++++++++ routers/api/v1/user/user.go | 1 + templates/swagger/v1_json.tmpl | 5 +++++ vendor/code.gitea.io/sdk/gitea/user.go | 2 ++ 6 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 05d54a2332..b1103ce083 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -11,11 +11,11 @@ [[projects]] branch = "master" - digest = "1:59b2036a2d4b51fc91018adebd33ec4a893aa2b11af3a606445fe8df5640f514" + digest = "1:dbf849e6552740945ac1c6c6acba590fbc594e4efa80cf05568dec8579ae0dab" name = "code.gitea.io/sdk" packages = ["gitea"] pruneopts = "NUT" - revision = "9c4f6485997bcff568e30cfe45165018ac5772c1" + revision = "e4effe4df2b895ca51482d24edb8748704326f1a" [[projects]] digest = "1:5d72bbcc9c8667b11c3dc3cbe681c5a6f71e5096744c0bf7726ab5c6425d5dc4" diff --git a/models/user.go b/models/user.go index f6c2d6e25b..b64454d213 100644 --- a/models/user.go +++ b/models/user.go @@ -211,6 +211,7 @@ func (u *User) APIFormat() *api.User { Email: u.getEmail(), AvatarURL: u.AvatarLink(), Language: u.Language, + IsAdmin: u.IsAdmin, } } diff --git a/models/user_test.go b/models/user_test.go index 35efc3959a..f0a8dbdd47 100644 --- a/models/user_test.go +++ b/models/user_test.go @@ -23,6 +23,23 @@ func TestGetUserEmailsByNames(t *testing.T) { assert.Equal(t, []string{"user8@example.com", "user5@example.com"}, GetUserEmailsByNames([]string{"user8", "user5"})) } +func TestUser_APIFormat(t *testing.T) { + + user, err := GetUserByID(1) + assert.NoError(t, err) + assert.True(t, user.IsAdmin) + + apiUser := user.APIFormat() + assert.True(t, apiUser.IsAdmin) + + user, err = GetUserByID(2) + assert.NoError(t, err) + assert.False(t, user.IsAdmin) + + apiUser = user.APIFormat() + assert.False(t, apiUser.IsAdmin) +} + func TestCanCreateOrganization(t *testing.T) { assert.NoError(t, PrepareTestDatabase()) diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go index 40d1b76805..cbc7bbc238 100644 --- a/routers/api/v1/user/user.go +++ b/routers/api/v1/user/user.go @@ -75,6 +75,7 @@ func Search(ctx *context.APIContext) { UserName: users[i].Name, AvatarURL: users[i].AvatarLink(), FullName: markup.Sanitize(users[i].FullName), + IsAdmin: users[i].IsAdmin, } if ctx.IsSigned && (!users[i].KeepEmailPrivate || ctx.User.IsAdmin) { results[i].Email = users[i].Email diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index b825c20db7..4a4b408bb9 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -8640,6 +8640,11 @@ "format": "int64", "x-go-name": "ID" }, + "is_admin": { + "description": "Is the user an administrator", + "type": "boolean", + "x-go-name": "IsAdmin" + }, "language": { "description": "User locale", "type": "string", diff --git a/vendor/code.gitea.io/sdk/gitea/user.go b/vendor/code.gitea.io/sdk/gitea/user.go index 57cf752316..42d9b983e3 100644 --- a/vendor/code.gitea.io/sdk/gitea/user.go +++ b/vendor/code.gitea.io/sdk/gitea/user.go @@ -24,6 +24,8 @@ type User struct { AvatarURL string `json:"avatar_url"` // User locale Language string `json:"language"` + // Is the user an administrator + IsAdmin bool `json:"is_admin"` } // MarshalJSON implements the json.Marshaler interface for User, adding field(s) for backward compatibility