Add admin menu skeleton
Previously, people using Polygerrit did not realize how to access admin functionality in GWT. This build out a skeleton menu, and displays the appropriate menu items based on role. The page itself will still show the default admin view placeholder. Bug: Issue 5470 Change-Id: I1266dd8fb70d0afac485f1e547fae85acee7e5aa
This commit is contained in:
		@@ -61,8 +61,9 @@ limitations under the License.
 | 
			
		||||
        justify-content: flex-end;
 | 
			
		||||
      }
 | 
			
		||||
      gr-search-bar {
 | 
			
		||||
        flex-grow: 1;
 | 
			
		||||
        margin-left: .5em;
 | 
			
		||||
        width: 500px;
 | 
			
		||||
        max-width: 500px;
 | 
			
		||||
      }
 | 
			
		||||
      .accountContainer:not(.loggedIn):not(.loggedOut) .loginButton,
 | 
			
		||||
      .accountContainer:not(.loggedIn):not(.loggedOut) gr-account-dropdown,
 | 
			
		||||
 
 | 
			
		||||
@@ -14,6 +14,32 @@
 | 
			
		||||
(function() {
 | 
			
		||||
  'use strict';
 | 
			
		||||
 | 
			
		||||
  var ADMIN_LINKS = [
 | 
			
		||||
    {
 | 
			
		||||
      url: '/admin/groups',
 | 
			
		||||
      name: 'Groups',
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      url: '/admin/create-group',
 | 
			
		||||
      name: 'Create Group',
 | 
			
		||||
      capability: 'createGroup'
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      url: '/admin/projects',
 | 
			
		||||
      name: 'Projects',
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      url: '/admin/create-project',
 | 
			
		||||
      name: 'Create Project',
 | 
			
		||||
      capability: 'createProject',
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      url: '/admin/plugins',
 | 
			
		||||
      name: 'Plugins',
 | 
			
		||||
      capability: 'viewPlugins',
 | 
			
		||||
    },
 | 
			
		||||
  ];
 | 
			
		||||
 | 
			
		||||
  var DEFAULT_LINKS = [{
 | 
			
		||||
    title: 'Changes',
 | 
			
		||||
    links: [
 | 
			
		||||
@@ -46,6 +72,10 @@
 | 
			
		||||
      },
 | 
			
		||||
 | 
			
		||||
      _account: Object,
 | 
			
		||||
      _adminLinks: {
 | 
			
		||||
        type: Array,
 | 
			
		||||
        value: function() { return []; },
 | 
			
		||||
      },
 | 
			
		||||
      _defaultLinks: {
 | 
			
		||||
        type: Array,
 | 
			
		||||
        value: function() {
 | 
			
		||||
@@ -54,7 +84,7 @@
 | 
			
		||||
      },
 | 
			
		||||
      _links: {
 | 
			
		||||
        type: Array,
 | 
			
		||||
        computed: '_computeLinks(_defaultLinks, _userLinks)',
 | 
			
		||||
        computed: '_computeLinks(_defaultLinks, _userLinks, _adminLinks)',
 | 
			
		||||
      },
 | 
			
		||||
      _loginURL: {
 | 
			
		||||
        type: String,
 | 
			
		||||
@@ -94,7 +124,7 @@
 | 
			
		||||
      return '//' + window.location.host + path;
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    _computeLinks: function(defaultLinks, userLinks) {
 | 
			
		||||
    _computeLinks: function(defaultLinks, userLinks, adminLinks) {
 | 
			
		||||
      var links = defaultLinks.slice();
 | 
			
		||||
      if (userLinks && userLinks.length > 0) {
 | 
			
		||||
        links.push({
 | 
			
		||||
@@ -102,6 +132,12 @@
 | 
			
		||||
          links: userLinks,
 | 
			
		||||
        });
 | 
			
		||||
      }
 | 
			
		||||
      if (adminLinks && adminLinks.length > 0) {
 | 
			
		||||
        links.push({
 | 
			
		||||
          title: 'Admin',
 | 
			
		||||
          links: adminLinks,
 | 
			
		||||
        });
 | 
			
		||||
      }
 | 
			
		||||
      return links;
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
@@ -120,6 +156,18 @@
 | 
			
		||||
        this._userLinks =
 | 
			
		||||
            prefs.my.map(this._stripHashPrefix).filter(this._isSupportedLink);
 | 
			
		||||
      }.bind(this));
 | 
			
		||||
      this._loadAccountCapabilities();
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    _loadAccountCapabilities: function() {
 | 
			
		||||
      var params = ['createProject', 'createGroup', 'viewPlugins'];
 | 
			
		||||
      return this.$.restAPI.getAccountCapabilities(params)
 | 
			
		||||
          .then(function(capabilities) {
 | 
			
		||||
        this._adminLinks = ADMIN_LINKS.filter(function(link) {
 | 
			
		||||
          return !link.capability ||
 | 
			
		||||
              capabilities.hasOwnProperty(link.capability);
 | 
			
		||||
        });
 | 
			
		||||
      }.bind(this));
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    _stripHashPrefix: function(linkObj) {
 | 
			
		||||
 
 | 
			
		||||
@@ -33,8 +33,10 @@ limitations under the License.
 | 
			
		||||
<script>
 | 
			
		||||
  suite('gr-main-header tests', function() {
 | 
			
		||||
    var element;
 | 
			
		||||
    var sandbox;
 | 
			
		||||
 | 
			
		||||
    setup(function() {
 | 
			
		||||
      sandbox = sinon.sandbox.create();
 | 
			
		||||
      stub('gr-rest-api-interface', {
 | 
			
		||||
        getConfig: function() { return Promise.resolve({}); },
 | 
			
		||||
      });
 | 
			
		||||
@@ -44,6 +46,10 @@ limitations under the License.
 | 
			
		||||
      element = fixture('basic');
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    teardown(function() {
 | 
			
		||||
      sandbox.restore();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('strip hash prefix', function() {
 | 
			
		||||
      assert.deepEqual([
 | 
			
		||||
        {url: '#/q/owner:self+is:draft'},
 | 
			
		||||
@@ -69,6 +75,30 @@ limitations under the License.
 | 
			
		||||
      ]);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('_loadAccountCapabilities admin', function(done) {
 | 
			
		||||
      sandbox.stub(element.$.restAPI, 'getAccountCapabilities', function() {
 | 
			
		||||
        return Promise.resolve({
 | 
			
		||||
          createGroup: true,
 | 
			
		||||
          createProject: true,
 | 
			
		||||
          viewPlugins: true,
 | 
			
		||||
        });
 | 
			
		||||
      });
 | 
			
		||||
      element._loadAccountCapabilities().then(function() {
 | 
			
		||||
        assert.equal(element._adminLinks.length, 5);
 | 
			
		||||
        done();
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('_loadAccountCapabilities non admin', function(done) {
 | 
			
		||||
      sandbox.stub(element.$.restAPI, 'getAccountCapabilities', function() {
 | 
			
		||||
        return Promise.resolve({});
 | 
			
		||||
      });
 | 
			
		||||
      element._loadAccountCapabilities().then(function() {
 | 
			
		||||
        assert.equal(element._adminLinks.length, 2);
 | 
			
		||||
        done();
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('user links', function() {
 | 
			
		||||
      var defaultLinks = [{
 | 
			
		||||
        title: 'Faves',
 | 
			
		||||
@@ -81,11 +111,21 @@ limitations under the License.
 | 
			
		||||
        name: 'Facebook',
 | 
			
		||||
        url: 'https://facebook.com',
 | 
			
		||||
      }];
 | 
			
		||||
      assert.deepEqual(element._computeLinks(defaultLinks, []), defaultLinks);
 | 
			
		||||
      assert.deepEqual(element._computeLinks(defaultLinks, userLinks),
 | 
			
		||||
      var adminLinks = [{
 | 
			
		||||
        url: '/admin/groups',
 | 
			
		||||
        name: 'Groups',
 | 
			
		||||
      }];
 | 
			
		||||
 | 
			
		||||
      assert.deepEqual(
 | 
			
		||||
          element._computeLinks(defaultLinks, [], []), defaultLinks);
 | 
			
		||||
      assert.deepEqual(
 | 
			
		||||
          element._computeLinks(defaultLinks, userLinks, adminLinks),
 | 
			
		||||
          defaultLinks.concat({
 | 
			
		||||
            title: 'Your',
 | 
			
		||||
            links: userLinks,
 | 
			
		||||
          }, {
 | 
			
		||||
            title: 'Admin',
 | 
			
		||||
            links: adminLinks,
 | 
			
		||||
          }));
 | 
			
		||||
    });
 | 
			
		||||
  });
 | 
			
		||||
 
 | 
			
		||||
@@ -317,6 +317,17 @@
 | 
			
		||||
      return this._fetchSharedCacheURL('/accounts/self/groups');
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    getAccountCapabilities: function(opt_params) {
 | 
			
		||||
      var queryString = '';
 | 
			
		||||
      if (opt_params) {
 | 
			
		||||
        queryString = '?q=' + opt_params
 | 
			
		||||
            .map(function(param) { return encodeURIComponent(param); })
 | 
			
		||||
            .join('&q=');
 | 
			
		||||
      }
 | 
			
		||||
      return this._fetchSharedCacheURL('/accounts/self/capabilities' +
 | 
			
		||||
          queryString);
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    getLoggedIn: function() {
 | 
			
		||||
      return this.getAccount().then(function(account) {
 | 
			
		||||
        return account != null;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user