From 52fcc90b1c5a6f61dd3227f34051b93c63e476d1 Mon Sep 17 00:00:00 2001
From: Jamie Curnow <jc@jc21.com>
Date: Tue, 24 Jul 2018 17:16:11 +1000
Subject: [PATCH] Functional redirection hosts

---
 .../js/app/nginx/redirection/list/item.ejs    | 31 ++++++-----
 .../js/app/nginx/redirection/list/item.js     | 53 +++----------------
 .../js/app/nginx/redirection/list/main.ejs    | 14 ++---
 .../js/app/nginx/redirection/list/main.js     | 11 ++--
 src/frontend/js/app/nginx/redirection/main.js | 20 +++----
 src/frontend/js/app/user/form.js              |  2 +-
 src/frontend/js/i18n/messages.json            |  2 +
 7 files changed, 55 insertions(+), 78 deletions(-)

diff --git a/src/frontend/js/app/nginx/redirection/list/item.ejs b/src/frontend/js/app/nginx/redirection/list/item.ejs
index bd4d19e..de197bf 100644
--- a/src/frontend/js/app/nginx/redirection/list/item.ejs
+++ b/src/frontend/js/app/nginx/redirection/list/item.ejs
@@ -1,32 +1,37 @@
 <td class="text-center">
-    <div class="avatar d-block" style="background-image: url(<%- avatar || '/images/default-avatar.jpg' %>)">
-        <span class="avatar-status <%- is_disabled ? 'bg-red' : 'bg-green' %>"></span>
+    <div class="avatar d-block" style="background-image: url(<%- owner.avatar || '/images/default-avatar.jpg' %>)" title="Owned by <%- owner.name %>">
+        <span class="avatar-status <%- owner.is_disabled ? 'bg-red' : 'bg-green' %>"></span>
     </div>
 </td>
 <td>
-    <div><%- name %></div>
+    <div>
+        <% domain_names.map(function(host) {
+            %>
+            <span class="tag"><%- host %></span>
+            <%
+        });
+        %>
+    </div>
     <div class="small text-muted">
-        Created: <%- formatDbDate(created_on, 'Do MMMM YYYY') %>
+        <%- i18n('str', 'created-on', {date: formatDbDate(created_on, 'Do MMMM YYYY')}) %>
     </div>
 </td>
 <td>
-    <div><%- email %></div>
+    <div class="text-monospace"><%- forward_domain_name %></div>
 </td>
 <td>
-    <div><%- roles.join(', ') %></div>
+    <div><%- ssl_enabled && ssl_provider ? i18n('ssl', ssl_provider) : i18n('ssl', 'none') %></div>
 </td>
+<% if (canManage) { %>
 <td class="text-center">
     <div class="item-action dropdown">
         <a href="#" data-toggle="dropdown" class="icon"><i class="fe fe-more-vertical"></i></a>
         <div class="dropdown-menu dropdown-menu-right">
-            <a href="#" class="edit-user dropdown-item"><i class="dropdown-icon fe fe-edit"></i> Edit Details</a>
-            <a href="#" class="edit-permissions dropdown-item"><i class="dropdown-icon fe fe-shield"></i> Edit Permissions</a>
-            <a href="#" class="set-password dropdown-item"><i class="dropdown-icon fe fe-lock"></i> Set Password</a>
-            <% if (!isSelf()) { %>
-            <a href="#" class="login dropdown-item"><i class="dropdown-icon fe fe-log-in"></i> Sign in as User</a>
+            <a href="#" class="edit dropdown-item"><i class="dropdown-icon fe fe-edit"></i> <%- i18n('str', 'edit') %></a>
+            <a href="#" class="logs dropdown-item"><i class="dropdown-icon fe fe-book"></i> <%- i18n('str', 'logs') %></a>
             <div class="dropdown-divider"></div>
-            <a href="#" class="delete-user dropdown-item"><i class="dropdown-icon fe fe-trash-2"></i> Delete User</a>
-            <% } %>
+            <a href="#" class="delete dropdown-item"><i class="dropdown-icon fe fe-trash-2"></i> <%- i18n('str', 'delete') %></a>
         </div>
     </div>
 </td>
+<% } %>
diff --git a/src/frontend/js/app/nginx/redirection/list/item.js b/src/frontend/js/app/nginx/redirection/list/item.js
index e2a6825..e8d74b0 100644
--- a/src/frontend/js/app/nginx/redirection/list/item.js
+++ b/src/frontend/js/app/nginx/redirection/list/item.js
@@ -1,69 +1,32 @@
 'use strict';
 
