1 module.exports = access
3 var assert = require('assert')
4 var url = require('url')
5 var npa = require('npm-package-arg')
8 function access (sub, uri, params, cb) {
9 accessAssertions(sub, uri, params, cb)
10 return subcommands[sub].call(this, uri, params, cb)
13 subcommands.public = function (uri, params, cb) {
14 return setAccess.call(this, 'public', uri, params, cb)
16 subcommands.restricted = function (uri, params, cb) {
17 return setAccess.call(this, 'restricted', uri, params, cb)
20 function setAccess (access, uri, params, cb) {
21 return this.request(apiUri(uri, 'package', params.package, 'access'), {
24 body: JSON.stringify({ access: access })
28 subcommands.grant = function (uri, params, cb) {
29 var reqUri = apiUri(uri, 'team', params.scope, params.team, 'package')
30 return this.request(reqUri, {
33 body: JSON.stringify({
34 permissions: params.permissions,
35 package: params.package
40 subcommands.revoke = function (uri, params, cb) {
41 var reqUri = apiUri(uri, 'team', params.scope, params.team, 'package')
42 return this.request(reqUri, {
45 body: JSON.stringify({
46 package: params.package
51 subcommands['ls-packages'] = function (uri, params, cb, type) {
52 type = type || (params.team ? 'team' : 'org')
54 var uriParams = '?format=cli'
55 var reqUri = apiUri(uri, type, params.scope, params.team, 'package')
56 return client.request(reqUri + uriParams, {
59 }, function (err, perms) {
60 if (err && err.statusCode === 404 && type === 'org') {
61 subcommands['ls-packages'].call(client, uri, params, cb, 'user')
63 cb(err, perms && translatePermissions(perms))
68 subcommands['ls-collaborators'] = function (uri, params, cb) {
69 var uriParams = '?format=cli'
71 uriParams += ('&user=' + encodeURIComponent(params.user))
73 var reqUri = apiUri(uri, 'package', params.package, 'collaborators')
74 return this.request(reqUri + uriParams, {
77 }, function (err, perms) {
78 cb(err, perms && translatePermissions(perms))
82 subcommands.edit = function () {
83 throw new Error('edit subcommand is not implemented yet')
86 function apiUri (registryUri) {
87 var path = Array.prototype.slice.call(arguments, 1)
88 .filter(function (x) { return x })
89 .map(encodeURIComponent)
91 return url.resolve(registryUri, '-/' + path)
94 function accessAssertions (subcommand, uri, params, cb) {
95 assert(subcommands.hasOwnProperty(subcommand),
96 'access subcommand must be one of ' +
97 Object.keys(subcommands).join(', '))
99 'uri': [uri, 'string'],
100 'params': [params, 'object'],
101 'auth': [params.auth, 'object'],
102 'callback': [cb, 'function']
105 'public', 'restricted'
107 typeChecks({ 'package': [params.package, 'string'] })
108 assert(!!npa(params.package).scope,
109 'access commands are only accessible for scoped packages')
111 if (contains(['grant', 'revoke', 'ls-packages'], subcommand)) {
112 typeChecks({ 'scope': [params.scope, 'string'] })
114 if (contains(['grant', 'revoke'], subcommand)) {
115 typeChecks({ 'team': [params.team, 'string'] })
117 if (subcommand === 'grant') {
118 typeChecks({ 'permissions': [params.permissions, 'string'] })
119 assert(params.permissions === 'read-only' ||
120 params.permissions === 'read-write',
121 'permissions must be either read-only or read-write')
125 function typeChecks (specs) {
126 Object.keys(specs).forEach(function (key) {
127 var checks = specs[key]
128 assert(typeof checks[0] === checks[1],
129 key + ' is required and must be of type ' + checks[1])
133 function contains (arr, item) {
134 return arr.indexOf(item) !== -1
137 function translatePermissions (perms) {
139 for (var key in perms) {
140 if (perms.hasOwnProperty(key)) {
141 if (perms[key] === 'read') {
142 newPerms[key] = 'read-only'
143 } else if (perms[key] === 'write') {
144 newPerms[key] = 'read-write'
146 // This shouldn't happen, but let's not break things
147 // if the API starts returning different things.
148 newPerms[key] = perms[key]