Merge "Add admin menu skeleton"
diff --git a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.html b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.html
index 74dc9e7..dc66bb6 100644
--- a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.html
+++ b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.html
@@ -61,8 +61,9 @@
         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,
diff --git a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.js b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.js
index ebeb9af..c9abac2 100644
--- a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.js
+++ b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.js
@@ -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) {
diff --git a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header_test.html b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header_test.html
index aef338b..75723b9 100644
--- a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header_test.html
+++ b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header_test.html
@@ -33,8 +33,10 @@
 <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 @@
       element = fixture('basic');
     });
 
+    teardown(function() {
+      sandbox.restore();
+    });
+
     test('strip hash prefix', function() {
       assert.deepEqual([
         {url: '#/q/owner:self+is:draft'},
@@ -69,6 +75,30 @@
       ]);
     });
 
+    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 @@
         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,
           }));
     });
   });
diff --git a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.js b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.js
index 5c2ad5c..b9b1f53 100644
--- a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.js
+++ b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.js
@@ -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;