From bea49cbcf3605a6cab28bc54be41d59d7b627f38 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Tue, 21 Mar 2017 18:43:16 -0400 Subject: Add ability to use 'me' in place of user id for APIv4 (#5826) --- api4/context.go | 4 ++++ api4/user_test.go | 17 +++++++++++++++++ model/client4.go | 10 ++++++++++ model/user.go | 1 + 4 files changed, 32 insertions(+) diff --git a/api4/context.go b/api4/context.go index fe2e8d35b..9fb1361bc 100644 --- a/api4/context.go +++ b/api4/context.go @@ -333,6 +333,10 @@ func (c *Context) RequireUserId() *Context { return c } + if c.Params.UserId == model.ME { + c.Params.UserId = c.Session.UserId + } + if len(c.Params.UserId) != 26 { c.SetInvalidUrlParam("user_id") } diff --git a/api4/user_test.go b/api4/user_test.go index 2e1a0adc2..923caa761 100644 --- a/api4/user_test.go +++ b/api4/user_test.go @@ -75,6 +75,23 @@ func TestCreateUser(t *testing.T) { } } +func TestGetMe(t *testing.T) { + th := Setup().InitBasic() + defer TearDown() + Client := th.Client + + ruser, resp := Client.GetMe("") + CheckNoError(t, resp) + + if ruser.Id != th.BasicUser.Id { + t.Fatal("wrong user") + } + + Client.Logout() + _, resp = Client.GetMe("") + CheckUnauthorizedStatus(t, resp) +} + func TestGetUser(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() defer TearDown() diff --git a/model/client4.go b/model/client4.go index b94412706..f6d665e1c 100644 --- a/model/client4.go +++ b/model/client4.go @@ -349,6 +349,16 @@ func (c *Client4) CreateUser(user *User) (*User, *Response) { } } +// GetMe returns the logged in user. +func (c *Client4) GetMe(etag string) (*User, *Response) { + if r, err := c.DoApiGet(c.GetUserRoute(ME), etag); err != nil { + return nil, &Response{StatusCode: r.StatusCode, Error: err} + } else { + defer closeBody(r) + return UserFromJson(r.Body), BuildResponse(r) + } +} + // GetUser returns a user based on the provided user id string. func (c *Client4) GetUser(userId, etag string) (*User, *Response) { if r, err := c.DoApiGet(c.GetUserRoute(userId), etag); err != nil { diff --git a/model/user.go b/model/user.go index f380a7550..c7e2b6352 100644 --- a/model/user.go +++ b/model/user.go @@ -17,6 +17,7 @@ import ( ) const ( + ME = "me" USER_NOTIFY_ALL = "all" USER_NOTIFY_MENTION = "mention" USER_NOTIFY_NONE = "none" -- cgit v1.2.3-1-g7c22