From 43210360e9f25f9bca1b3676db54570ac2a8d815 Mon Sep 17 00:00:00 2001 From: 2458773093 <2458773093@protonmail.com> Date: Mon, 29 Jul 2019 22:45:29 +0300 Subject: [PATCH] group admin methods for essential management --- app/controllers/admin/groups_controller.rb | 67 ++++++++++++++++++++++ app/policies/group_policy.rb | 20 +++++-- app/views/admin/groups/_group.html.haml | 14 +++++ app/views/admin/groups/index.html.haml | 26 +++++++++ config/locales/en.yml | 7 +++ config/navigation.rb | 1 + config/routes.rb | 7 +++ 7 files changed, 138 insertions(+), 4 deletions(-) create mode 100644 app/controllers/admin/groups_controller.rb create mode 100644 app/views/admin/groups/_group.html.haml create mode 100644 app/views/admin/groups/index.html.haml diff --git a/app/controllers/admin/groups_controller.rb b/app/controllers/admin/groups_controller.rb new file mode 100644 index 00000000..18519287 --- /dev/null +++ b/app/controllers/admin/groups_controller.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +module Admin + class GroupsController < BaseController + before_action :set_group, except: [:index] + before_action :set_filter_params + + def index + authorize :group, :index? + @groups = filtered_groups.page(params[:page]) + end + + def destroy + authorize @group, :destroy? + @group.destroy! + log_action :destroy, @group + flash[:notice] = I18n.t('admin.groups.destroyed_msg') + redirect_to admin_groups_path(page: params[:page], **@filter_params) + end + + def enable_featured + authorize @group, :update? + @group.is_featured = true + @group.save! + log_action :update, @group + flash[:notice] = I18n.t('admin.groups.updated_msg') + redirect_to admin_groups_path(page: params[:page], **@filter_params) + end + + def disable_featured + authorize @group, :update? + @group.is_featured = false + @group.save! + log_action :update, @group + flash[:notice] = I18n.t('admin.groups.updated_msg') + redirect_to admin_groups_path(page: params[:page], **@filter_params) + end + + private + + def set_group + @group = Group.find(params[:id]) + end + + def set_filter_params + @filter_params = filter_params.to_hash.symbolize_keys + end + + def resource_params + params.require(:group).permit(:is_featured, :is_nsfw) + end + + def filtered_groups + query = Group.order('is_featured DESC, member_count DESC') + + if params[:title] + query = query.where("LOWER(title) LIKE LOWER(?)", "%#{params[:title]}%") + end + + return query + end + + def filter_params + params.permit(:sort,) + end + end +end diff --git a/app/policies/group_policy.rb b/app/policies/group_policy.rb index 6f2b914b..76843abc 100644 --- a/app/policies/group_policy.rb +++ b/app/policies/group_policy.rb @@ -1,14 +1,26 @@ # frozen_string_literal: true class GroupPolicy < ApplicationPolicy + def index? + true + end + def update? - check_archive! - is_group_admin? + if admin? + true + else + check_archive! + is_group_admin? + end end def destroy? - check_archive! - is_group_admin? + if admin? + true + else + check_archive! + is_group_admin? + end end def approve_status? diff --git a/app/views/admin/groups/_group.html.haml b/app/views/admin/groups/_group.html.haml new file mode 100644 index 00000000..9ab97ff8 --- /dev/null +++ b/app/views/admin/groups/_group.html.haml @@ -0,0 +1,14 @@ +%tr + %td= group.id + %td= group.title + %td= group.member_count + %td + - if group.is_featured? + = t('admin.groups.featured') + %td + - if not group.is_featured? + = table_link_to 'power-off', t('admin.groups.enable_featured'), enable_featured_admin_group_path(group, page: params[:page], **@filter_params), method: :post, data: { confirm: t('admin.accounts.are_you_sure') } + - else + = table_link_to 'power-off', t('admin.groups.disable_featured'), disable_featured_admin_group_path(group, page: params[:page], **@filter_params), method: :post, data: { confirm: t('admin.accounts.are_you_sure') } + %td + = table_link_to 'times', t('admin.groups.delete'), admin_group_path(group, page: params[:page], **@filter_params), method: :delete, data: { confirm: t('admin.accounts.are_you_sure') } diff --git a/app/views/admin/groups/index.html.haml b/app/views/admin/groups/index.html.haml new file mode 100644 index 00000000..27efacbb --- /dev/null +++ b/app/views/admin/groups/index.html.haml @@ -0,0 +1,26 @@ +- content_for :page_title do + = t('admin.groups.title') + += form_tag admin_groups_url, method: 'GET', class: 'simple_form' do + .fields-group + .input.string.optional + = text_field_tag :title, params[:title], class: 'string optional', placeholder: I18n.t("admin.groups.name") + + .actions + %button= t('admin.accounts.search') + = link_to t('admin.accounts.reset'), admin_groups_path, class: 'button negative' + +.table-wrapper + %table.table + %thead + %tr + %th= t('admin.groups.id') + %th= t('admin.groups.title') + %th= t('admin.groups.member_count') + %th + %th + %th + %tbody + = render @groups + += paginate @groups \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 56a785c8..46e98496 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -224,6 +224,13 @@ en: update_status: "%{name} updated status by %{target}" deleted_status: "(deleted status)" title: Audit log + groups: + title: Groups + name: Group title + member_count: Members + featured: Featured + enable_featured: Feature + disable_featured: Unfeature custom_emojis: by_domain: Domain copied_msg: Successfully created local copy of the emoji diff --git a/config/navigation.rb b/config/navigation.rb index 5d5b3385..531a19b3 100644 --- a/config/navigation.rb +++ b/config/navigation.rb @@ -48,6 +48,7 @@ SimpleNavigation::Configuration.run do |navigation| n.item :admin, safe_join([fa_icon('cogs fw'), t('admin.title')]), admin_dashboard_url, if: proc { current_user.staff? } do |s| s.item :dashboard, safe_join([fa_icon('tachometer fw'), t('admin.dashboard.title')]), admin_dashboard_url s.item :settings, safe_join([fa_icon('cogs fw'), t('admin.settings.title')]), edit_admin_settings_url, if: -> { current_user.admin? }, highlights_on: %r{/admin/settings} + s.item :groups, safe_join([fa_icon('smile-o fw'), t('admin.groups.title')]), admin_groups_url, highlights_on: %r{/admin/groups} s.item :custom_emojis, safe_join([fa_icon('smile-o fw'), t('admin.custom_emojis.title')]), admin_custom_emojis_url, highlights_on: %r{/admin/custom_emojis} s.item :relays, safe_join([fa_icon('exchange fw'), t('admin.relays.title')]), admin_relays_url, if: -> { current_user.admin? }, highlights_on: %r{/admin/relays} s.item :subscriptions, safe_join([fa_icon('paper-plane-o fw'), t('admin.subscriptions.title')]), admin_subscriptions_url, if: -> { current_user.admin? } diff --git a/config/routes.rb b/config/routes.rb index 78a44017..30707326 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -245,6 +245,13 @@ Rails.application.routes.draw do end end + resources :groups, only: [:index, :destroy] do + member do + post :enable_featured + post :disable_featured + end + end + resources :account_moderation_notes, only: [:create, :destroy] resources :tags, only: [:index] do