{"action":"created","comment":{"url":"https://api.github.com/repos/cozy/cozy-data-system/pulls/comments/59136267","id":59136267,"diff_hunk":"@@ -81,10 +89,73 @@ module.exports.checkDocTypeSync = (auth, docType) ->\n         else\n             return [null, false, false]\n     else\n-        [err, isAuthenticated, name] = checkToken auth\n+        [err, isAuthenticated, name] = checkToken auth, tokens\n         name ?= 'unknown'\n         return [null, name, true]\n \n+\n+## function checkSharingRule (auth, doc, callback)\n+## @auth {string} Field 'authorization' of request\n+## @doc {Object} Contains the document to evaluate\n+## @callback {function} Continuation to pass control back to when complete.\n+## Check if the sharing can manage the document\n+module.exports.checkSharingRule = (auth, doc, callback) ->\n+    if productionOrTest\n+        # Check if requester is authenticated\n+        [err, isAuthenticated, name] = checkToken auth, sharingTokens\n+        if isAuthenticated\n+            if doc?.id? && doc?.docType?\n+                doc.docType = doc.docType.toLowerCase()\n+                # Check all the sharing rules defined for this login\n+                for rule in sharingPermissions[name]\n+                    if rule.id is doc.id && rule.docType is doc.docType\n+                        # Request is granted\n+                        return callback null, name, true\n+                # Request is denied\n+                callback null, name, false\n+            else\n+                # Particular case for couchDB requests without id or docType\n+                callback null, name, true\n+        else\n+            # Requester is not authenticated: no way in!\n+            callback null, false, false\n+    else\n+        [err, isAuthenticated, name] = checkToken auth, sharingTokens\n+        name ?= 'unknown sharing'\n+        callback null, name, true\n+\n+\n+\n+## function checkSharingRuleSync (auth, doc, callback)\n+## @auth {string} Field 'authorization' of request\n+## @doc {Object} Contains the document to evaluate\n+## Check if the sharing can manage the document\n+module.exports.checkSharingRuleSync = (auth, doc) ->\n+    if productionOrTest\n+        # Check if requester is authenticated\n+        [err, isAuthenticated, name] = checkToken auth, sharingTokens\n+        if isAuthenticated\n+            if doc?.id? && doc?.docType?\n+                doc.docType = doc.docType.toLowerCase()\n+                # Check all the sharing rules defined for this login\n+                for rule in sharingPermissions[name]","path":"server/lib/token.coffee","position":104,"original_position":135,"commit_id":"08179aa1fe05bf61f33bd6c725d12620ad0c1290","original_commit_id":"08179aa1fe05bf61f33bd6c725d12620ad0c1290","user":{"login":"frankrousseau","id":1336623,"avatar_url":"https://avatars.githubusercontent.com/u/1336623?v=3","gravatar_id":"","url":"https://api.github.com/users/frankrousseau","html_url":"https://github.com/frankrousseau","followers_url":"https://api.github.com/users/frankrousseau/followers","following_url":"https://api.github.com/users/frankrousseau/following{/other_user}","gists_url":"https://api.github.com/users/frankrousseau/gists{/gist_id}","starred_url":"https://api.github.com/users/frankrousseau/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/frankrousseau/subscriptions","organizations_url":"https://api.github.com/users/frankrousseau/orgs","repos_url":"https://api.github.com/users/frankrousseau/repos","events_url":"https://api.github.com/users/frankrousseau/events{/privacy}","received_events_url":"https://api.github.com/users/frankrousseau/received_events","type":"User","site_admin":false},"body":"same here, at first sight, findWhere looks to do a better job.","created_at":"2016-04-10T15:59:15Z","updated_at":"2016-04-10T15:59:15Z","html_url":"https://github.com/cozy/cozy-data-system/pull/214#discussion_r59136267","pull_request_url":"https://api.github.com/repos/cozy/cozy-data-system/pulls/214","_links":{"self":{"href":"https://api.github.com/repos/cozy/cozy-data-system/pulls/comments/59136267"},"html":{"href":"https://github.com/cozy/cozy-data-system/pull/214#discussion_r59136267"},"pull_request":{"href":"https://api.github.com/repos/cozy/cozy-data-system/pulls/214"}}},"pull_request":{"url":"https://api.github.com/repos/cozy/cozy-data-system/pulls/214","id":62723615,"html_url":"https://github.com/cozy/cozy-data-system/pull/214","diff_url":"https://github.com/cozy/cozy-data-system/pull/214.diff","patch_url":"https://github.com/cozy/cozy-data-system/pull/214.patch","issue_url":"https://api.github.com/repos/cozy/cozy-data-system/issues/214","number":214,"state":"open","locked":false,"title":"Add sharing support","user":{"login":"Gara64","id":7010087,"avatar_url":"https://avatars.githubusercontent.com/u/7010087?v=3","gravatar_id":"","url":"https://api.github.com/users/Gara64","html_url":"https://github.com/Gara64","followers_url":"https://api.github.com/users/Gara64/followers","following_url":"https://api.github.com/users/Gara64/following{/other_user}","gists_url":"https://api.github.com/users/Gara64/gists{/gist_id}","starred_url":"https://api.github.com/users/Gara64/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Gara64/subscriptions","organizations_url":"https://api.github.com/users/Gara64/orgs","repos_url":"https://api.github.com/users/Gara64/repos","events_url":"https://api.github.com/users/Gara64/events{/privacy}","received_events_url":"https://api.github.com/users/Gara64/received_events","type":"User","site_admin":false},"body":"Implements sharing capabilities for the data-system, as described [here](https://github.com/Ljinod/cozy-docs/blob/master/src/documents/en/hack/cookbooks/sharing.html.md).\r\n\r\nThe logic is split in 2 files : \r\n\r\n- controllers/sharing -> the sharing process, for the creation, deletion, answering, etc.\r\n- lib/sharing -> sharing methods to replicate data, send notification, etc.\r\n\r\nNote this code can only work if the proxy and home are able to receive and interpret sharing requests. \r\nBut this will come later :)","created_at":"2016-03-14T10:06:32Z","updated_at":"2016-04-10T15:59:16Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b95d62ce6d494aaec31634b6b84dd5e598e75e2d","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/cozy/cozy-data-system/pulls/214/commits","review_comments_url":"https://api.github.com/repos/cozy/cozy-data-system/pulls/214/comments","review_comment_url":"https://api.github.com/repos/cozy/cozy-data-system/pulls/comments{/number}","comments_url":"https://api.github.com/repos/cozy/cozy-data-system/issues/214/comments","statuses_url":"https://api.github.com/repos/cozy/cozy-data-system/statuses/08179aa1fe05bf61f33bd6c725d12620ad0c1290","head":{"label":"Gara64:sharing_experimentations","ref":"sharing_experimentations","sha":"08179aa1fe05bf61f33bd6c725d12620ad0c1290","user":{"login":"Gara64","id":7010087,"avatar_url":"https://avatars.githubusercontent.com/u/7010087?v=3","gravatar_id":"","url":"https://api.github.com/users/Gara64","html_url":"https://github.com/Gara64","followers_url":"https://api.github.com/users/Gara64/followers","following_url":"https://api.github.com/users/Gara64/following{/other_user}","gists_url":"https://api.github.com/users/Gara64/gists{/gist_id}","starred_url":"https://api.github.com/users/Gara64/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Gara64/subscriptions","organizations_url":"https://api.github.com/users/Gara64/orgs","repos_url":"https://api.github.com/users/Gara64/repos","events_url":"https://api.github.com/users/Gara64/events{/privacy}","received_events_url":"https://api.github.com/users/Gara64/received_events","type":"User","site_admin":false},"repo":{"id":39389397,"name":"cozy-data-system","full_name":"Gara64/cozy-data-system","owner":{"login":"Gara64","id":7010087,"avatar_url":"https://avatars.githubusercontent.com/u/7010087?v=3","gravatar_id":"","url":"https://api.github.com/users/Gara64","html_url":"https://github.com/Gara64","followers_url":"https://api.github.com/users/Gara64/followers","following_url":"https://api.github.com/users/Gara64/following{/other_user}","gists_url":"https://api.github.com/users/Gara64/gists{/gist_id}","starred_url":"https://api.github.com/users/Gara64/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Gara64/subscriptions","organizations_url":"https://api.github.com/users/Gara64/orgs","repos_url":"https://api.github.com/users/Gara64/repos","events_url":"https://api.github.com/users/Gara64/events{/privacy}","received_events_url":"https://api.github.com/users/Gara64/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Gara64/cozy-data-system","description":"Data Layer for Cozy Platform","fork":true,"url":"https://api.github.com/repos/Gara64/cozy-data-system","forks_url":"https://api.github.com/repos/Gara64/cozy-data-system/forks","keys_url":"https://api.github.com/repos/Gara64/cozy-data-system/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Gara64/cozy-data-system/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Gara64/cozy-data-system/teams","hooks_url":"https://api.github.com/repos/Gara64/cozy-data-system/hooks","issue_events_url":"https://api.github.com/repos/Gara64/cozy-data-system/issues/events{/number}","events_url":"https://api.github.com/repos/Gara64/cozy-data-system/events","assignees_url":"https://api.github.com/repos/Gara64/cozy-data-system/assignees{/user}","branches_url":"https://api.github.com/repos/Gara64/cozy-data-system/branches{/branch}","tags_url":"https://api.github.com/repos/Gara64/cozy-data-system/tags","blobs_url":"https://api.github.com/repos/Gara64/cozy-data-system/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Gara64/cozy-data-system/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Gara64/cozy-data-system/git/refs{/sha}","trees_url":"https://api.github.com/repos/Gara64/cozy-data-system/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Gara64/cozy-data-system/statuses/{sha}","languages_url":"https://api.github.com/repos/Gara64/cozy-data-system/languages","stargazers_url":"https://api.github.com/repos/Gara64/cozy-data-system/stargazers","contributors_url":"https://api.github.com/repos/Gara64/cozy-data-system/contributors","subscribers_url":"https://api.github.com/repos/Gara64/cozy-data-system/subscribers","subscription_url":"https://api.github.com/repos/Gara64/cozy-data-system/subscription","commits_url":"https://api.github.com/repos/Gara64/cozy-data-system/commits{/sha}","git_commits_url":"https://api.github.com/repos/Gara64/cozy-data-system/git/commits{/sha}","comments_url":"https://api.github.com/repos/Gara64/cozy-data-system/comments{/number}","issue_comment_url":"https://api.github.com/repos/Gara64/cozy-data-system/issues/comments{/number}","contents_url":"https://api.github.com/repos/Gara64/cozy-data-system/contents/{+path}","compare_url":"https://api.github.com/repos/Gara64/cozy-data-system/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Gara64/cozy-data-system/merges","archive_url":"https://api.github.com/repos/Gara64/cozy-data-system/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Gara64/cozy-data-system/downloads","issues_url":"https://api.github.com/repos/Gara64/cozy-data-system/issues{/number}","pulls_url":"https://api.github.com/repos/Gara64/cozy-data-system/pulls{/number}","milestones_url":"https://api.github.com/repos/Gara64/cozy-data-system/milestones{/number}","notifications_url":"https://api.github.com/repos/Gara64/cozy-data-system/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Gara64/cozy-data-system/labels{/name}","releases_url":"https://api.github.com/repos/Gara64/cozy-data-system/releases{/id}","deployments_url":"https://api.github.com/repos/Gara64/cozy-data-system/deployments","created_at":"2015-07-20T14:42:29Z","updated_at":"2016-03-14T09:00:25Z","pushed_at":"2016-04-08T12:04:56Z","git_url":"git://github.com/Gara64/cozy-data-system.git","ssh_url":"git@github.com:Gara64/cozy-data-system.git","clone_url":"https://github.com/Gara64/cozy-data-system.git","svn_url":"https://github.com/Gara64/cozy-data-system","homepage":"cozy.io","size":45038,"stargazers_count":0,"watchers_count":0,"language":"CoffeeScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"cozy:sharing","ref":"sharing","sha":"aa072a895008eee49d71d77c507a155ab2a9aa43","user":{"login":"cozy","id":7690525,"avatar_url":"https://avatars.githubusercontent.com/u/7690525?v=3","gravatar_id":"","url":"https://api.github.com/users/cozy","html_url":"https://github.com/cozy","followers_url":"https://api.github.com/users/cozy/followers","following_url":"https://api.github.com/users/cozy/following{/other_user}","gists_url":"https://api.github.com/users/cozy/gists{/gist_id}","starred_url":"https://api.github.com/users/cozy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cozy/subscriptions","organizations_url":"https://api.github.com/users/cozy/orgs","repos_url":"https://api.github.com/users/cozy/repos","events_url":"https://api.github.com/users/cozy/events{/privacy}","received_events_url":"https://api.github.com/users/cozy/received_events","type":"Organization","site_admin":false},"repo":{"id":5093292,"name":"cozy-data-system","full_name":"cozy/cozy-data-system","owner":{"login":"cozy","id":7690525,"avatar_url":"https://avatars.githubusercontent.com/u/7690525?v=3","gravatar_id":"","url":"https://api.github.com/users/cozy","html_url":"https://github.com/cozy","followers_url":"https://api.github.com/users/cozy/followers","following_url":"https://api.github.com/users/cozy/following{/other_user}","gists_url":"https://api.github.com/users/cozy/gists{/gist_id}","starred_url":"https://api.github.com/users/cozy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cozy/subscriptions","organizations_url":"https://api.github.com/users/cozy/orgs","repos_url":"https://api.github.com/users/cozy/repos","events_url":"https://api.github.com/users/cozy/events{/privacy}","received_events_url":"https://api.github.com/users/cozy/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/cozy/cozy-data-system","description":"Data Layer for Cozy Platform","fork":false,"url":"https://api.github.com/repos/cozy/cozy-data-system","forks_url":"https://api.github.com/repos/cozy/cozy-data-system/forks","keys_url":"https://api.github.com/repos/cozy/cozy-data-system/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cozy/cozy-data-system/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cozy/cozy-data-system/teams","hooks_url":"https://api.github.com/repos/cozy/cozy-data-system/hooks","issue_events_url":"https://api.github.com/repos/cozy/cozy-data-system/issues/events{/number}","events_url":"https://api.github.com/repos/cozy/cozy-data-system/events","assignees_url":"https://api.github.com/repos/cozy/cozy-data-system/assignees{/user}","branches_url":"https://api.github.com/repos/cozy/cozy-data-system/branches{/branch}","tags_url":"https://api.github.com/repos/cozy/cozy-data-system/tags","blobs_url":"https://api.github.com/repos/cozy/cozy-data-system/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cozy/cozy-data-system/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cozy/cozy-data-system/git/refs{/sha}","trees_url":"https://api.github.com/repos/cozy/cozy-data-system/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cozy/cozy-data-system/statuses/{sha}","languages_url":"https://api.github.com/repos/cozy/cozy-data-system/languages","stargazers_url":"https://api.github.com/repos/cozy/cozy-data-system/stargazers","contributors_url":"https://api.github.com/repos/cozy/cozy-data-system/contributors","subscribers_url":"https://api.github.com/repos/cozy/cozy-data-system/subscribers","subscription_url":"https://api.github.com/repos/cozy/cozy-data-system/subscription","commits_url":"https://api.github.com/repos/cozy/cozy-data-system/commits{/sha}","git_commits_url":"https://api.github.com/repos/cozy/cozy-data-system/git/commits{/sha}","comments_url":"https://api.github.com/repos/cozy/cozy-data-system/comments{/number}","issue_comment_url":"https://api.github.com/repos/cozy/cozy-data-system/issues/comments{/number}","contents_url":"https://api.github.com/repos/cozy/cozy-data-system/contents/{+path}","compare_url":"https://api.github.com/repos/cozy/cozy-data-system/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cozy/cozy-data-system/merges","archive_url":"https://api.github.com/repos/cozy/cozy-data-system/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cozy/cozy-data-system/downloads","issues_url":"https://api.github.com/repos/cozy/cozy-data-system/issues{/number}","pulls_url":"https://api.github.com/repos/cozy/cozy-data-system/pulls{/number}","milestones_url":"https://api.github.com/repos/cozy/cozy-data-system/milestones{/number}","notifications_url":"https://api.github.com/repos/cozy/cozy-data-system/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cozy/cozy-data-system/labels{/name}","releases_url":"https://api.github.com/repos/cozy/cozy-data-system/releases{/id}","deployments_url":"https://api.github.com/repos/cozy/cozy-data-system/deployments","created_at":"2012-07-18T07:58:49Z","updated_at":"2016-03-15T12:53:14Z","pushed_at":"2016-04-06T16:13:58Z","git_url":"git://github.com/cozy/cozy-data-system.git","ssh_url":"git@github.com:cozy/cozy-data-system.git","clone_url":"https://github.com/cozy/cozy-data-system.git","svn_url":"https://github.com/cozy/cozy-data-system","homepage":"cozy.io","size":45144,"stargazers_count":25,"watchers_count":25,"language":"CoffeeScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":19,"mirror_url":null,"open_issues_count":25,"forks":19,"open_issues":25,"watchers":25,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/cozy/cozy-data-system/pulls/214"},"html":{"href":"https://github.com/cozy/cozy-data-system/pull/214"},"issue":{"href":"https://api.github.com/repos/cozy/cozy-data-system/issues/214"},"comments":{"href":"https://api.github.com/repos/cozy/cozy-data-system/issues/214/comments"},"review_comments":{"href":"https://api.github.com/repos/cozy/cozy-data-system/pulls/214/comments"},"review_comment":{"href":"https://api.github.com/repos/cozy/cozy-data-system/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/cozy/cozy-data-system/pulls/214/commits"},"statuses":{"href":"https://api.github.com/repos/cozy/cozy-data-system/statuses/08179aa1fe05bf61f33bd6c725d12620ad0c1290"}}}}