multi_client 2.2.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.rdoc +18 -0
- data/app/controllers/concerns/multi_client/controller_with_client.rb +1 -1
- data/app/helpers/multi_client_helper.rb +1 -0
- data/app/models/multi_client/client.rb +7 -7
- data/app/services/multi_client/create_client_service.rb +158 -0
- data/config/locales/de.yml +14 -1
- data/lib/generators/multi_client/install/templates/initializer.rb +4 -4
- data/lib/multi_client/configuration.rb +1 -1
- data/lib/multi_client/spec_helper.rb +3 -3
- data/lib/multi_client/version.rb +1 -1
- data/lib/multi_client.rb +4 -0
- data/lib/tasks/multi_client_tasks.rake +15 -4
- data/spec/factories/multi_client/client_factories.rb +12 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9fe6bf2d773b9549c7b637e9905ec592c059e68
|
4
|
+
data.tar.gz: 68e12f5338a2a36fe5a2d23fb94d67ebd794a02b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 70599b057fdddb47846ec157b43b5225d9c52860822597e3d2c7beb8f6aeeb90c0a3d02deba2a33298a0304135c6ce8b31db1df362c70c4c2000e64016ee8726
|
7
|
+
data.tar.gz: a421e291f1837fccb98e8df58e19dbe0cedf3f0b493d2cdb5a29fedf4522069d8434f7f6ea9e95e4607a98dd9f7390a2c7ec17ee9f9d5d36389af983d0143ea0
|
data/README.rdoc
CHANGED
@@ -20,6 +20,24 @@ Add migrations and migrate
|
|
20
20
|
|
21
21
|
> rake multi_client:install:migrations && rake db:migrate
|
22
22
|
|
23
|
+
= How do I scope routing to clients?
|
24
|
+
|
25
|
+
# config/routes.rb
|
26
|
+
constraints MultiClient::Subdomain do
|
27
|
+
resources :posts
|
28
|
+
end
|
29
|
+
|
30
|
+
=> acme.example.com/posts
|
31
|
+
|
32
|
+
= How do I scope routing to non-clients?
|
33
|
+
|
34
|
+
# config/routes.rb
|
35
|
+
constraints MultiClient::NoSubdomain do
|
36
|
+
resources :posts
|
37
|
+
end
|
38
|
+
|
39
|
+
=> www.example.com/posts
|
40
|
+
|
23
41
|
= How do I set the current client?
|
24
42
|
|
25
43
|
# console
|
@@ -9,7 +9,7 @@ module MultiClient
|
|
9
9
|
scope :enabled, -> { where(enabled: true) }
|
10
10
|
|
11
11
|
def self.master
|
12
|
-
where(identifier: Configuration.
|
12
|
+
where(identifier: Configuration.master_client_identifier).first
|
13
13
|
end
|
14
14
|
|
15
15
|
def self.current_id=(id)
|
@@ -36,26 +36,26 @@ module MultiClient
|
|
36
36
|
self.current_id = nil
|
37
37
|
end
|
38
38
|
|
39
|
-
def self.
|
39
|
+
def self.with_client(client, &block)
|
40
40
|
original_id = current_id
|
41
41
|
begin
|
42
|
-
self.current_id =
|
43
|
-
Rails.logger.info "Temporarily set
|
42
|
+
self.current_id = client.id
|
43
|
+
Rails.logger.info "Temporarily set client id to #{client.id}"
|
44
44
|
block.call
|
45
45
|
rescue
|
46
46
|
raise
|
47
47
|
ensure
|
48
|
-
Rails.logger.info "Restored
|
48
|
+
Rails.logger.info "Restored client id to #{original_id}"
|
49
49
|
self.current_id = original_id
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
53
|
def self.master?
|
54
|
-
!!(current && current.identifier == Configuration.
|
54
|
+
!!(current && current.identifier == Configuration.master_client_identifier)
|
55
55
|
end
|
56
56
|
|
57
57
|
def self.set_current_to_master
|
58
|
-
set_current_by_identifier(Configuration.
|
58
|
+
set_current_by_identifier(Configuration.master_client_identifier)
|
59
59
|
end
|
60
60
|
|
61
61
|
end
|
@@ -0,0 +1,158 @@
|
|
1
|
+
module MultiClient
|
2
|
+
class CreateClientService < Itsf::Services::V2::Service::Base
|
3
|
+
class Response < Itsf::Services::V2::Response::Base
|
4
|
+
# attr_accessor :client, :client_setting, :users, :licensed_products, :roles, :permissions
|
5
|
+
attr_accessor :client, :users, :roles, :permissions
|
6
|
+
end
|
7
|
+
|
8
|
+
# attr_accessor :subdomain, :identifier, :enabled, :brand_primary_color, :logo, :user_emails, :product_ids, :create_roles_and_permissions, :request_host
|
9
|
+
attr_accessor :subdomain, :identifier, :enabled, :user_emails, :create_roles_and_permissions, :request_host
|
10
|
+
|
11
|
+
# validates :subdomain, :identifier, :enabled, :brand_primary_color, :logo, :user_emails, :create_roles_and_permissions, :request_host, presence: true
|
12
|
+
validates :subdomain, :identifier, :enabled, :user_emails, :create_roles_and_permissions, :request_host, presence: true
|
13
|
+
|
14
|
+
def do_work
|
15
|
+
say "Running on environment #{Rails.env}"
|
16
|
+
say "Validating input"
|
17
|
+
return response unless valid?
|
18
|
+
say "Input is valid"
|
19
|
+
ActiveRecord::Base.transaction do
|
20
|
+
response.client = create_client
|
21
|
+
Client.with_client(response.client) do
|
22
|
+
# response.client_setting = create_client_setting
|
23
|
+
response.roles, response.permissions = create_default_roles_and_permissions if create_roles_and_permissions == true
|
24
|
+
response.users = create_users
|
25
|
+
# response.licensed_products = create_licensed_products
|
26
|
+
raise ActiveRecord::Rollback if errors.any?
|
27
|
+
request_user_password_resets(response.users.map(&:email))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
say "Done"
|
31
|
+
respond
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def create_client
|
37
|
+
say "Creating client", indent: 1
|
38
|
+
client = Client.new(client_attributes)
|
39
|
+
if client.save
|
40
|
+
say "Created #{client.class} #{client}", indent: 2
|
41
|
+
else
|
42
|
+
add_error_and_say :client, "Error creating #{client.class} #{client}. Errors: #{client.errors.full_messages.to_sentence}", indent: 2
|
43
|
+
end
|
44
|
+
say "Done", indent: 1
|
45
|
+
return client
|
46
|
+
end
|
47
|
+
|
48
|
+
# def create_client_setting
|
49
|
+
# say "Creating client setting", indent: 1
|
50
|
+
# client_setting = ClientSetting.new(client_setting_attributes)
|
51
|
+
# if client_setting.save
|
52
|
+
# say "Created #{client_setting.class} #{client_setting}", indent: 2
|
53
|
+
# else
|
54
|
+
# add_error_and_say :client_setting, "Error creating #{client_setting.class} #{client_setting}. Errors: #{client_setting.errors.full_messages.to_sentence}", indent: 2
|
55
|
+
# end
|
56
|
+
# say "Done", indent: 1
|
57
|
+
# return client_setting
|
58
|
+
# end
|
59
|
+
|
60
|
+
def create_default_roles_and_permissions
|
61
|
+
say "Creating roles", indent: 1
|
62
|
+
result = Ecm::Rbac::ImportDefaultPermissionsService.call(filename: Rails.root.join("config", "rbac.yml"))
|
63
|
+
if result.success?
|
64
|
+
say "Created roles", indent: 2
|
65
|
+
else
|
66
|
+
add_error_and_say :create_roles_and_permissions, "Error creating roles. Errors: #{result.errors.full_messages.to_sentence}", indent: 2
|
67
|
+
end
|
68
|
+
say "Done", indent: 1
|
69
|
+
return result.roles, result.permissions
|
70
|
+
end
|
71
|
+
|
72
|
+
def create_users
|
73
|
+
say "Creating #{splitted_user_emails.size} users", indent: 1
|
74
|
+
users = splitted_user_emails.collect do |user_email|
|
75
|
+
create_user(user_email)
|
76
|
+
end
|
77
|
+
say "Done", indent: 1
|
78
|
+
return users
|
79
|
+
end
|
80
|
+
|
81
|
+
def create_user(email)
|
82
|
+
say "Creating user", indent: 1
|
83
|
+
random_password = SecureRandom.hex
|
84
|
+
user = Ecm::UserArea::User.new(email: email, password: random_password, password_confirmation: random_password, active: true, confirmed: true, approved: true, roles: user_roles)
|
85
|
+
if user.save
|
86
|
+
say "Created #{user.class} #{user}", indent: 2
|
87
|
+
else
|
88
|
+
add_error_and_say :user_emails, "Error creating #{user.class} #{user}. Errors: #{user.errors.full_messages.to_sentence}", indent: 2
|
89
|
+
end
|
90
|
+
say "Done", indent: 1
|
91
|
+
return user
|
92
|
+
end
|
93
|
+
|
94
|
+
# def create_licensed_products
|
95
|
+
# say "Creating #{product_ids.size} licensed products", indent: 1
|
96
|
+
# licensed_products = product_ids.collect do |product_id|
|
97
|
+
# create_licensed_product(product_id)
|
98
|
+
# end
|
99
|
+
# say "Done", indent: 1
|
100
|
+
# return licensed_products
|
101
|
+
# end
|
102
|
+
|
103
|
+
# def create_licensed_product(product_id)
|
104
|
+
# say "Creating licensed product", indent: 1
|
105
|
+
# licensed_product = Chi::Licensing::LicensedProduct.new(licensee: response.client)
|
106
|
+
# licensed_product.product = load_product(product_id)
|
107
|
+
# if licensed_product.save
|
108
|
+
# say "Created #{licensed_product.class} #{licensed_product}", indent: 2
|
109
|
+
# else
|
110
|
+
# add_error_and_say :product_ids, "Error creating #{licensed_product.class} #{licensed_product}. Errors: #{licensed_product.errors.full_messages.to_sentence}", indent: 2
|
111
|
+
# end
|
112
|
+
# say "Done", indent: 1
|
113
|
+
# return licensed_product
|
114
|
+
# end
|
115
|
+
|
116
|
+
def client_attributes
|
117
|
+
{ subdomain: subdomain, identifier: identifier, enabled: enabled }
|
118
|
+
end
|
119
|
+
|
120
|
+
def client_setting_attributes
|
121
|
+
{ brand_primary_color: brand_primary_color, logo: logo }
|
122
|
+
end
|
123
|
+
|
124
|
+
def splitted_user_emails
|
125
|
+
user_emails.split(/[^\w+@\.\+\-\_]/)
|
126
|
+
end
|
127
|
+
|
128
|
+
def request_user_password_resets(emails)
|
129
|
+
host = request_host.gsub(Client.master.subdomain, subdomain)
|
130
|
+
emails.map do |email|
|
131
|
+
Ecm::UserArea::UserPasswordResetRequest.call(email: email, host: host)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
# def load_product(product_id)
|
136
|
+
# Chi::Licensing::Product.where(id: product_id).first
|
137
|
+
# end
|
138
|
+
|
139
|
+
def create_roles_and_permissions
|
140
|
+
if Rails.version < '5'
|
141
|
+
ActiveRecord::Type::Boolean.new.type_cast_from_user(@create_roles_and_permissions)
|
142
|
+
else
|
143
|
+
ActiveRecord::Type::Boolean.new.cast(@create_roles_and_permissions)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def user_roles
|
148
|
+
[
|
149
|
+
Ecm::Rbac::Role.where(identifier: 'rt_imager/admin').first,
|
150
|
+
Ecm::Rbac::Role.where(identifier: 'rt_imager/web').first,
|
151
|
+
].compact
|
152
|
+
end
|
153
|
+
|
154
|
+
# def product_ids
|
155
|
+
# @product_ids ||= []
|
156
|
+
# end
|
157
|
+
end
|
158
|
+
end
|
data/config/locales/de.yml
CHANGED
@@ -1,3 +1,16 @@
|
|
1
1
|
de:
|
2
2
|
classes:
|
3
|
-
multi_client/client: Mandant
|
3
|
+
multi_client/client: 'Mandant'
|
4
|
+
multi_client/create_client_service: 'Dienst zum einrichten von Mandanten'
|
5
|
+
activerecord:
|
6
|
+
attributes:
|
7
|
+
multi_client/client:
|
8
|
+
identifier: 'Bezeichner'
|
9
|
+
subdomain: 'Subdomain'
|
10
|
+
enabled: 'Aktiv'
|
11
|
+
created_at: 'Erstellt am'
|
12
|
+
updated_at: 'Aktualisiert am'
|
13
|
+
models:
|
14
|
+
multi_client/client:
|
15
|
+
one: 'Mandant'
|
16
|
+
other: 'Mandanten'
|
@@ -61,10 +61,10 @@ MultiClient.configure do |config|
|
|
61
61
|
#
|
62
62
|
config.no_subdomain_prefixes = ->(request) { ['www', '', nil] }
|
63
63
|
|
64
|
-
# The master
|
65
|
-
# check, if the actual
|
64
|
+
# The master client is a special client. You can ask MultiClient::Client.master? to
|
65
|
+
# check, if the actual client is the master client.
|
66
66
|
#
|
67
|
-
# Default: config.
|
67
|
+
# Default: config.master_client_identifier = '000'
|
68
68
|
#
|
69
|
-
config.
|
69
|
+
config.master_client_identifier = '000'
|
70
70
|
end
|
@@ -24,7 +24,7 @@ module MultiClient
|
|
24
24
|
|
25
25
|
mattr_accessor(:force_client_scope_for_unscoped_callers) { [] }
|
26
26
|
|
27
|
-
mattr_accessor(:
|
27
|
+
mattr_accessor(:master_client_identifier) { '000' }
|
28
28
|
|
29
29
|
def self.namespaced_model_name
|
30
30
|
"MultiClient::#{model_name}"
|
@@ -1,9 +1,9 @@
|
|
1
1
|
module MultiClient
|
2
2
|
module SpecHelper
|
3
|
-
def
|
4
|
-
|
3
|
+
def use_client(client)
|
4
|
+
MultiClient::Client.current_id = client.id
|
5
5
|
Capybara.current_session.driver.reset!
|
6
|
-
Capybara.default_host = Capybara.default_host.sub(/(.*?\/\/)(.*?)(\..*)/, "\\1#{
|
6
|
+
Capybara.default_host = Capybara.default_host.sub(/(.*?\/\/)(.*?)(\..*)/, "\\1#{client.subdomain}\\3")
|
7
7
|
end
|
8
8
|
end
|
9
9
|
end
|
data/lib/multi_client/version.rb
CHANGED
data/lib/multi_client.rb
CHANGED
@@ -1,4 +1,15 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
namespace :multi_client do
|
2
|
+
namespace :create do
|
3
|
+
desc "Explaining what the task does"
|
4
|
+
task create_master: :environment do
|
5
|
+
if MultiClient::Client.exists?(subdomain: 'master')
|
6
|
+
puts "Master tenant already exists"
|
7
|
+
next
|
8
|
+
end
|
9
|
+
if MultiClient::Client.enabled.create!(subdomain: 'master', identifier: MultiClient::Configuration.master_client_identifier)
|
10
|
+
puts "Created master tenant"
|
11
|
+
end
|
12
|
+
puts "Done"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
FactoryGirl.define do
|
2
|
+
factory :multi_client_client, class: MultiClient::Client do
|
3
|
+
sequence(:identifier)
|
4
|
+
sequence(:subdomain)
|
5
|
+
enabled true
|
6
|
+
|
7
|
+
trait(:master) do
|
8
|
+
identifier MultiClient::Configuration.master_client_identifier
|
9
|
+
subdomain 'master'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multi_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roberto Vasquez Angel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-05-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -141,6 +141,7 @@ files:
|
|
141
141
|
- app/helpers/multi_client_helper.rb
|
142
142
|
- app/models/concerns/multi_client/model_with_client.rb
|
143
143
|
- app/models/multi_client/client.rb
|
144
|
+
- app/services/multi_client/create_client_service.rb
|
144
145
|
- app/views/layouts/multi_client/application.html.erb
|
145
146
|
- app/views/multi_client/_client_navigation.haml
|
146
147
|
- app/views/multi_client/_current_client.html.erb
|
@@ -159,6 +160,7 @@ files:
|
|
159
160
|
- lib/multi_client/subdomain.rb
|
160
161
|
- lib/multi_client/version.rb
|
161
162
|
- lib/tasks/multi_client_tasks.rake
|
163
|
+
- spec/factories/multi_client/client_factories.rb
|
162
164
|
homepage: https://github.com/robotex82/multi_client
|
163
165
|
licenses:
|
164
166
|
- MIT
|