Skip to content

Commit 0bbbb64

Browse files
committed
Make Alchemy's signup page work with Solidus' user controller
Solidus ships an admin users controller within the core backend distribution, while Alchemy does this within the auth extension `alchemy-devise`. So when using `alchemy-devise` with `solidus-backend`, Solidus` admin users controller takes over, and we need to make sure our non-users has the right abilites to create a first admin user, and that that admin user then has admin rights, too.
1 parent 58a279e commit 0bbbb64

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# /home/anselm/code/alchemy-solidus/app/patches/controllers/alchemy/solidus/spree_admin_users_controller_patch.rb
2+
3+
module Alchemy
4+
module Solidus
5+
module SpreeAdminUsersControllerPatch
6+
def self.prepended(base)
7+
base.after_action :assign_admin_roles_to_first_user, only: :create
8+
end
9+
10+
private
11+
12+
def assign_admin_roles_to_first_user
13+
if Spree.user_class.count == 1
14+
user = Spree.user_class.last
15+
user.spree_roles = [Spree::Role.find_or_create_by(name: "admin")]
16+
user.alchemy_roles = ["admin"]
17+
user.save
18+
end
19+
end
20+
end
21+
end
22+
end
23+
24+
::Spree::Admin::UsersController.prepend(Alchemy::Solidus::SpreeAdminUsersControllerPatch)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
module Alchemy
4+
module Solidus
5+
module AlchemyDeviseAbilityPatch
6+
def initialize(user)
7+
super
8+
9+
# Without these abilities, Alchemy's signup page does not work with Solidus' UsersController.
10+
if Alchemy::User.count == 0
11+
can :admin, Alchemy::User
12+
can :update_password, Alchemy::User
13+
can :update_email, Alchemy::User
14+
end
15+
end
16+
17+
if defined?(::Alchemy::Devise::Ability)
18+
::Alchemy::Devise::Ability.prepend self
19+
end
20+
end
21+
end
22+
end

config/initializers/spree.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
if defined?(Alchemy::Devise::Engine)
22
Spree.user_class = "Alchemy::User"
3+
Spree::PermittedAttributes.user_attributes << :login
34
end
45

56
Rails.application.config.after_initialize do

lib/alchemy/solidus/engine.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Alchemy
1414
module Solidus
1515
class Engine < ::Rails::Engine
1616
include SolidusSupport::EngineExtensions
17+
1718
engine_name "alchemy_solidus"
1819

1920
initializer "alchemy_solidus.assets", before: "alchemy.importmap" do |app|
@@ -30,6 +31,9 @@ class Engine < ::Rails::Engine
3031
config.to_prepare do
3132
Alchemy.register_ability ::Spree::Ability
3233
::Spree::Ability.register_ability ::Alchemy::Permissions
34+
Alchemy.registered_abilities.reject { _1 == Spree::Ability }.each do |ability|
35+
::Spree::Ability.register_ability ability
36+
end
3337

3438
if SolidusSupport.frontend_available?
3539
# Allows to render Alchemy content within Solidus' controller views

0 commit comments

Comments
 (0)