-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa9a461
commit 00245d1
Showing
28 changed files
with
493 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
class AuditEvent < ApplicationRecord | ||
ACTION_TYPES = { | ||
user_registered: 1, | ||
user_logged_in: 2, | ||
}.with_indifferent_access | ||
|
||
enum :action_type, ACTION_TYPES, prefix: :action | ||
|
||
belongs_to :author, class_name: 'User', inverse_of: :authored_audit_events | ||
|
||
validates :entity_id, presence: true | ||
validates :entity_type, presence: true | ||
validates :action_type, presence: true, | ||
inclusion: { | ||
in: ACTION_TYPES.keys.map(&:to_s), | ||
} | ||
validate :validate_details | ||
validates :target_id, presence: true | ||
validates :target_type, presence: true | ||
|
||
def validate_details | ||
errors.add(:details, :blank) if details.nil? | ||
|
||
errors.add(:details, :invalid) unless details.is_a?(Hash) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module AuditService | ||
module_function | ||
|
||
REQUIRED_EVENT_KEYS = %i[author_id entity_id entity_type details target_id target_type].freeze | ||
|
||
def audit(type, payload) | ||
Sagittarius::Context.with_context do |context| | ||
payload[:author_id] ||= context[:user][:id] | ||
payload[:ip_address] = context[:ip_address] | ||
|
||
if payload.key?(:entity) | ||
entity = payload.delete(:entity) | ||
payload[:entity_id] = entity.id | ||
payload[:entity_type] = entity.class.name | ||
end | ||
|
||
if payload.key?(:target) | ||
target = payload.delete(:target) | ||
payload[:target_id] = target.id | ||
payload[:target_type] = target.class.name | ||
end | ||
|
||
missing_keys = REQUIRED_EVENT_KEYS.reject do |key| | ||
payload.key?(key) | ||
end | ||
|
||
raise InvalidAuditEvent, "Audit Event is missing the #{missing_keys} attributes" unless missing_keys.empty? | ||
|
||
AuditEvent.create!( | ||
action_type: type, | ||
**payload | ||
) | ||
end | ||
end | ||
|
||
class InvalidAuditEvent < StandardError; end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
class UserRegisterService | ||
include Sagittarius::Database::Transactional | ||
include Sagittarius::Loggable | ||
|
||
attr_reader :username, :email, :password | ||
|
||
def initialize(username, email, password) | ||
@username = username | ||
@email = email | ||
@password = password | ||
end | ||
|
||
def execute | ||
transactional do | ||
user = User.create(username: username, email: email, password: password) | ||
return ServiceResponse.error(message: 'User is invalid', payload: user.errors) unless user.persisted? | ||
|
||
AuditService.audit( | ||
:user_registered, | ||
author_id: user.id, | ||
entity: user, | ||
details: { username: username, email: email }, | ||
target: user | ||
) | ||
|
||
logger.info(message: 'Created new user', user_id: user.id, username: user.username) | ||
ServiceResponse.success(payload: user) | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../lib/sagittarius/context' | ||
require_relative '../../lib/sagittarius/middleware/rack/context' | ||
require_relative '../../lib/sagittarius/middleware/rack/ip_address' | ||
|
||
Rails.application.config.middleware.move(1, ActionDispatch::RequestId) | ||
Rails.application.config.middleware.insert(1, Sagittarius::Middleware::Rack::Context) | ||
Rails.application.config.middleware.insert_after(ActionDispatch::RemoteIp, Sagittarius::Middleware::Rack::IpAddress) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateAuditEvents < Sagittarius::Database::Migration[1.0] | ||
def change | ||
create_table :audit_events do |t| | ||
t.references :author, null: false, foreign_key: { to_table: :users } | ||
t.integer :entity_id, null: false | ||
t.text :entity_type, null: false | ||
t.integer :action_type, null: false | ||
t.jsonb :details, null: false | ||
t.inet :ip_address | ||
t.integer :target_id, null: false | ||
t.text :target_type, null: false | ||
|
||
t.timestamps_with_timezone | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2a886db15701ff25494a5a3b7aad16f18a4db7831cfaa681d5265093b2d989b7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sagittarius | ||
module Database | ||
module Transactional | ||
module_function | ||
|
||
def transactional | ||
return_value = nil | ||
|
||
ActiveRecord::Base.transaction do | ||
return_value = yield | ||
end | ||
|
||
return_value | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sagittarius | ||
module Middleware | ||
module Rack | ||
class Context | ||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
Sagittarius::Context.with_context( | ||
Sagittarius::Context::CORRELATION_ID_KEY => correlation_id(env), | ||
application: 'puma' | ||
) do |context| | ||
status, headers, response = @app.call env | ||
headers['X-Sagittarius-Meta'] = context_to_json context | ||
[status, headers, response] | ||
end | ||
end | ||
|
||
def correlation_id(env) | ||
ActionDispatch::Request.new(env).request_id | ||
end | ||
|
||
def context_to_json(context) | ||
context | ||
.to_h | ||
.transform_keys { |k| k.delete_prefix("#{Sagittarius::Context::LOG_KEY}.") } | ||
.to_json | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.