diff options
author | Adrian Carolli <adrian.caarolli@gmail.com> | 2018-01-05 14:46:48 -0500 |
---|---|---|
committer | Joram Wilander <jwawilander@gmail.com> | 2018-01-05 14:46:48 -0500 |
commit | fd3fa8f8dcfa5de42a16db9b62e1d6628f43b0fd (patch) | |
tree | f60a1d693dc554dddfc9a365e53420359be932a1 /api4 | |
parent | 66bdf830b5c7bc2e2fe7598355a142720010de91 (diff) | |
download | chat-fd3fa8f8dcfa5de42a16db9b62e1d6628f43b0fd.tar.gz chat-fd3fa8f8dcfa5de42a16db9b62e1d6628f43b0fd.tar.bz2 chat-fd3fa8f8dcfa5de42a16db9b62e1d6628f43b0fd.zip |
[PLT-7793] Added /users/tokens endpoint (#8038)
* Added /users/tokens/all endpoint
- UserAccessStore now has getAll method
- Added tests
- Added route
- Added handler
* Remove space fix check-style
* Remove blank space check-style
* Fixes for make check-style
* Remove extra code that is un-needed in user_test.go
* Rename endpoint + grammar
- Renamed /users/tokens/all to /users/tokens
- Renamed getUserAccessTokens to getUserAccessTokensForUser
- Renamed getAllUserAccessTokens to getUserAccessTokens
- Minor Grammar changes
* Add localization for sql_user_access_token.get_all
* Fix minor plural spelling
Diffstat (limited to 'api4')
-rw-r--r-- | api4/user.go | 18 | ||||
-rw-r--r-- | api4/user_test.go | 17 |
2 files changed, 34 insertions, 1 deletions
diff --git a/api4/user.go b/api4/user.go index 0b07f8dc7..cd26b00e3 100644 --- a/api4/user.go +++ b/api4/user.go @@ -58,7 +58,8 @@ func (api *API) InitUser() { api.BaseRoutes.User.Handle("/audits", api.ApiSessionRequired(getUserAudits)).Methods("GET") api.BaseRoutes.User.Handle("/tokens", api.ApiSessionRequired(createUserAccessToken)).Methods("POST") - api.BaseRoutes.User.Handle("/tokens", api.ApiSessionRequired(getUserAccessTokens)).Methods("GET") + api.BaseRoutes.User.Handle("/tokens", api.ApiSessionRequired(getUserAccessTokensForUser)).Methods("GET") + api.BaseRoutes.Users.Handle("/tokens", api.ApiSessionRequired(getUserAccessTokens)).Methods("GET") api.BaseRoutes.Users.Handle("/tokens/{token_id:[A-Za-z0-9]+}", api.ApiSessionRequired(getUserAccessToken)).Methods("GET") api.BaseRoutes.Users.Handle("/tokens/revoke", api.ApiSessionRequired(revokeUserAccessToken)).Methods("POST") api.BaseRoutes.Users.Handle("/tokens/disable", api.ApiSessionRequired(disableUserAccessToken)).Methods("POST") @@ -1241,6 +1242,21 @@ func createUserAccessToken(c *Context, w http.ResponseWriter, r *http.Request) { } func getUserAccessTokens(c *Context, w http.ResponseWriter, r *http.Request) { + if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) { + c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM) + return + } + + accessTokens, err := c.App.GetUserAccessTokens(c.Params.Page, c.Params.PerPage) + if err != nil { + c.Err = err + return + } + + w.Write([]byte(model.UserAccessTokenListToJson(accessTokens))) +} + +func getUserAccessTokensForUser(c *Context, w http.ResponseWriter, r *http.Request) { c.RequireUserId() if c.Err != nil { return diff --git a/api4/user_test.go b/api4/user_test.go index fb9222d8f..7b103d23b 100644 --- a/api4/user_test.go +++ b/api4/user_test.go @@ -2450,6 +2450,23 @@ func TestGetUserAccessToken(t *testing.T) { if len(rtokens) != 2 { t.Fatal("should have 2 tokens") } + + _, resp = Client.GetUserAccessTokens(0,100) + CheckForbiddenStatus(t, resp) + + rtokens, resp = AdminClient.GetUserAccessTokens(1,1) + CheckNoError(t, resp) + + if len(rtokens) != 1 { + t.Fatal("should have 1 token") + } + + rtokens, resp = AdminClient.GetUserAccessTokens(0,2) + CheckNoError(t, resp) + + if len(rtokens) != 2 { + t.Fatal("should have 2 tokens") + } } func TestRevokeUserAccessToken(t *testing.T) { |