-const Mn         = require('backbone.marionette');
-const Controller = require('../../../controller');
-const Api        = require('../../../api');
-const Cache      = require('../../../cache');
-const Tokens     = require('../../../tokens');
-const template   = require('./item.ejs');
+const Mn       = require('backbone.marionette');
+const App      = require('../../../main');
+const template = require('./item.ejs');
 
 module.exports = Mn.View.extend({
     template: template,
     tagName:  'tr',
 
     ui: {
-        edit:        'a.edit-user',
-        permissions: 'a.edit-permissions',
-        password:    'a.set-password',
-        login:       'a.login',
-        delete:      'a.delete-user'
+        edit:   'a.edit',
+        delete: 'a.delete'
     },
 
     events: {
         'click @ui.edit': function (e) {
             e.preventDefault();
-            Controller.showUserForm(this.model);
-        },
-
-        'click @ui.permissions': function (e) {
-            e.preventDefault();
-            Controller.showUserPermissions(this.model);
-        },
-
-        'click @ui.password': function (e) {
-            e.preventDefault();
-            Controller.showUserPasswordForm(this.model);
+            App.Controller.showNginxRedirectionForm(this.model);
         },
 
         'click @ui.delete': function (e) {
             e.preventDefault();
-            Controller.showUserDeleteConfirm(this.model);
-        },
-
-        'click @ui.login': function (e) {
-            e.preventDefault();
-
-            if (Cache.User.get('id') !== this.model.get('id')) {
-                this.ui.login.prop('disabled', true).addClass('btn-disabled');
-
-                Api.Users.loginAs(this.model.get('id'))
-                    .then(res => {
-                        Tokens.addToken(res.token, res.user.nickname || res.user.name);
-                        window.location = '/';
-                        window.location.reload();
-                    })
-                    .catch(err => {
-                        alert(err.message);
-                        this.ui.login.prop('disabled', false).removeClass('btn-disabled');
-                    });
-            }
+            App.Controller.showNginxRedirectionDeleteConfirm(this.model);
         }
     },
 
     templateContext: {
-        isSelf: function () {
-            return Cache.User.get('id') === this.id;
-        }
+        canManage: App.Cache.User.canManage('redirection_hosts')
     },
 
     initialize: function () {
diff --git a/src/frontend/js/app/nginx/redirection/list/main.ejs b/src/frontend/js/app/nginx/redirection/list/main.ejs
index 83afd4b..14e75eb 100644
--- a/src/frontend/js/app/nginx/redirection/list/main.ejs
+++ b/src/frontend/js/app/nginx/redirection/list/main.ejs
@@ -1,10 +1,12 @@
 <thead>
-<th width="30">&nbsp;</th>
-<th>Name</th>
-<th>Email</th>
-<th>Roles</th>
-<th>&nbsp;</th>
+    <th width="30">&nbsp;</th>
+    <th><%- i18n('str', 'source') %></th>
+    <th><%- i18n('str', 'destination') %></th>
+    <th><%- i18n('str', 'ssl') %></th>
+    <% if (canManage) { %>
+        <th>&nbsp;</th>
+    <% } %>
 </thead>
 <tbody>
-<!-- items -->
+    <!-- items -->
 </tbody>
diff --git a/src/frontend/js/app/nginx/redirection/list/main.js b/src/frontend/js/app/nginx/redirection/list/main.js
index 80b7bd5..e51e03e 100644
--- a/src/frontend/js/app/nginx/redirection/list/main.js
+++ b/src/frontend/js/app/nginx/redirection/list/main.js
@@ -1,8 +1,9 @@
 'use strict';
 
-const Mn         = require('backbone.marionette');
-const ItemView   = require('./item');
-const template   = require('./main.ejs');
+const Mn       = require('backbone.marionette');
+const App      = require('../../../main');
+const ItemView = require('./item');
+const template = require('./main.ejs');
 
 const TableBody = Mn.CollectionView.extend({
     tagName:   'tbody',
@@ -21,6 +22,10 @@ module.exports = Mn.View.extend({
         }
     },
 
+    templateContext: {
+        canManage: App.Cache.User.canManage('redirection_hosts')
+    },
+
     onRender: function () {
         this.showChildView('body', new TableBody({
             collection: this.collection
diff --git a/src/frontend/js/app/nginx/redirection/main.js b/src/frontend/js/app/nginx/redirection/main.js
index ca82c85..146dd72 100644
--- a/src/frontend/js/app/nginx/redirection/main.js
+++ b/src/frontend/js/app/nginx/redirection/main.js
@@ -25,7 +25,7 @@ module.exports = Mn.View.extend({
     events: {
         'click @ui.add': function (e) {
             e.preventDefault();
-            App.Controller.showNginxProxyForm();
+            App.Controller.showNginxRedirectionForm();
         }
     },
 
@@ -36,24 +36,24 @@ module.exports = Mn.View.extend({
     onRender: function () {
         let view = this;
 
-        App.Api.Nginx.ProxyHosts.getAll(['owner', 'access_list'])
+        App.Api.Nginx.RedirectionHosts.getAll(['owner'])
             .then(response => {
                 if (!view.isDestroyed()) {
                     if (response && response.length) {
                         view.showChildView('list_region', new ListView({
-                            collection: new ProxyHostModel.Collection(response)
+                            collection: new RedirectionHostModel.Collection(response)
                         }));
                     } else {
-                        let manage = App.Cache.User.canManage('proxy_hosts');
+                        let manage = App.Cache.User.canManage('redirection_hosts');
 
                         view.showChildView('list_region', new EmptyView({
-                            title:      App.i18n('proxy-hosts', 'empty'),
+                            title:      App.i18n('redirection-hosts', 'empty'),
                             subtitle:   App.i18n('all-hosts', 'empty-subtitle', {manage: manage}),
-                            link:       manage ? App.i18n('proxy-hosts', 'add') : null,
-                            btn_color:  'success',
-                            permission: 'proxy_hosts',
+                            link:       manage ? App.i18n('redirection-hosts', 'add') : null,
+                            btn_color:  'yellow',
+                            permission: 'redirection_hosts',
                             action:     function () {
-                                App.Controller.showNginxProxyForm();
+                                App.Controller.showNginxRedirectionForm();
                             }
                         }));
                     }
@@ -64,7 +64,7 @@ module.exports = Mn.View.extend({
                     code:    err.code,
                     message: err.message,
                     retry:   function () {
-                        App.Controller.showNginxProxy();
+                        App.Controller.showNginxRedirection();
                     }
                 }));
 
diff --git a/src/frontend/js/app/user/form.js b/src/frontend/js/app/user/form.js
index de0339c..dca797c 100644
--- a/src/frontend/js/app/user/form.js
+++ b/src/frontend/js/app/user/form.js
@@ -64,7 +64,7 @@ module.exports = Mn.View.extend({
                     }
 
                     view.model.set(result);
-                    App.App.UI.closeModal(function () {
+                    App.UI.closeModal(function () {
                         if (method === App.Api.Users.create) {
                             // Show permissions dialog immediately
                             App.Controller.showUserPermissions(view.model);
diff --git a/src/frontend/js/i18n/messages.json b/src/frontend/js/i18n/messages.json
index 926b215..b6f750a 100644
--- a/src/frontend/js/i18n/messages.json
+++ b/src/frontend/js/i18n/messages.json
@@ -85,6 +85,8 @@
     },
     "redirection-hosts": {
       "title": "Redirection Hosts",
+      "empty": "There are no Redirection Hosts",
+      "add": "Add Redirection Host",
       "form-title": "{id, select, undefined{New} other{Edit}} Redirection Host",
       "forward-domain": "Forward Domain",
       "preserve-path": "Preserve Path",