Skip to content

Commit b7cbbaf

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 6112aee commit b7cbbaf

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class Engine < ::Rails::Engine
3232
config.to_prepare do
3333
Alchemy.register_ability ::Spree::Ability
3434
::Spree::Ability.register_ability ::Alchemy::Permissions
35+
Alchemy.registered_abilities.reject { _1 == Spree::Ability }.each do |ability|
36+
::Spree::Ability.register_ability ability
37+
end
3538

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

0 commit comments

Comments
 (0)