From 27fd17d3a1885f64a4a3d775d728bbb670be5349 Mon Sep 17 00:00:00 2001 From: Mir Bhatia Date: Wed, 4 Mar 2026 15:20:21 +0100 Subject: [PATCH] Extract dev_tools from OP monorepo module --- .../component.html.erb | 60 +++++++++++++++++ .../dev_tools_user_switcher/component.rb | 47 ++++++++++++++ .../dev_tools/user_switcher_controller.rb | 50 +++++++++++++++ config/locales/en.yml | 9 +++ config/routes.rb | 35 ++++++++++ dev_tools.gemspec | 42 ++++++++++++ lib/open_project/dev_tools.rb | 64 +++++++++++++++++++ lib/open_project/dev_tools/engine.rb | 50 +++++++++++++++ .../dev_tools/hooks/layout_hook.rb | 45 +++++++++++++ lib/openproject-dev_tools.rb | 31 +++++++++ 10 files changed, 433 insertions(+) create mode 100644 app/components/dev_tools_user_switcher/component.html.erb create mode 100644 app/components/dev_tools_user_switcher/component.rb create mode 100644 app/controllers/dev_tools/user_switcher_controller.rb create mode 100644 config/locales/en.yml create mode 100644 config/routes.rb create mode 100644 dev_tools.gemspec create mode 100644 lib/open_project/dev_tools.rb create mode 100644 lib/open_project/dev_tools/engine.rb create mode 100644 lib/open_project/dev_tools/hooks/layout_hook.rb create mode 100644 lib/openproject-dev_tools.rb diff --git a/app/components/dev_tools_user_switcher/component.html.erb b/app/components/dev_tools_user_switcher/component.html.erb new file mode 100644 index 0000000..bd63271 --- /dev/null +++ b/app/components/dev_tools_user_switcher/component.html.erb @@ -0,0 +1,60 @@ +<%#-- copyright +OpenProject is an open source project management software. +Copyright (C) the OpenProject GmbH + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License version 3. + +OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +Copyright (C) 2006-2013 Jean-Philippe Lang +Copyright (C) 2010-2013 the ChiliProject Team + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +See COPYRIGHT and LICENSE files for more details. + +++#%> + +<%= + render( + Primer::Alpha::ActionMenu.new( + classes: "op-app-menu--item", + menu_id: "op-dev-user-switcher-menu", + anchor_align: :end + ) + ) do |menu| + menu.with_show_button( + scheme: :invisible, + classes: "op-app-header--primer-button" + ) do |button| + button.with_leading_visual_icon(icon: :"person-add") + button.with_tooltip(text: I18n.t("dev_tools.user_switcher.tooltip")) + + current_user_name + end + + menu.with_group do |group| + group.with_heading(title: I18n.t("dev_tools.user_switcher.active_users")) + + users.each do |user| + group.with_item( + href: dev_tools_switch_user_path(user_id: user.id), + label: user.name, + content_arguments: { data: { "turbo-method": "post" } } + ) + end + end + end +%> diff --git a/app/components/dev_tools_user_switcher/component.rb b/app/components/dev_tools_user_switcher/component.rb new file mode 100644 index 0000000..38030b1 --- /dev/null +++ b/app/components/dev_tools_user_switcher/component.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +#-- copyright +# OpenProject is an open source project management software. +# Copyright (C) the OpenProject GmbH +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See COPYRIGHT and LICENSE files for more details. +#++ + +module DevToolsUserSwitcher + class Component < ApplicationComponent + def render? + User.current.logged? + end + + def users + @users ||= User.active + .not_builtin + .order(:lastname, :firstname) + end + + def current_user_name + User.current.name + end + end +end diff --git a/app/controllers/dev_tools/user_switcher_controller.rb b/app/controllers/dev_tools/user_switcher_controller.rb new file mode 100644 index 0000000..ae1aaf5 --- /dev/null +++ b/app/controllers/dev_tools/user_switcher_controller.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +#-- copyright +# OpenProject is an open source project management software. +# Copyright (C) the OpenProject GmbH +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See COPYRIGHT and LICENSE files for more details. +#++ + +module DevTools + class UserSwitcherController < ApplicationController + # No authorization required - this module only loads in development + # Non-admin users need to be able to switch back to admin + no_authorization_required! :switch + + def switch + user = User.find_by(id: params[:user_id]) + + if user&.active? + login_user(user) + flash[:notice] = I18n.t("dev_tools.user_switcher.switched", name: user.name) + else + flash[:error] = I18n.t("dev_tools.user_switcher.user_not_found") + end + + redirect_back_or_to root_path + end + end +end diff --git a/config/locales/en.yml b/config/locales/en.yml new file mode 100644 index 0000000..8863a8d --- /dev/null +++ b/config/locales/en.yml @@ -0,0 +1,9 @@ +en: + dev_tools: + user_switcher: + switched: "Switched to user: %{name}" + user_not_found: "User not found or inactive" + title: "Switch User (Dev Only)" + tooltip: "Switch User (Dev Only)" + heading: "Switch User (Dev Only)" + active_users: "Active Users" diff --git a/config/routes.rb b/config/routes.rb new file mode 100644 index 0000000..8403a85 --- /dev/null +++ b/config/routes.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +#-- copyright +# OpenProject is an open source project management software. +# Copyright (C) the OpenProject GmbH +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See COPYRIGHT and LICENSE files for more details. +#++ + +OpenProject::Application.routes.draw do + scope module: :dev_tools, path: "dev_tools", as: :dev_tools do + match "switch_user", to: "user_switcher#switch", via: %i[get post] + end +end diff --git a/dev_tools.gemspec b/dev_tools.gemspec new file mode 100644 index 0000000..b3cb51c --- /dev/null +++ b/dev_tools.gemspec @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +#-- copyright +# OpenProject is an open source project management software. +# Copyright (C) the OpenProject GmbH +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See COPYRIGHT and LICENSE files for more details. +#++ + +Gem::Specification.new do |s| + s.name = "openproject-dev_tools" + s.version = "1.0.0" + s.authors = "OpenProject GmbH" + s.email = "info@openproject.com" + s.summary = "OpenProject Development Tools" + s.description = "Development only tools for OpenProject (user switcher, custom styles, etc.)" + s.license = "GPL-3.0" + + s.files = Dir["{app,config,lib}/**/*"] + s.metadata["rubygems_mfa_required"] = "true" +end diff --git a/lib/open_project/dev_tools.rb b/lib/open_project/dev_tools.rb new file mode 100644 index 0000000..a8f896c --- /dev/null +++ b/lib/open_project/dev_tools.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: true + +#-- copyright +# OpenProject is an open source project management software. +# Copyright (C) the OpenProject GmbH +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See COPYRIGHT and LICENSE files for more details. +#++# frozen_string_literal: true +# +# #-- copyright +# # OpenProject is an open source project management software. +# # Copyright (C) the OpenProject GmbH +# # +# # This program is free software; you can redistribute it and/or +# # modify it under the terms of the GNU General Public License version 3. +# # +# # OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# # Copyright (C) 2006-2013 Jean-Philippe Lang +# # Copyright (C) 2010-2013 the ChiliProject Team +# # +# # This program is free software; you can redistribute it and/or +# # modify it under the terms of the GNU General Public License +# # as published by the Free Software Foundation; either version 2 +# # of the License, or (at your option) any later version. +# # +# # This program is distributed in the hope that it will be useful, +# # but WITHOUT ANY WARRANTY; without even the implied warranty of +# # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# # GNU General Public License for more details. +# # +# # You should have received a copy of the GNU General Public License +# # along with this program; if not, write to the Free Software +# # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# # +# # See COPYRIGHT and LICENSE files for more details. +# #++ + +require "open_project/dev_tools/engine" + +module OpenProject + module DevTools + end +end diff --git a/lib/open_project/dev_tools/engine.rb b/lib/open_project/dev_tools/engine.rb new file mode 100644 index 0000000..92dbd55 --- /dev/null +++ b/lib/open_project/dev_tools/engine.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +#-- copyright +# OpenProject is an open source project management software. +# Copyright (C) the OpenProject GmbH +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See COPYRIGHT and LICENSE files for more details. +#++ + +require "open_project/plugins" + +module OpenProject + module DevTools + class Engine < ::Rails::Engine + engine_name :openproject_dev_tools + + include OpenProject::Plugins::ActsAsOpEngine + + register "openproject-dev_tools", + author_url: "https://www.openproject.org", + bundled: false, + name: "OpenProject Dev Tools" + + config.to_prepare do + OpenProject::DevTools::Hooks::LayoutHook + end + end + end +end diff --git a/lib/open_project/dev_tools/hooks/layout_hook.rb b/lib/open_project/dev_tools/hooks/layout_hook.rb new file mode 100644 index 0000000..e12900c --- /dev/null +++ b/lib/open_project/dev_tools/hooks/layout_hook.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +#-- copyright +# OpenProject is an open source project management software. +# Copyright (C) the OpenProject GmbH +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See COPYRIGHT and LICENSE files for more details. +#++ + +module OpenProject + module DevTools + module Hooks + class LayoutHook < OpenProject::Hook::ViewListener + def view_layouts_base_top_menu(context = {}) + context[:controller].send( + :render_to_string, + DevToolsUserSwitcher::Component.new, + layout: false + ) + end + end + end + end +end diff --git a/lib/openproject-dev_tools.rb b/lib/openproject-dev_tools.rb new file mode 100644 index 0000000..71b7c9e --- /dev/null +++ b/lib/openproject-dev_tools.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +#-- copyright +# OpenProject is an open source project management software. +# Copyright (C) the OpenProject GmbH +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See COPYRIGHT and LICENSE files for more details. +#++ + +require "open_project/dev_tools"