Skip to content

Commit eee92b6

Browse files
committed
Return role-inherited channels in the userCtx.channels part of the session response
1 parent a1fdf23 commit eee92b6

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

rest/session_api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ func (h *handler) formatSessionResponse(user auth.User) db.Body {
331331
name = &userName
332332
}
333333
allChannels = user.Channels()
334+
for _, role := range user.GetRoles() {
335+
allChannels.Add(role.Channels())
336+
}
334337
}
335338

336339
// Return a JSON struct similar to what CouchDB returns:

rest/session_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,38 @@ func TestSessionAPI(t *testing.T) {
498498

499499
}
500500

501+
func TestSessionResponseWithRoleChannels(t *testing.T) {
502+
// The /db/_session endpoint only returns info about the default collection
503+
rt := NewRestTesterDefaultCollection(t, nil)
504+
defer rt.Close()
505+
506+
// create session test users
507+
response := rt.SendAdminRequest("POST", "/db/_user/", `{"name":"user1", "password":"1234", "admin_channels":["userChan1"], "admin_roles":["role1"]}`)
508+
RequireStatus(t, response, 201)
509+
510+
// create role for user with a channel
511+
response = rt.SendAdminRequest("POST", "/db/_role/", `{"name":"role1", "admin_channels":["roleChan1"]}`)
512+
RequireStatus(t, response, 201)
513+
514+
// create a session for the user
515+
response = rt.SendRequest("POST", "/db/_session", `{"name":"user1", "password":"1234"}`)
516+
RequireStatus(t, response, 200)
517+
518+
var body struct {
519+
UserCtx struct {
520+
Channels map[string]uint64 `json:"channels"`
521+
} `json:"userCtx"`
522+
}
523+
require.NoError(t, base.JSONUnmarshal(response.Body.Bytes(), &body))
524+
525+
// Check that the response contains userCtx with channels populated from the role
526+
require.NotNil(t, body.UserCtx.Channels)
527+
require.Equal(t, 3, len(body.UserCtx.Channels))
528+
assert.Contains(t, body.UserCtx.Channels, "!")
529+
assert.Contains(t, body.UserCtx.Channels, "userChan1")
530+
assert.Contains(t, body.UserCtx.Channels, "roleChan1")
531+
}
532+
501533
func TestSessionPasswordInvalidation(t *testing.T) {
502534
testCases := []struct {
503535
name string

0 commit comments

Comments
 (0)