mautic 2.3.7 → 2.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0a1c181282f3dfa4350d619d42c0abdea2b310d2ba85b2f8ca06e875e5eece65
4
- data.tar.gz: 5f9921e8982d447ff37493791b88d0a00a37cdc6d52058c10734e23ed55eaef4
3
+ metadata.gz: c03bde50c40a75d830eb7e281af2ecc36143b62a9db7b30647a26bb1044013d4
4
+ data.tar.gz: 0eda55684550a7512e911792846a0d7646ba4da0a1a15a216ecc6836876deda5
5
5
  SHA512:
6
- metadata.gz: 4640a5a580ec8deb680a05eac9d558bb99d5685e84282e3d41d3edf38b4d3143a484cf3fd58f0d0fd7ac20c42a97f62144d03820540e4ad45dd9ffaf9e3b99a1
7
- data.tar.gz: 4a12224e16bb2cd5adbf64538b81f17914699dacc84ddf57d9ba10e95b5e69c17e5672b00bfc8fe4e4d41501946d808a364c92eb3ea50269fb8ce78f2f26b9f8
6
+ metadata.gz: 7382580246a31dcc47c03fb1c9ef98168fd4a0429439b8b45794db9f8dc263d839866a3f3e4c0c4aeab741d074c8fa6029d0cef25be67e0a279b2f68b1d8d8c0
7
+ data.tar.gz: 8e801c01c6aec779c05e7f858dfdfe45dc3ca06b21c86086e42f4f0a180a0251e1ab02bf4edb84ed0c2c91658b7e8e8f9370b81511b04324ed10efa2bbdf6e15
data/README.md CHANGED
@@ -32,13 +32,40 @@ Mautic.configure do |config|
32
32
  # OR it can be Proc
33
33
  # *optional* This is your default mautic URL - used in form helper
34
34
  config.mautic_url = "https://mautic.my.app"
35
+ # Set authorize condition for manage Mautic::Connections
36
+ config.authorize_mautic_connections = ->(controller) { false }
35
37
  end
36
38
  ```
39
+ ### Manage mautic connections
40
+ You can use builtin Mautic:ConnectionsController:
37
41
 
38
42
  add to `config/routes.rb`
39
43
  ```ruby
40
44
  mount Mautic::Engine => "/mautic"
