diff options
Diffstat (limited to 'api4')
-rw-r--r-- | api4/user.go | 32 | ||||
-rw-r--r-- | api4/user_test.go | 58 |
2 files changed, 83 insertions, 7 deletions
diff --git a/api4/user.go b/api4/user.go index 24c1c917b..04faf13c4 100644 --- a/api4/user.go +++ b/api4/user.go @@ -277,9 +277,21 @@ func getUsers(c *Context, w http.ResponseWriter, r *http.Request) { inChannelId := r.URL.Query().Get("in_channel") notInChannelId := r.URL.Query().Get("not_in_channel") withoutTeam := r.URL.Query().Get("without_team") + sort := r.URL.Query().Get("sort") if len(notInChannelId) > 0 && len(inTeamId) == 0 { - c.SetInvalidParam("team_id") + c.SetInvalidUrlParam("team_id") + return + } + + if sort != "" && sort != "last_activity_at" && sort != "create_at" { + c.SetInvalidUrlParam("sort") + return + } + + // Currently only supports sorting on a team + if (sort == "last_activity_at" || sort == "create_at") && (inTeamId == "" || notInTeamId != "" || inChannelId != "" || notInChannelId != "" || withoutTeam != "") { + c.SetInvalidUrlParam("sort") return } @@ -287,7 +299,7 @@ func getUsers(c *Context, w http.ResponseWriter, r *http.Request) { var err *model.AppError etag := "" - if withoutTeamBool, err := strconv.ParseBool(withoutTeam); err == nil && withoutTeamBool { + if withoutTeamBool, _ := strconv.ParseBool(withoutTeam); withoutTeamBool { // Use a special permission for now if !app.SessionHasPermissionTo(c.Session, model.PERMISSION_LIST_USERS_WITHOUT_TEAM) { c.SetPermissionError(model.PERMISSION_LIST_USERS_WITHOUT_TEAM) @@ -320,12 +332,18 @@ func getUsers(c *Context, w http.ResponseWriter, r *http.Request) { return } - etag = app.GetUsersInTeamEtag(inTeamId) - if HandleEtag(etag, "Get Users in Team", w, r) { - return - } + if sort == "last_activity_at" { + profiles, err = app.GetRecentlyActiveUsersForTeamPage(inTeamId, c.Params.Page, c.Params.PerPage, c.IsSystemAdmin()) + } else if sort == "create_at" { + profiles, err = app.GetNewUsersForTeamPage(inTeamId, c.Params.Page, c.Params.PerPage, c.IsSystemAdmin()) + } else { + etag = app.GetUsersInTeamEtag(inTeamId) + if HandleEtag(etag, "Get Users in Team", w, r) { + return + } - profiles, err = app.GetUsersInTeamPage(inTeamId, c.Params.Page, c.Params.PerPage, c.IsSystemAdmin()) + profiles, err = app.GetUsersInTeamPage(inTeamId, c.Params.Page, c.Params.PerPage, c.IsSystemAdmin()) + } } else if len(inChannelId) > 0 { if !app.SessionHasPermissionToChannel(c.Session, inChannelId, model.PERMISSION_READ_CHANNEL) { c.SetPermissionError(model.PERMISSION_READ_CHANNEL) diff --git a/api4/user_test.go b/api4/user_test.go index 1067ebaf6..77157e250 100644 --- a/api4/user_test.go +++ b/api4/user_test.go @@ -1224,6 +1224,64 @@ func TestGetUsers(t *testing.T) { CheckUnauthorizedStatus(t, resp) } +func TestGetNewUsersInTeam(t *testing.T) { + th := Setup().InitBasic() + defer TearDown() + Client := th.Client + teamId := th.BasicTeam.Id + + rusers, resp := Client.GetNewUsersInTeam(teamId, 0, 60, "") + CheckNoError(t, resp) + + lastCreateAt := model.GetMillis() + for _, u := range rusers { + if u.CreateAt > lastCreateAt { + t.Fatal("bad sorting") + } + lastCreateAt = u.CreateAt + CheckUserSanitization(t, u) + } + + rusers, resp = Client.GetNewUsersInTeam(teamId, 1, 1, "") + CheckNoError(t, resp) + if len(rusers) != 1 { + t.Fatal("should be 1 per page") + } + + Client.Logout() + _, resp = Client.GetNewUsersInTeam(teamId, 1, 1, "") + CheckUnauthorizedStatus(t, resp) +} + +func TestGetRecentlyActiveUsersInTeam(t *testing.T) { + th := Setup().InitBasic() + defer TearDown() + Client := th.Client + teamId := th.BasicTeam.Id + + app.SetStatusOnline(th.BasicUser.Id, "", true) + + rusers, resp := Client.GetRecentlyActiveUsersInTeam(teamId, 0, 60, "") + CheckNoError(t, resp) + + for _, u := range rusers { + if u.LastActivityAt == 0 { + t.Fatal("did not return last activity at") + } + CheckUserSanitization(t, u) + } + + rusers, resp = Client.GetRecentlyActiveUsersInTeam(teamId, 0, 1, "") + CheckNoError(t, resp) + if len(rusers) != 1 { + t.Fatal("should be 1 per page") + } + + Client.Logout() + _, resp = Client.GetRecentlyActiveUsersInTeam(teamId, 0, 1, "") + CheckUnauthorizedStatus(t, resp) +} + func TestGetUsersWithoutTeam(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() defer TearDown() |