-
Notifications
You must be signed in to change notification settings - Fork 34
Add papertrail gem #587
base: master
Are you sure you want to change the base?
Add papertrail gem #587
Changes from all commits
e98d484
8db48ad
9317dcd
5cd488a
e9d37a3
045b832
7983e14
e20bd83
7f50548
8775a62
5c9f03a
96a63c0
610a59b
f33f40f
2473cb2
acfc099
d73cae2
3744505
c135350
f9efbb4
f265b72
ad72818
590be35
cf7c8b7
d2a96b3
d744841
9ae217a
6be41fe
739a20f
5a064b1
fbe53cf
ab7e6cb
00dd3e1
477125a
5b61e29
8508e8a
1b63d3a
04d1424
cc89947
1017f8c
133b5a5
cb57571
6ac6a91
d537036
b88a00e
e98beef
1417b36
5c1ac4b
df3a9c9
5aec2e0
b991304
0de70d1
a1aabdd
51c8933
23fd312
f79ad57
c06353f
6d84e7f
b355ea1
e055936
5b352a9
37a0ba0
3286250
143eb24
791a15e
25937f6
93aebfb
30b9714
77d08a7
d478e36
c56df1a
679ea3f
17bb5d9
f439030
b737cbc
58f413c
f19dc36
8402150
c2096e0
f21bbbf
94f42b4
b4a0fa5
8aa4ddc
93dc302
41e728c
67661b0
1217ef4
ee2bc92
8c7782a
65b5d3d
7923d78
0b45274
f3a4e62
1ad9130
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class Api::LogsController < ApiController | ||
|
|
||
| def index(options = {}) | ||
| authorize(team, :index?, policy_class: LogPolicy) | ||
| render({ json: fetch_entries, | ||
| each_serializer: @model_serializer ||= LogsSerializer } | ||
| .merge(render_options) | ||
| .merge(options.fetch(:render_options, {}))) | ||
| end | ||
|
|
||
| def fetch_entries | ||
Robin481 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 | ||
|
|
||
| def team | ||
| @team ||= Encryptable.find(params[:encryptable_id]).folder.team | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,12 @@ | |
|
|
||
| class Encryptable < ApplicationRecord | ||
|
|
||
| has_paper_trail on: [:touch, :update], ignore: [:tag, :type], versions: { | ||
| class_name: 'Version' | ||
| } | ||
|
|
||
| before_destroy :destroy_versions | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is that necessary? (not necessarily criticizing just trying to understand)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @Robin481 The papertrail versions are not deleted from the db by default, if the encryptable they correspond to is deleted. We discussed if it would make sense to keep the old versions because the api user may still want to know what happened to an encryptable that doesn't exist anymore. However, keeping the old encryptables may pollute the db quite heavily. What do you think?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes deleting them makes sense.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Robin481 Yes,
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update on this front?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems to be a bug within the papertrail gem, |
||
|
|
||
| serialize :encrypted_data, ::EncryptedData | ||
|
|
||
| attr_readonly :type | ||
|
|
@@ -71,4 +77,8 @@ def decrypt_attr(attr, team_password) | |
| instance_variable_set("@cleartext_#{attr}", cleartext_value) | ||
| end | ||
|
|
||
| def destroy_versions | ||
| versions.destroy_all | ||
| end | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class Version < PaperTrail::Version | ||
| belongs_to :user, class_name: 'User', foreign_key: :whodunnit | ||
| belongs_to :encryptable, class_name: 'Encryptable', foreign_key: :item_id | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class LogPolicy < TeamPolicy | ||
| def index? | ||
| team_member? | ||
| end | ||
|
|
||
| def show? | ||
| @team.teammember?(@user.id) && [email protected]_a?(User::Api) | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class LogsSerializer < ApplicationSerializer | ||
| attributes :id, :item_type, :event, :user_id, :username, :created_at | ||
|
|
||
| belongs_to :encryptable | ||
RamonaChristen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def user_id | ||
| object.whodunnit | ||
| end | ||
|
|
||
| def encryptable | ||
| object.item | ||
| end | ||
|
|
||
| def username | ||
| object.user.username | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| 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); | ||
| } | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.