From e98d48459b77eab08c3ee56ed07ae42b939c8aed Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Fri, 1 Apr 2022 14:21:05 +0200 Subject: [PATCH 01/79] Add papertrail gem --- Gemfile | 1 + Gemfile.lock | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/Gemfile b/Gemfile index 02335d6bb..5f82ac514 100644 --- a/Gemfile +++ b/Gemfile @@ -23,6 +23,7 @@ gem 'pundit' gem 'rails-i18n' gem 'seed-fu' gem 'sentry-raven' +gem 'papertrail', '~> 0.9.16' group :development, :test do gem 'bullet' diff --git a/Gemfile.lock b/Gemfile.lock index b5d3738bd..a908e23c4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -77,6 +77,7 @@ GEM annotate (3.2.0) activerecord (>= 3.2, < 8.0) rake (>= 10.4, < 14.0) + ansi (1.5.0) ast (2.4.2) attr_required (1.0.1) bindata (2.4.10) @@ -101,6 +102,7 @@ GEM activesupport childprocess (4.1.0) choice (0.2.0) + chronic (0.10.2) coderay (1.1.3) concurrent-ruby (1.1.9) countries (4.2.2) @@ -195,6 +197,9 @@ GEM validate_email validate_url webfinger (>= 1.0.1) + papertrail (0.9.18) + ansi (~> 1.5) + chronic (~> 0.10) parallel (1.21.0) parser (3.1.1.0) ast (~> 2.4.1) @@ -383,6 +388,7 @@ DEPENDENCIES mysql2 net-ldap openid_connect + papertrail (~> 0.9.16) password_strength pry pry-stack_explorer From 8db48ade2bfa8d55ce54710fb33b82d511f42942 Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Fri, 1 Apr 2022 15:00:24 +0200 Subject: [PATCH 02/79] Replaced papertrail with paper_trail gem --- Gemfile | 2 +- Gemfile.lock | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Gemfile b/Gemfile index 5f82ac514..37179d309 100644 --- a/Gemfile +++ b/Gemfile @@ -23,7 +23,7 @@ gem 'pundit' gem 'rails-i18n' gem 'seed-fu' gem 'sentry-raven' -gem 'papertrail', '~> 0.9.16' +gem 'paper_trail', '~> 12.3' group :development, :test do gem 'bullet' diff --git a/Gemfile.lock b/Gemfile.lock index a908e23c4..3bf4d25ab 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -77,7 +77,6 @@ GEM annotate (3.2.0) activerecord (>= 3.2, < 8.0) rake (>= 10.4, < 14.0) - ansi (1.5.0) ast (2.4.2) attr_required (1.0.1) bindata (2.4.10) @@ -102,7 +101,6 @@ GEM activesupport childprocess (4.1.0) choice (0.2.0) - chronic (0.10.2) coderay (1.1.3) concurrent-ruby (1.1.9) countries (4.2.2) @@ -197,9 +195,9 @@ GEM validate_email validate_url webfinger (>= 1.0.1) - papertrail (0.9.18) - ansi (~> 1.5) - chronic (~> 0.10) + paper_trail (12.3.0) + activerecord (>= 5.2) + request_store (~> 1.1) parallel (1.21.0) parser (3.1.1.0) ast (~> 2.4.1) @@ -267,6 +265,8 @@ GEM rainbow (3.1.1) rake (13.0.6) regexp_parser (2.2.1) + request_store (1.5.1) + rack (>= 1.4) rexml (3.2.5) rspec-core (3.11.0) rspec-support (~> 3.11.0) @@ -388,7 +388,7 @@ DEPENDENCIES mysql2 net-ldap openid_connect - papertrail (~> 0.9.16) + paper_trail (~> 12.3) password_strength pry pry-stack_explorer From 9317dcd67a8671f1eddd1587784f6cec85606e1e Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Fri, 1 Apr 2022 15:03:30 +0200 Subject: [PATCH 03/79] Sort gems alphabetically --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 37179d309..69aa23e7c 100644 --- a/Gemfile +++ b/Gemfile @@ -18,12 +18,12 @@ gem 'faker' gem 'haml' gem 'maxmind-db' gem 'openid_connect' +gem 'paper_trail', '~> 12.3' gem 'password_strength' gem 'pundit' gem 'rails-i18n' gem 'seed-fu' gem 'sentry-raven' -gem 'paper_trail', '~> 12.3' group :development, :test do gem 'bullet' From 5cd488a885967ae48bca7db4ac39e83f9d45110d Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Fri, 1 Apr 2022 15:19:17 +0200 Subject: [PATCH 04/79] Add papertrail to model and migration --- .../api/encryptables_controller.rb | 2 + app/models/encryptable.rb | 2 + db/migrate/20220401130939_create_versions.rb | 38 +++++++++++++++++++ db/schema.rb | 12 +++++- 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20220401130939_create_versions.rb diff --git a/app/controllers/api/encryptables_controller.rb b/app/controllers/api/encryptables_controller.rb index 08e302c13..f875a5257 100644 --- a/app/controllers/api/encryptables_controller.rb +++ b/app/controllers/api/encryptables_controller.rb @@ -3,6 +3,8 @@ class Api::EncryptablesController < ApiController self.permitted_attrs = [:name, :description, :folder_id, :tag] + before_action :set_paper_trail_whodunnit + helper_method :team # GET /api/encryptables diff --git a/app/models/encryptable.rb b/app/models/encryptable.rb index c8d7b77ab..75ce28a30 100644 --- a/app/models/encryptable.rb +++ b/app/models/encryptable.rb @@ -17,6 +17,8 @@ class Encryptable < ApplicationRecord + has_paper_trail on: [:show] + serialize :encrypted_data, ::EncryptedData attr_readonly :type diff --git a/db/migrate/20220401130939_create_versions.rb b/db/migrate/20220401130939_create_versions.rb new file mode 100644 index 000000000..63bdc6cd6 --- /dev/null +++ b/db/migrate/20220401130939_create_versions.rb @@ -0,0 +1,38 @@ +# This migration creates the `versions` table, the only schema PT requires. +# All other migrations PT provides are optional. +class CreateVersions < ActiveRecord::Migration[7.0] + + # The largest text column available in all supported RDBMS is + # 1024^3 - 1 bytes, roughly one gibibyte. We specify a size + # so that MySQL will use `longtext` instead of `text`. Otherwise, + # when serializing very large objects, `text` might not be big enough. + TEXT_BYTES = 1_073_741_823 + + def change + create_table :versions do |t| + t.string :item_type, null: false + t.bigint :item_id, null: false + t.string :event, null: false + t.string :whodunnit + t.text :object, limit: TEXT_BYTES + + # Known issue in MySQL: fractional second precision + # ------------------------------------------------- + # + # MySQL timestamp columns do not support fractional seconds unless + # defined with "fractional seconds precision". MySQL users should manually + # add fractional seconds precision to this migration, specifically, to + # the `created_at` column. + # (https://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html) + # + # MySQL users should also upgrade to at least rails 4.2, which is the first + # version of ActiveRecord with support for fractional seconds in MySQL. + # (https://github.com/rails/rails/pull/14359) + # + # MySQL users should use the following line for `created_at` + # t.datetime :created_at, limit: 6 + t.datetime :created_at + end + add_index :versions, %i(item_type item_id) + end +end diff --git a/db/schema.rb b/db/schema.rb index 0d2ffc403..201bb4a9c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2022_03_29_122335) do +ActiveRecord::Schema[7.0].define(version: 2022_04_01_130939) do create_table "encryptables", force: :cascade do |t| t.string "name", limit: 70, default: "", null: false t.integer "folder_id", default: 0, null: false @@ -98,4 +98,14 @@ t.index ["default_ccli_user_id"], name: "index_users_on_default_ccli_user_id" end + create_table "versions", force: :cascade do |t| + t.string "item_type", null: false + t.bigint "item_id", null: false + t.string "event", null: false + t.string "whodunnit" + t.text "object", limit: 1073741823 + t.datetime "created_at" + t.index ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id" + end + end From e9d37a38acdd6db5c6260fbe0c6b18b6f402ef1d Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Fri, 1 Apr 2022 15:30:09 +0200 Subject: [PATCH 05/79] Add ignore to papertrail --- app/models/encryptable.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/encryptable.rb b/app/models/encryptable.rb index 75ce28a30..58b47939b 100644 --- a/app/models/encryptable.rb +++ b/app/models/encryptable.rb @@ -17,7 +17,7 @@ class Encryptable < ApplicationRecord - has_paper_trail on: [:show] + has_paper_trail on: [:show], ignore: [ :tag, :type, :encrypted_data ] serialize :encrypted_data, ::EncryptedData From 045b8321c99f6db1706cfb607c59168ad5411d33 Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Fri, 1 Apr 2022 16:03:23 +0200 Subject: [PATCH 06/79] Fixed rubocop error --- app/models/encryptable.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/encryptable.rb b/app/models/encryptable.rb index 58b47939b..8b3ec19f1 100644 --- a/app/models/encryptable.rb +++ b/app/models/encryptable.rb @@ -17,7 +17,7 @@ class Encryptable < ApplicationRecord - has_paper_trail on: [:show], ignore: [ :tag, :type, :encrypted_data ] + has_paper_trail on: [:show], ignore: [:tag, :type, :encrypted_data] serialize :encrypted_data, ::EncryptedData From 7983e1427a665431a0dc19c276c5e15600d15984 Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Fri, 1 Apr 2022 16:11:48 +0200 Subject: [PATCH 07/79] Changed show function to touch --- app/models/encryptable.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/encryptable.rb b/app/models/encryptable.rb index 8b3ec19f1..03d97e1b5 100644 --- a/app/models/encryptable.rb +++ b/app/models/encryptable.rb @@ -17,7 +17,7 @@ class Encryptable < ApplicationRecord - has_paper_trail on: [:show], ignore: [:tag, :type, :encrypted_data] + has_paper_trail on: [:touch], ignore: [:tag, :type, :encrypted_data] serialize :encrypted_data, ::EncryptedData From e20bd83cb0fa2a05219a717e5569e9fe1504f057 Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Fri, 1 Apr 2022 17:00:41 +0200 Subject: [PATCH 08/79] Add spec for touch --- spec/models/encryptable_spec.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spec/models/encryptable_spec.rb b/spec/models/encryptable_spec.rb index ad5b6bb09..b90bd8f77 100644 --- a/spec/models/encryptable_spec.rb +++ b/spec/models/encryptable_spec.rb @@ -84,6 +84,16 @@ end end + context 'papertrail' do + context 'touch' do + it 'creates a log entry', versioning: true do + encryptable.touch + + expect(encryptable.versions.count).to equal(1) + end + end + end + private def create_legacy_ose_secret From 7f50548dd5a3e7cdadee4eacb00f14b06feeccc4 Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Fri, 1 Apr 2022 18:28:12 +0200 Subject: [PATCH 09/79] Add specs for papertrail --- spec/models/encryptable_spec.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/spec/models/encryptable_spec.rb b/spec/models/encryptable_spec.rb index b90bd8f77..1ce434ad5 100644 --- a/spec/models/encryptable_spec.rb +++ b/spec/models/encryptable_spec.rb @@ -84,12 +84,20 @@ end end - context 'papertrail' do + context 'papertrail', versioning: true do context 'touch' do - it 'creates a log entry', versioning: true do + PaperTrail.request.whodunnit = 'Bob' + + before do encryptable.touch + end + + it 'creates a log entry' do + expect(encryptable.versions.count).to eq(1) + end - expect(encryptable.versions.count).to equal(1) + it 'contains user in whodunnit' do + expect(encryptable.versions.last.whodunnit).to eq('Bob') end end end From 8775a62c26dc1c0ef06d35088deffb6e20776e81 Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Sun, 3 Apr 2022 14:00:20 +0200 Subject: [PATCH 10/79] Change rubocop rule to allow touch method --- .rubocop.yml | 3 +++ spec/models/encryptable_spec.rb | 6 ++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 824c97eb3..752902541 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -83,6 +83,9 @@ Naming/FileName: Rails/EnumHash: Enabled: false +Rails/SkipsModelValidations: + AllowedMethods: ["touch"] + # Keep for now, easier with superclass definitions Style/ClassAndModuleChildren: Enabled: false diff --git a/spec/models/encryptable_spec.rb b/spec/models/encryptable_spec.rb index 1ce434ad5..1b5cbd33c 100644 --- a/spec/models/encryptable_spec.rb +++ b/spec/models/encryptable_spec.rb @@ -88,15 +88,13 @@ context 'touch' do PaperTrail.request.whodunnit = 'Bob' - before do - encryptable.touch - end - it 'creates a log entry' do + encryptable.touch expect(encryptable.versions.count).to eq(1) end it 'contains user in whodunnit' do + encryptable.touch expect(encryptable.versions.last.whodunnit).to eq('Bob') end end From 5c9f03a7940c64a9d3d63983e01a3ca047989943 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Sun, 3 Apr 2022 19:49:04 +0200 Subject: [PATCH 11/79] add route to logs and first draft of logs_controller --- .../api/encryptables/logs_controller.rb | 14 ++++++++++++++ config/routes/api.rb | 1 + 2 files changed, 15 insertions(+) create mode 100644 app/controllers/api/encryptables/logs_controller.rb diff --git a/app/controllers/api/encryptables/logs_controller.rb b/app/controllers/api/encryptables/logs_controller.rb new file mode 100644 index 000000000..e6eff3da9 --- /dev/null +++ b/app/controllers/api/encryptables/logs_controller.rb @@ -0,0 +1,14 @@ +class Api::Encryptables::LogsController < ApiController + + def index + render({ json: fetch_entries, + root: model_root_key.pluralize } + .merge(render_options) + .merge(options.fetch(:render_options, {}))) + end + + def fetch_entries + logs = current_user.encryptables.find_by(id: params[:id]).versions + logs.sort { |a, b| b.created_at <=> a.created_at } + end +end diff --git a/config/routes/api.rb b/config/routes/api.rb index d5258bbd2..bf1e34d94 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -13,6 +13,7 @@ resources :encryptables, except: [:new, :edit] do resources :file_entries, only: [:create, :index, :destroy, :show] + resources :logs end resources :api_users, except: [:new, :edit] do From 96a63c0f8ec5403d7bb2a17c53d5f8734d6ba53b Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Sun, 3 Apr 2022 21:15:35 +0200 Subject: [PATCH 12/79] Fix rubocop and spec --- app/controllers/api/encryptables/logs_controller.rb | 2 ++ spec/models/encryptable_spec.rb | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/encryptables/logs_controller.rb b/app/controllers/api/encryptables/logs_controller.rb index e6eff3da9..fb5651c44 100644 --- a/app/controllers/api/encryptables/logs_controller.rb +++ b/app/controllers/api/encryptables/logs_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Api::Encryptables::LogsController < ApiController def index diff --git a/spec/models/encryptable_spec.rb b/spec/models/encryptable_spec.rb index 1b5cbd33c..b939ce9d6 100644 --- a/spec/models/encryptable_spec.rb +++ b/spec/models/encryptable_spec.rb @@ -86,14 +86,14 @@ context 'papertrail', versioning: true do context 'touch' do - PaperTrail.request.whodunnit = 'Bob' - it 'creates a log entry' do encryptable.touch expect(encryptable.versions.count).to eq(1) end it 'contains user in whodunnit' do + PaperTrail.request.whodunnit = 'Bob' + encryptable.touch expect(encryptable.versions.last.whodunnit).to eq('Bob') end From 610a59b2a726127130f0637b62e37103d38b6e3e Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Sun, 3 Apr 2022 21:36:05 +0200 Subject: [PATCH 13/79] Add log policy --- app/policies/encryptables/log_policy.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 app/policies/encryptables/log_policy.rb diff --git a/app/policies/encryptables/log_policy.rb b/app/policies/encryptables/log_policy.rb new file mode 100644 index 000000000..8eb33ba64 --- /dev/null +++ b/app/policies/encryptables/log_policy.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +class Encryptables::LogPolicy < TeamDependantPolicy + def initialize(user, record) + @user = user + @record = record + end + + def show? + team.teammember?(@user.id) + end + + protected + + def team + @record.folder.team + end +end From f33f40f84373d5ea3d7977ee15305f2972e2f4f1 Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Tue, 5 Apr 2022 13:57:49 +0200 Subject: [PATCH 14/79] Change show to index --- app/policies/encryptables/log_policy.rb | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/app/policies/encryptables/log_policy.rb b/app/policies/encryptables/log_policy.rb index 8eb33ba64..23b80d4ce 100644 --- a/app/policies/encryptables/log_policy.rb +++ b/app/policies/encryptables/log_policy.rb @@ -6,13 +6,7 @@ def initialize(user, record) @record = record end - def show? - team.teammember?(@user.id) - end - - protected - - def team - @record.folder.team + def index? + return true if @user.is_a?(User::Human) end end From 2473cb279f94839c424b3e98e6bea74eb518ed0d Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Tue, 5 Apr 2022 13:58:16 +0200 Subject: [PATCH 15/79] Refactor index? method --- app/policies/encryptables/log_policy.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/policies/encryptables/log_policy.rb b/app/policies/encryptables/log_policy.rb index 23b80d4ce..a05459ea1 100644 --- a/app/policies/encryptables/log_policy.rb +++ b/app/policies/encryptables/log_policy.rb @@ -7,6 +7,6 @@ def initialize(user, record) end def index? - return true if @user.is_a?(User::Human) + @user.is_a?(User::Human) end end From acfc09967f0951c0cbcddfcdea112d08647041d0 Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Tue, 5 Apr 2022 14:04:06 +0200 Subject: [PATCH 16/79] Re-add show restriction --- app/policies/encryptables/log_policy.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/policies/encryptables/log_policy.rb b/app/policies/encryptables/log_policy.rb index a05459ea1..8d573cae6 100644 --- a/app/policies/encryptables/log_policy.rb +++ b/app/policies/encryptables/log_policy.rb @@ -9,4 +9,14 @@ def initialize(user, record) def index? @user.is_a?(User::Human) end + + def show? + team.teammember?(@user.id) + end + + protected + + def team + @record.folder.team + end end From d73cae2ae6484912880b22e6fb4be20ea3582835 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Tue, 5 Apr 2022 15:30:37 +0200 Subject: [PATCH 17/79] add controller spec --- .../api/{encryptables => }/logs_controller.rb | 3 ++- spec/controllers/api/logs_controller_spec.rb | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) rename app/controllers/api/{encryptables => }/logs_controller.rb (81%) create mode 100644 spec/controllers/api/logs_controller_spec.rb diff --git a/app/controllers/api/encryptables/logs_controller.rb b/app/controllers/api/logs_controller.rb similarity index 81% rename from app/controllers/api/encryptables/logs_controller.rb rename to app/controllers/api/logs_controller.rb index fb5651c44..fbf7342d6 100644 --- a/app/controllers/api/encryptables/logs_controller.rb +++ b/app/controllers/api/logs_controller.rb @@ -1,8 +1,9 @@ # frozen_string_literal: true -class Api::Encryptables::LogsController < ApiController +class Api::LogsController < ApiController def index + # authorize ( policy_class: LogsPolicy ) render({ json: fetch_entries, root: model_root_key.pluralize } .merge(render_options) diff --git a/spec/controllers/api/logs_controller_spec.rb b/spec/controllers/api/logs_controller_spec.rb new file mode 100644 index 000000000..d6a566861 --- /dev/null +++ b/spec/controllers/api/logs_controller_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Api::LogsController do + include ControllerHelpers + + let(:bob) { users(:bob) } + let(:alice) { users(:alice) } + let(:api_user) { bob.api_users.create } + let(:id1) { encryptables(:id) } + + context 'GET index' do + it 'something' do + login_as(:alice) + get :index, params: { encryptable_id: :id1 } + end + end +end From 374450547ae341088ba9f13d93ead4fd120fd71b Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Tue, 5 Apr 2022 15:41:21 +0200 Subject: [PATCH 18/79] Fixed encryptable controller spec --- app/controllers/api/logs_controller.rb | 2 +- spec/controllers/api/logs_controller_spec.rb | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/logs_controller.rb b/app/controllers/api/logs_controller.rb index fbf7342d6..cd794a674 100644 --- a/app/controllers/api/logs_controller.rb +++ b/app/controllers/api/logs_controller.rb @@ -11,7 +11,7 @@ def index end def fetch_entries - logs = current_user.encryptables.find_by(id: params[:id]).versions + logs = current_user.encryptables.find_by!(id: params[:id]).versions logs.sort { |a, b| b.created_at <=> a.created_at } end end diff --git a/spec/controllers/api/logs_controller_spec.rb b/spec/controllers/api/logs_controller_spec.rb index d6a566861..18aa4eb55 100644 --- a/spec/controllers/api/logs_controller_spec.rb +++ b/spec/controllers/api/logs_controller_spec.rb @@ -8,12 +8,12 @@ let(:bob) { users(:bob) } let(:alice) { users(:alice) } let(:api_user) { bob.api_users.create } - let(:id1) { encryptables(:id) } + let(:id_one) { encryptables(:id) } context 'GET index' do - it 'something' do + it 'calls index action' do login_as(:alice) - get :index, params: { encryptable_id: :id1 } + get :index, params: { encryptable_id: :id_one } end end end From c13535068baa5164d2652e1404a12846fbe4c6f0 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Tue, 5 Apr 2022 17:46:19 +0200 Subject: [PATCH 19/79] extend controller spec, specs fail because controller doesn't work as it should --- app/controllers/api/logs_controller.rb | 3 +-- spec/controllers/api/logs_controller_spec.rb | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/logs_controller.rb b/app/controllers/api/logs_controller.rb index cd794a674..9b341b587 100644 --- a/app/controllers/api/logs_controller.rb +++ b/app/controllers/api/logs_controller.rb @@ -3,7 +3,6 @@ class Api::LogsController < ApiController def index - # authorize ( policy_class: LogsPolicy ) render({ json: fetch_entries, root: model_root_key.pluralize } .merge(render_options) @@ -11,7 +10,7 @@ def index end def fetch_entries - logs = current_user.encryptables.find_by!(id: params[:id]).versions + logs = current_user.encryptables.find_by!(id: params[:encryptable_id]).versions logs.sort { |a, b| b.created_at <=> a.created_at } end end diff --git a/spec/controllers/api/logs_controller_spec.rb b/spec/controllers/api/logs_controller_spec.rb index 18aa4eb55..918e281b8 100644 --- a/spec/controllers/api/logs_controller_spec.rb +++ b/spec/controllers/api/logs_controller_spec.rb @@ -8,12 +8,14 @@ let(:bob) { users(:bob) } let(:alice) { users(:alice) } let(:api_user) { bob.api_users.create } - let(:id_one) { encryptables(:id) } + let(:credentials1) { encryptables(:credentials1) } context 'GET index' do it 'calls index action' do login_as(:alice) - get :index, params: { encryptable_id: :id_one } + credentials1.touch + get :index, params: { encryptable_id: credentials1.id } + expect(data.count).to eq 1 end end end From f9efbb441f320a87c97ab23becb210794656e0a9 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Wed, 6 Apr 2022 13:27:01 +0200 Subject: [PATCH 20/79] add logs serializer and fix controller --- app/controllers/api/logs_controller.rb | 15 ++++++++++++--- app/serializers/logs_serializer.rb | 7 +++++++ spec/controllers/api/logs_controller_spec.rb | 6 ++++-- 3 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 app/serializers/logs_serializer.rb diff --git a/app/controllers/api/logs_controller.rb b/app/controllers/api/logs_controller.rb index 9b341b587..bf68d81e2 100644 --- a/app/controllers/api/logs_controller.rb +++ b/app/controllers/api/logs_controller.rb @@ -1,16 +1,25 @@ # frozen_string_literal: true class Api::LogsController < ApiController + self.permitted_attrs = :encryptable_id - def index + def index(options = {}) + authorize Encryptable render({ json: fetch_entries, - root: model_root_key.pluralize } + each_serializer: list_serializer, + root: 'Logs_'.pluralize } .merge(render_options) .merge(options.fetch(:render_options, {}))) end def fetch_entries - logs = current_user.encryptables.find_by!(id: params[:encryptable_id]).versions + PaperTrail.serializer = JSON + logs = current_user.encryptables.find_by(id: params[:encryptable_id]).versions logs.sort { |a, b| b.created_at <=> a.created_at } end + + def list_serializer + @model_serializer ||= 'LogsSerializer'.constantize + end + end diff --git a/app/serializers/logs_serializer.rb b/app/serializers/logs_serializer.rb new file mode 100644 index 000000000..3e8519c35 --- /dev/null +++ b/app/serializers/logs_serializer.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class LogsSerializer < ApplicationSerializer + attributes :id, :item_type, :event, :whodunnit, :object, :created_at + +end + diff --git a/spec/controllers/api/logs_controller_spec.rb b/spec/controllers/api/logs_controller_spec.rb index 918e281b8..b25a8f127 100644 --- a/spec/controllers/api/logs_controller_spec.rb +++ b/spec/controllers/api/logs_controller_spec.rb @@ -9,13 +9,15 @@ let(:alice) { users(:alice) } let(:api_user) { bob.api_users.create } let(:credentials1) { encryptables(:credentials1) } + let(:credentials2) { encryptables(:credentials2) } context 'GET index' do - it 'calls index action' do + it 'returns right amount of logs' do login_as(:alice) credentials1.touch + credentials1.touch get :index, params: { encryptable_id: credentials1.id } - expect(data.count).to eq 1 + expect(data.count).to eq 2 end end end From f265b72228f766c605991376722b3350bb0ffcb1 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Wed, 6 Apr 2022 13:37:38 +0200 Subject: [PATCH 21/79] fix rubocop error --- app/serializers/logs_serializer.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/serializers/logs_serializer.rb b/app/serializers/logs_serializer.rb index 3e8519c35..75c184f2a 100644 --- a/app/serializers/logs_serializer.rb +++ b/app/serializers/logs_serializer.rb @@ -4,4 +4,3 @@ class LogsSerializer < ApplicationSerializer attributes :id, :item_type, :event, :whodunnit, :object, :created_at end - From ad7281894b052fb88f884195a56df49d704cf39f Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Wed, 6 Apr 2022 15:22:16 +0200 Subject: [PATCH 22/79] Add logs component --- frontend/app/components/encryptable/log.js | 16 ++++++++++++++ frontend/app/templates/encryptables/show.hbs | 22 +++++++++++++++++++- frontend/config/environment.js | 1 + 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 frontend/app/components/encryptable/log.js diff --git a/frontend/app/components/encryptable/log.js b/frontend/app/components/encryptable/log.js new file mode 100644 index 000000000..f8a55b12b --- /dev/null +++ b/frontend/app/components/encryptable/log.js @@ -0,0 +1,16 @@ +import Component from "@glimmer/component"; +import { action } from "@ember/object"; +import { tracked } from "@glimmer/tracking"; +import { inject as service } from "@ember/service"; +import { isNone } from "@ember/utils"; + +export default class LogComponent extends Component { + constructor() { + super(...arguments); + } + + @action + get encryptableLogs() { + return `/api/encryptables/${this.args.encryptable.get("id")}/logs`; + } +} diff --git a/frontend/app/templates/encryptables/show.hbs b/frontend/app/templates/encryptables/show.hbs index 2098fc37d..e657c3d1a 100644 --- a/frontend/app/templates/encryptables/show.hbs +++ b/frontend/app/templates/encryptables/show.hbs @@ -1 +1,21 @@ - +
+ +
+
+
+

