{"action":"created","issue":{"url":"https://api.github.com/repos/couchbase/sync_gateway/issues/986","labels_url":"https://api.github.com/repos/couchbase/sync_gateway/issues/986/labels{/name}","comments_url":"https://api.github.com/repos/couchbase/sync_gateway/issues/986/comments","events_url":"https://api.github.com/repos/couchbase/sync_gateway/issues/986/events","html_url":"https://github.com/couchbase/sync_gateway/issues/986","id":95020873,"number":986,"title":"Sync function not granting channel access to some users","user":{"login":"mjq","id":354504,"avatar_url":"https://avatars.githubusercontent.com/u/354504?v=3","gravatar_id":"","url":"https://api.github.com/users/mjq","html_url":"https://github.com/mjq","followers_url":"https://api.github.com/users/mjq/followers","following_url":"https://api.github.com/users/mjq/following{/other_user}","gists_url":"https://api.github.com/users/mjq/gists{/gist_id}","starred_url":"https://api.github.com/users/mjq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mjq/subscriptions","organizations_url":"https://api.github.com/users/mjq/orgs","repos_url":"https://api.github.com/users/mjq/repos","events_url":"https://api.github.com/users/mjq/events{/privacy}","received_events_url":"https://api.github.com/users/mjq/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-07-14T19:35:02Z","updated_at":"2015-07-15T22:58:49Z","closed_at":null,"body":"I'm trying to track down why a particular user document doesn't have access to a channel.\r\n\r\nOur DB has Group documents that have a userIds field. Every ID in userIds gets access to the group channel (`g_a43...`) and each pair of userIds gets access to a DM channel between them (`dm_93e..._2dc...`).\r\n\r\nHere's the sync function:\r\n```javascript\r\nfunction(doc, oldDoc) {\r\n  function dmChannelName(id1, id2) {\r\n    var ids = [id1, id2];\r\n    ids.sort();\r\n    return 'dm_' + ids[0] + '_' + ids[1];\r\n  }\r\n\r\n  if (doc.type === 'Group') {\r\n    requireUser('admin');\r\n    var groupChannel = 'g_' + doc._id;\r\n    access(doc.userIds, groupChannel);\r\n    for (var i = 0; i < doc.userIds.length - 1; ++i) {\r\n      for (var j = i + 1; j < doc.userIds.length; ++j) {\r\n        var id1 = doc.userIds[i], id2 = doc.userIds[j];\r\n        access([id1, id2], dmChannelName(id1, id2));\r\n      }\r\n    }\r\n    channel(groupChannel);\r\n  } else {\r\n    throw('unknown type: ' + doc.type);\r\n  }\r\n}\r\n```\r\n\r\nHere's the group document in question, from Couchbase. It has two users, and it looks like it's assigning the channels correctly.\r\n```javascript\r\n{\r\n  \"_sync\": {\r\n    \"rev\": \"4-7897bc456754fe437c67abcfa36a6234\",\r\n    \"sequence\": 48291,\r\n    \"recent_sequences\": [\r\n      41322,\r\n      45506,\r\n      47792,\r\n      48291\r\n    ],\r\n    \"history\": {\r\n      \"revs\": [\r\n        \"1-b3bf537ab0e0ed510ef897cdd05ef504\",\r\n        \"2-19365f26a21fa050089e3b62fd646391\",\r\n        \"3-fab773c4b54410c754c3167514ef2cdd\",\r\n        \"4-7897bc456754fe437c67abcfa36a6234\"\r\n      ],\r\n      \"parents\": [\r\n        -1,\r\n        0,\r\n        1,\r\n        2\r\n      ],\r\n      \"channels\": [\r\n        [\r\n          \"g_2bb88abb-a07b-4cca-add4-670b5ca2ae58\"\r\n        ],\r\n        [\r\n          \"g_2bb88abb-a07b-4cca-add4-670b5ca2ae58\"\r\n        ],\r\n        [\r\n          \"g_2bb88abb-a07b-4cca-add4-670b5ca2ae58\"\r\n        ],\r\n        [\r\n          \"g_2bb88abb-a07b-4cca-add4-670b5ca2ae58\"\r\n        ]\r\n      ]\r\n    },\r\n    \"channels\": {\r\n      \"g_2bb88abb-a07b-4cca-add4-670b5ca2ae58\": null\r\n    },\r\n    \"access\": {\r\n      \"b384a792-743e-4866-92d0-d491bea5010a\": {\r\n        \"dm_b384a792-743e-4866-92d0-d491bea5010a_eb40057f-6c48-433a-9abc-9a5334742b10\": 45506,\r\n        \"g_2bb88abb-a07b-4cca-add4-670b5ca2ae58\": 45506\r\n      },\r\n      \"eb40057f-6c48-433a-9abc-9a5334742b10\": {\r\n        \"dm_b384a792-743e-4866-92d0-d491bea5010a_eb40057f-6c48-433a-9abc-9a5334742b10\": 45506,\r\n        \"g_2bb88abb-a07b-4cca-add4-670b5ca2ae58\": 41322\r\n      }\r\n    },\r\n    \"time_saved\": \"2015-07-14T18:57:20.359147684Z\"\r\n  },\r\n  \"brand\": \"Workbase\",\r\n  \"createdAt\": \"2015-07-03T01:08:07Z\",\r\n  \"createdBy\": \"eb40057f-6c48-433a-9abc-9a5334742b10\",\r\n  \"name\": \"Test\",\r\n  \"type\": \"Group\",\r\n  \"updatedAt\": \"2015-07-09T23:14:28Z\",\r\n  \"updatedBy\": \"b384a792-743e-4866-92d0-d491bea5010a\",\r\n  \"userIds\": [\r\n    \"eb40057f-6c48-433a-9abc-9a5334742b10\",\r\n    \"b384a792-743e-4866-92d0-d491bea5010a\"\r\n  ]\r\n}\r\n```\r\nHere's the first user, as they appear in Couchbase and then via Sync Gateway's Admin API. Looks good.\r\n```javascript\r\n{\r\n  \"name\": \"eb40057f-6c48-433a-9abc-9a5334742b10\",\r\n  \"all_channels\": {\r\n    \"!\": 1,\r\n    \"dm_b384a792-743e-4866-92d0-d491bea5010a_eb40057f-6c48-433a-9abc-9a5334742b10\": 45506,\r\n    \"g_2bb88abb-a07b-4cca-add4-670b5ca2ae58\": 41322\r\n  },\r\n  \"sequence\": 41321,\r\n  \"email\": \"works@example.com\",\r\n  \"passwordhash_bcrypt\": \"...\",\r\n  \"rolesSince\": {}\r\n}\r\n\r\n{\r\n  \"name\": \"eb40057f-6c48-433a-9abc-9a5334742b10\",\r\n  \"all_channels\": [\"!\", \"dm_b384a792-743e-4866-92d0-d491bea5010a_eb40057f-6c48-433a-9abc-9a5334742b10\", \"g_2bb88abb-a07b-4cca-add4-670b5ca2ae58\"],\r\n  \"email\": \"works@example.com\"\r\n}\r\n```\r\n\r\nHere's the second user in the group. Mysteriously, this user doesn't have access to either channel it should.\r\n```javascript\r\n{\r\n  \"name\": \"b384a792-743e-4866-92d0-d491bea5010a\",\r\n  \"all_channels\": {\r\n    \"!\": 1\r\n  },\r\n  \"sequence\": 45507,\r\n  \"email\": \"does_not_work@example.com\",\r\n  \"passwordhash_bcrypt\": \"...\",\r\n  \"rolesSince\": {}\r\n}\r\n\r\n{\r\n  \"name\": \"b384a792-743e-4866-92d0-d491bea5010a\",\r\n  \"all_channels\": [\"!\"],\r\n  \"email\": \"does_not_work@example.com\"\r\n}\r\n```\r\n\r\nDoes anyone have any ideas why the second user might not have channel access, or what steps I can take to try to track down the problem? I tried updating the Group document (didn't change any fields, just pushed a new, identical revision) and also tried restarting Sync Gateway. There were no errors in the logs when I updated the group doc.\r\n\r\nThanks very much for your help!"},"comment":{"url":"https://api.github.com/repos/couchbase/sync_gateway/issues/comments/121771892","html_url":"https://github.com/couchbase/sync_gateway/issues/986#issuecomment-121771892","issue_url":"https://api.github.com/repos/couchbase/sync_gateway/issues/986","id":121771892,"user":{"login":"mjq","id":354504,"avatar_url":"https://avatars.githubusercontent.com/u/354504?v=3","gravatar_id":"","url":"https://api.github.com/users/mjq","html_url":"https://github.com/mjq","followers_url":"https://api.github.com/users/mjq/followers","following_url":"https://api.github.com/users/mjq/following{/other_user}","gists_url":"https://api.github.com/users/mjq/gists{/gist_id}","starred_url":"https://api.github.com/users/mjq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mjq/subscriptions","organizations_url":"https://api.github.com/users/mjq/orgs","repos_url":"https://api.github.com/users/mjq/repos","events_url":"https://api.github.com/users/mjq/events{/privacy}","received_events_url":"https://api.github.com/users/mjq/received_events","type":"User","site_admin":false},"created_at":"2015-07-15T22:58:49Z","updated_at":"2015-07-15T22:58:49Z","body":"Thanks for the lead. Resaving the user document using the Admin API fixed the issue -- that user now has access to all the expected channels.\r\n\r\nIs there any way that the Save() error you mentioned could be handled better by Sync Gateway?"}}