Skip to content

Commit

Permalink
chore(CE): add audit logs to users controller (#449)
Browse files Browse the repository at this point in the history
Co-authored-by: TivonB-AI2 <[email protected]>
  • Loading branch information
github-actions[bot] and TivonB-AI2 authored Oct 30, 2024
1 parent 4db64a2 commit 82172fc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
9 changes: 9 additions & 0 deletions server/app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@
module Api
module V1
class UsersController < ApplicationController
include AuditLogger
after_action :create_audit_log

def me
authorize current_user
render json: current_user, serializer: UserSerializer, workspace_id: current_workspace.id
end

private

def create_audit_log
audit!(resource_id: params[:id])
end
end
end
end
33 changes: 33 additions & 0 deletions server/spec/requests/api/v1/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@
expect(response_hash.dig(:data, :attributes, :created_at)).not_to be_nil
expect(response_hash.dig(:data, :attributes, :role)).to eq("Admin")
expect(response_hash.dig(:data, :attributes, :status)).to eq("active")

audit_log = AuditLog.last
expect(audit_log).not_to be_nil
expect(audit_log.user_id).to eq(user.id)
expect(audit_log.action).to eq("me")
expect(audit_log.resource_type).to eq("User")
expect(audit_log.resource_id).to eq(nil)
expect(audit_log.resource).to eq(nil)
expect(audit_log.workspace_id).to eq(workspace.id)
expect(audit_log.created_at).not_to be_nil
expect(audit_log.updated_at).not_to be_nil
end

it "returns user details withg member role" do
Expand All @@ -45,6 +56,17 @@
expect(response_hash.dig(:data, :attributes, :created_at)).not_to be_nil
expect(response_hash.dig(:data, :attributes, :role)).to eq("Member")
expect(response_hash.dig(:data, :attributes, :status)).to eq("active")

audit_log = AuditLog.last
expect(audit_log).not_to be_nil
expect(audit_log.user_id).to eq(user.id)
expect(audit_log.action).to eq("me")
expect(audit_log.resource_type).to eq("User")
expect(audit_log.resource_id).to eq(nil)
expect(audit_log.resource).to eq(nil)
expect(audit_log.workspace_id).to eq(workspace.id)
expect(audit_log.created_at).not_to be_nil
expect(audit_log.updated_at).not_to be_nil
end

it "returns user details withg viewer role" do
Expand All @@ -59,6 +81,17 @@
expect(response_hash.dig(:data, :attributes, :created_at)).not_to be_nil
expect(response_hash.dig(:data, :attributes, :role)).to eq("Viewer")
expect(response_hash.dig(:data, :attributes, :status)).to eq("active")

audit_log = AuditLog.last
expect(audit_log).not_to be_nil
expect(audit_log.user_id).to eq(user.id)
expect(audit_log.action).to eq("me")
expect(audit_log.resource_type).to eq("User")
expect(audit_log.resource_id).to eq(nil)
expect(audit_log.resource).to eq(nil)
expect(audit_log.workspace_id).to eq(workspace.id)
expect(audit_log.created_at).not_to be_nil
expect(audit_log.updated_at).not_to be_nil
end
end
end
Expand Down

0 comments on commit 82172fc

Please sign in to comment.