Skip to content

Commit

Permalink
feat: Ability to update avatars from super admin (#7264)
Browse files Browse the repository at this point in the history
- Ability to update user avatars from super admin
- Ability to update bot avatars from super admin

fixes: #7060
  • Loading branch information
sojan-official committed Jun 9, 2023
1 parent c715e39 commit 48f2e58
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 3 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -75,6 +75,7 @@ gem 'jwt'
gem 'pundit'
# super admin
gem 'administrate'
gem 'administrate-field-active_storage'

##--- gems for pubsub service ---##
# https://karolgalanciak.com/blog/2019/11/30/from-activerecord-callbacks-to-publish-slash-subscribe-pattern-and-event-driven-design/
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Expand Up @@ -113,6 +113,9 @@ GEM
kaminari (>= 1.0)
sassc-rails (~> 2.1)
selectize-rails (~> 0.6)
administrate-field-active_storage (0.4.2)
administrate (>= 0.2.2)
rails (>= 7.0)
annotate (3.2.0)
activerecord (>= 3.2, < 8.0)
rake (>= 10.4, < 14.0)
Expand Down Expand Up @@ -798,6 +801,7 @@ DEPENDENCIES
activerecord-import
acts-as-taggable-on
administrate
administrate-field-active_storage
annotate
attr_extras
audited (~> 5.3)
Expand Down
1 change: 1 addition & 0 deletions app/assets/config/manifest.js
@@ -1,5 +1,6 @@
//= link_tree ../images
//= link administrate/application.css
//= link administrate/application.js
//= link administrate-field-active_storage/application.css
//= link dashboardChart.js
//= link secretField.js
10 changes: 10 additions & 0 deletions app/controllers/super_admin/agent_bots_controller.rb
Expand Up @@ -41,4 +41,14 @@ class SuperAdmin::AgentBotsController < SuperAdmin::ApplicationController

# See https://administrate-prototype.herokuapp.com/customizing_controller_actions
# for more information

def destroy_avatar
avatar = requested_resource.avatar
avatar.purge
redirect_back(fallback_location: super_admin_agent_bots_path)
end

def scoped_resource
resource_class.with_attached_avatar
end
end
11 changes: 11 additions & 0 deletions app/controllers/super_admin/users_controller.rb
Expand Up @@ -45,6 +45,17 @@ def create
# empty values into nil values. It uses other APIs such as `resource_class`
# and `dashboard`:
#

def destroy_avatar
avatar = requested_resource.avatar
avatar.purge
redirect_back(fallback_location: super_admin_users_path)
end

def scoped_resource
resource_class.with_attached_avatar
end

def resource_params
permitted_params = super
permitted_params.delete(:password) if permitted_params[:password].blank?
Expand Down
7 changes: 7 additions & 0 deletions app/dashboards/agent_bot_dashboard.rb
Expand Up @@ -10,6 +10,11 @@ class AgentBotDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
access_token: Field::HasOne,
avatar_url: AvatarField,
avatar: Field::ActiveStorage.with_options(
destroy_url: proc do |_namespace, _resource, attachment|
[:avatar_super_admin_agent_bot, { attachment_id: attachment.id }]
end
),
id: Field::Number,
name: Field::String,
account: Field::BelongsTo.with_options(searchable: true, searchable_field: 'name', order: 'id DESC'),
Expand All @@ -36,6 +41,7 @@ class AgentBotDashboard < Administrate::BaseDashboard
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = %i[
id
avatar_url
account
name
description
Expand All @@ -47,6 +53,7 @@ class AgentBotDashboard < Administrate::BaseDashboard
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = %i[
name
avatar
account
description
outgoing_url
Expand Down
6 changes: 6 additions & 0 deletions app/dashboards/user_dashboard.rb
Expand Up @@ -11,6 +11,11 @@ class UserDashboard < Administrate::BaseDashboard
account_users: Field::HasMany,
id: Field::Number,
avatar_url: AvatarField,
avatar: Field::ActiveStorage.with_options(
destroy_url: proc do |_namespace, _resource, attachment|
[:avatar_super_admin_user, { attachment_id: attachment.id }]
end
),
provider: Field::String,
uid: Field::String,
password: Field::Password,
Expand Down Expand Up @@ -69,6 +74,7 @@ class UserDashboard < Administrate::BaseDashboard
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = %i[
name
avatar
display_name
email
password
Expand Down
9 changes: 7 additions & 2 deletions config/routes.rb
Expand Up @@ -399,10 +399,15 @@
post :seed, on: :member
post :reset_cache, on: :member
end
resources :users, only: [:index, :new, :create, :show, :edit, :update, :destroy]
resources :users, only: [:index, :new, :create, :show, :edit, :update, :destroy] do
delete :avatar, on: :member, action: :destroy_avatar
end

resources :access_tokens, only: [:index, :show]
resources :installation_configs, only: [:index, :new, :create, :show, :edit, :update]
resources :agent_bots, only: [:index, :new, :create, :show, :edit, :update]
resources :agent_bots, only: [:index, :new, :create, :show, :edit, :update] do
delete :avatar, on: :member, action: :destroy_avatar
end
resources :platform_apps, only: [:index, :new, :create, :show, :edit, :update]
resource :instance_status, only: [:show]

Expand Down
21 changes: 21 additions & 0 deletions spec/controllers/super_admin/agent_bots_controller_spec.rb
Expand Up @@ -22,4 +22,25 @@
end
end
end

describe 'DELETE /super_admin/agent_bots/:id/destroy_avatar' do
let!(:agent_bot) { create(:agent_bot, :with_avatar) }

context 'when it is an unauthenticated super admin' do
it 'returns unauthorized' do
delete "/super_admin/agent_bots/#{agent_bot.id}/avatar", params: { attachment_id: agent_bot.avatar.id }
expect(response).to have_http_status(:redirect)
expect(agent_bot.reload.avatar).to be_attached
end
end

context 'when it is an authenticated super admin' do
it 'destroys the avatar' do
sign_in(super_admin, scope: :super_admin)
delete "/super_admin/agent_bots/#{agent_bot.id}/avatar", params: { attachment_id: agent_bot.avatar.id }
expect(response).to have_http_status(:redirect)
expect(agent_bot.reload.avatar).not_to be_attached
end
end
end
end
21 changes: 21 additions & 0 deletions spec/controllers/super_admin/users_controller_spec.rb
Expand Up @@ -45,4 +45,25 @@
end
end
end

describe 'DELETE /super_admin/users/:id/avatar' do
let!(:user) { create(:user, :with_avatar) }

context 'when it is an unauthenticated super admin' do
it 'returns unauthorized' do
delete "/super_admin/users/#{user.id}/avatar", params: { attachment_id: user.avatar.id }
expect(response).to have_http_status(:redirect)
expect(user.reload.avatar).to be_attached
end
end

context 'when it is an authenticated super admin' do
it 'destroys the avatar' do
sign_in(super_admin, scope: :super_admin)
delete "/super_admin/users/#{user.id}/avatar", params: { attachment_id: user.avatar.id }
expect(response).to have_http_status(:redirect)
expect(user.reload.avatar).not_to be_attached
end
end
end
end
4 changes: 4 additions & 0 deletions spec/factories/agent_bots.rb
Expand Up @@ -9,5 +9,9 @@
trait :skip_validate do
to_create { |instance| instance.save(validate: false) }
end

trait :with_avatar do
avatar { fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png') }
end
end
end
2 changes: 1 addition & 1 deletion spec/factories/users.rb
Expand Up @@ -26,7 +26,7 @@
end

trait :with_avatar do
avatar { Rack::Test::UploadedFile.new('spec/assets/avatar.png', 'image/png') }
avatar { fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png') }
end

trait :administrator do
Expand Down

0 comments on commit 48f2e58

Please sign in to comment.