Skip to content

Commit 5e24cd9

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 5e24cd9

4 files changed

Lines changed: 50 additions & 0 deletions

File tree

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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class Engine < ::Rails::Engine
3030
config.to_prepare do
3131
Alchemy.register_ability ::Spree::Ability
3232
::Spree::Ability.register_ability ::Alchemy::Permissions
33+
Alchemy.registered_abilities.reject { _1 == Spree::Ability }.each do |ability|
34+
::Spree::Ability.register_ability ability
35+
end
3336

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

0 commit comments

Comments
 (0)