Encryptable

+
+
+ +
+
+ + + + + + + + +
+
diff --git a/frontend/config/environment.js b/frontend/config/environment.js index 23ee2b6da..8709564cf 100644 --- a/frontend/config/environment.js +++ b/frontend/config/environment.js @@ -9,6 +9,7 @@ module.exports = function (environment) { sentryDsn: "", appVersion: undefined, currentUserId: undefined, + encryptable_id: undefined, "changeset-validations": { rawOutput: true }, EmberENV: { FEATURES: { From cf7c8b741e3fdb468b70f4e9cee4dc44cad6f7bc Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Thu, 7 Apr 2022 14:11:26 +0200 Subject: [PATCH 23/79] restrict access for api users, add specs --- app/controllers/api/logs_controller.rb | 5 ++++- app/policies/{encryptables => }/log_policy.rb | 2 +- spec/controllers/api/logs_controller_spec.rb | 12 ++++++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) rename app/policies/{encryptables => }/log_policy.rb (83%) diff --git a/app/controllers/api/logs_controller.rb b/app/controllers/api/logs_controller.rb index bf68d81e2..a58814050 100644 --- a/app/controllers/api/logs_controller.rb +++ b/app/controllers/api/logs_controller.rb @@ -4,7 +4,7 @@ class Api::LogsController < ApiController self.permitted_attrs = :encryptable_id def index(options = {}) - authorize Encryptable + authorize(team, :team_member?, policy_class: TeamPolicy) render({ json: fetch_entries, each_serializer: list_serializer, root: 'Logs_'.pluralize } @@ -22,4 +22,7 @@ def list_serializer @model_serializer ||= 'LogsSerializer'.constantize end + def team + @team ||= Encryptable.find(params[:encryptable_id]).folder.team + end end diff --git a/app/policies/encryptables/log_policy.rb b/app/policies/log_policy.rb similarity index 83% rename from app/policies/encryptables/log_policy.rb rename to app/policies/log_policy.rb index 8d573cae6..913367cc1 100644 --- a/app/policies/encryptables/log_policy.rb +++ b/app/policies/log_policy.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class Encryptables::LogPolicy < TeamDependantPolicy +class LogPolicy < TeamDependantPolicy def initialize(user, record) @user = user @record = record diff --git a/spec/controllers/api/logs_controller_spec.rb b/spec/controllers/api/logs_controller_spec.rb index b25a8f127..493c5ac4b 100644 --- a/spec/controllers/api/logs_controller_spec.rb +++ b/spec/controllers/api/logs_controller_spec.rb @@ -9,15 +9,23 @@ let(:alice) { users(:alice) } let(:api_user) { bob.api_users.create } let(:credentials1) { encryptables(:credentials1) } - let(:credentials2) { encryptables(:credentials2) } context 'GET index' do it 'returns right amount of logs' do login_as(:alice) + PaperTrail.request(whodunnit: 'alice') do + credentials1.touch + credentials1.touch + end + get :index, params: { encryptable_id: credentials1.id } + expect(data.count).to eq 2 + end + it 'returns sorted results' do + login_as(:bob) credentials1.touch credentials1.touch get :index, params: { encryptable_id: credentials1.id } - expect(data.count).to eq 2 + expect(data.first['attributes']['created_at']).to be > data.second['attributes']['created_at'] end end end From d2a96b36c589cdcb6299c678c19d5d7a4dd26a00 Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Thu, 7 Apr 2022 14:30:15 +0200 Subject: [PATCH 24/79] Change policy to check for team membership --- app/controllers/api/logs_controller.rb | 4 ++-- app/policies/log_policy.rb | 4 ++-- spec/controllers/api/encryptables_controller_spec.rb | 4 +--- spec/controllers/api/logs_controller_spec.rb | 12 ++++++++++++ 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/app/controllers/api/logs_controller.rb b/app/controllers/api/logs_controller.rb index a58814050..ac2e35ad9 100644 --- a/app/controllers/api/logs_controller.rb +++ b/app/controllers/api/logs_controller.rb @@ -4,7 +4,7 @@ class Api::LogsController < ApiController self.permitted_attrs = :encryptable_id def index(options = {}) - authorize(team, :team_member?, policy_class: TeamPolicy) + authorize(team, :index?, policy_class: LogPolicy) render({ json: fetch_entries, each_serializer: list_serializer, root: 'Logs_'.pluralize } @@ -14,7 +14,7 @@ def index(options = {}) def fetch_entries PaperTrail.serializer = JSON - logs = current_user.encryptables.find_by(id: params[:encryptable_id]).versions + logs = current_user.encryptables.find_by!(id: params[:encryptable_id]).versions logs.sort { |a, b| b.created_at <=> a.created_at } end diff --git a/app/policies/log_policy.rb b/app/policies/log_policy.rb index 913367cc1..540552b47 100644 --- a/app/policies/log_policy.rb +++ b/app/policies/log_policy.rb @@ -7,7 +7,7 @@ def initialize(user, record) end def index? - @user.is_a?(User::Human) + @user.is_a?(User::Human) && team.teammember?(@user.id) end def show? @@ -17,6 +17,6 @@ def show? protected def team - @record.folder.team + @record end end diff --git a/spec/controllers/api/encryptables_controller_spec.rb b/spec/controllers/api/encryptables_controller_spec.rb index aad389076..d3e47e3b5 100644 --- a/spec/controllers/api/encryptables_controller_spec.rb +++ b/spec/controllers/api/encryptables_controller_spec.rb @@ -573,9 +573,7 @@ end.to change { Encryptable::OseSecret.count }.by(1) expect(response).to have_http_status(201) - end - - context 'DELETE destroy' do + end context 'DELETE destroy' do it 'cant destroy an encryptable if not in team' do login_as(:alice) diff --git a/spec/controllers/api/logs_controller_spec.rb b/spec/controllers/api/logs_controller_spec.rb index 493c5ac4b..2351f7caa 100644 --- a/spec/controllers/api/logs_controller_spec.rb +++ b/spec/controllers/api/logs_controller_spec.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'spec_helper' +require 'pry' describe Api::LogsController do include ControllerHelpers @@ -20,6 +21,7 @@ get :index, params: { encryptable_id: credentials1.id } expect(data.count).to eq 2 end + it 'returns sorted results' do login_as(:bob) credentials1.touch @@ -27,5 +29,15 @@ get :index, params: { encryptable_id: credentials1.id } expect(data.first['attributes']['created_at']).to be > data.second['attributes']['created_at'] end + + it 'denies access' do + login_as(:alice) + + team2 = teams(:team2) + encryptable = team2.folders.first.encryptables.first + + get :index, params: { encryptable_id: encryptable.id } + binding.pry + end end end From d744841a72a446a640238e51b9b6f00a2dfd790e Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Thu, 7 Apr 2022 15:05:36 +0200 Subject: [PATCH 25/79] Add tabs for logs --- frontend/app/templates/encryptables/show.hbs | 5 ++--- spec/controllers/api/logs_controller_spec.rb | 6 ++++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/frontend/app/templates/encryptables/show.hbs b/frontend/app/templates/encryptables/show.hbs index e657c3d1a..e3d4f9fab 100644 --- a/frontend/app/templates/encryptables/show.hbs +++ b/frontend/app/templates/encryptables/show.hbs @@ -1,5 +1,4 @@
-
@@ -13,8 +12,8 @@ - - + +
diff --git a/spec/controllers/api/logs_controller_spec.rb b/spec/controllers/api/logs_controller_spec.rb index 2351f7caa..4775e3ace 100644 --- a/spec/controllers/api/logs_controller_spec.rb +++ b/spec/controllers/api/logs_controller_spec.rb @@ -18,6 +18,7 @@ credentials1.touch credentials1.touch end + get :index, params: { encryptable_id: credentials1.id } expect(data.count).to eq 2 end @@ -26,18 +27,19 @@ login_as(:bob) credentials1.touch credentials1.touch + get :index, params: { encryptable_id: credentials1.id } expect(data.first['attributes']['created_at']).to be > data.second['attributes']['created_at'] end - it 'denies access' do + it 'denies access if not in team' do login_as(:alice) team2 = teams(:team2) encryptable = team2.folders.first.encryptables.first get :index, params: { encryptable_id: encryptable.id } - binding.pry + expect(true).to be true end end end From 9ae217a6418556b073d87b7b1f1f13181cc2f32e Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Thu, 7 Apr 2022 15:55:32 +0200 Subject: [PATCH 26/79] Removed tautologous expectation+ --- frontend/app/models/encryptable.js | 1 + .../templates/components/encryptable/log.hbs | 92 +++++++++++++++++++ frontend/app/templates/encryptables/show.hbs | 6 +- spec/controllers/api/logs_controller_spec.rb | 2 +- 4 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 frontend/app/templates/components/encryptable/log.hbs diff --git a/frontend/app/models/encryptable.js b/frontend/app/models/encryptable.js index b6c13a113..e7dc38967 100644 --- a/frontend/app/models/encryptable.js +++ b/frontend/app/models/encryptable.js @@ -7,6 +7,7 @@ export default class Encryptable extends Model { @attr("string") updatedAt; @belongsTo("folder") folder; @hasMany("file-entry") fileEntries; + @hasMany("logs") logs; get isOseSecret() { return this.constructor.modelName === "encryptable-ose-secret"; diff --git a/frontend/app/templates/components/encryptable/log.hbs b/frontend/app/templates/components/encryptable/log.hbs new file mode 100644 index 000000000..08a046d12 --- /dev/null +++ b/frontend/app/templates/components/encryptable/log.hbs @@ -0,0 +1,92 @@ +{{#if this.isEncryptableEditing}} + +{{/if}} +{{#if this.isFileEntryCreating}} + +{{/if}} +
+
+ + {{#unless @encryptable.isOseSecret}} +
+ + delete + + + edit + +
+ {{/unless}} +
+
+
+

{{t "encryptables.show.encryptable"}}: {{@encryptable.name}}

+
+
+

{{t "encryptables.show.created_at"}}: {{moment-format @encryptable.createdAt "DD.MM.YYYY hh:mm"}}

+ / +

{{t "encryptables.show.last_update"}}: {{moment-format @encryptable.updatedAt "DD.MM.YYYY hh:mm"}}

+
+
+ + {{#if @encryptable.description}} +
+
+

{{@encryptable.description}}

+
+
+ {{/if}} +
+ {{#if @encryptable.isOseSecret}} + {{t "encryptables.show.show_secret"}} +
+ + + clip + +
+ {{else}} +
+ + + clip + +
+ {{t "encryptables.show.show_password"}} +
+ + + clip + +
+ {{/if}} +
+
+
+

{{t "encryptables.show.attachments"}}

+
+
+ + {{t "encryptables.show.add_attachment"}} + +
+
+ + + + + + + + {{#each @encryptable.fileEntries as |fileEntry|}} + + {{/each}} + +
{{t "encryptables.show.file"}}{{t "description"}}{{t "actions"}}
+
diff --git a/frontend/app/templates/encryptables/show.hbs b/frontend/app/templates/encryptables/show.hbs index e3d4f9fab..810d876ef 100644 --- a/frontend/app/templates/encryptables/show.hbs +++ b/frontend/app/templates/encryptables/show.hbs @@ -9,11 +9,11 @@
- + - - + +
diff --git a/spec/controllers/api/logs_controller_spec.rb b/spec/controllers/api/logs_controller_spec.rb index 4775e3ace..94bc9f80d 100644 --- a/spec/controllers/api/logs_controller_spec.rb +++ b/spec/controllers/api/logs_controller_spec.rb @@ -39,7 +39,7 @@ encryptable = team2.folders.first.encryptables.first get :index, params: { encryptable_id: encryptable.id } - expect(true).to be true + expect(response.status).to be 403 end end end From 6be41fe8c6edf95e50fc51e879b4d1407414d1a4 Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Thu, 7 Apr 2022 15:55:51 +0200 Subject: [PATCH 27/79] Remove pry --- spec/controllers/api/logs_controller_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/controllers/api/logs_controller_spec.rb b/spec/controllers/api/logs_controller_spec.rb index 94bc9f80d..717ff7c8b 100644 --- a/spec/controllers/api/logs_controller_spec.rb +++ b/spec/controllers/api/logs_controller_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'spec_helper' -require 'pry' describe Api::LogsController do include ControllerHelpers From 739a20f475bdfbd00660f61992358171de1a18f7 Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Thu, 7 Apr 2022 16:18:13 +0200 Subject: [PATCH 28/79] Add stub components for table and table row --- frontend/app/components/encryptable/log.js | 16 --- .../app/components/encryptable/table-row.js | 98 +++++++++++++++++++ frontend/app/components/encryptable/table.js | 59 +++++++++++ .../templates/components/encryptable/log.hbs | 92 ----------------- .../components/encryptable/table-row.hbs | 44 +++++++++ .../components/encryptable/table.hbs | 39 ++++++++ frontend/app/templates/encryptables/show.hbs | 4 +- 7 files changed, 242 insertions(+), 110 deletions(-) delete mode 100644 frontend/app/components/encryptable/log.js create mode 100644 frontend/app/components/encryptable/table-row.js create mode 100644 frontend/app/components/encryptable/table.js delete mode 100644 frontend/app/templates/components/encryptable/log.hbs create mode 100644 frontend/app/templates/components/encryptable/table-row.hbs create mode 100644 frontend/app/templates/components/encryptable/table.hbs diff --git a/frontend/app/components/encryptable/log.js b/frontend/app/components/encryptable/log.js deleted file mode 100644 index f8a55b12b..000000000 --- a/frontend/app/components/encryptable/log.js +++ /dev/null @@ -1,16 +0,0 @@ -import Component from "@glimmer/component"; -import { action } from "@ember/object"; -import { tracked } from "@glimmer/tracking"; -import { inject as service } from "@ember/service"; -import { isNone } from "@ember/utils"; - -export default class LogComponent extends Component { - constructor() { - super(...arguments); - } - - @action - get encryptableLogs() { - return `/api/encryptables/${this.args.encryptable.get("id")}/logs`; - } -} diff --git a/frontend/app/components/encryptable/table-row.js b/frontend/app/components/encryptable/table-row.js new file mode 100644 index 000000000..aec3cbeb6 --- /dev/null +++ b/frontend/app/components/encryptable/table-row.js @@ -0,0 +1,98 @@ +import Component from "@glimmer/component"; +import { action } from "@ember/object"; +import { inject as service } from "@ember/service"; + +export default class TableRow extends Component { + @service intl; + @service notify; + @service store; + @service fetchService; + @service clipboardService; + + constructor() { + super(...arguments); + } + + validityTimes = [ + { label: "profile.api_users.options.one_min", value: 60 }, + { label: "profile.api_users.options.five_mins", value: 300 }, + { label: "profile.api_users.options.twelve_hours", value: 43200 }, + { label: "profile.api_users.options.infinite", value: 0 } + ]; + + get selectedValidFor() { + return this.validityTimes.find( + (t) => t.value === this.args.apiUser.validFor + ); + } + + @action + toggleApiUser(user) { + let httpMethod = user.locked ? "delete" : "post"; + return this.fetchService + .send(`/api/api_users/${user.id}/lock`, { method: httpMethod }) + .then(() => (user.locked = !user.locked)); + } + + @action + renewApiUser(user) { + /* eslint-disable no-undef */ + return this.fetchService + .send(`/api/api_users/${user.id}/token`, { + method: "get" + }) + .then((response) => { + response.json().then((json) => { + if (this.args.parent.setRenewMessage) + this.args.parent.setRenewMessage(json.info[0]); + }); + }); + /* eslint-enable no-undef */ + } + + @action + updateApiUser(user) { + if (user.hasDirtyAttributes) { + user.save(); + } + } + + @action + updateValidFor(user, validityTime) { + user.validFor = validityTime.value; + this.updateApiUser(user); + } + + @action + copyCcliLogin(apiUser) { + this.fetchService + .send(`/api/api_users/${apiUser.id}/token`, { method: "GET" }) + .then((response) => { + response.json().then((json) => { + this.clipboardService.copy( + `cry login ${btoa(`${json.username}:${json.token}`)}@${ + window.location.origin + }` + ); + this.notify.success( + this.intl.t("flashes.api.api-users.ccli_login.copied") + ); + }); + }); + } + + get isDefaultCcliUser() { + if (this.args.parent.defaultCcliApiUserId == null) return false; + + return ( + this.args.parent.defaultCcliApiUserId.toString() === this.args.apiUser.id + ); + } + + @action + setDefaultCcliUser(apiUser) { + if (this.isDefaultCcliUser) return; + + this.args.parent.setDefaultCcliUser(apiUser); + } +} diff --git a/frontend/app/components/encryptable/table.js b/frontend/app/components/encryptable/table.js new file mode 100644 index 000000000..46c9c76a8 --- /dev/null +++ b/frontend/app/components/encryptable/table.js @@ -0,0 +1,59 @@ +import Component from "@ember/component"; +import { action } from "@ember/object"; +import { inject as service } from "@ember/service"; +import { tracked } from "@glimmer/tracking"; +import ENV from "../../config/environment"; + +export default class Table extends Component { + @service store; + @service fetchService; + @service userService; + + @tracked + renewMessage; + + @tracked + defaultCcliApiUserId; + + constructor() { + super(...arguments); + this.defaultCcliApiUserId = ENV.currentUserDefaultCcliUserId; + } + + @action + createApiUser() { + this.store + .createRecord("user-api") + .save() + .then((apiUser) => { + this.apiUsers.addObject(apiUser); + }); + } + + setRenewMessage(message) { + this.renewMessage = message; + } + + setDefaultCcliUser(apiUser) { + let data = { + data: { attributes: { default_ccli_user_id: apiUser.id } } + }; + + this.fetchService + .send(`/api/admin/users/${ENV.currentUserId}`, { + method: "PATCH", + headers: { + "Content-Type": "application/json", + "X-CSRF-Token": ENV.CSRFToken + }, + body: JSON.stringify(data) + }) + .then(() => { + this.defaultCcliApiUserId = apiUser.id; + }); + } + + get isAllowedToUpdateDefaultCcliUser() { + return this.userService.isAdmin && ENV.currentUserGivenname !== "root"; + } +} diff --git a/frontend/app/templates/components/encryptable/log.hbs b/frontend/app/templates/components/encryptable/log.hbs deleted file mode 100644 index 08a046d12..000000000 --- a/frontend/app/templates/components/encryptable/log.hbs +++ /dev/null @@ -1,92 +0,0 @@ -{{#if this.isEncryptableEditing}} - -{{/if}} -{{#if this.isFileEntryCreating}} - -{{/if}} -
-
- - {{#unless @encryptable.isOseSecret}} -
- - delete - - - edit - -
- {{/unless}} -
-
-
-

{{t "encryptables.show.encryptable"}}: {{@encryptable.name}}

-
-
-

{{t "encryptables.show.created_at"}}: {{moment-format @encryptable.createdAt "DD.MM.YYYY hh:mm"}}

- / -

{{t "encryptables.show.last_update"}}: {{moment-format @encryptable.updatedAt "DD.MM.YYYY hh:mm"}}

-
-
- - {{#if @encryptable.description}} -
-
-

{{@encryptable.description}}

-
-
- {{/if}} -
- {{#if @encryptable.isOseSecret}} - {{t "encryptables.show.show_secret"}} -
- - - clip - -
- {{else}} -
- - - clip - -
- {{t "encryptables.show.show_password"}} -
- - - clip - -
- {{/if}} -
-
-
-

{{t "encryptables.show.attachments"}}

-
-
- - {{t "encryptables.show.add_attachment"}} - -
-
- - - - - - - - {{#each @encryptable.fileEntries as |fileEntry|}} - - {{/each}} - -
{{t "encryptables.show.file"}}{{t "description"}}{{t "actions"}}
-
diff --git a/frontend/app/templates/components/encryptable/table-row.hbs b/frontend/app/templates/components/encryptable/table-row.hbs new file mode 100644 index 000000000..23097c0eb --- /dev/null +++ b/frontend/app/templates/components/encryptable/table-row.hbs @@ -0,0 +1,44 @@ + + + + + +
+ {{#if @encryptable.validUntil}} + {{moment-format @encryptable.validUntil "DD.MM.YYYY hh:mm"}} + {{/if}} +
+ + + + {{t validityTime.label}} + + + + {{#if @encryptable.lastLoginAt}} + {{t "profile.api_users.at"}} {{moment-format @encryptable.lastLoginAt "DD.MM.YYYY hh:mm"}} + {{/if}} + {{#if @encryptable.lastLoginFrom}} +
{{t "profile.api_users.from"}} {{@encryptable.lastLoginFrom}} + {{/if}} + + + + + {{#if this.args.parent.isAllowedToUpdateDefaultCcliUser}} + + + + {{/if}} + + refresh + + remove + + + clipboard + + + + diff --git a/frontend/app/templates/components/encryptable/table.hbs b/frontend/app/templates/components/encryptable/table.hbs new file mode 100644 index 000000000..c236d536c --- /dev/null +++ b/frontend/app/templates/components/encryptable/table.hbs @@ -0,0 +1,39 @@ +{{#if this.renewMessage}} + +{{/if}} + +
+
+
+ +
+
+
+ +{{#if @encryptables}} + + + + + + + + + + {{#if this.isAllowedToUpdateDefaultCcliUser }} + + {{/if}} + + + + + {{#each @encryptables as |encryptable|}} + + {{/each}} + +
{{t "username"}} {{t "description" }} {{t "profile.api_users.valid_until"}} {{t "profile.api_users.valid_for"}} {{t "profile.api_users.last_login"}} {{t "profile.api_users.locked"}} {{t "profile.api_users.ccli_default_user"}} {{t "actions"}}
+{{else}} +

{{t "profile.api_users.no_api_users" }}

+{{/if}} diff --git a/frontend/app/templates/encryptables/show.hbs b/frontend/app/templates/encryptables/show.hbs index 810d876ef..0e86ade4d 100644 --- a/frontend/app/templates/encryptables/show.hbs +++ b/frontend/app/templates/encryptables/show.hbs @@ -9,11 +9,11 @@ - + - + From 5a064b12fbf2c8cebbffab12f8bb58d7bbbc5831 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Thu, 7 Apr 2022 16:19:58 +0200 Subject: [PATCH 29/79] add logs model to frontend --- frontend/app/models/encryptable.js | 1 + frontend/app/models/logs.js | 9 +++++++++ 2 files changed, 10 insertions(+) create mode 100644 frontend/app/models/logs.js diff --git a/frontend/app/models/encryptable.js b/frontend/app/models/encryptable.js index b6c13a113..e7dc38967 100644 --- a/frontend/app/models/encryptable.js +++ b/frontend/app/models/encryptable.js @@ -7,6 +7,7 @@ export default class Encryptable extends Model { @attr("string") updatedAt; @belongsTo("folder") folder; @hasMany("file-entry") fileEntries; + @hasMany("logs") logs; get isOseSecret() { return this.constructor.modelName === "encryptable-ose-secret"; diff --git a/frontend/app/models/logs.js b/frontend/app/models/logs.js new file mode 100644 index 000000000..7a8a7f99b --- /dev/null +++ b/frontend/app/models/logs.js @@ -0,0 +1,9 @@ +import Model, { attr, belongsTo } from "@ember-data/model"; + +export default class Logs extends Model { + @attr("string") event; + @attr("string") whodunnit; + @attr("string") createdAt; + @attr("encryptable") object; + @belongsTo("encryptable") encryptable; +} From ab7e6cba1a9d323161676c80eaee6eb43fc9e590 Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Thu, 7 Apr 2022 17:38:52 +0200 Subject: [PATCH 30/79] Fix log model not found error --- frontend/app/models/encryptable.js | 2 +- frontend/app/models/{logs.js => log.js} | 2 +- frontend/app/router.js | 4 ++ .../components/encryptable/table-row.hbs | 45 +------------------ .../components/encryptable/table.hbs | 24 +++------- 5 files changed, 13 insertions(+), 64 deletions(-) rename frontend/app/models/{logs.js => log.js} (83%) diff --git a/frontend/app/models/encryptable.js b/frontend/app/models/encryptable.js index e7dc38967..bb0d6c8b2 100644 --- a/frontend/app/models/encryptable.js +++ b/frontend/app/models/encryptable.js @@ -7,7 +7,7 @@ export default class Encryptable extends Model { @attr("string") updatedAt; @belongsTo("folder") folder; @hasMany("file-entry") fileEntries; - @hasMany("logs") logs; + @hasMany("log") logs; get isOseSecret() { return this.constructor.modelName === "encryptable-ose-secret"; diff --git a/frontend/app/models/logs.js b/frontend/app/models/log.js similarity index 83% rename from frontend/app/models/logs.js rename to frontend/app/models/log.js index 7a8a7f99b..300899779 100644 --- a/frontend/app/models/logs.js +++ b/frontend/app/models/log.js @@ -1,6 +1,6 @@ import Model, { attr, belongsTo } from "@ember-data/model"; -export default class Logs extends Model { +export default class Log extends Model { @attr("string") event; @attr("string") whodunnit; @attr("string") createdAt; diff --git a/frontend/app/router.js b/frontend/app/router.js index f75906592..d2c3be903 100644 --- a/frontend/app/router.js +++ b/frontend/app/router.js @@ -11,6 +11,10 @@ Router.map(function () { this.route("new"); this.route("edit", { path: "/edit/:id" }); this.route("show", { path: "/:id" }); + this.route( + "logs", + { path: "/:encryptable_id/logs" }, + ); this.route( "file-entries", { path: "/:encryptable_id/file-entries" }, diff --git a/frontend/app/templates/components/encryptable/table-row.hbs b/frontend/app/templates/components/encryptable/table-row.hbs index 23097c0eb..cc9c1a3f1 100644 --- a/frontend/app/templates/components/encryptable/table-row.hbs +++ b/frontend/app/templates/components/encryptable/table-row.hbs @@ -1,44 +1,3 @@ - - - - - -
- {{#if @encryptable.validUntil}} - {{moment-format @encryptable.validUntil "DD.MM.YYYY hh:mm"}} - {{/if}} -
- - - - {{t validityTime.label}} - - - - {{#if @encryptable.lastLoginAt}} - {{t "profile.api_users.at"}} {{moment-format @encryptable.lastLoginAt "DD.MM.YYYY hh:mm"}} - {{/if}} - {{#if @encryptable.lastLoginFrom}} -
{{t "profile.api_users.from"}} {{@encryptable.lastLoginFrom}} - {{/if}} - - - - - {{#if this.args.parent.isAllowedToUpdateDefaultCcliUser}} - - - - {{/if}} - - refresh - - remove - - - clipboard - - - + + @logs diff --git a/frontend/app/templates/components/encryptable/table.hbs b/frontend/app/templates/components/encryptable/table.hbs index c236d536c..c37f6e3e3 100644 --- a/frontend/app/templates/components/encryptable/table.hbs +++ b/frontend/app/templates/components/encryptable/table.hbs @@ -4,33 +4,19 @@ {{/if}} -
-
-
- -
-
-
- -{{#if @encryptables}} +{{#if @encryptable}} - - - - - {{#if this.isAllowedToUpdateDefaultCcliUser }} - - {{/if}} - + - {{#each @encryptables as |encryptable|}} - + {{@encryptable.logs}} + {{#each @encryptable.log as |l|}} + {{/each}}
{{t "username"}} {{t "description" }} {{t "profile.api_users.valid_until"}} {{t "profile.api_users.valid_for"}} {{t "profile.api_users.last_login"}} {{t "profile.api_users.locked"}} {{t "profile.api_users.ccli_default_user"}} {{t "actions"}} {{t "actions" }}
From 00dd3e15d6bfac94e9dc190a60b5c33c19b3039b Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Fri, 8 Apr 2022 12:18:19 +0200 Subject: [PATCH 31/79] implement table-row.hbs, work on getting logs to frontend --- .../app/components/encryptable/table-row.js | 93 ------------------- frontend/app/components/encryptable/table.js | 49 ++-------- .../components/encryptable/table-row.hbs | 4 +- .../components/encryptable/table.hbs | 7 +- 4 files changed, 10 insertions(+), 143 deletions(-) diff --git a/frontend/app/components/encryptable/table-row.js b/frontend/app/components/encryptable/table-row.js index aec3cbeb6..449f4900e 100644 --- a/frontend/app/components/encryptable/table-row.js +++ b/frontend/app/components/encryptable/table-row.js @@ -1,98 +1,5 @@ import Component from "@glimmer/component"; -import { action } from "@ember/object"; -import { inject as service } from "@ember/service"; export default class TableRow extends Component { - @service intl; - @service notify; - @service store; - @service fetchService; - @service clipboardService; - constructor() { - super(...arguments); - } - - validityTimes = [ - { label: "profile.api_users.options.one_min", value: 60 }, - { label: "profile.api_users.options.five_mins", value: 300 }, - { label: "profile.api_users.options.twelve_hours", value: 43200 }, - { label: "profile.api_users.options.infinite", value: 0 } - ]; - - get selectedValidFor() { - return this.validityTimes.find( - (t) => t.value === this.args.apiUser.validFor - ); - } - - @action - toggleApiUser(user) { - let httpMethod = user.locked ? "delete" : "post"; - return this.fetchService - .send(`/api/api_users/${user.id}/lock`, { method: httpMethod }) - .then(() => (user.locked = !user.locked)); - } - - @action - renewApiUser(user) { - /* eslint-disable no-undef */ - return this.fetchService - .send(`/api/api_users/${user.id}/token`, { - method: "get" - }) - .then((response) => { - response.json().then((json) => { - if (this.args.parent.setRenewMessage) - this.args.parent.setRenewMessage(json.info[0]); - }); - }); - /* eslint-enable no-undef */ - } - - @action - updateApiUser(user) { - if (user.hasDirtyAttributes) { - user.save(); - } - } - - @action - updateValidFor(user, validityTime) { - user.validFor = validityTime.value; - this.updateApiUser(user); - } - - @action - copyCcliLogin(apiUser) { - this.fetchService - .send(`/api/api_users/${apiUser.id}/token`, { method: "GET" }) - .then((response) => { - response.json().then((json) => { - this.clipboardService.copy( - `cry login ${btoa(`${json.username}:${json.token}`)}@${ - window.location.origin - }` - ); - this.notify.success( - this.intl.t("flashes.api.api-users.ccli_login.copied") - ); - }); - }); - } - - get isDefaultCcliUser() { - if (this.args.parent.defaultCcliApiUserId == null) return false; - - return ( - this.args.parent.defaultCcliApiUserId.toString() === this.args.apiUser.id - ); - } - - @action - setDefaultCcliUser(apiUser) { - if (this.isDefaultCcliUser) return; - - this.args.parent.setDefaultCcliUser(apiUser); - } } diff --git a/frontend/app/components/encryptable/table.js b/frontend/app/components/encryptable/table.js index 46c9c76a8..38502e04d 100644 --- a/frontend/app/components/encryptable/table.js +++ b/frontend/app/components/encryptable/table.js @@ -6,54 +6,17 @@ import ENV from "../../config/environment"; export default class Table extends Component { @service store; - @service fetchService; - @service userService; - - @tracked - renewMessage; - - @tracked - defaultCcliApiUserId; + @service router; + @service intl; + @service notify; constructor() { super(...arguments); - this.defaultCcliApiUserId = ENV.currentUserDefaultCcliUserId; - } - - @action - createApiUser() { - this.store - .createRecord("user-api") - .save() - .then((apiUser) => { - this.apiUsers.addObject(apiUser); - }); - } - - setRenewMessage(message) { - this.renewMessage = message; - } - setDefaultCcliUser(apiUser) { - let data = { - data: { attributes: { default_ccli_user_id: apiUser.id } } - }; - - this.fetchService - .send(`/api/admin/users/${ENV.currentUserId}`, { - method: "PATCH", - headers: { - "Content-Type": "application/json", - "X-CSRF-Token": ENV.CSRFToken - }, - body: JSON.stringify(data) - }) - .then(() => { - this.defaultCcliApiUserId = apiUser.id; + this.store.query("logs", { + encryptableId: this.args.encryptable.id }); - } - get isAllowedToUpdateDefaultCcliUser() { - return this.userService.isAdmin && ENV.currentUserGivenname !== "root"; + window.scrollTo(0, 0); } } diff --git a/frontend/app/templates/components/encryptable/table-row.hbs b/frontend/app/templates/components/encryptable/table-row.hbs index cc9c1a3f1..1039bde39 100644 --- a/frontend/app/templates/components/encryptable/table-row.hbs +++ b/frontend/app/templates/components/encryptable/table-row.hbs @@ -1,3 +1,5 @@ - @logs + >{{@logs.whodunnit}} + {{@logs.created_at}} + {{@logs.action}} diff --git a/frontend/app/templates/components/encryptable/table.hbs b/frontend/app/templates/components/encryptable/table.hbs index c37f6e3e3..17b25ffe7 100644 --- a/frontend/app/templates/components/encryptable/table.hbs +++ b/frontend/app/templates/components/encryptable/table.hbs @@ -4,7 +4,6 @@ {{/if}} -{{#if @encryptable}} @@ -14,12 +13,8 @@ - {{@encryptable.logs}} - {{#each @encryptable.log as |l|}} + {{#each @encryptable.logs as |l|}} {{/each}}
-{{else}} -

{{t "profile.api_users.no_api_users" }}

-{{/if}} From 477125a72c2e7205363a35490d88942850db328c Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Fri, 8 Apr 2022 12:49:01 +0200 Subject: [PATCH 32/79] Add adapter and fix component --- frontend/app/adapters/log.js | 21 +++++++++++++++++++ .../app/components/encryptable/table-row.js | 6 ++++-- frontend/app/components/encryptable/table.js | 7 +++---- frontend/app/templates/encryptables/show.hbs | 2 +- 4 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 frontend/app/adapters/log.js diff --git a/frontend/app/adapters/log.js b/frontend/app/adapters/log.js new file mode 100644 index 000000000..e17ce2532 --- /dev/null +++ b/frontend/app/adapters/log.js @@ -0,0 +1,21 @@ +import ApplicationAdapter from "./application"; + +export default ApplicationAdapter.extend({ + namespace: "api/encryptables", + + pathForType() { + return "logs"; + }, + + urlForQuery(query, modelName) { + if (query.encryptableId) { + let url = `/${this.namespace}/${ + query.encryptableId + }/logs`; + + delete query.encryptableId; + return url; + } + return super.urlForQuery(query, modelName); + }, +}); diff --git a/frontend/app/components/encryptable/table-row.js b/frontend/app/components/encryptable/table-row.js index 449f4900e..cc7d90fd3 100644 --- a/frontend/app/components/encryptable/table-row.js +++ b/frontend/app/components/encryptable/table-row.js @@ -1,5 +1,7 @@ import Component from "@glimmer/component"; -export default class TableRow extends Component { - +export default class TableRowComponen extends Component { + constructor() { + super(...arguments); + } } diff --git a/frontend/app/components/encryptable/table.js b/frontend/app/components/encryptable/table.js index 38502e04d..1a8aeb6b8 100644 --- a/frontend/app/components/encryptable/table.js +++ b/frontend/app/components/encryptable/table.js @@ -1,10 +1,9 @@ -import Component from "@ember/component"; +import Component from "@glimmer/component"; import { action } from "@ember/object"; import { inject as service } from "@ember/service"; import { tracked } from "@glimmer/tracking"; -import ENV from "../../config/environment"; -export default class Table extends Component { +export default class TableComponent extends Component { @service store; @service router; @service intl; @@ -13,7 +12,7 @@ export default class Table extends Component { constructor() { super(...arguments); - this.store.query("logs", { + this.store.query("log", { encryptableId: this.args.encryptable.id }); diff --git a/frontend/app/templates/encryptables/show.hbs b/frontend/app/templates/encryptables/show.hbs index 0e86ade4d..913dd0987 100644 --- a/frontend/app/templates/encryptables/show.hbs +++ b/frontend/app/templates/encryptables/show.hbs @@ -13,7 +13,7 @@ - + From 5b61e297c6803b4a32fa356e7689d57998945505 Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Fri, 8 Apr 2022 14:36:36 +0200 Subject: [PATCH 33/79] Change log adapter name --- frontend/app/templates/components/encryptable/table-row.hbs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/app/templates/components/encryptable/table-row.hbs b/frontend/app/templates/components/encryptable/table-row.hbs index 1039bde39..006500f21 100644 --- a/frontend/app/templates/components/encryptable/table-row.hbs +++ b/frontend/app/templates/components/encryptable/table-row.hbs @@ -1,5 +1,5 @@ - >{{@logs.whodunnit}} + {{@logs.whodunnit}} {{@logs.created_at}} {{@logs.action}} From 8508e8a137b4c47a9cba364b3e5c0a782450c4ce Mon Sep 17 00:00:00 2001 From: Robin Steiner Date: Fri, 8 Apr 2022 15:06:11 +0200 Subject: [PATCH 34/79] Fix backend to frontend serialization --- app/serializers/logs_serializer.rb | 5 +++++ frontend/app/adapters/{log.js => paper-trail-version.js} | 0 frontend/app/components/encryptable/table-row.js | 2 +- frontend/app/components/encryptable/table.js | 2 +- frontend/app/models/encryptable.js | 2 +- frontend/app/models/{log.js => paper-trail-version.js} | 4 ++-- frontend/app/router.js | 5 +---- frontend/app/templates/components/encryptable/table.hbs | 2 +- 8 files changed, 12 insertions(+), 10 deletions(-) rename frontend/app/adapters/{log.js => paper-trail-version.js} (100%) rename frontend/app/models/{log.js => paper-trail-version.js} (71%) diff --git a/app/serializers/logs_serializer.rb b/app/serializers/logs_serializer.rb index 75c184f2a..57974c37c 100644 --- a/app/serializers/logs_serializer.rb +++ b/app/serializers/logs_serializer.rb @@ -3,4 +3,9 @@ class LogsSerializer < ApplicationSerializer attributes :id, :item_type, :event, :whodunnit, :object, :created_at + belongs_to :encryptable + + def encryptable + object.item + end end diff --git a/frontend/app/adapters/log.js b/frontend/app/adapters/paper-trail-version.js similarity index 100% rename from frontend/app/adapters/log.js rename to frontend/app/adapters/paper-trail-version.js diff --git a/frontend/app/components/encryptable/table-row.js b/frontend/app/components/encryptable/table-row.js index cc7d90fd3..8847ef24c 100644 --- a/frontend/app/components/encryptable/table-row.js +++ b/frontend/app/components/encryptable/table-row.js @@ -1,6 +1,6 @@ import Component from "@glimmer/component"; -export default class TableRowComponen extends Component { +export default class TableRowComponent extends Component { constructor() { super(...arguments); } diff --git a/frontend/app/components/encryptable/table.js b/frontend/app/components/encryptable/table.js index 1a8aeb6b8..bd86c3760 100644 --- a/frontend/app/components/encryptable/table.js +++ b/frontend/app/components/encryptable/table.js @@ -12,7 +12,7 @@ export default class TableComponent extends Component { constructor() { super(...arguments); - this.store.query("log", { + this.store.query("paper-trail-version", { encryptableId: this.args.encryptable.id }); diff --git a/frontend/app/models/encryptable.js b/frontend/app/models/encryptable.js index bb0d6c8b2..57827e2a6 100644 --- a/frontend/app/models/encryptable.js +++ b/frontend/app/models/encryptable.js @@ -7,7 +7,7 @@ export default class Encryptable extends Model { @attr("string") updatedAt; @belongsTo("folder") folder; @hasMany("file-entry") fileEntries; - @hasMany("log") logs; + @hasMany("paper-trail-version") paperTrailVersions; get isOseSecret() { return this.constructor.modelName === "encryptable-ose-secret"; diff --git a/frontend/app/models/log.js b/frontend/app/models/paper-trail-version.js similarity index 71% rename from frontend/app/models/log.js rename to frontend/app/models/paper-trail-version.js index 300899779..70046b494 100644 --- a/frontend/app/models/log.js +++ b/frontend/app/models/paper-trail-version.js @@ -1,9 +1,9 @@ import Model, { attr, belongsTo } from "@ember-data/model"; -export default class Log extends Model { +export default class PaperTrailVersion extends Model { @attr("string") event; @attr("string") whodunnit; @attr("string") createdAt; - @attr("encryptable") object; + @attr() object; @belongsTo("encryptable") encryptable; } diff --git a/frontend/app/router.js b/frontend/app/router.js index d2c3be903..eabcf5eba 100644 --- a/frontend/app/router.js +++ b/frontend/app/router.js @@ -11,10 +11,7 @@ Router.map(function () { this.route("new"); this.route("edit", { path: "/edit/:id" }); this.route("show", { path: "/:id" }); - this.route( - "logs", - { path: "/:encryptable_id/logs" }, - ); + this.route("logs", { path: "/:encryptable_id/logs" }); this.route( "file-entries", { path: "/:encryptable_id/file-entries" }, diff --git a/frontend/app/templates/components/encryptable/table.hbs b/frontend/app/templates/components/encryptable/table.hbs index 17b25ffe7..f373eb941 100644 --- a/frontend/app/templates/components/encryptable/table.hbs +++ b/frontend/app/templates/components/encryptable/table.hbs @@ -13,7 +13,7 @@ - {{#each @encryptable.logs as |l|}} + {{#each @encryptable.paperTrailVersions as |l|}} {{/each}} From 1b63d3a72818083dd9ae6f61cb1bb6e00c092ebc Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Fri, 8 Apr 2022 15:54:55 +0200 Subject: [PATCH 35/79] Fix table and add update to papertrail --- app/models/encryptable.rb | 2 +- frontend/app/templates/components/encryptable/table-row.hbs | 6 +++--- frontend/app/templates/components/encryptable/table.hbs | 4 ++-- frontend/app/templates/encryptables/show.hbs | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/models/encryptable.rb b/app/models/encryptable.rb index 03d97e1b5..d45e48aa9 100644 --- a/app/models/encryptable.rb +++ b/app/models/encryptable.rb @@ -17,7 +17,7 @@ class Encryptable < ApplicationRecord - has_paper_trail on: [:touch], ignore: [:tag, :type, :encrypted_data] + has_paper_trail on: [:touch, :update], ignore: [:tag, :type, :encrypted_data] serialize :encrypted_data, ::EncryptedData diff --git a/frontend/app/templates/components/encryptable/table-row.hbs b/frontend/app/templates/components/encryptable/table-row.hbs index 006500f21..ca5502d9f 100644 --- a/frontend/app/templates/components/encryptable/table-row.hbs +++ b/frontend/app/templates/components/encryptable/table-row.hbs @@ -1,5 +1,5 @@ - {{@logs.whodunnit}} - {{@logs.created_at}} - {{@logs.action}} + {{@paperTrailVersion.whodunnit}} + {{@paperTrailVersion.created_at}} + {{@paperTrailVersion.action}} diff --git a/frontend/app/templates/components/encryptable/table.hbs b/frontend/app/templates/components/encryptable/table.hbs index f373eb941..736f0c379 100644 --- a/frontend/app/templates/components/encryptable/table.hbs +++ b/frontend/app/templates/components/encryptable/table.hbs @@ -13,8 +13,8 @@ - {{#each @encryptable.paperTrailVersions as |l|}} - + {{#each @encryptable.paperTrailVersions as |paperTrailVersion|}} + {{/each}} diff --git a/frontend/app/templates/encryptables/show.hbs b/frontend/app/templates/encryptables/show.hbs index 913dd0987..1439fa537 100644 --- a/frontend/app/templates/encryptables/show.hbs +++ b/frontend/app/templates/encryptables/show.hbs @@ -9,10 +9,10 @@ - + - + From 04d14245fd84767f85cbfc3e2bc90b29edde736e Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Fri, 8 Apr 2022 17:36:04 +0200 Subject: [PATCH 36/79] fix logs table, make prettier --- frontend/app/adapters/paper-trail-version.js | 6 ++-- frontend/app/components/encryptable/table.js | 6 ++-- .../templates/components/encryptable/show.hbs | 10 ++++-- .../components/encryptable/table-row.hbs | 4 +-- .../components/encryptable/table.hbs | 34 ++++++++----------- frontend/app/templates/encryptables/show.hbs | 22 ++---------- 6 files changed, 30 insertions(+), 52 deletions(-) diff --git a/frontend/app/adapters/paper-trail-version.js b/frontend/app/adapters/paper-trail-version.js index e17ce2532..20ad8b2d6 100644 --- a/frontend/app/adapters/paper-trail-version.js +++ b/frontend/app/adapters/paper-trail-version.js @@ -9,13 +9,11 @@ export default ApplicationAdapter.extend({ urlForQuery(query, modelName) { if (query.encryptableId) { - let url = `/${this.namespace}/${ - query.encryptableId - }/logs`; + let url = `/${this.namespace}/${ query.encryptableId }/logs`; delete query.encryptableId; return url; } return super.urlForQuery(query, modelName); - }, + } }); diff --git a/frontend/app/components/encryptable/table.js b/frontend/app/components/encryptable/table.js index bd86c3760..bb2f09c3e 100644 --- a/frontend/app/components/encryptable/table.js +++ b/frontend/app/components/encryptable/table.js @@ -1,7 +1,5 @@ import Component from "@glimmer/component"; -import { action } from "@ember/object"; import { inject as service } from "@ember/service"; -import { tracked } from "@glimmer/tracking"; export default class TableComponent extends Component { @service store; @@ -13,8 +11,8 @@ export default class TableComponent extends Component { super(...arguments); this.store.query("paper-trail-version", { - encryptableId: this.args.encryptable.id - }); + encryptableId: this.args.encryptable.id + }); window.scrollTo(0, 0); } diff --git a/frontend/app/templates/components/encryptable/show.hbs b/frontend/app/templates/components/encryptable/show.hbs index 08a046d12..f9c0e799a 100644 --- a/frontend/app/templates/components/encryptable/show.hbs +++ b/frontend/app/templates/components/encryptable/show.hbs @@ -34,7 +34,8 @@

{{t "encryptables.show.last_update"}}: {{moment-format @encryptable.updatedAt "DD.MM.YYYY hh:mm"}}

- + + {{#if @encryptable.description}}
@@ -89,4 +90,9 @@ {{/each}} -
+ + + + + +
\ No newline at end of file diff --git a/frontend/app/templates/components/encryptable/table-row.hbs b/frontend/app/templates/components/encryptable/table-row.hbs index ca5502d9f..8c21ee5be 100644 --- a/frontend/app/templates/components/encryptable/table-row.hbs +++ b/frontend/app/templates/components/encryptable/table-row.hbs @@ -1,5 +1,5 @@ + {{@paperTrailVersion.event}} {{@paperTrailVersion.whodunnit}} - {{@paperTrailVersion.created_at}} - {{@paperTrailVersion.action}} + {{moment-format @paperTrailVersion.createdAt "DD.MM.YYYY hh:mm"}} diff --git a/frontend/app/templates/components/encryptable/table.hbs b/frontend/app/templates/components/encryptable/table.hbs index 736f0c379..82597f011 100644 --- a/frontend/app/templates/components/encryptable/table.hbs +++ b/frontend/app/templates/components/encryptable/table.hbs @@ -1,20 +1,14 @@ -{{#if this.renewMessage}} - -{{/if}} - - - - - - - - - - - {{#each @encryptable.paperTrailVersions as |paperTrailVersion|}} - - {{/each}} - -
{{t "username"}} {{t "description" }} {{t "actions" }}
+ + + + + + + + + + {{#each @encryptable.paperTrailVersions as |paperTrailVersion|}} + + {{/each}} + +
{{t "actions"}} {{t "username" }} {{t "encryptables.show.created_at" }}
diff --git a/frontend/app/templates/encryptables/show.hbs b/frontend/app/templates/encryptables/show.hbs index 1439fa537..bef74a099 100644 --- a/frontend/app/templates/encryptables/show.hbs +++ b/frontend/app/templates/encryptables/show.hbs @@ -1,20 +1,2 @@ -
-
-
-
-

Encryptable

-
-
- -
-
- - - - - - - - -
-
+ + From cc89947e4847da1e7ef33dc45f5f7e3c65581d06 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Mon, 11 Apr 2022 14:14:47 +0200 Subject: [PATCH 37/79] log username instead of userid --- app/controllers/application_controller.rb | 4 ++++ .../templates/components/encryptable/show.hbs | 24 ++++++++++--------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index faf1e4122..f04bae608 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -50,6 +50,10 @@ def model_params params.require(model_identifier).permit(permitted_attrs) end + def user_for_paper_trail + current_user.username + end + class << self def model_identifier @model_identifier ||= model_class.model_name.param_key diff --git a/frontend/app/templates/components/encryptable/show.hbs b/frontend/app/templates/components/encryptable/show.hbs index f9c0e799a..9118290c3 100644 --- a/frontend/app/templates/components/encryptable/show.hbs +++ b/frontend/app/templates/components/encryptable/show.hbs @@ -36,15 +36,15 @@ - {{#if @encryptable.description}} -
-
+ {{#if @encryptable.description}} +
+

{{@encryptable.description}}

- {{/if}} -
- {{#if @encryptable.isOseSecret}} + {{/if}} +
+ {{#if @encryptable.isOseSecret}} {{t "encryptables.show.show_secret"}}
@@ -68,7 +68,7 @@
{{/if}}
-
+

{{t "encryptables.show.attachments"}}

@@ -78,7 +78,7 @@
- +
@@ -91,8 +91,10 @@
{{t "encryptables.show.file"}}
- - - + +
+ +
+
\ No newline at end of file From 133b5a5ecd9112324016feaf0ba243e3f9fc75c1 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Tue, 12 Apr 2022 16:48:56 +0200 Subject: [PATCH 38/79] log view access --- app/controllers/api/encryptables_controller.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/controllers/api/encryptables_controller.rb b/app/controllers/api/encryptables_controller.rb index f875a5257..9612a77bd 100644 --- a/app/controllers/api/encryptables_controller.rb +++ b/app/controllers/api/encryptables_controller.rb @@ -19,6 +19,8 @@ def index(options = {}) # GET /api/encryptables/:id def show authorize encryptable + encryptable.paper_trail_event = 'viewed' + encryptable.touch encryptable.decrypt(decrypted_team_password(team)) render_entry end From cb5757160602bdd7da9503f6b4af504fb501d9ac Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Tue, 12 Apr 2022 19:48:35 +0200 Subject: [PATCH 39/79] add translations for log entry event --- frontend/app/templates/components/encryptable/table-row.hbs | 2 +- frontend/translations/ch_be.yml | 3 +++ frontend/translations/de.yml | 4 +++- frontend/translations/en.yml | 3 +++ frontend/translations/fr.yml | 4 ++++ 5 files changed, 14 insertions(+), 2 deletions(-) diff --git a/frontend/app/templates/components/encryptable/table-row.hbs b/frontend/app/templates/components/encryptable/table-row.hbs index 8c21ee5be..73dc6eb86 100644 --- a/frontend/app/templates/components/encryptable/table-row.hbs +++ b/frontend/app/templates/components/encryptable/table-row.hbs @@ -1,5 +1,5 @@ - {{@paperTrailVersion.event}} + {{t (concat "encryptables.log." @paperTrailVersion.event)}} {{@paperTrailVersion.whodunnit}} {{moment-format @paperTrailVersion.createdAt "DD.MM.YYYY hh:mm"}} diff --git a/frontend/translations/ch_be.yml b/frontend/translations/ch_be.yml index 5dac51e3b..4699146fb 100644 --- a/frontend/translations/ch_be.yml +++ b/frontend/translations/ch_be.yml @@ -131,6 +131,9 @@ ch_be: add_attachment: Anhang hinzufüegä new: title: Nöie Encryptable + log: + update: Bearbeitet + viewed: Aagluegt admin: title: Admin diff --git a/frontend/translations/de.yml b/frontend/translations/de.yml index 6aa451cdf..47e3068b4 100644 --- a/frontend/translations/de.yml +++ b/frontend/translations/de.yml @@ -100,7 +100,9 @@ de: add_attachment: Anhang hinzufügen new: title: Neues Encryptable - + log: + update: Bearbeitet + viewed: Angesehen admin: title: "Admin" maintenance_tasks: diff --git a/frontend/translations/en.yml b/frontend/translations/en.yml index 087aa4715..412afb40f 100644 --- a/frontend/translations/en.yml +++ b/frontend/translations/en.yml @@ -98,6 +98,9 @@ en: add_attachment: Add Attachment new: title: New Encryptable + log: + update: edited + viewed: viewed admin: title: Admin diff --git a/frontend/translations/fr.yml b/frontend/translations/fr.yml index e02ef38a3..2678ef4e0 100644 --- a/frontend/translations/fr.yml +++ b/frontend/translations/fr.yml @@ -100,6 +100,10 @@ fr: add_attachment: Ajouter un attachement new: title: Nouveau compte + log: + update: modifié + viewed: regardé + admin: title: "Admin" From d53703665ad2ac18e08593a36ddb72a8eb533477 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Sat, 23 Apr 2022 23:53:11 +0200 Subject: [PATCH 40/79] reload logs when switching tabs, fix linting errors --- frontend/app/adapters/paper-trail-version.js | 2 +- frontend/app/components/encryptable/show.js | 11 ++ frontend/app/components/encryptable/table.js | 6 - .../templates/components/encryptable/show.hbs | 128 +++++++++--------- .../components/encryptable/table.hbs | 2 +- 5 files changed, 79 insertions(+), 70 deletions(-) diff --git a/frontend/app/adapters/paper-trail-version.js b/frontend/app/adapters/paper-trail-version.js index 20ad8b2d6..a0dcb368e 100644 --- a/frontend/app/adapters/paper-trail-version.js +++ b/frontend/app/adapters/paper-trail-version.js @@ -9,7 +9,7 @@ export default ApplicationAdapter.extend({ urlForQuery(query, modelName) { if (query.encryptableId) { - let url = `/${this.namespace}/${ query.encryptableId }/logs`; + let url = `/${this.namespace}/${query.encryptableId}/logs`; delete query.encryptableId; return url; diff --git a/frontend/app/components/encryptable/show.js b/frontend/app/components/encryptable/show.js index 208f94cf3..9f211ecac 100644 --- a/frontend/app/components/encryptable/show.js +++ b/frontend/app/components/encryptable/show.js @@ -15,6 +15,7 @@ export default class ShowComponent extends Component { this.store.query("file-entry", { encryptableId: this.args.encryptable.id }); + this.getLogs(); window.scrollTo(0, 0); } @@ -61,4 +62,14 @@ export default class ShowComponent extends Component { onCopied(attribute) { this.notify.info(this.intl.t(`flashes.encryptables.${attribute}_copied`)); } + + @action + getLogs() { + this.store + .query("paper-trail-version", { + encryptableId: this.args.encryptable.id + }) + .sortBy("created_at") + .reverse(); + } } diff --git a/frontend/app/components/encryptable/table.js b/frontend/app/components/encryptable/table.js index bb2f09c3e..b028d289f 100644 --- a/frontend/app/components/encryptable/table.js +++ b/frontend/app/components/encryptable/table.js @@ -9,11 +9,5 @@ export default class TableComponent extends Component { constructor() { super(...arguments); - - this.store.query("paper-trail-version", { - encryptableId: this.args.encryptable.id - }); - - window.scrollTo(0, 0); } } diff --git a/frontend/app/templates/components/encryptable/show.hbs b/frontend/app/templates/components/encryptable/show.hbs index 9118290c3..af534efd3 100644 --- a/frontend/app/templates/components/encryptable/show.hbs +++ b/frontend/app/templates/components/encryptable/show.hbs @@ -34,67 +34,71 @@

{{t "encryptables.show.last_update"}}: {{moment-format @encryptable.updatedAt "DD.MM.YYYY hh:mm"}}

- - - {{#if @encryptable.description}} -
-
-

{{@encryptable.description}}

-
-
- {{/if}} -
- {{#if @encryptable.isOseSecret}} - {{t "encryptables.show.show_secret"}} -
- - - clip - -
- {{else}} -
- - - clip - -
- {{t "encryptables.show.show_password"}} -
- - - clip - -
- {{/if}} +
+ + + {{#if @encryptable.description}} +
+
+

{{@encryptable.description}}

+
+
+ {{/if}} +
+ {{#if @encryptable.isOseSecret}} + {{t "encryptables.show.show_secret"}} +
+ + + clip + +
+ {{else}} +
+ + + clip + +
+ {{t "encryptables.show.show_password"}} +
+ + + clip + +
+ {{/if}} +
+
+
+

{{t "encryptables.show.attachments"}}

+
+
+ + {{t "encryptables.show.add_attachment"}} + +
+
+
+ + + + + + + + {{#each @encryptable.fileEntries as |fileEntry|}} + + {{/each}} + +
{{t "encryptables.show.file"}}{{t "description"}}{{t "actions"}}
+
+
+ +
+ +
+
+
-
-
-

{{t "encryptables.show.attachments"}}

-
-
- - {{t "encryptables.show.add_attachment"}} - -
-
- - - - - - - - {{#each @encryptable.fileEntries as |fileEntry|}} - - {{/each}} - -
{{t "encryptables.show.file"}}{{t "description"}}{{t "actions"}}
- - -
- -
-
-
\ No newline at end of file diff --git a/frontend/app/templates/components/encryptable/table.hbs b/frontend/app/templates/components/encryptable/table.hbs index 82597f011..6afe071e3 100644 --- a/frontend/app/templates/components/encryptable/table.hbs +++ b/frontend/app/templates/components/encryptable/table.hbs @@ -7,7 +7,7 @@ - {{#each @encryptable.paperTrailVersions as |paperTrailVersion|}} + {{#each @logs as |paperTrailVersion|}} {{/each}} From b88a00edf995f8d15ed9e9fdd4617cf2a7bf64f2 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Sun, 24 Apr 2022 00:07:22 +0200 Subject: [PATCH 41/79] fix rubocop errors --- spec/controllers/api/encryptables_controller_spec.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/controllers/api/encryptables_controller_spec.rb b/spec/controllers/api/encryptables_controller_spec.rb index d3e47e3b5..aad389076 100644 --- a/spec/controllers/api/encryptables_controller_spec.rb +++ b/spec/controllers/api/encryptables_controller_spec.rb @@ -573,7 +573,9 @@ end.to change { Encryptable::OseSecret.count }.by(1) expect(response).to have_http_status(201) - end context 'DELETE destroy' do + end + + context 'DELETE destroy' do it 'cant destroy an encryptable if not in team' do login_as(:alice) From 1417b36a96e22b56ab5e3607d443bca20a77c0ef Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Mon, 25 Apr 2022 15:23:22 +0200 Subject: [PATCH 42/79] Rebase with master --- frontend/app/templates/components/encryptable/show.hbs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/app/templates/components/encryptable/show.hbs b/frontend/app/templates/components/encryptable/show.hbs index af534efd3..77d35e3b8 100644 --- a/frontend/app/templates/components/encryptable/show.hbs +++ b/frontend/app/templates/components/encryptable/show.hbs @@ -101,4 +101,4 @@
- \ No newline at end of file + From 5c1ac4b891b3dad141233b6b04b32e0823f222ff Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Tue, 26 Apr 2022 14:50:59 +0200 Subject: [PATCH 43/79] Add encryptable specs --- .../components/encryptable/show-test.js | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 frontend/tests/integration/components/encryptable/show-test.js diff --git a/frontend/tests/integration/components/encryptable/show-test.js b/frontend/tests/integration/components/encryptable/show-test.js new file mode 100644 index 000000000..6acd441f8 --- /dev/null +++ b/frontend/tests/integration/components/encryptable/show-test.js @@ -0,0 +1,25 @@ +import { module, test } from "qunit"; +import { setupRenderingTest } from "ember-qunit"; +import { render } from "@ember/test-helpers"; +import { hbs } from "ember-cli-htmlbars"; + +module("Integration | Component | encryptable/show", function(hooks) { + setupRenderingTest(hooks); + + test("it renders with data", async function (assert) { + this.set("encryptable", { + name: "Foo", + description: "Bla", + folder: { + get() { + return 1; + } + } + }); + + await render(hbs``); + + let text = this.element.textContent.trim(); + assert.ok(text.includes("Foo")); + }); +}); From df3a9c97ad3df7ec96616be837d35a9ba04b2c1d Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Wed, 27 Apr 2022 22:42:10 +0200 Subject: [PATCH 44/79] add username and userid to log --- app/controllers/application_controller.rb | 4 ---- app/serializers/logs_serializer.rb | 10 +++++++++- frontend/app/models/paper-trail-version.js | 3 ++- .../app/templates/components/encryptable/table-row.hbs | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f04bae608..faf1e4122 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -50,10 +50,6 @@ def model_params params.require(model_identifier).permit(permitted_attrs) end - def user_for_paper_trail - current_user.username - end - class << self def model_identifier @model_identifier ||= model_class.model_name.param_key diff --git a/app/serializers/logs_serializer.rb b/app/serializers/logs_serializer.rb index 57974c37c..fc70044d0 100644 --- a/app/serializers/logs_serializer.rb +++ b/app/serializers/logs_serializer.rb @@ -1,10 +1,18 @@ # frozen_string_literal: true class LogsSerializer < ApplicationSerializer - attributes :id, :item_type, :event, :whodunnit, :object, :created_at + attributes :id, :item_type, :event, :user_id, :username, :object, :created_at belongs_to :encryptable + def user_id + object.whodunnit + end + + def username + User.find_by(id: user_id).username + end + def encryptable object.item end diff --git a/frontend/app/models/paper-trail-version.js b/frontend/app/models/paper-trail-version.js index 70046b494..7e03a272d 100644 --- a/frontend/app/models/paper-trail-version.js +++ b/frontend/app/models/paper-trail-version.js @@ -2,7 +2,8 @@ import Model, { attr, belongsTo } from "@ember-data/model"; export default class PaperTrailVersion extends Model { @attr("string") event; - @attr("string") whodunnit; + @attr("number") user_id; + @attr("string") username; @attr("string") createdAt; @attr() object; @belongsTo("encryptable") encryptable; diff --git a/frontend/app/templates/components/encryptable/table-row.hbs b/frontend/app/templates/components/encryptable/table-row.hbs index 73dc6eb86..fed68854e 100644 --- a/frontend/app/templates/components/encryptable/table-row.hbs +++ b/frontend/app/templates/components/encryptable/table-row.hbs @@ -1,5 +1,5 @@ {{t (concat "encryptables.log." @paperTrailVersion.event)}} - {{@paperTrailVersion.whodunnit}} + {{@paperTrailVersion.username}} {{moment-format @paperTrailVersion.createdAt "DD.MM.YYYY hh:mm"}} From b991304ee30a04b9acc45d264dcb477cb39971a4 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Thu, 28 Apr 2022 13:27:29 +0200 Subject: [PATCH 45/79] correct spelling of model attribute --- frontend/app/models/paper-trail-version.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/app/models/paper-trail-version.js b/frontend/app/models/paper-trail-version.js index 7e03a272d..3f03cd7c9 100644 --- a/frontend/app/models/paper-trail-version.js +++ b/frontend/app/models/paper-trail-version.js @@ -2,7 +2,7 @@ import Model, { attr, belongsTo } from "@ember-data/model"; export default class PaperTrailVersion extends Model { @attr("string") event; - @attr("number") user_id; + @attr("number") userId; @attr("string") username; @attr("string") createdAt; @attr() object; From 0de70d12b910894efd4d2209c11771d8e70beabe Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Thu, 28 Apr 2022 13:39:47 +0200 Subject: [PATCH 46/79] add more frontend tests --- .../components/encryptable/show-test.js | 67 ++++++++++++++++--- 1 file changed, 58 insertions(+), 9 deletions(-) diff --git a/frontend/tests/integration/components/encryptable/show-test.js b/frontend/tests/integration/components/encryptable/show-test.js index 6acd441f8..6504411e3 100644 --- a/frontend/tests/integration/components/encryptable/show-test.js +++ b/frontend/tests/integration/components/encryptable/show-test.js @@ -2,24 +2,73 @@ import { module, test } from "qunit"; import { setupRenderingTest } from "ember-qunit"; import { render } from "@ember/test-helpers"; import { hbs } from "ember-cli-htmlbars"; +import Service from "@ember/service"; +import { setLocale } from "ember-intl/test-support"; -module("Integration | Component | encryptable/show", function(hooks) { +const storeStub = Service.extend({ + query(modelName, params) { + if (params) { + return []; + } + } +}); + +module("Integration | Component | encryptable/show", function (hooks) { setupRenderingTest(hooks); - test("it renders with data", async function (assert) { + hooks.beforeEach(function () { + this.owner.unregister("service:store"); + this.owner.register("service:store", storeStub); + setLocale("en"); this.set("encryptable", { - name: "Foo", - description: "Bla", - folder: { - get() { - return 1; + id: 1, + name: "Ninjas test encryptable", + description: "Encryptable for the ninjas", + cleartextUsername: "mail", + cleartextPassword: "e2jd2rh4g5io7", + createdAt: "2021-06-14 09:23:02.750627", + updatedAt: "2021-06-22 11:33:13.766879", + paperTrailVersions: [ + { + userId: 1, + username: "alice", + event: "viewed", + createdAt: "2021-06-14 09:23:02.750627", + encryptable: { + get() { + return 1; + }, + id: 1 + } + }, + { + userId: 2, + username: "bob", + event: "update", + createdAt: "2021-06-15 09:23:02.750627", + encryptable: { + get() { + return 1; + }, + id: 1 + } } - } + ] }); + }); + + test("it renders with data", async function (assert) { + await render(hbs``); + + let text = this.element.textContent.trim(); + assert.ok(text.includes("Ninjas test encryptable")); + }); + test("shows log and credentials tabs", async function (assert) { await render(hbs``); let text = this.element.textContent.trim(); - assert.ok(text.includes("Foo")); + assert.ok(text.includes("Credentials")); + assert.ok(text.includes("Logs")); }); }); From a1aabddf35205952819bea6cca27c089caf19b9c Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Thu, 28 Apr 2022 14:40:48 +0200 Subject: [PATCH 47/79] improve frontend tests and add id to tabs --- .../app/templates/components/encryptable/show.hbs | 4 ++-- .../integration/components/encryptable/show-test.js | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/frontend/app/templates/components/encryptable/show.hbs b/frontend/app/templates/components/encryptable/show.hbs index 77d35e3b8..42eaebc23 100644 --- a/frontend/app/templates/components/encryptable/show.hbs +++ b/frontend/app/templates/components/encryptable/show.hbs @@ -36,7 +36,7 @@
- + {{#if @encryptable.description}}
@@ -94,7 +94,7 @@
- +
diff --git a/frontend/tests/integration/components/encryptable/show-test.js b/frontend/tests/integration/components/encryptable/show-test.js index 6504411e3..3cd3d2426 100644 --- a/frontend/tests/integration/components/encryptable/show-test.js +++ b/frontend/tests/integration/components/encryptable/show-test.js @@ -4,6 +4,7 @@ import { render } from "@ember/test-helpers"; import { hbs } from "ember-cli-htmlbars"; import Service from "@ember/service"; import { setLocale } from "ember-intl/test-support"; +import { isPresent } from "@ember/utils"; const storeStub = Service.extend({ query(modelName, params) { @@ -64,11 +65,11 @@ module("Integration | Component | encryptable/show", function (hooks) { assert.ok(text.includes("Ninjas test encryptable")); }); - test("shows log and credentials tabs", async function (assert) { + test("log and credentials tabs ase present", async function (assert) { await render(hbs``); - - let text = this.element.textContent.trim(); - assert.ok(text.includes("Credentials")); - assert.ok(text.includes("Logs")); + let credTab = document.getElementById("credentials-tab"); + let logTab = document.getElementById("log-tab"); + assert.ok(isPresent(credTab)); + assert.ok(isPresent(logTab)); }); }); From 51c8933bbdc500023b351c333673019d623d74cf Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Thu, 28 Apr 2022 17:09:00 +0200 Subject: [PATCH 48/79] fix serializer and backend specs --- app/serializers/logs_serializer.rb | 2 +- spec/controllers/api/logs_controller_spec.rb | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/serializers/logs_serializer.rb b/app/serializers/logs_serializer.rb index fc70044d0..1f2a8aa06 100644 --- a/app/serializers/logs_serializer.rb +++ b/app/serializers/logs_serializer.rb @@ -10,7 +10,7 @@ def user_id end def username - User.find_by(id: user_id).username + User.find_by(id: object.whodunnit).username end def encryptable diff --git a/spec/controllers/api/logs_controller_spec.rb b/spec/controllers/api/logs_controller_spec.rb index 717ff7c8b..87a6781c6 100644 --- a/spec/controllers/api/logs_controller_spec.rb +++ b/spec/controllers/api/logs_controller_spec.rb @@ -13,7 +13,7 @@ context 'GET index' do it 'returns right amount of logs' do login_as(:alice) - PaperTrail.request(whodunnit: 'alice') do + PaperTrail.request(whodunnit: alice.id) do credentials1.touch credentials1.touch end @@ -24,8 +24,10 @@ it 'returns sorted results' do login_as(:bob) - credentials1.touch - credentials1.touch + PaperTrail.request(whodunnit: bob.id) do + credentials1.touch + credentials1.touch + end get :index, params: { encryptable_id: credentials1.id } expect(data.first['attributes']['created_at']).to be > data.second['attributes']['created_at'] From 23fd3123ca1b2cc753e5aad87ea4b1a9e34fc2f0 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Fri, 29 Apr 2022 16:00:16 +0200 Subject: [PATCH 49/79] use correct date format in paper_trail table --- db/migrate/20220401130939_create_versions.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db/migrate/20220401130939_create_versions.rb b/db/migrate/20220401130939_create_versions.rb index 63bdc6cd6..f6e7ba97a 100644 --- a/db/migrate/20220401130939_create_versions.rb +++ b/db/migrate/20220401130939_create_versions.rb @@ -30,8 +30,8 @@ def change # (https://github.com/rails/rails/pull/14359) # # MySQL users should use the following line for `created_at` - # t.datetime :created_at, limit: 6 - t.datetime :created_at + t.datetime :created_at, limit: 6 + #t.datetime :created_at end add_index :versions, %i(item_type item_id) end From f79ad57365ff00b8ad96bccd24b38961f5e7d1c7 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Fri, 29 Apr 2022 16:01:16 +0200 Subject: [PATCH 50/79] user save_with_version on encryptable show --- app/controllers/api/encryptables_controller.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/encryptables_controller.rb b/app/controllers/api/encryptables_controller.rb index 9612a77bd..896bdf9cd 100644 --- a/app/controllers/api/encryptables_controller.rb +++ b/app/controllers/api/encryptables_controller.rb @@ -19,9 +19,11 @@ def index(options = {}) # GET /api/encryptables/:id def show authorize encryptable - encryptable.paper_trail_event = 'viewed' - encryptable.touch - encryptable.decrypt(decrypted_team_password(team)) + encryptable.paper_trail.save_with_version + v = encryptable.versions.last + v.event = "viewed" + v.created_at = DateTime.now + v.save render_entry end From c06353f7adcce3bdab371394da4e57dcb6e3e9f6 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Fri, 29 Apr 2022 16:16:13 +0200 Subject: [PATCH 51/79] fix mistake --- app/controllers/api/encryptables_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/encryptables_controller.rb b/app/controllers/api/encryptables_controller.rb index 896bdf9cd..e08ea049a 100644 --- a/app/controllers/api/encryptables_controller.rb +++ b/app/controllers/api/encryptables_controller.rb @@ -19,9 +19,10 @@ def index(options = {}) # GET /api/encryptables/:id def show authorize encryptable + encryptable.decrypt(decrypted_team_password(team)) encryptable.paper_trail.save_with_version v = encryptable.versions.last - v.event = "viewed" + v.event = 'viewed' v.created_at = DateTime.now v.save render_entry From 6d84e7f2dd439a78ba9f38f6e1d2ca77114d30b7 Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Fri, 29 Apr 2022 16:36:10 +0200 Subject: [PATCH 52/79] Add destroy on delete to encryptable --- app/models/encryptable.rb | 8 +++++++- spec/models/encryptable_spec.rb | 15 ++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/app/models/encryptable.rb b/app/models/encryptable.rb index d45e48aa9..0141d0461 100644 --- a/app/models/encryptable.rb +++ b/app/models/encryptable.rb @@ -17,7 +17,9 @@ class Encryptable < ApplicationRecord - has_paper_trail on: [:touch, :update], ignore: [:tag, :type, :encrypted_data] + has_paper_trail on: [:touch, :update], ignore: [:tag, :type, :encrypted_data], dependent: :destroy + + before_destroy :destroy_versions serialize :encrypted_data, ::EncryptedData @@ -72,4 +74,8 @@ def decrypt_attr(attr, team_password) instance_variable_set("@cleartext_#{attr}", cleartext_value) end + def destroy_versions + self.versions.destroy_all + end + end diff --git a/spec/models/encryptable_spec.rb b/spec/models/encryptable_spec.rb index b939ce9d6..9ef0f5d90 100644 --- a/spec/models/encryptable_spec.rb +++ b/spec/models/encryptable_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'spec_helper' - describe Encryptable do let(:bob) { users(:bob) } @@ -85,6 +84,20 @@ end context 'papertrail', versioning: true do + context 'delete' do + it 'deletes log history if encryptable is delted' do + 1000.times do + encryptable.touch + end + + expect(encryptable.versions.size).to eq(1000) + + encryptable.destroy + + expect(PaperTrail::Version.all.size).to be(0) + end + end + context 'touch' do it 'creates a log entry' do encryptable.touch From e055936e2856235897e5dad0db5d2b094d39a27b Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Sun, 1 May 2022 19:35:22 +0200 Subject: [PATCH 53/79] log all update actions, fix problem with sorting logs on update --- app/models/encryptable.rb | 4 +-- app/serializers/logs_serializer.rb | 2 +- frontend/app/components/encryptable/show.js | 8 ++++-- .../templates/components/encryptable/show.hbs | 2 +- .../components/account/show-test.js | 27 ++++++++++++++++++- .../components/encryptable/show-test.js | 27 ++++++++++++++++++- spec/controllers/api/logs_controller_spec.rb | 1 + 7 files changed, 63 insertions(+), 8 deletions(-) diff --git a/app/models/encryptable.rb b/app/models/encryptable.rb index 0141d0461..6f49af1fa 100644 --- a/app/models/encryptable.rb +++ b/app/models/encryptable.rb @@ -17,7 +17,7 @@ class Encryptable < ApplicationRecord - has_paper_trail on: [:touch, :update], ignore: [:tag, :type, :encrypted_data], dependent: :destroy + has_paper_trail on: [:touch, :update], ignore: [:tag, :type], dependent: :destroy before_destroy :destroy_versions @@ -75,7 +75,7 @@ def decrypt_attr(attr, team_password) end def destroy_versions - self.versions.destroy_all + versions.destroy_all end end diff --git a/app/serializers/logs_serializer.rb b/app/serializers/logs_serializer.rb index 1f2a8aa06..0968bfe95 100644 --- a/app/serializers/logs_serializer.rb +++ b/app/serializers/logs_serializer.rb @@ -10,7 +10,7 @@ def user_id end def username - User.find_by(id: object.whodunnit).username + User.find_by(id: object.whodunnit.to_i).username end def encryptable diff --git a/frontend/app/components/encryptable/show.js b/frontend/app/components/encryptable/show.js index 9f211ecac..73d99469a 100644 --- a/frontend/app/components/encryptable/show.js +++ b/frontend/app/components/encryptable/show.js @@ -9,6 +9,8 @@ export default class ShowComponent extends Component { @service intl; @service notify; + @tracked logs; + constructor() { super(...arguments); @@ -32,6 +34,7 @@ export default class ShowComponent extends Component { @action toggleEncryptableEdit() { this.isEncryptableEditing = !this.isEncryptableEditing; + this.getLogs(); } @action @@ -69,7 +72,8 @@ export default class ShowComponent extends Component { .query("paper-trail-version", { encryptableId: this.args.encryptable.id }) - .sortBy("created_at") - .reverse(); + .then((res) => { + this.logs = res; + }); } } diff --git a/frontend/app/templates/components/encryptable/show.hbs b/frontend/app/templates/components/encryptable/show.hbs index 42eaebc23..1e9de5509 100644 --- a/frontend/app/templates/components/encryptable/show.hbs +++ b/frontend/app/templates/components/encryptable/show.hbs @@ -96,7 +96,7 @@
- +
diff --git a/frontend/tests/integration/components/account/show-test.js b/frontend/tests/integration/components/account/show-test.js index e9f8f124f..a3efb25d3 100644 --- a/frontend/tests/integration/components/account/show-test.js +++ b/frontend/tests/integration/components/account/show-test.js @@ -9,7 +9,32 @@ import { isPresent } from "@ember/utils"; const storeStub = Service.extend({ query(modelName, params) { if (params) { - return []; + return Promise.all([ + { + userId: 1, + username: "alice", + event: "viewed", + createdAt: "2021-06-14 09:23:02.750627", + encryptable: { + get() { + return 1; + }, + id: 1 + } + }, + { + userId: 2, + username: "bob", + event: "update", + createdAt: "2021-06-15 09:23:02.750627", + encryptable: { + get() { + return 1; + }, + id: 1 + } + } + ]); } } }); diff --git a/frontend/tests/integration/components/encryptable/show-test.js b/frontend/tests/integration/components/encryptable/show-test.js index 3cd3d2426..218e77672 100644 --- a/frontend/tests/integration/components/encryptable/show-test.js +++ b/frontend/tests/integration/components/encryptable/show-test.js @@ -9,7 +9,32 @@ import { isPresent } from "@ember/utils"; const storeStub = Service.extend({ query(modelName, params) { if (params) { - return []; + return Promise.all([ + { + userId: 1, + username: "alice", + event: "viewed", + createdAt: "2021-06-14 09:23:02.750627", + encryptable: { + get() { + return 1; + }, + id: 1 + } + }, + { + userId: 2, + username: "bob", + event: "update", + createdAt: "2021-06-15 09:23:02.750627", + encryptable: { + get() { + return 1; + }, + id: 1 + } + } + ]); } } }); diff --git a/spec/controllers/api/logs_controller_spec.rb b/spec/controllers/api/logs_controller_spec.rb index 87a6781c6..95ebcbe2d 100644 --- a/spec/controllers/api/logs_controller_spec.rb +++ b/spec/controllers/api/logs_controller_spec.rb @@ -20,6 +20,7 @@ get :index, params: { encryptable_id: credentials1.id } expect(data.count).to eq 2 + expect(data.first['attributes']['username']).to eq 'alice' end it 'returns sorted results' do From 5b352a96badb0dfaaea1075ef140b2b27032ae02 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Mon, 2 May 2022 00:07:39 +0200 Subject: [PATCH 54/79] limit query to only get 20 logs --- app/controllers/api/logs_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/logs_controller.rb b/app/controllers/api/logs_controller.rb index ac2e35ad9..b1c793910 100644 --- a/app/controllers/api/logs_controller.rb +++ b/app/controllers/api/logs_controller.rb @@ -15,7 +15,7 @@ def index(options = {}) def fetch_entries PaperTrail.serializer = JSON logs = current_user.encryptables.find_by!(id: params[:encryptable_id]).versions - logs.sort { |a, b| b.created_at <=> a.created_at } + logs.last(20).sort { |a, b| b.created_at <=> a.created_at } end def list_serializer From 37a0ba0cba14c4fa0b0f6499779e95d3558b3283 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Mon, 2 May 2022 00:10:57 +0200 Subject: [PATCH 55/79] add load more button to always load 10 logs at a time --- app/controllers/api/logs_controller.rb | 6 +++++- frontend/app/components/encryptable/show.js | 20 +++++++++++++++++-- .../templates/components/encryptable/show.hbs | 5 +++++ frontend/translations/ch_be.yml | 1 + frontend/translations/de.yml | 1 + frontend/translations/en.yml | 1 + frontend/translations/fr.yml | 1 + spec/controllers/api/logs_controller_spec.rb | 1 - 8 files changed, 32 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/logs_controller.rb b/app/controllers/api/logs_controller.rb index b1c793910..b702d5f7d 100644 --- a/app/controllers/api/logs_controller.rb +++ b/app/controllers/api/logs_controller.rb @@ -15,7 +15,11 @@ def index(options = {}) def fetch_entries PaperTrail.serializer = JSON logs = current_user.encryptables.find_by!(id: params[:encryptable_id]).versions - logs.last(20).sort { |a, b| b.created_at <=> a.created_at } + if params[:load] + logs.last(params[:load]).sort { |a, b| b.created_at <=> a.created_at } + else + logs.last(10).sort { |a, b| b.created_at <=> a.created_at } + end end def list_serializer diff --git a/frontend/app/components/encryptable/show.js b/frontend/app/components/encryptable/show.js index 73d99469a..fc146ac26 100644 --- a/frontend/app/components/encryptable/show.js +++ b/frontend/app/components/encryptable/show.js @@ -9,7 +9,8 @@ export default class ShowComponent extends Component { @service intl; @service notify; - @tracked logs; + @tracked logs = []; + loadAmount = 10; constructor() { super(...arguments); @@ -31,6 +32,9 @@ export default class ShowComponent extends Component { @tracked isPasswordVisible = false; + @tracked + canLoadMore = true; + @action toggleEncryptableEdit() { this.isEncryptableEditing = !this.isEncryptableEditing; @@ -70,10 +74,22 @@ export default class ShowComponent extends Component { getLogs() { this.store .query("paper-trail-version", { - encryptableId: this.args.encryptable.id + encryptableId: this.args.encryptable.id, + load: this.loadAmount }) .then((res) => { this.logs = res; + this.toggleLoadMore(); }); } + + @action + loadMore() { + this.loadAmount += 10; + this.getLogs(); + } + + toggleLoadMore() { + this.canLoadMore = this.loadAmount <= this.logs.length; + } } diff --git a/frontend/app/templates/components/encryptable/show.hbs b/frontend/app/templates/components/encryptable/show.hbs index 1e9de5509..5ec8bb573 100644 --- a/frontend/app/templates/components/encryptable/show.hbs +++ b/frontend/app/templates/components/encryptable/show.hbs @@ -98,6 +98,11 @@
+ {{#if this.canLoadMore}} + + {{t "encryptables.show.load_more"}} + + {{/if}}
diff --git a/frontend/translations/ch_be.yml b/frontend/translations/ch_be.yml index 4699146fb..c8de3f7e7 100644 --- a/frontend/translations/ch_be.yml +++ b/frontend/translations/ch_be.yml @@ -129,6 +129,7 @@ ch_be: copy_password: Passwort kopierä copy_username: Benutzername kopierä add_attachment: Anhang hinzufüegä + load_more: Meh aazeige new: title: Nöie Encryptable log: diff --git a/frontend/translations/de.yml b/frontend/translations/de.yml index 47e3068b4..ee90daa32 100644 --- a/frontend/translations/de.yml +++ b/frontend/translations/de.yml @@ -98,6 +98,7 @@ de: copy_password: Passwort kopieren copy_username: Benutzername kopieren add_attachment: Anhang hinzufügen + load_more: Mehr anzeigen new: title: Neues Encryptable log: diff --git a/frontend/translations/en.yml b/frontend/translations/en.yml index 412afb40f..6c0801a10 100644 --- a/frontend/translations/en.yml +++ b/frontend/translations/en.yml @@ -96,6 +96,7 @@ en: copy_password: Copy password copy_username: Copy username add_attachment: Add Attachment + load_more: Load More new: title: New Encryptable log: diff --git a/frontend/translations/fr.yml b/frontend/translations/fr.yml index 2678ef4e0..a238773e8 100644 --- a/frontend/translations/fr.yml +++ b/frontend/translations/fr.yml @@ -98,6 +98,7 @@ fr: copy_password: Copier le mot de passe copy_username: Copier le nom d'utilisateur add_attachment: Ajouter un attachement + load_more: Charger plus new: title: Nouveau compte log: diff --git a/spec/controllers/api/logs_controller_spec.rb b/spec/controllers/api/logs_controller_spec.rb index 95ebcbe2d..7e58f9fc7 100644 --- a/spec/controllers/api/logs_controller_spec.rb +++ b/spec/controllers/api/logs_controller_spec.rb @@ -29,7 +29,6 @@ credentials1.touch credentials1.touch end - get :index, params: { encryptable_id: credentials1.id } expect(data.first['attributes']['created_at']).to be > data.second['attributes']['created_at'] end From 3286250091d38b740b854b618dcae369f4d748aa Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Mon, 2 May 2022 10:00:34 +0200 Subject: [PATCH 56/79] Change find_by to find --- app/serializers/logs_serializer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/serializers/logs_serializer.rb b/app/serializers/logs_serializer.rb index 0968bfe95..620fa29cd 100644 --- a/app/serializers/logs_serializer.rb +++ b/app/serializers/logs_serializer.rb @@ -10,7 +10,7 @@ def user_id end def username - User.find_by(id: object.whodunnit.to_i).username + User.find(object.whodunnit).username end def encryptable From 143eb242cb859399036f66aeb57de918a662eb9e Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Mon, 2 May 2022 10:01:37 +0200 Subject: [PATCH 57/79] Format code --- app/controllers/api/logs_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/logs_controller.rb b/app/controllers/api/logs_controller.rb index b702d5f7d..2c93f2a98 100644 --- a/app/controllers/api/logs_controller.rb +++ b/app/controllers/api/logs_controller.rb @@ -8,8 +8,8 @@ def index(options = {}) render({ json: fetch_entries, each_serializer: list_serializer, root: 'Logs_'.pluralize } - .merge(render_options) - .merge(options.fetch(:render_options, {}))) + .merge(render_options) + .merge(options.fetch(:render_options, {}))) end def fetch_entries From 791a15ee2c03ca892eb497e2eea31acd4ee3631b Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Tue, 3 May 2022 09:39:59 +0200 Subject: [PATCH 58/79] Fix rubocop issue --- app/models/encryptable.rb | 3 +-- spec/models/encryptable_spec.rb | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/app/models/encryptable.rb b/app/models/encryptable.rb index 6f49af1fa..efe19ccbb 100644 --- a/app/models/encryptable.rb +++ b/app/models/encryptable.rb @@ -17,8 +17,7 @@ class Encryptable < ApplicationRecord - has_paper_trail on: [:touch, :update], ignore: [:tag, :type], dependent: :destroy - + has_paper_trail on: [:touch, :update], ignore: [:tag, :type] before_destroy :destroy_versions serialize :encrypted_data, ::EncryptedData diff --git a/spec/models/encryptable_spec.rb b/spec/models/encryptable_spec.rb index 9ef0f5d90..8158bd4d5 100644 --- a/spec/models/encryptable_spec.rb +++ b/spec/models/encryptable_spec.rb @@ -2,7 +2,6 @@ require 'spec_helper' describe Encryptable do - let(:bob) { users(:bob) } let(:bobs_private_key) { bob.decrypt_private_key('password') } let(:encryptable) { encryptables(:credentials1) } From 93aebfb6926636cdc8ef9080cde25e9bc8449734 Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Wed, 4 May 2022 16:58:50 +0200 Subject: [PATCH 59/79] Adjust to PR review --- app/controllers/api/encryptables_controller.rb | 8 ++++++-- app/controllers/api/logs_controller.rb | 8 +------- app/policies/log_policy.rb | 2 +- app/serializers/logs_serializer.rb | 2 +- config/routes/api.rb | 2 +- 5 files changed, 10 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/encryptables_controller.rb b/app/controllers/api/encryptables_controller.rb index e08ea049a..f6499514c 100644 --- a/app/controllers/api/encryptables_controller.rb +++ b/app/controllers/api/encryptables_controller.rb @@ -20,11 +20,11 @@ def index(options = {}) def show authorize encryptable encryptable.decrypt(decrypted_team_password(team)) - encryptable.paper_trail.save_with_version + log_read_access v = encryptable.versions.last v.event = 'viewed' v.created_at = DateTime.now - v.save + v.save! render_entry end @@ -128,4 +128,8 @@ def permitted_attrs permitted_attrs end end + + def log_read_access + encryptable.paper_trail.save_with_version + end end diff --git a/app/controllers/api/logs_controller.rb b/app/controllers/api/logs_controller.rb index 2c93f2a98..c29a078d1 100644 --- a/app/controllers/api/logs_controller.rb +++ b/app/controllers/api/logs_controller.rb @@ -1,13 +1,11 @@ # frozen_string_literal: true class Api::LogsController < ApiController - self.permitted_attrs = :encryptable_id def index(options = {}) authorize(team, :index?, policy_class: LogPolicy) render({ json: fetch_entries, - each_serializer: list_serializer, - root: 'Logs_'.pluralize } + each_serializer: @model_serializer ||= LogsSerializer } .merge(render_options) .merge(options.fetch(:render_options, {}))) end @@ -22,10 +20,6 @@ def fetch_entries end end - def list_serializer - @model_serializer ||= 'LogsSerializer'.constantize - end - def team @team ||= Encryptable.find(params[:encryptable_id]).folder.team end diff --git a/app/policies/log_policy.rb b/app/policies/log_policy.rb index 540552b47..184c27f45 100644 --- a/app/policies/log_policy.rb +++ b/app/policies/log_policy.rb @@ -7,7 +7,7 @@ def initialize(user, record) end def index? - @user.is_a?(User::Human) && team.teammember?(@user.id) + team_member? end def show? diff --git a/app/serializers/logs_serializer.rb b/app/serializers/logs_serializer.rb index 620fa29cd..620f721c5 100644 --- a/app/serializers/logs_serializer.rb +++ b/app/serializers/logs_serializer.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class LogsSerializer < ApplicationSerializer - attributes :id, :item_type, :event, :user_id, :username, :object, :created_at + attributes :id, :item_type, :event, :user_id, :username, :created_at belongs_to :encryptable diff --git a/config/routes/api.rb b/config/routes/api.rb index bf1e34d94..1e7a04ff5 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -13,7 +13,7 @@ resources :encryptables, except: [:new, :edit] do resources :file_entries, only: [:create, :index, :destroy, :show] - resources :logs + resources :logs, only: [:index] end resources :api_users, except: [:new, :edit] do From 30b97144f23c24eff77c36e4fe6458c12d721d34 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Wed, 4 May 2022 22:36:53 +0200 Subject: [PATCH 60/79] add system spec for view log --- .../api/encryptables_controller.rb | 8 ++-- spec/system/encryptable_log_system_spec.rb | 38 +++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 spec/system/encryptable_log_system_spec.rb diff --git a/app/controllers/api/encryptables_controller.rb b/app/controllers/api/encryptables_controller.rb index f6499514c..08c2bdc53 100644 --- a/app/controllers/api/encryptables_controller.rb +++ b/app/controllers/api/encryptables_controller.rb @@ -21,10 +21,6 @@ def show authorize encryptable encryptable.decrypt(decrypted_team_password(team)) log_read_access - v = encryptable.versions.last - v.event = 'viewed' - v.created_at = DateTime.now - v.save! render_entry end @@ -131,5 +127,9 @@ def permitted_attrs def log_read_access encryptable.paper_trail.save_with_version + v = encryptable.versions.last + v.event = 'viewed' + v.created_at = DateTime.now + v.save! end end diff --git a/spec/system/encryptable_log_system_spec.rb b/spec/system/encryptable_log_system_spec.rb new file mode 100644 index 000000000..e6e88e67e --- /dev/null +++ b/spec/system/encryptable_log_system_spec.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'encryptable log', type: :system, js: true do + include SystemHelpers + + it 'logs view access' do + login_as_user(:alice) + visit("/encryptables/#{encryptables(:credentials1).id}") + expect(page).to have_text('Credentials') + click_link('Logs') + + expect(page).to have_css('table') + within 'table' do + table_rows = all('tr') + + # expect one but header counts as well + expect(table_rows.length).to eq(2) + top_row = table_rows[1] + + within top_row do + expect(page).to have_text('viewed') + expect(page).to have_text('alice') + end + end + end + + private + + def edit_encryptable + find('#edit_account_button').click + within('form.ember-view[role="form"]', visible: false) do + find("input[name='cleartextUsername']", visible: false).set 'username2' + end + click_button('Save', visible: false) + end +end From 77d08a78f3ffc4142a14e1286ef100da8d052827 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Thu, 5 May 2022 11:55:36 +0200 Subject: [PATCH 61/79] add test for edit log (doesnt pass yet) --- spec/system/encryptable_log_system_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spec/system/encryptable_log_system_spec.rb b/spec/system/encryptable_log_system_spec.rb index e6e88e67e..fc1907cc0 100644 --- a/spec/system/encryptable_log_system_spec.rb +++ b/spec/system/encryptable_log_system_spec.rb @@ -24,6 +24,24 @@ expect(page).to have_text('alice') end end + edit_encryptable + click_link('Logs') + + within 'table' do + table_rows = all('tr') + + expect(table_rows.length).to eq(3) + + # somehow the following fails here but the sorting works fine when testing manually... + + # top_row = table_rows[1] + + # within top_row do + # expect(page).to have_text('edited') + # expect(page).to have_text('alice') + # end + + end end private From d478e3667ddc532a7d8e5abbf2ca4493d7e9aafa Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Thu, 5 May 2022 14:09:58 +0200 Subject: [PATCH 62/79] fix bug where view access is logged as update --- app/controllers/api/encryptables_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/encryptables_controller.rb b/app/controllers/api/encryptables_controller.rb index 08c2bdc53..db4d194df 100644 --- a/app/controllers/api/encryptables_controller.rb +++ b/app/controllers/api/encryptables_controller.rb @@ -127,7 +127,7 @@ def permitted_attrs def log_read_access encryptable.paper_trail.save_with_version - v = encryptable.versions.last + v = encryptable.versions.first v.event = 'viewed' v.created_at = DateTime.now v.save! From c56df1a37cf103d25f64da01a16a45088b0a947d Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Fri, 6 May 2022 12:44:39 +0200 Subject: [PATCH 63/79] Replace alice with bob to fix spec --- spec/system/encryptable_log_system_spec.rb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/spec/system/encryptable_log_system_spec.rb b/spec/system/encryptable_log_system_spec.rb index fc1907cc0..b2d80422d 100644 --- a/spec/system/encryptable_log_system_spec.rb +++ b/spec/system/encryptable_log_system_spec.rb @@ -1,12 +1,13 @@ # frozen_string_literal: true require 'spec_helper' +require 'pry' describe 'encryptable log', type: :system, js: true do include SystemHelpers it 'logs view access' do - login_as_user(:alice) + login_as_user(:bob) visit("/encryptables/#{encryptables(:credentials1).id}") expect(page).to have_text('Credentials') click_link('Logs') @@ -21,7 +22,7 @@ within top_row do expect(page).to have_text('viewed') - expect(page).to have_text('alice') + expect(page).to have_text('bob') end end edit_encryptable @@ -34,12 +35,12 @@ # somehow the following fails here but the sorting works fine when testing manually... - # top_row = table_rows[1] + top_row = table_rows[1] - # within top_row do - # expect(page).to have_text('edited') - # expect(page).to have_text('alice') - # end + within top_row do + expect(page).to have_text('edited') + expect(page).to have_text('bob') + end end end From 679ea3f06638fda092f6d8798e29cd6b912a8475 Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Fri, 6 May 2022 12:56:13 +0200 Subject: [PATCH 64/79] Restructure specs --- spec/system/encryptable_log_system_spec.rb | 59 ++++++++++++---------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/spec/system/encryptable_log_system_spec.rb b/spec/system/encryptable_log_system_spec.rb index b2d80422d..755015047 100644 --- a/spec/system/encryptable_log_system_spec.rb +++ b/spec/system/encryptable_log_system_spec.rb @@ -6,42 +6,45 @@ describe 'encryptable log', type: :system, js: true do include SystemHelpers - it 'logs view access' do - login_as_user(:bob) - visit("/encryptables/#{encryptables(:credentials1).id}") - expect(page).to have_text('Credentials') - click_link('Logs') - - expect(page).to have_css('table') - within 'table' do - table_rows = all('tr') - - # expect one but header counts as well - expect(table_rows.length).to eq(2) - top_row = table_rows[1] - - within top_row do - expect(page).to have_text('viewed') - expect(page).to have_text('bob') + describe 'logs view access' do + it 'contains credentials and logs table in page' do + login_as_user(:bob) + visit("/encryptables/#{encryptables(:credentials1).id}") + expect(page).to have_text('Credentials') + click_link('Logs') + + expect(page).to have_css('table') + within 'table' do + table_rows = all('tr') + + expect(table_rows.length).to eq(2) + top_row = table_rows[1] + + within top_row do + expect(page).to have_text('viewed') + expect(page).to have_text('bob') + end end end - edit_encryptable - click_link('Logs') - within 'table' do - table_rows = all('tr') + it 'contains log for update' do + login_as_user(:bob) + visit("/encryptables/#{encryptables(:credentials1).id}") + edit_encryptable + click_link('Logs') - expect(table_rows.length).to eq(3) + within 'table' do + table_rows = all('tr') - # somehow the following fails here but the sorting works fine when testing manually... + expect(table_rows.length).to eq(3) - top_row = table_rows[1] + top_row = table_rows[1] - within top_row do - expect(page).to have_text('edited') - expect(page).to have_text('bob') + within top_row do + expect(page).to have_text('edited') + expect(page).to have_text('bob') + end end - end end From 17bb5d9311b863260db7794dbeb009a0e78edc57 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Sat, 7 May 2022 18:06:04 +0200 Subject: [PATCH 65/79] adjust frontend log model to backend model --- frontend/app/models/paper-trail-version.js | 1 - 1 file changed, 1 deletion(-) diff --git a/frontend/app/models/paper-trail-version.js b/frontend/app/models/paper-trail-version.js index 3f03cd7c9..0031d0eaa 100644 --- a/frontend/app/models/paper-trail-version.js +++ b/frontend/app/models/paper-trail-version.js @@ -5,6 +5,5 @@ export default class PaperTrailVersion extends Model { @attr("number") userId; @attr("string") username; @attr("string") createdAt; - @attr() object; @belongsTo("encryptable") encryptable; } From f4390301f3bf19f6382609a2fa8f21d8cf5705fa Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Mon, 9 May 2022 14:34:48 +0200 Subject: [PATCH 66/79] load logs directly from Versions table, optimize viewed access log --- app/controllers/api/encryptables_controller.rb | 5 ++--- app/controllers/api/logs_controller.rb | 11 +++++------ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/app/controllers/api/encryptables_controller.rb b/app/controllers/api/encryptables_controller.rb index db4d194df..ac26b77d1 100644 --- a/app/controllers/api/encryptables_controller.rb +++ b/app/controllers/api/encryptables_controller.rb @@ -126,9 +126,8 @@ def permitted_attrs end def log_read_access - encryptable.paper_trail.save_with_version - v = encryptable.versions.first - v.event = 'viewed' + v = encryptable.paper_trail.save_with_version + v.event = :viewed v.created_at = DateTime.now v.save! end diff --git a/app/controllers/api/logs_controller.rb b/app/controllers/api/logs_controller.rb index c29a078d1..50fb87f1a 100644 --- a/app/controllers/api/logs_controller.rb +++ b/app/controllers/api/logs_controller.rb @@ -12,12 +12,11 @@ def index(options = {}) def fetch_entries PaperTrail.serializer = JSON - logs = current_user.encryptables.find_by!(id: params[:encryptable_id]).versions - if params[:load] - logs.last(params[:load]).sort { |a, b| b.created_at <=> a.created_at } - else - logs.last(10).sort { |a, b| b.created_at <=> a.created_at } - end + limit = params[:load] || 20 + PaperTrail::Version + .where(item_id: params[:encryptable_id]) + .order(created_at: :desc) + .limit(limit) end def team From b737cbc362e9e3f188d610379ed6149b0a234553 Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Mon, 9 May 2022 14:46:36 +0200 Subject: [PATCH 67/79] Change parent class of log policy --- app/controllers/api/encryptables_controller.rb | 5 ++--- app/policies/log_policy.rb | 15 ++------------- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/app/controllers/api/encryptables_controller.rb b/app/controllers/api/encryptables_controller.rb index db4d194df..ac26b77d1 100644 --- a/app/controllers/api/encryptables_controller.rb +++ b/app/controllers/api/encryptables_controller.rb @@ -126,9 +126,8 @@ def permitted_attrs end def log_read_access - encryptable.paper_trail.save_with_version - v = encryptable.versions.first - v.event = 'viewed' + v = encryptable.paper_trail.save_with_version + v.event = :viewed v.created_at = DateTime.now v.save! end diff --git a/app/policies/log_policy.rb b/app/policies/log_policy.rb index 184c27f45..93eabd6cb 100644 --- a/app/policies/log_policy.rb +++ b/app/policies/log_policy.rb @@ -1,22 +1,11 @@ # frozen_string_literal: true -class LogPolicy < TeamDependantPolicy - def initialize(user, record) - @user = user - @record = record - end - +class LogPolicy < TeamPolicy def index? team_member? end def show? - team.teammember?(@user.id) - end - - protected - - def team - @record + @team.teammember?(@user.id) && !@user.is_a?(User::Api) end end From f19dc369c20e6a1af40134fcfb361270c03aadf2 Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Mon, 9 May 2022 14:55:49 +0200 Subject: [PATCH 68/79] Move papertrail spec to encryptable controller spec --- .../api/encryptables_controller_spec.rb | 33 +++++++++++++++++++ spec/models/encryptable_spec.rb | 30 ----------------- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/spec/controllers/api/encryptables_controller_spec.rb b/spec/controllers/api/encryptables_controller_spec.rb index 2e7adfb3a..c12bebd0c 100644 --- a/spec/controllers/api/encryptables_controller_spec.rb +++ b/spec/controllers/api/encryptables_controller_spec.rb @@ -13,6 +13,7 @@ let(:attributes) { %w[name cleartext_password cleartext_username] } let!(:ose_secret) { create_ose_secret } let(:credentials1) { encryptables(:credentials1) } + let(:encryptable) { encryptables.first } context 'GET index' do it 'returns encryptable with matching name' do @@ -600,6 +601,38 @@ end end end + + context 'papertrail', versioning: true do + context 'delete' do + it 'deletes log history if encryptable is delted' do + 1000.times do + encryptable.touch + end + + expect(encryptable.versions.size).to eq(1000) + + encryptable.destroy + + expect(PaperTrail::Version.all.size).to be(0) + end + end + + context 'touch' do + it 'creates a log entry' do + login_as(:bob) + + encryptable.touch + expect(encryptable.versions.count).to eq(1) + end + + it 'contains user in whodunnit' do + login_as(:bob) + + encryptable.touch + expect(encryptable.versions.last.whodunnit).to eq(bob.id.to_s) + end + end + end private diff --git a/spec/models/encryptable_spec.rb b/spec/models/encryptable_spec.rb index 8158bd4d5..531ae2a0f 100644 --- a/spec/models/encryptable_spec.rb +++ b/spec/models/encryptable_spec.rb @@ -82,36 +82,6 @@ end end - context 'papertrail', versioning: true do - context 'delete' do - it 'deletes log history if encryptable is delted' do - 1000.times do - encryptable.touch - end - - expect(encryptable.versions.size).to eq(1000) - - encryptable.destroy - - expect(PaperTrail::Version.all.size).to be(0) - end - end - - context 'touch' do - it 'creates a log entry' do - encryptable.touch - expect(encryptable.versions.count).to eq(1) - end - - it 'contains user in whodunnit' do - PaperTrail.request.whodunnit = 'Bob' - - encryptable.touch - expect(encryptable.versions.last.whodunnit).to eq('Bob') - end - end - end - private def create_legacy_ose_secret From 8402150f0ea5a6552e3915bdbedb8045662f9fcc Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Mon, 9 May 2022 15:10:14 +0200 Subject: [PATCH 69/79] change header names in logs table, add space between tabs and log table --- frontend/app/templates/components/encryptable/show.hbs | 1 + frontend/app/templates/components/encryptable/table.hbs | 6 +++--- frontend/translations/ch_be.yml | 2 ++ frontend/translations/de.yml | 2 ++ frontend/translations/en.yml | 2 ++ frontend/translations/fr.yml | 2 ++ 6 files changed, 12 insertions(+), 3 deletions(-) diff --git a/frontend/app/templates/components/encryptable/show.hbs b/frontend/app/templates/components/encryptable/show.hbs index 5ec8bb573..94fa2241c 100644 --- a/frontend/app/templates/components/encryptable/show.hbs +++ b/frontend/app/templates/components/encryptable/show.hbs @@ -95,6 +95,7 @@
+
diff --git a/frontend/app/templates/components/encryptable/table.hbs b/frontend/app/templates/components/encryptable/table.hbs index 6afe071e3..be7195c4b 100644 --- a/frontend/app/templates/components/encryptable/table.hbs +++ b/frontend/app/templates/components/encryptable/table.hbs @@ -1,9 +1,9 @@ - - - + + + diff --git a/frontend/translations/ch_be.yml b/frontend/translations/ch_be.yml index c8de3f7e7..22ab9664f 100644 --- a/frontend/translations/ch_be.yml +++ b/frontend/translations/ch_be.yml @@ -130,6 +130,8 @@ ch_be: copy_username: Benutzername kopierä add_attachment: Anhang hinzufüegä load_more: Meh aazeige + date_time: Datum/Zyt + action: Aktion new: title: Nöie Encryptable log: diff --git a/frontend/translations/de.yml b/frontend/translations/de.yml index ee90daa32..7832c5d64 100644 --- a/frontend/translations/de.yml +++ b/frontend/translations/de.yml @@ -99,6 +99,8 @@ de: copy_username: Benutzername kopieren add_attachment: Anhang hinzufügen load_more: Mehr anzeigen + date_time: Datum/Uhrzeit + action: Aktion new: title: Neues Encryptable log: diff --git a/frontend/translations/en.yml b/frontend/translations/en.yml index 6c0801a10..545f087f7 100644 --- a/frontend/translations/en.yml +++ b/frontend/translations/en.yml @@ -97,6 +97,8 @@ en: copy_username: Copy username add_attachment: Add Attachment load_more: Load More + date_time: Date/Time + action: Action new: title: New Encryptable log: diff --git a/frontend/translations/fr.yml b/frontend/translations/fr.yml index a238773e8..91069a8ae 100644 --- a/frontend/translations/fr.yml +++ b/frontend/translations/fr.yml @@ -99,6 +99,8 @@ fr: copy_username: Copier le nom d'utilisateur add_attachment: Ajouter un attachement load_more: Charger plus + date_time: Date/Temps + action: Action new: title: Nouveau compte log: From c2096e0500df9f2b7e34ca01149cd66676fce52d Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Mon, 9 May 2022 15:12:20 +0200 Subject: [PATCH 70/79] Add encryptables controller show test --- spec/controllers/api/encryptables_controller_spec.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/spec/controllers/api/encryptables_controller_spec.rb b/spec/controllers/api/encryptables_controller_spec.rb index c12bebd0c..4fe97c471 100644 --- a/spec/controllers/api/encryptables_controller_spec.rb +++ b/spec/controllers/api/encryptables_controller_spec.rb @@ -604,7 +604,7 @@ context 'papertrail', versioning: true do context 'delete' do - it 'deletes log history if encryptable is delted' do + it 'deletes log history if encryptable is deleted' do 1000.times do encryptable.touch end @@ -632,6 +632,16 @@ expect(encryptable.versions.last.whodunnit).to eq(bob.id.to_s) end end + + context 'show' do + it 'logs show access' do + login_as(:bob) + + get :show, params: { id: encryptable.id} + + expect(encryptable.versions.last.event).to eq('viewed') + end + end end private From 94f42b487cbab40b3bb3966ac232868670eb8e9e Mon Sep 17 00:00:00 2001 From: Dario Marti Date: Mon, 9 May 2022 15:14:27 +0200 Subject: [PATCH 71/79] Remove trailing whitespaces --- spec/controllers/api/encryptables_controller_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/controllers/api/encryptables_controller_spec.rb b/spec/controllers/api/encryptables_controller_spec.rb index 4fe97c471..357e67e29 100644 --- a/spec/controllers/api/encryptables_controller_spec.rb +++ b/spec/controllers/api/encryptables_controller_spec.rb @@ -601,7 +601,7 @@ end end end - + context 'papertrail', versioning: true do context 'delete' do it 'deletes log history if encryptable is deleted' do @@ -627,7 +627,7 @@ it 'contains user in whodunnit' do login_as(:bob) - + encryptable.touch expect(encryptable.versions.last.whodunnit).to eq(bob.id.to_s) end @@ -637,7 +637,7 @@ it 'logs show access' do login_as(:bob) - get :show, params: { id: encryptable.id} + get :show, params: { id: encryptable.id } expect(encryptable.versions.last.event).to eq('viewed') end From b4a0fa589cdcf8ed622a756537adb3a4cd3ba52e Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Fri, 13 May 2022 10:19:49 +0200 Subject: [PATCH 72/79] query for username in log query to aviod n+1 --- app/controllers/api/logs_controller.rb | 2 ++ app/serializers/logs_serializer.rb | 4 ---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/logs_controller.rb b/app/controllers/api/logs_controller.rb index 50fb87f1a..e94fbdfea 100644 --- a/app/controllers/api/logs_controller.rb +++ b/app/controllers/api/logs_controller.rb @@ -16,6 +16,8 @@ def fetch_entries PaperTrail::Version .where(item_id: params[:encryptable_id]) .order(created_at: :desc) + .select('versions.*, users.username as username') + .joins('INNER JOIN users ON users.id = versions.whodunnit') .limit(limit) end diff --git a/app/serializers/logs_serializer.rb b/app/serializers/logs_serializer.rb index 620f721c5..02762674a 100644 --- a/app/serializers/logs_serializer.rb +++ b/app/serializers/logs_serializer.rb @@ -9,10 +9,6 @@ def user_id object.whodunnit end - def username - User.find(object.whodunnit).username - end - def encryptable object.item end From 8aa4ddcace7a3ec9e3b0e11a883ade7ab9d95265 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Tue, 17 May 2022 17:02:21 +0200 Subject: [PATCH 73/79] add version model --- app/models/encryptable.rb | 5 ++++- app/models/user.rb | 2 ++ app/models/version.rb | 6 ++++++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 app/models/version.rb diff --git a/app/models/encryptable.rb b/app/models/encryptable.rb index efe19ccbb..52926aa85 100644 --- a/app/models/encryptable.rb +++ b/app/models/encryptable.rb @@ -17,7 +17,10 @@ class Encryptable < ApplicationRecord - has_paper_trail on: [:touch, :update], ignore: [:tag, :type] + has_paper_trail on: [:touch, :update], ignore: [:tag, :type], versions: { + class_name: 'Version' + } + before_destroy :destroy_versions serialize :encrypted_data, ::EncryptedData diff --git a/app/models/user.rb b/app/models/user.rb index f10cc272f..188962877 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -31,6 +31,8 @@ class User < ApplicationRecord validates :username, uniqueness: :username validates :username, presence: true + has_many :versions, foreign_key: :whodunnit, dependent: :destroy, class_name: 'Version' + def update_password(old, new) return unless auth_db? diff --git a/app/models/version.rb b/app/models/version.rb new file mode 100644 index 000000000..ff677990e --- /dev/null +++ b/app/models/version.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +class Version < PaperTrail::Version + belongs_to :user, class_name: 'User' + belongs_to :encryptable, class_name: 'Encryptable' +end From 93dc3028c6acfd4e79c689b13613726d222ebbdd Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Tue, 17 May 2022 23:36:34 +0200 Subject: [PATCH 74/79] use includes to get username to logs, refactor from paper-trail-version to version --- app/controllers/api/logs_controller.rb | 5 ++--- app/models/version.rb | 4 ++-- app/serializers/logs_serializer.rb | 4 ++++ frontend/app/adapters/{paper-trail-version.js => version.js} | 0 frontend/app/components/encryptable/show.js | 2 +- frontend/app/models/encryptable.js | 2 +- frontend/app/models/{paper-trail-version.js => version.js} | 2 +- 7 files changed, 11 insertions(+), 8 deletions(-) rename frontend/app/adapters/{paper-trail-version.js => version.js} (100%) rename frontend/app/models/{paper-trail-version.js => version.js} (79%) diff --git a/app/controllers/api/logs_controller.rb b/app/controllers/api/logs_controller.rb index e94fbdfea..bd104ff02 100644 --- a/app/controllers/api/logs_controller.rb +++ b/app/controllers/api/logs_controller.rb @@ -13,11 +13,10 @@ def index(options = {}) def fetch_entries PaperTrail.serializer = JSON limit = params[:load] || 20 - PaperTrail::Version + Version + .includes(:user) .where(item_id: params[:encryptable_id]) .order(created_at: :desc) - .select('versions.*, users.username as username') - .joins('INNER JOIN users ON users.id = versions.whodunnit') .limit(limit) end diff --git a/app/models/version.rb b/app/models/version.rb index ff677990e..8ab1f54d3 100644 --- a/app/models/version.rb +++ b/app/models/version.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true class Version < PaperTrail::Version - belongs_to :user, class_name: 'User' - belongs_to :encryptable, class_name: 'Encryptable' + belongs_to :user, class_name: 'User', foreign_key: :whodunnit + belongs_to :encryptable, class_name: 'Encryptable', foreign_key: :item_id end diff --git a/app/serializers/logs_serializer.rb b/app/serializers/logs_serializer.rb index 02762674a..4fd6c298b 100644 --- a/app/serializers/logs_serializer.rb +++ b/app/serializers/logs_serializer.rb @@ -12,4 +12,8 @@ def user_id def encryptable object.item end + + def username + object.user.username + end end diff --git a/frontend/app/adapters/paper-trail-version.js b/frontend/app/adapters/version.js similarity index 100% rename from frontend/app/adapters/paper-trail-version.js rename to frontend/app/adapters/version.js diff --git a/frontend/app/components/encryptable/show.js b/frontend/app/components/encryptable/show.js index fc146ac26..2b53de054 100644 --- a/frontend/app/components/encryptable/show.js +++ b/frontend/app/components/encryptable/show.js @@ -73,7 +73,7 @@ export default class ShowComponent extends Component { @action getLogs() { this.store - .query("paper-trail-version", { + .query("version", { encryptableId: this.args.encryptable.id, load: this.loadAmount }) diff --git a/frontend/app/models/encryptable.js b/frontend/app/models/encryptable.js index 57827e2a6..ce9384b8a 100644 --- a/frontend/app/models/encryptable.js +++ b/frontend/app/models/encryptable.js @@ -7,7 +7,7 @@ export default class Encryptable extends Model { @attr("string") updatedAt; @belongsTo("folder") folder; @hasMany("file-entry") fileEntries; - @hasMany("paper-trail-version") paperTrailVersions; + @hasMany("version") versions; get isOseSecret() { return this.constructor.modelName === "encryptable-ose-secret"; diff --git a/frontend/app/models/paper-trail-version.js b/frontend/app/models/version.js similarity index 79% rename from frontend/app/models/paper-trail-version.js rename to frontend/app/models/version.js index 0031d0eaa..3fb94e1a9 100644 --- a/frontend/app/models/paper-trail-version.js +++ b/frontend/app/models/version.js @@ -1,6 +1,6 @@ import Model, { attr, belongsTo } from "@ember-data/model"; -export default class PaperTrailVersion extends Model { +export default class Version extends Model { @attr("string") event; @attr("number") userId; @attr("string") username; From 67661b059448f95671ef5b1f368aecf8a4be027c Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Wed, 18 May 2022 15:26:32 +0200 Subject: [PATCH 75/79] Fix frontend test after merge conflict --- .../components/encryptable/show-test.js | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/frontend/tests/integration/components/encryptable/show-test.js b/frontend/tests/integration/components/encryptable/show-test.js index a2bbc952a..776c16bf7 100644 --- a/frontend/tests/integration/components/encryptable/show-test.js +++ b/frontend/tests/integration/components/encryptable/show-test.js @@ -9,7 +9,32 @@ import { isPresent } from "@ember/utils"; const storeStub = Service.extend({ query(modelName, params) { if (params) { - return []; + return Promise.all([ + { + userId: 1, + username: "alice", + event: "viewed", + createdAt: "2021-06-14 09:23:02.750627", + encryptable: { + get() { + return 1; + }, + id: 1 + } + }, + { + userId: 2, + username: "bob", + event: "update", + createdAt: "2021-06-15 09:23:02.750627", + encryptable: { + get() { + return 1; + }, + id: 1 + } + } + ]); } } }); @@ -21,9 +46,6 @@ module("Integration | Component | encryptable/show", function (hooks) { this.owner.unregister("service:store"); this.owner.register("service:store", storeStub); setLocale("en"); - }); - - test("it renders with data and shows edit buttons credentials encryptable entry", async function (assert) { this.set("encryptable", { id: 1, name: "Ninjas test encryptable", @@ -77,7 +99,9 @@ module("Integration | Component | encryptable/show", function (hooks) { } ] }); + }); + test("it renders with data and shows edit buttons credentials encryptable entry", async function (assert) { await render( hbs`` ); @@ -99,7 +123,9 @@ module("Integration | Component | encryptable/show", function (hooks) { }); test("log and credentials tabs ase present", async function (assert) { - await render(hbs``); + await render( + hbs`` + ); let credTab = document.getElementById("credentials-tab"); let logTab = document.getElementById("log-tab"); assert.ok(isPresent(credTab)); From 1217ef43237d09a775607f6547c7ebf43e029df2 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Wed, 18 May 2022 16:30:07 +0200 Subject: [PATCH 76/79] improve load more function --- app/controllers/api/logs_controller.rb | 2 ++ frontend/app/components/encryptable/show.js | 13 ++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/logs_controller.rb b/app/controllers/api/logs_controller.rb index bd104ff02..3f1f1fcce 100644 --- a/app/controllers/api/logs_controller.rb +++ b/app/controllers/api/logs_controller.rb @@ -13,10 +13,12 @@ def index(options = {}) def fetch_entries PaperTrail.serializer = JSON limit = params[:load] || 20 + offset = params[:offset] || 0 Version .includes(:user) .where(item_id: params[:encryptable_id]) .order(created_at: :desc) + .offset(offset) .limit(limit) end diff --git a/frontend/app/components/encryptable/show.js b/frontend/app/components/encryptable/show.js index 6c7f3e34b..e5959bb65 100644 --- a/frontend/app/components/encryptable/show.js +++ b/frontend/app/components/encryptable/show.js @@ -11,6 +11,7 @@ export default class ShowComponent extends Component { @tracked logs = []; loadAmount = 10; + offset = 0; constructor() { super(...arguments); @@ -71,17 +72,23 @@ export default class ShowComponent extends Component { this.store .query("version", { encryptableId: this.args.encryptable.id, - load: this.loadAmount + load: this.loadAmount, + offset: this.offset }) .then((res) => { - this.logs = res; + this.logs = res + .toArray() + .filter((log) => { + return !this.logs.includes(log); + }) + .concat(this.logs); this.toggleLoadMore(); }); } @action loadMore() { - this.loadAmount += 10; + this.offset += this.loadAmount; this.getLogs(); } From ee2bc927ba29c4b1af99ead68812432684356465 Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Tue, 24 May 2022 14:41:11 +0200 Subject: [PATCH 77/79] extend versions destroy spec --- spec/controllers/api/encryptables_controller_spec.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/spec/controllers/api/encryptables_controller_spec.rb b/spec/controllers/api/encryptables_controller_spec.rb index e551e166a..5cb08486d 100644 --- a/spec/controllers/api/encryptables_controller_spec.rb +++ b/spec/controllers/api/encryptables_controller_spec.rb @@ -704,15 +704,23 @@ context 'papertrail', versioning: true do context 'delete' do it 'deletes log history if encryptable is deleted' do + encryptable2 = encryptables(:credentials2) + 1000.times do encryptable.touch end + 500.times do + encryptable2.touch + end + expect(encryptable.versions.size).to eq(1000) + expect(encryptable2.versions.size).to eq(500) encryptable.destroy - expect(PaperTrail::Version.all.size).to be(0) + expect(encryptable.versions.size).to be(0) + expect(encryptable2.versions.size).to eq(500) end end From 8c7782af3619115dc5476fb6b5022b523da103ad Mon Sep 17 00:00:00 2001 From: renato-oester <91009092+renato-oester@users.noreply.github.com> Date: Tue, 24 May 2022 14:56:20 +0200 Subject: [PATCH 78/79] 572 log recent credentials on dashboard (#608) * add translation for Log * add translation * Started frontend implementation of Log butten/page not working atm. * Continued frontend implementation for personal log. Refresh on /log doesnt work and paper trail feature aren't included yet. * removed nav side bar from log page and fixed prettier/prettier error * Changed icon for Log in nav bar drop down menu, took the closest one available. * Fixed refresh bug when on /log page. (the bug mentioned in earlier commit) * shows encryptables on dashboard loads all encryptables of user * Added basic frontend test for nav bar * deleted unused code * Revert "deleted unused code" Not smart to do this in this branch This reverts commit 7c435b20d0139f13c5bbc815a7474d6a6bb0ae4c. * Add spec Test file for the Personal log * change name and add testes * change a 'not' mistake * Adds system specs for encryptables on dashboard * Fixes frontend spec for dashboard-card * Updated table in personal log. Added personal log controller and other stuff. * Added missing files from last commit * Renamed all files for personal log to personal_logs (was log before) * Fixed prettier/prettier errors and renamed one more file to personal_logs that i forgot before * Fixed one more file with prettier/prettier errors * personal log now loads logs from backend, first working version * some minor improvements, but personal log only works if you access it after opening an encryptable * Loads recent credentials from encryptables controller * cleanup encryptables_controller.rb * Revert "some minor improvements, but personal log only works if you access it after opening an encryptable" This reverts commit 00f99a8cfa3b4944f3f2b592cc221f9ae464e92b. * Revert "personal log now loads logs from backend, first working version" This reverts commit 516c69b4ef3ec9e6179a0ea0537677b4b473cee0. * Revert "Fixed one more file with prettier/prettier errors" This reverts commit 600369338866f379e01f68caabeaca18e87cc8bc. * Revert "Fixed prettier/prettier errors and renamed one more file to personal_logs that i forgot before" This reverts commit 032c002208cef08417827905f863b43d3f3712b1. * Revert "Renamed all files for personal log to personal_logs (was log before)" This reverts commit 682f3e01d7d5746329d787c2da1856401e1c11d9. * Revert "Added missing files from last commit" This reverts commit b71a8adae98049b507e9290771e13a216503ae03. * Revert "Updated table in personal log." This reverts commit bd243a2bd4eee36dda2102b4d8e9bbd2f82aaf38. * Revert " change a 'not' mistake" This reverts commit 81548f86f13e7e652b25f63ea80afb03e4482e47. * Revert " change name and add testes" This reverts commit 0eb12574c10207ed0c4c621cf3bb8501151c582f. * Revert " Add spec Test file for the Personal log" This reverts commit 08b59b35b0acbf4d7d45ef8d3990134eefd9234d. * Revert "Revert "deleted unused code"" This reverts commit 4d42a8058d8aa247f3c9db8cb1ef279d235b0326. * Revert "deleted unused code" This reverts commit 7c435b20d0139f13c5bbc815a7474d6a6bb0ae4c. * Revert "Added basic frontend test for nav bar" This reverts commit f52c87a0cf400ecad1fd51c6f2748c4b61174894. * Revert "Fixed refresh bug when on /log page. (the bug mentioned in earlier commit)" This reverts commit 24dd0767656425d696f8b929d15ccb89f0e76dab. * Revert "Changed icon for Log in nav bar drop down menu, took the closest one available." This reverts commit 4bcdc377243a51793bcc2dac11b508bda4697314. * Revert "removed nav side bar from log page and fixed prettier/prettier error" This reverts commit fc1cb07c7adf588073c8dc675ec7cd88e08ab986. * Revert "Continued frontend implementation for personal log. Refresh on /log doesnt work and paper trail feature aren't included yet." This reverts commit 903cecdbb8a7beaf06a208f99973997791d3aa9f. * Revert "Started frontend implementation of Log butten/page" This reverts commit 208e9af550334991a4e4a39b005b440df2483d16. * Revert " add translation" This reverts commit 992980a16e0ce30da2ed4c99fb5004a0bb1593e8. * Revert " add translation for Log" This reverts commit 5f57938b21fa056345c33336930d0fe1fa346db1. * Creates encryptables filtered_list presenter to handle parameters passed with requests * fixes rubocop warnings * Adds versions and corresponding encryptable fixtures to test database. Fixes specs * Adds system/controller specs for recent Credentials * starts implementing filter_by_query functionality for encryptables * completes filter_by_query functionality and specs for encryptables * fixes specs and fixtures * Recent credential requests now only return valid encryptables * fixes encryptables_controller_system_spec.rb * Fixes specs * Cleanup recent credentials fetching * Fix rubocop complaint * Changes the handling of request parameters * Removes some fixtures and fixes related specs * fixes recent Credentials specs by creating encryptables/logs during testing * code cleanup * extends controller specs for recent credentials functionality * fixes system specs * fixes rubocop errors * removes remains from merge conflict * pressed backspace once * change spelling in specs * add method true? to evaluate params * rewrite variable in camel_case * move method true? to base class filtered_list * include encryptables when loading logs * extend includes to also include folders * change fabricated credentials * fix error from merge conflict * fix rubocop errors * fix encryptables controller spec * change method fetch_entries to work with encryptable_file lookup * create method log_read_access to create logs instead of touch * fix rubocop errors * correct spelling and fill in mariana trench * rework fetch_entries method * fix fetch_entries method Co-authored-by: MuStAgEDelta Co-authored-by: Julien Gaumez Co-authored-by: Robin Steiner --- .../api/encryptables_controller.rb | 10 ++ app/presenters/encryptables/filtered_list.rb | 49 ++++++++++ app/presenters/filtered_list.rb | 4 + frontend/app/routes/dashboard.js | 15 ++- .../templates/components/dashboard-card.hbs | 4 +- frontend/app/templates/dashboard.hbs | 16 +++- .../components/dashboard-card-test.js | 2 +- .../api/encryptables_controller_spec.rb | 94 ++++++++++++++++++- .../encryptables/credentials_fabricator.rb | 2 +- spec/system/dashboard_system_spec.rb | 37 ++++++-- 10 files changed, 216 insertions(+), 17 deletions(-) create mode 100644 app/presenters/encryptables/filtered_list.rb diff --git a/app/controllers/api/encryptables_controller.rb b/app/controllers/api/encryptables_controller.rb index 371b7c3aa..bf8d8eaf5 100644 --- a/app/controllers/api/encryptables_controller.rb +++ b/app/controllers/api/encryptables_controller.rb @@ -72,6 +72,16 @@ def model_class end # rubocop:enable Metrics/MethodLength + def fetch_entries + if encryptable_file? + super + elsif tag_param.present? + user_encryptables.find_by(tag: tag_param) + else + Encryptables::FilteredList.new(current_user, params).fetch_entries + end + end + def build_entry return build_encryptable_file if encryptable_file? diff --git a/app/presenters/encryptables/filtered_list.rb b/app/presenters/encryptables/filtered_list.rb new file mode 100644 index 000000000..785dd0f47 --- /dev/null +++ b/app/presenters/encryptables/filtered_list.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +module ::Encryptables + class FilteredList < ::FilteredList + + def fetch_entries + filtered_encryptables = encryptables + + filtered_encryptables = filter_by_recent if recent? + filtered_encryptables = filter_by_query(filtered_encryptables) if query + + + filtered_encryptables + end + + private + + def query + @params[:q]&.strip&.downcase + end + + def recent? + true?(@params[:recent]) + end + + def encryptables + @current_user.encryptables + end + + def filter_by_query(encryptables) + encryptables.where( + 'lower(encryptables.description) LIKE :query + OR lower(encryptables.name) LIKE :query', + query: "%#{query}%" + ) + end + + def filter_by_recent + Version + .includes(:encryptable, encryptable: [:folder]) + .where(whodunnit: @current_user) + .order(created_at: :desc) + .group(:item_id, :item_type) + .select(:item_id, :item_type) + .limit(5) + .map(&:encryptable) + end + end +end diff --git a/app/presenters/filtered_list.rb b/app/presenters/filtered_list.rb index 92b3a2b62..fb3dce893 100644 --- a/app/presenters/filtered_list.rb +++ b/app/presenters/filtered_list.rb @@ -15,4 +15,8 @@ def fetch_entries def list_param(key) @params[key].to_a.map(&:to_i) end + + def true?(value) + %w[1 yes true].include?(value.to_s.downcase) + end end diff --git a/frontend/app/routes/dashboard.js b/frontend/app/routes/dashboard.js index 3094f7e35..a09592dba 100644 --- a/frontend/app/routes/dashboard.js +++ b/frontend/app/routes/dashboard.js @@ -12,22 +12,31 @@ export default class DashboardRoute extends BaseRoute { }; async model(params) { - params["limit"] = 20; const favouriteTeams = await this.getFavouriteTeams(params); - const teams = this.getTeams(params); + const teams = await this.getTeams(params); + const recentCredentials = await this.getRecentCredentials(params); + return RSVP.hash({ favouriteTeams, - teams + teams, + recentCredentials }); } async getFavouriteTeams(params) { + params["limit"] = 20; params["favourite"] = true; return await this.store.query("team", params); } async getTeams(params) { + params["limit"] = 20; params["favourite"] = false; return await this.store.query("team", params); } + + async getRecentCredentials(params) { + params["recent"] = true; + return await this.store.query("encryptable", params); + } } diff --git a/frontend/app/templates/components/dashboard-card.hbs b/frontend/app/templates/components/dashboard-card.hbs index 1c13f91bc..be8342ea0 100644 --- a/frontend/app/templates/components/dashboard-card.hbs +++ b/frontend/app/templates/components/dashboard-card.hbs @@ -7,5 +7,5 @@ -

{{this.team.name}}

- \ No newline at end of file +

{{this.content.name}}

+ diff --git a/frontend/app/templates/dashboard.hbs b/frontend/app/templates/dashboard.hbs index 1046fa61a..bfc3e46c8 100644 --- a/frontend/app/templates/dashboard.hbs +++ b/frontend/app/templates/dashboard.hbs @@ -1,20 +1,32 @@
+ {{#if @model.recentCredentials}} +

Recent Credentials

+
+ {{#each this.model.recentCredentials as |encryptable|}} + + + + {{/each}} +
+ {{/if}} + {{#if @model.favouriteTeams}}

Favourites

{{#each this.model.favouriteTeams as |team|}} - + {{/each}}
{{/if}} + {{#if this.model.teams}}

Teams

{{#each this.model.teams as |team|}} - + {{/each}}
diff --git a/frontend/tests/integration/components/dashboard-card-test.js b/frontend/tests/integration/components/dashboard-card-test.js index 42a1806c1..316b4b05a 100644 --- a/frontend/tests/integration/components/dashboard-card-test.js +++ b/frontend/tests/integration/components/dashboard-card-test.js @@ -20,7 +20,7 @@ module("Integration | Component | dashboard-card", function (hooks) { // Template block usage: await render(hbs` - + `); diff --git a/spec/controllers/api/encryptables_controller_spec.rb b/spec/controllers/api/encryptables_controller_spec.rb index 5cb08486d..5337ed2c4 100644 --- a/spec/controllers/api/encryptables_controller_spec.rb +++ b/spec/controllers/api/encryptables_controller_spec.rb @@ -21,7 +21,8 @@ get :index, params: { 'q': 'Personal' }, xhr: true - credentials1_json = data.second + expect(data.count).to eq 1 + credentials1_json = data.first credentials1_json_attributes = credentials1_json['attributes'] credentials1_json_relationships = credentials1_json['relationships'] @@ -37,7 +38,7 @@ expect_json_object_includes_keys(credentials1_json_relationships, nested_models) end - it 'returns all enncryptables if empty query param given' do + it 'returns all encryptables if empty query param given' do login_as(:alice) get :index, params: { 'q': '' }, xhr: true @@ -129,6 +130,87 @@ expect(response.status).to eq(403) end + + context 'recent Credentials' do + let!(:recent_credentials) do + folder = teams(:team1).folders.first + private_key = decrypt_private_key(bob) + team_password = folder.team.decrypt_team_password(bob, private_key) + Fabricate.times( + 6, + :credential, + folder: folder, + team_password: team_password + ) + end + + it 'returns most recent credentials' do + login_as(:alice) + + recent_credentials.each do |credential| + log_read_access(alice.id, credential) + end + + get :index, params: { recent: true }, xhr: true + + expect(response.status).to be(200) + expect(data.size).to eq(5) + attributes = data.first['attributes'] + expect(attributes['name']).to eq recent_credentials.last.name + expect(attributes['description']).to eq recent_credentials.last.description + end + + it 'shows most recently used credential first in list' do + login_as(:alice) + + + recent_credentials.each do |credential| + log_read_access(alice.id, credential) + end + log_read_access(alice.id, credentials1) + + get :index, params: { recent: true }, xhr: true + + expect(response.status).to be(200) + expect(data.size).to eq(5) + attributes = data.first['attributes'] + expect(attributes['name']).to eq credentials1.name + expect(attributes['description']).to eq credentials1.description + + end + it 'does not show credentials with no access' do + login_as(:bob) + + recent_credentials1 = recent_credentials.first + log_read_access(alice.id, recent_credentials1) + + get :index, params: { recent: true }, xhr: true + + expect(response.status).to be(200) + expect(data.size).to eq(0) + end + + it 'does not show deleted credentials' do + login_as(:alice) + + recent_credentials1 = recent_credentials.first + log_read_access(alice.id, recent_credentials1) + + get :index, params: { recent: true }, xhr: true + + expect(data.size).to eq(1) + attributes = data.first['attributes'] + expect(attributes['name']).to eq recent_credentials1.name + expect(attributes['description']).to eq recent_credentials1.description + + recent_credentials1.destroy! + + get :index, params: { recent: true }, xhr: true + + expect(response.status).to be(200) + expect(data.size).to eq(0) + end + end end context 'GET show' do @@ -785,4 +867,12 @@ def set_auth_headers request.headers['Authorization-User'] = bob.username request.headers['Authorization-Password'] = Base64.encode64('password') end + + def log_read_access(user_id, credential) + v = credential.paper_trail.save_with_version + v.whodunnit = user_id + v.event = :viewed + v.created_at = DateTime.now + v.save! + end end diff --git a/spec/fabricators/encryptables/credentials_fabricator.rb b/spec/fabricators/encryptables/credentials_fabricator.rb index dd4cd5d7a..8797270d9 100644 --- a/spec/fabricators/encryptables/credentials_fabricator.rb +++ b/spec/fabricators/encryptables/credentials_fabricator.rb @@ -7,7 +7,7 @@ Fabricator(:credential, from: 'Encryptable::Credentials') do transient :team_password - name { Faker::Team.creature } + name { sequence(:name) { |i| Faker::Team.creature + " #{i}" } } cleartext_username { Faker::Internet.user_name } cleartext_password { Faker::Internet.password } before_save do |account, attrs| diff --git a/spec/system/dashboard_system_spec.rb b/spec/system/dashboard_system_spec.rb index 20d2e2182..44f9a987e 100644 --- a/spec/system/dashboard_system_spec.rb +++ b/spec/system/dashboard_system_spec.rb @@ -5,30 +5,55 @@ describe 'Dashboard', type: :system, js: true do include SystemHelpers + let(:credentials) { encryptables(:credentials1) } + let(:team) { teams(:team1) } + + it 'renders dashboard grid' do login_as_user(:alice) + visit("/encryptables/#{credentials.id}") + visit('/dashboard') expect(page.current_path).to eq('/dashboard') - expect(page).to have_selector('pzsh-hero', visible: true) expect(page).to have_selector('pzsh-banner', visible: true) + expect(page).to have_text('Recent Credentials', count: 1) expect(page).to have_text('Favourites', count: 1) expect(page).to have_text('Teams', count: 1) - expect(page).not_to have_selector 'div.content' - - expect(page).to have_selector('div.dashboard-grid-card', count: 3) + expect(page).to have_selector('div.primary-content', count: 3) end it 'navigates to team on team card click' do login_as_user(:alice) + visit("/encryptables/#{credentials.id}") + visit('/dashboard') + expect(page.current_path).to eq('/dashboard') expect(page).to have_selector('pzsh-hero', visible: true) - all('div.dashboard-grid-card').first.click - expect(page.current_path).to eq('/teams/235930340') + first('div.dashboard-grid-card', text: team.name).click + + expect(page.current_path).to eq("/teams/#{team.id}") expect(page).to have_selector 'div.content' end + + it 'lists recently accessed encryptable' do + login_as_user(:alice) + visit("/encryptables/#{credentials.id}") + visit('/dashboard') + + + expect(page.current_path).to eq('/dashboard') + + expect(page).to have_selector('pzsh-hero', visible: true) + + first('div.dashboard-grid-card', text: credentials.name).click + + expect(page.current_path).to eq("/encryptables/#{credentials.id}") + expect(page).to have_selector 'div.content' + end + end From 0b4527434b37e8779a06d7c302ddfc510dc041ef Mon Sep 17 00:00:00 2001 From: RamonaChristen Date: Fri, 3 Jun 2022 13:37:25 +0200 Subject: [PATCH 79/79] change translations in line with changes on main --- frontend/app/templates/components/encryptable/table-row.hbs | 2 +- frontend/app/templates/components/encryptable/table.hbs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frontend/app/templates/components/encryptable/table-row.hbs b/frontend/app/templates/components/encryptable/table-row.hbs index fed68854e..cd49c1eb7 100644 --- a/frontend/app/templates/components/encryptable/table-row.hbs +++ b/frontend/app/templates/components/encryptable/table-row.hbs @@ -1,5 +1,5 @@
- + diff --git a/frontend/app/templates/components/encryptable/table.hbs b/frontend/app/templates/components/encryptable/table.hbs index be7195c4b..bfe687565 100644 --- a/frontend/app/templates/components/encryptable/table.hbs +++ b/frontend/app/templates/components/encryptable/table.hbs @@ -1,9 +1,9 @@
{{t "actions"}} {{t "username" }} {{t "encryptables.show.created_at" }} {{t "encryptables.show.action"}} {{t "user" }} {{t "encryptables.show.date_time" }}
{{t (concat "encryptables.log." @paperTrailVersion.event)}}{{t (concat "encryptable/credentials.log." @paperTrailVersion.event)}} {{@paperTrailVersion.username}} {{moment-format @paperTrailVersion.createdAt "DD.MM.YYYY hh:mm"}}
- + - +
{{t "encryptables.show.action"}} {{t "encryptable/credentials.show.action"}} {{t "user" }} {{t "encryptables.show.date_time" }} {{t "encryptable/credentials.show.date_time" }}