41
45
  ```
46
+ note: Make sure that you have some user authorization. There is builtin mechanism, in `Mautic.config.authorize_mautic_connections` = which return `false` to prevent all access by default (see: app/controllers/mautic/connections_controller.rb:3). For change this, you need add to `config/initializers/mautic.rb`:
47
+ ```ruby
48
+ Mautic.config.authorize_mautic_connections = ->(controller) { current_user.admin? }
49
+ ```
50
+
51
+ OR use your own controller, by including concern
52
+ ```ruby
53
+ class MyOwnController < ApplicationController
54
+ before_action :authorize_user
55
+
56
+ include Mautic::ConnectionsControllerConcern
57
+ end
58
+ ```
59
+ Concern require additional routes (authorize and oauth2) in `routes.rb`
60
+ ```ruby
61
+ resources :my_resources do
62
+ member do
63
+ get :authorize
64
+ get :oauth2
65
+ end
66
+ end
67
+ ```
68
+
42
69
  ### Create mautic connection
43
70
 
44
71
  1. In your mautic, create new
@@ -85,12 +85,12 @@ module Mautic
85
85
  # ==--==--==--==--
86
86
 
87
87
  def authorize
88
- redirect_to @mautic_connection.authorize
88
+ redirect_to @mautic_connection.authorize(self)
89
89
  end
90
90
 
91
91
  def oauth2
92
92
  begin
93
- response = @mautic_connection.get_code(params.require(:code))
93
+ response = @mautic_connection.get_code(params.require(:code), self)
94
94
  @mautic_connection.update(token: response.token, refresh_token: response.refresh_token)
95
95
  return render plain: t('mautic.text_mautic_authorize_successfully')
96
96
  rescue OAuth2::Error => e
@@ -1,5 +1,15 @@
1
1
  module Mautic
2
2
  class ConnectionsController < ApplicationController
3
+ before_action :authorize_me
3
4
  include ::Mautic::ConnectionsControllerConcern
5
+
6
+ private
7
+
8
+ def authorize_me
9
+ unless Mautic.config.authorize_mautic_connections.call(self)
10
+ logger.warn "Mautic::ConnectionsController unauthorized, you can change this by Mautic.config.authorize_mautic_connections. See: lib/mautic.rb:77"
11
+ render plain: "Unauthorized", status: 403
12
+ end
13
+ end
4
14
  end
5
15
  end
@@ -0,0 +1,22 @@
1
+ module Mautic
2
+ class Campaign < Model
3
+
4
+ # @see https://developer.mautic.org/#add-contact-to-a-campaign
5
+ # @param [Integer] id of Mautic::Contact
6
+ def add_contact!(id)
7
+ json = @connection.request(:post, "api/campaigns/#{self.id}/contact/#{id}/add")
8
+ json["success"]
9
+ rescue RequestError => _e
10
+ false
11
+ end
12
+
13
+ # @see https://developer.mautic.org/#remove-contact-from-a-campaign
14
+ # @param [Integer] id of Mautic::Contact
15
+ def remove_contact!(id)
16
+ json = @connection.request(:post, "api/campaigns/#{self.id}/contact/#{id}/remove")
17
+ json["success"]
18
+ rescue RequestError => _e
19
+ false
20
+ end
21
+ end
22
+ end
@@ -17,11 +17,11 @@ module Mautic
17
17
  raise NotImplementedError
18
18
  end
19
19
 
20
- def authorize
20
+ def authorize(context)
21
21
  raise NotImplementedError
22
22
  end
23
23
 
24
- def get_code(code)
24
+ def get_code(code, context)
25
25
  raise NotImplementedError
26
26
  end
27
27
 
@@ -55,7 +55,7 @@ module Mautic
55
55
 
56
56
  private
57
57
 
58
- def callback_url
58
+ def callback_url(context)
59
59
  if (conf = Mautic.config.base_url).is_a?(Proc)
60
60
  conf = conf.call(self)
61
61
  end
@@ -11,12 +11,12 @@ module Mautic
11
11
  })
12
12
  end
13
13
 
14
- def authorize
15
- client.auth_code.authorize_url(redirect_uri: callback_url)
14
+ def authorize(context)
15
+ client.auth_code.authorize_url(redirect_uri: callback_url(context))
16
16
  end
17
17
 
18
- def get_code(code)
19
- client.auth_code.get_token(code, redirect_uri: callback_url)
18
+ def get_code(code, context)
19
+ client.auth_code.get_token(code, redirect_uri: callback_url(context))
20
20
  end
21
21
 
22
22
  def connection
@@ -37,9 +37,10 @@ module Mautic
37
37
 
38
38
  private
39
39
 
40
- def callback_url
40
+ def callback_url(context)
41
41
  uri = super
42
- uri.path = Mautic::Engine.routes.url_helpers.oauth2_connection_path(self)
42
+ # uri.path = Mautic::Engine.routes.url_helpers.oauth2_connection_path(self)
43
+ uri.path = context.url_for(action: "oauth2", id: self , only_path: true)
43
44
  uri.to_s
44
45
  end
45
46
 
@@ -41,14 +41,20 @@ module Mautic
41
41
 
42
42
  if source
43
43
  self.owner = source['owner'] || {}
44
+ tags = (source['tags'] || []).map { |t| Mautic::Tag.new(self, t) }.sort_by(&:name)
44
45
  self.attributes = {
45
- tags: (source['tags'] || []).collect { |t| Mautic::Tag.new(@connection, t) }.sort_by(&:name),
46
+ tags: Tag::Collection.new(self, *tags),
46
47
  doNotContact: source['doNotContact'] || [],
47
48
  owner: owner['id'],
48
49
  }
49
50
  end
50
51
  end
51
52
 
53
+ def to_mautic(data = @table)
54
+ data.delete(:doNotContact)
55
+ super(data)
56
+ end
57
+
52
58
  def events
53
59
  @proxy_events ||= Proxy.new(connection, "contacts/#{id}/events", klass: "Mautic::Event")
54
60
  end
@@ -90,7 +96,7 @@ module Mautic
90
96
  self.errors = e.errors
91
97
  end
92
98
 
93
- self.errors.blank?
99
+ errors.blank?
94
100
  end
95
101
 
96
102
  alias add_dnc do_not_contact!
@@ -111,6 +117,23 @@ module Mautic
111
117
 
112
118
  # !endgroup
113
119
 
120
+ # @!group Campaigns
121
+
122
+ # @return [Array<Mautic::Campaign>]
123
+ def campaigns
124
+ return @campaigns if @campaigns
125
+
126
+ json = @connection.request(:get, "api/contacts/#{id}/campaigns")
127
+
128
+ @campaigns = json["campaigns"].collect do |_campaign_id, campaign_attributes|
129
+ Mautic::Campaign.new @connection, campaign_attributes
130
+ end
131
+ rescue RequestError => _e
132
+ []
133
+ end
134
+
135
+ # !endgroup
136
+
114
137
  private
115
138
 
116
139
  def clear_change
@@ -1,11 +1,43 @@
1
1
  module Mautic
2
2
  class Tag < Model
3
3
 
4
+ class Collection < Array
5
+ attr_reader :model
6
+
7
+ # @param [Mautic::Model] model
8
+ def initialize(model, *several_variants)
9
+ @model = model
10
+ @tags_to_remove = []
11
+ super(several_variants)
12
+ end
13
+
14
+ def <<(item)
15
+ @model.changed = true
16
+ item = Tag.new(@model, { tag: item }) if item.is_a?(String)
17
+ super(item)
18
+ end
19
+
20
+ def remove(item)
21
+ @model.changed = true
22
+ item = detect { |t| t.name == item } if item.is_a?(String)
23
+ @tags_to_remove << "-#{item}"
24
+ delete item
25
+ end
26
+
27
+ def to_mautic
28
+ map(&:name) + @tags_to_remove
29
+ end
30
+ end
31
+
4
32
  # alias for attribute :tag
5
33
  def name
6
34
  tag
7
35
  end
8
36
 
37
+ def to_s
38
+ name
39
+ end
40
+
9
41
  def name=(name)
10
42
  self.tag = name
11
43
  end
@@ -12,10 +12,10 @@ module Mautic
12
12
 
13
13
  def form_submissions
14
14
  @forms ||= Array.wrap(@params.require("mautic.form_on_submit")).collect do |data|
15
- p = data.permit(submission: [:id, form: {}, lead: {}, results: {}]).to_h
15
+ p = data.permit(submission: [:id, :referer, form: {}, lead: {}, results: {}]).to_h
16
16
  ::Mautic::Submissions::Form.new(@connection, p["submission"]) if p["submission"]
17
17
  end.compact
18
18
  end
19
19
 
20
20
  end
21
- end
21
+ end
@@ -24,7 +24,7 @@
24
24
  <% if form.object.persisted? %>
25
25
  <div class="field">
26
26
  <label>Now generate pair of tokens with this callback url:</label>
27
- <pre><%= @mautic_connection.send :callback_url %></pre>
27
+ <pre><%= @mautic_connection.send :callback_url, controller %></pre>
28
28
  </div>
29
29
 
30
30
  <div class="field">
@@ -3,8 +3,6 @@ Mautic::Engine.routes.draw do
3
3
  member do
4
4
  get :authorize
5
5
  get :oauth2
6
-
7
6
  end
8
- # post "webhook/:mautic_connection_id", action: "webhook", on: :collection
9
7
  end
10
8
  end
@@ -67,10 +67,14 @@ module Mautic
67
67
  end
68
68
 
69
69
  configure do |config|
70
- # This is URL your application - its for oauth callbacks
70
+ # This is URL of your application - its for oauth callbacks
71
71
  config.base_url = "http://localhost:3000"
72
+
72
73
  # *optional* This is your default mautic URL - used in form helper
73
74
  config.mautic_url = "https://mautic.my.app"
75
+
76
+ # Set authorize condition for manage Mautic::Connections
77
+ config.authorize_mautic_connections = ->(controller) { false }
74
78
  end
75
79
  # Your code goes here...
76
80
 
@@ -39,6 +39,7 @@ module Mautic
39
39
 
40
40
  attr_reader :connection
41
41
  attr_accessor :errors
42
+ attr_writer :changed
42
43
 
43
44
  # @param [Mautic::Connection] connection
44
45
  def initialize(connection, hash = nil)
@@ -58,7 +59,7 @@ module Mautic
58
59
  end
59
60
 
60
61
  def update(force = false)
61
- return false if changes.blank?
62
+ return false unless changed?
62
63
 
63
64
  begin
64
65
  json = @connection.request((force && :put || :patch), "api/#{endpoint}/#{id}/edit", body: to_mautic)
@@ -68,7 +69,7 @@ module Mautic
68
69
  self.errors = e.errors
69
70
  end
70
71
 
71
- self.errors.blank?
72
+ errors.blank?
72
73
  end
73
74
 
74
75
  def update_columns(attributes = {})
@@ -88,7 +89,7 @@ module Mautic
88
89
  self.errors = e.errors
89
90
  end
90
91
 
91
- self.errors.blank?
92
+ errors.blank?
92
93
  end
93
94
 
94
95
  def destroy
@@ -105,6 +106,12 @@ module Mautic
105
106
  @table.changes
106
107
  end
107
108
 
109
+ def changed?
110
+ return @changed unless @changed.nil?
111
+
112
+ @changed = !changes.empty?
113
+ end
114
+
108
115
  def attributes
109
116
  @table.to_h
110
117
  end
@@ -118,13 +125,20 @@ module Mautic
118
125
 
119
126
  def to_mautic(data = @table)
120
127
  data.each_with_object({}) do |(key, val), mem|
121
- mem[key] = val.is_a?(Array) ? val.join("|") : val
128
+ mem[key] = if val.respond_to?(:to_mautic)
129
+ val.to_mautic
130
+ elsif val.is_a?(Array)
131
+ val.join("|")
132
+ else
133
+ val
134
+ end
122
135
  end
123
136
  end
124
137
 
125
138
  private
126
139
 
127
140
  def clear_changes
141
+ @changed = nil
128
142
  @table.instance_variable_set(:@changes, nil)
129
143
  end
130
144
 
@@ -30,6 +30,12 @@ module Mautic
30
30
  def contact
31
31
  @contact ||= @connection.contacts.new(@raw["lead"])
32
32
  end
33
+
34
+ # @return [String]
35
+ def referer
36
+ @raw["referer"].to_s
37
+ end
38
+
33
39
  end
34
40
  end
35
- end
41
+ end
@@ -1,3 +1,3 @@
1
1
  module Mautic
2
- VERSION = '2.3.7'
2
+ VERSION = '2.4.0'
3
3
  end
@@ -46,6 +46,9 @@ RSpec.configure do |config|
46
46
 
47
47
  config.before(:each) do
48
48
  DatabaseCleaner.strategy = :transaction
49
+ Mautic.configure do |config|
50
+ config.authorize_mautic_connections = ->(controller) { true }
51
+ end
49
52
  end
50
53
 
51
54
  config.before(:each) do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mautic
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.7
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukáš Pokorný
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-10 00:00:00.000000000 Z
11
+ date: 2020-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -174,6 +174,7 @@ files:
174
174
  - app/jobs/mautic/application_job.rb
175
175
  - app/mailers/mautic/application_mailer.rb
176
176
  - app/models/mautic/application_record.rb
177
+ - app/models/mautic/campaign.rb
177
178
  - app/models/mautic/connection.rb
178
179
  - app/models/mautic/connections/oauth2.rb
179
180
  - app/models/mautic/contact.rb
@@ -207,7 +208,7 @@ licenses:
207
208
  - MIT
208
209
  metadata:
209
210
  allowed_push_host: https://rubygems.org
210
- post_install_message:
211
+ post_install_message:
211
212
  rdoc_options: []
212
213
  require_paths:
213
214
  - lib
@@ -222,8 +223,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
222
223
  - !ruby/object:Gem::Version
223
224
  version: '0'
224
225
  requirements: []
225
- rubygems_version: 3.0.8
226
- signing_key:
226
+ rubygems_version: 3.0.6
227
+ signing_key:
227
228
  specification_version: 4
228
229
  summary: Ruby on Rails Mautic integration
229
230
  test_files: