From b2551361d7cdefc33764c6b909a4fff383ac2111 Mon Sep 17 00:00:00 2001 From: songtianlun Date: Tue, 11 Feb 2025 17:40:13 +0800 Subject: [PATCH] feat: add admin management features for cities - Update menu labels for cities, countries, and regions - Introduce new entities for states and subregions in Admin Panel - Implement admin authentication for weather art generation - Modify application controller to check for admin user - Refactor view to display admin panel based on user permissions - Update routes to include weather art generation action These changes enhance the admin interface for better management of cities and related entities. The new admin checks ensure that only authorized users can generate weather art, improving security and functionality. --- app/admin/cities.rb | 2 +- app/admin/countries.rb | 2 +- app/admin/regions.rb | 2 +- app/admin/states.rb | 19 +++++ app/admin/subregions.rb | 19 +++++ app/admin/users.rb | 19 +++++ app/controllers/application_controller.rb | 7 ++ app/controllers/cities_controller.rb | 25 +++++++ app/helpers/application_helper.rb | 2 +- app/views/cities/_admin_panel.html.erb | 89 +++++++++++++---------- app/views/cities/show.html.erb | 2 + config/initializers/active_admin.rb | 7 +- config/initializers/devise.rb | 2 +- config/routes.rb | 8 +- 14 files changed, 159 insertions(+), 46 deletions(-) create mode 100644 app/admin/states.rb create mode 100644 app/admin/subregions.rb create mode 100644 app/admin/users.rb diff --git a/app/admin/cities.rb b/app/admin/cities.rb index 52019b5..5c2c4e6 100644 --- a/app/admin/cities.rb +++ b/app/admin/cities.rb @@ -1,5 +1,5 @@ ActiveAdmin.register City do - menu label: "City Manager", parent: "系统管理" + menu label: "Cities", parent: "数据管理" controller do def find_resource scoped_collection.friendly.find(params[:id]) diff --git a/app/admin/countries.rb b/app/admin/countries.rb index c6be79f..1989501 100644 --- a/app/admin/countries.rb +++ b/app/admin/countries.rb @@ -1,5 +1,5 @@ ActiveAdmin.register Country do - menu label: "Country Manager", parent: "系统管理" + menu label: "Countries", parent: "数据管理" controller do def find_resource scoped_collection.friendly.find(params[:id]) diff --git a/app/admin/regions.rb b/app/admin/regions.rb index 3827b06..ee11c03 100644 --- a/app/admin/regions.rb +++ b/app/admin/regions.rb @@ -1,5 +1,5 @@ ActiveAdmin.register Region do - menu label: "Region Manager", parent: "系统管理" + menu label: "Regions", parent: "数据管理" # See permitted parameters documentation: # https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters # diff --git a/app/admin/states.rb b/app/admin/states.rb new file mode 100644 index 0000000..ff4edf5 --- /dev/null +++ b/app/admin/states.rb @@ -0,0 +1,19 @@ +ActiveAdmin.register State do + menu label: "States", parent: "数据管理" + + # See permitted parameters documentation: + # https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters + # + # Uncomment all parameters which should be permitted for assignment + # + # permit_params :name, :code, :country_id, :country_code, :fips_code, :iso2, :state_type, :level, :parent_id, :latitude, :longitude, :flag, :wiki_data_id + # + # or + # + # permit_params do + # permitted = [:name, :code, :country_id, :country_code, :fips_code, :iso2, :state_type, :level, :parent_id, :latitude, :longitude, :flag, :wiki_data_id] + # permitted << :other if params[:action] == 'create' && current_user.admin? + # permitted + # end + +end diff --git a/app/admin/subregions.rb b/app/admin/subregions.rb new file mode 100644 index 0000000..303eb68 --- /dev/null +++ b/app/admin/subregions.rb @@ -0,0 +1,19 @@ +ActiveAdmin.register Subregion do + menu label: "SubRegions", parent: "数据管理" + + # See permitted parameters documentation: + # https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters + # + # Uncomment all parameters which should be permitted for assignment + # + # permit_params :name, :translations, :region_id, :flag, :wiki_data_id + # + # or + # + # permit_params do + # permitted = [:name, :translations, :region_id, :flag, :wiki_data_id] + # permitted << :other if params[:action] == 'create' && current_user.admin? + # permitted + # end + +end diff --git a/app/admin/users.rb b/app/admin/users.rb new file mode 100644 index 0000000..594f14b --- /dev/null +++ b/app/admin/users.rb @@ -0,0 +1,19 @@ +ActiveAdmin.register User do + menu label: "Users", parent: "数据管理" + + # See permitted parameters documentation: + # https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters + # + # Uncomment all parameters which should be permitted for assignment + # + # permit_params :email, :encrypted_password, :reset_password_token, :reset_password_sent_at, :remember_created_at, :sign_in_count, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, :last_sign_in_ip, :confirmation_token, :confirmed_at, :confirmation_sent_at, :unconfirmed_email, :failed_attempts, :unlock_token, :locked_at, :admin + # + # or + # + # permit_params do + # permitted = [:email, :encrypted_password, :reset_password_token, :reset_password_sent_at, :remember_created_at, :sign_in_count, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, :last_sign_in_ip, :confirmation_token, :confirmed_at, :confirmation_sent_at, :unconfirmed_email, :failed_attempts, :unlock_token, :locked_at, :admin] + # permitted << :other if params[:action] == 'create' && current_user.admin? + # permitted + # end + +end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7fb8d85..f6a2ad0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -66,6 +66,13 @@ class ApplicationController < ActionController::Base ahoy.track "Viewed Application", request.path_parameters end + def authenticate_admin_user! + unless current_user&.admin? + flash[:alert] = "您没有权限访问该页面。" + redirect_to root_path + end + end + private def set_locale diff --git a/app/controllers/cities_controller.rb b/app/controllers/cities_controller.rb index a339000..47aaf36 100644 --- a/app/controllers/cities_controller.rb +++ b/app/controllers/cities_controller.rb @@ -1,4 +1,7 @@ class CitiesController < ApplicationController + before_action :authenticate_user!, only: [:generate_weather_art] + before_action :require_admin, only: [:generate_weather_art] + def index @regions = Region.includes(:countries).order(:name) @cities = City.includes(:country, country: :region).order(:name) @@ -39,4 +42,26 @@ class CitiesController < ApplicationController } ) end + + def generate_weather_art + @city = City.friendly.find(params[:id]) + GenerateWeatherArtWorker.perform_async(@city.id) + + respond_to do |format| + format.html do + flash[:notice] = "Weather art generation has been queued" + redirect_to @city + end + end + end + + private + + def require_admin + unless current_user&.admin? + flash[:error] = "You are not authorized to perform this action" + redirect_to root_path + end + end + end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 045b7e0..cf3282d 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -22,7 +22,7 @@ module ApplicationHelper }.to_json.html_safe if weather_art.image.attached? end - def admin? + def current_user_is_admin? current_user&.admin? end end diff --git a/app/views/cities/_admin_panel.html.erb b/app/views/cities/_admin_panel.html.erb index bcc918c..4d6e2e3 100644 --- a/app/views/cities/_admin_panel.html.erb +++ b/app/views/cities/_admin_panel.html.erb @@ -1,45 +1,58 @@ -<% if admin? %> -
-
- - + +<% if current_user_is_admin? %> +
+

+ + -

Admin Panel

+ Admin Panel + + + +
+
+
Success Rate
+
+ <%= "hello" %>% +
+
Generation Success Rate
+
+ +
+
Last Generated
+
+ <%= time_ago_in_words(@city.weather_arts.last&.created_at) if @city.weather_arts.last %> +
+
Time since last generation
+
+ +
+
Failed Attempts
+
+ <%= "hello" %> +
+
Total failed generations
+
-
- -
-

Statistics

-
-
-
Total Images
-
<%= @weather_art.city.weather_arts.count %>
-
+ +
+ <%= button_to generate_weather_art_city_path(@city), + method: :post, + class: "btn btn-primary gap-2" do %> + + + + Generate New Art + <% end %> -
-
Today's Images
-
<%= @weather_art.city.weather_arts.where("created_at >= ?", Time.zone.now.beginning_of_day).count %>
-
-
-
- - -
-

Actions

-
- <%= button_to "Generate New Art", "#", - method: :post, - data: { - controller: "generate-art", - action: "generate-art#generate", - city_id: @weather_art.city.id - }, - class: "btn btn-primary" %> - - <%= link_to "Edit City", edit_city_path(@weather_art.city), class: "btn btn-secondary" %> -
-
+ <%= link_to edit_city_path(@city), + class: "btn btn-secondary gap-2" do %> + + + + Edit City + <% end %>
<% end %> \ No newline at end of file diff --git a/app/views/cities/show.html.erb b/app/views/cities/show.html.erb index f5b8981..654b980 100644 --- a/app/views/cities/show.html.erb +++ b/app/views/cities/show.html.erb @@ -71,6 +71,8 @@
Total Weather Arts
+ + <%= render 'cities/admin_panel' %>
diff --git a/config/initializers/active_admin.rb b/config/initializers/active_admin.rb index ee93aae..526560e 100644 --- a/config/initializers/active_admin.rb +++ b/config/initializers/active_admin.rb @@ -108,7 +108,8 @@ ActiveAdmin.setup do |config| # # This setting changes the method which Active Admin calls # (within the application controller) to return the currently logged in user. - config.current_user_method = :current_admin_user + # config.current_user_method = :current_admin_user + config.current_user_method = :current_user # == Logging Out # @@ -120,13 +121,15 @@ ActiveAdmin.setup do |config| # will call the method to return the path. # # Default: - config.logout_link_path = :destroy_admin_user_session_path + # config.logout_link_path = :destroy_admin_user_session_path + config.logout_link_path = :destroy_user_session_path # This setting changes the http method used when rendering the # link. For example :get, :delete, :put, etc.. # # Default: # config.logout_link_method = :get + config.logout_link_method = :delete # == Root # diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index f75ca3a..46c3dff 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -24,7 +24,7 @@ Devise.setup do |config| # Configure the e-mail address which will be shown in Devise::Mailer, # note that it will be overwritten if you use your own mailer class # with default "from" parameter. - config.mailer_sender = "please-change-me-at-config-initializers-devise@example.com" + config.mailer_sender = "noreply@mail.frytea.com" # Configure the class responsible to send e-mails. # config.mailer = 'Devise::Mailer' diff --git a/config/routes.rb b/config/routes.rb index b406b01..efa9519 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,6 +7,11 @@ Rails.application.routes.draw do resources :cities, only: [ :index, :show ] do resources :weather_arts, path: "weather", only: [ :show ], param: :slug end + resources :cities do + member do + post :generate_weather_art, param: :slug + end + end resources :arts, only: [ :index ] # namespace :admin do @@ -25,7 +30,8 @@ Rails.application.routes.draw do ActiveAdmin.routes(self) # mount Sidekiq::Web => '/sidekiq' - authenticate :admin_user do + # authenticate :admin_user do + authenticate :user, lambda { |u| u.admin? } do mount Sidekiq::Web => "/admin/tasks" end # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html