Skip to content
This repository was archived by the owner on Sep 2, 2021. It is now read-only.

Add initial support for github organizations #70

Merged
merged 1 commit into from
Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ var express = require("express"),
fs = require("fs"),
http = require("http"),
https = require("https"),
url = require("url"),
passport = require("passport"),
GitHubStrategy = require("passport-github").Strategy,
repository = require("./lib/repository"),
routes = require("./lib/routes"),
downloadData = require("./lib/downloadData"),
logging = require("./lib/logging"),
_ = require("lodash"),
constants = require("constants");
constants = require("constants"),
request = require("request");

// Load cert and secret configuration
var config = JSON.parse(fs.readFileSync(path.resolve(__dirname, "config/config.json"))),
Expand Down Expand Up @@ -119,7 +121,38 @@ passport.use(
callbackURL: callbackScheme + config.hostname + ":" + callbackPort + "/auth/github/callback"
},
function (accessToken, refreshToken, profile, done) {
done(null, "github:" + profile.username);
request({
method: 'GET',
uri: url.parse(profile._json.organizations_url),
qs: {
access_token: accessToken
},
headers: {
"User-Agent": "brackets-registry"
},
json: true,
}, function (error, response, body) {
if (error) {
return done(error);
}

// body must be an array, if not it is an error.
if (!Array.isArray(body)) {
return done(body);
}

var orgs = body.map(function (item) {
return {
login: "github:" + item.login,
id: item.id
};
});
var user = {
owner: "github:" + profile.username,
orgs: orgs
};
done(null, user);
});
}
)
);
Expand Down
8 changes: 4 additions & 4 deletions lib/registry_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ exports.lastVersionDate = function () {
*/
exports.formatUserId = function () {
var friendlyName;
if (this.owner) {
var nameComponents = this.owner.split(":");
if (this.user && this.user.owner) {
var nameComponents = this.user.owner.split(":");
friendlyName = nameComponents[1];
}
return friendlyName;
Expand All @@ -86,8 +86,8 @@ exports.formatUserId = function () {
*/
exports.ownerLink = function () {
var url;
if (this.owner) {
var nameComponents = this.owner.split(":");
if (this.user && this.user.owner) {
var nameComponents = this.user.owner.split(":");
if (nameComponents[0] === "github") {
url = "https://github.com/" + nameComponents[1];
}
Expand Down
11 changes: 6 additions & 5 deletions lib/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var validate = require("brackets-extensibility/package-validator").validate,
semver = require("semver"),
clone = require("clone"),
logger = require("./logging"),
user_utils = require("./user_utils"),
_ = require("lodash");

/**
Expand Down Expand Up @@ -238,7 +239,7 @@ function addDownloadDataToPackage(name, newVersionDownloads, recentDownloads) {
* @param {String} user identifier for the person submitting the file (e.g. "github:someusername")
* @param {Function} callback (err, entry)
*/
function addPackage(packagePath, userID, callback) {
function addPackage(packagePath, user, callback) {
if (!validConfiguration(callback)) {
return;
}
Expand Down Expand Up @@ -276,7 +277,7 @@ function addPackage(packagePath, userID, callback) {
entry = clone(registry[result.metadata.name]);

// Verify that the user is authorized to add this package
if (entry.owner !== userID) {
if (!user_utils.isOwner(entry, user)) {
callback(new Error(Errors.NOT_AUTHORIZED), null);
return;
}
Expand All @@ -299,7 +300,7 @@ function addPackage(packagePath, userID, callback) {
// add
entry = {
metadata: result.metadata,
owner: userID,
owner: user.owner,
versions: [{
version: result.metadata.version,
published: new Date().toJSON()
Expand Down Expand Up @@ -371,14 +372,14 @@ function changePackageRequirements(entry, requirements) {
* @param {string} name extension name
* @param {string} userID owner or admin that is submitting the request
*/
function _packageManipulatorWrapper(func, name, userID) {
function _packageManipulatorWrapper(func, name, user) {
var callback = arguments[arguments.length - 1];
var entry = registry[name];
if (!entry) {
callback(new Error(Errors.UNKNOWN_EXTENSION));
return;
}
if (entry.owner !== userID && config.admins.indexOf(userID) === -1) {
if (config.admins.indexOf(user.owner) === -1 && !user_utils.isOwner(entry, user)) {
callback(new Error(Errors.NOT_AUTHORIZED));
return;
}
Expand Down
7 changes: 4 additions & 3 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
var passport = require("passport"),
repository = require("./repository"),
registry_utils = require("./registry_utils"),
user_utils = require("./user_utils"),
hbs = require("hbs"),
fs = require("fs"),
path = require("path"),
Expand Down Expand Up @@ -194,9 +195,9 @@ function _getRegistryList(user) {
var registryList = registry_utils.sortRegistry(repository.getRegistry());

if (user) {
var isAdmin = config.admins.indexOf(user) > -1;
var isAdmin = config.admins.indexOf(user.owner) > -1;
registryList = registryList.map(function (entry) {
if (isAdmin || entry.owner === user) {
if (isAdmin || user_utils.isOwner(entry, user)) {
var newEntry = _.clone(entry);
newEntry.canAdmin = true;
return newEntry;
Expand All @@ -216,7 +217,7 @@ function _index(req, res) {
var registryList = _getRegistryList(req.user);

_respond(req, res, "index", {
user: registry_utils.formatUserId.call({owner: req.user}),
user: registry_utils.formatUserId.call({user: req.user}),
registry: registryList,
repositoryBaseURL: config.repositoryBaseURL,
helpURL: config.helpURL
Expand Down
54 changes: 54 additions & 0 deletions lib/user_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2016 - present Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/

/*jslint vars: true, plusplus: true, nomen: true, node: true, indent: 4, maxerr: 50 */

"use strict";

var _ = require("lodash");

function isUserOwner(entry, user) {
return entry.owner === user.owner;
}

function isUserOrgOwner(entry, user) {
return _.some(user.orgs, {
login: entry.owner
});
}

function isOwner(entry, user) {
return isUserOwner(entry, user) || isUserOrgOwner(entry, user);
}

function _createFakeUser(username) {
return {
owner: username,
orgs: []
};
}

exports.isUserOwner = isUserOwner;
exports.isUserOrgOwner = isUserOrgOwner;
exports.isOwner = isOwner;
exports._createFakeUser = _createFakeUser;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"filequeue": "~0.5.0",
"bluebird": "~1.2.4",
"lodash": "~2.4.1",
"request": "~2.31.0",
"request": "^2.74.0",
"request-json": "~0.4.6",
"temporary": "~0.0.8",
"commander": "~2.1.0"
Expand Down
4 changes: 2 additions & 2 deletions public/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ $(function () {
$("body").on("click", "button.changeOwner", function (event) {
var $target = $(event.target),
name = $target.data("name");
bootbox.prompt("Enter the GitHub username of the new owner for " + name, function (newOwner) {
bootbox.prompt("Enter the GitHub username / organization of the new owner for " + name, function (newOwner) {
if (newOwner === null) {
return;
}
if (!newOwner) {
displayStatus("info", "No new owner provided. No action taken.");
return;
}

$.ajax("/package/" + name + "/changeOwner", {
type: "POST",
data: {
Expand Down
Loading