mautic 2.3.6 → 2.3.11

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: 78b5aecca30c838b83909bf68ad262c18f202b4a1f4ebb91d25b3e0db9ef6050
4
- data.tar.gz: c0a942ada27b2ede12b190d59c72f1ad8eb902f476f172ed4fd1601a577c1efb
3
+ metadata.gz: 54130da5f454542319794381488da40d7ddf9b44ae18e9071cee61a4ae6cee65
4
+ data.tar.gz: f81597d7b092e25ed9cc7f18905e681ae92f1afc61e1cd143f2fecb1f704b026
5
5
  SHA512:
6
- metadata.gz: 983fef37892340a7f03754baf4de27c509ec14ea61d0d323427050f7e36992ef2025adbb8ab193cf3c2b75c9e8edd4a5d198597805c6dc8b3dcff9f42113d4b9
7
- data.tar.gz: 2e34254779f1c5c651f93ce358462bd135e9da2df6e09676c815dd4b1deed7ebe431a6ca7f8e141ebd84354eb81160b30ed92846fd6173c26c586dda4afc6ee1
6
+ metadata.gz: fdf11781f6797c3174c5e49612d0369660b7fde520cdfecc237396879b110bbe3ac56fd23c6ffd20dc9cf1878ea76faec45e0132930c3992afffd92c3b66cd53
7
+ data.tar.gz: 5d82c6eed700f377967347e8bc0bb759557d3f006a7a3ec4fb709a78026086eb8dfaedc6be3c0d3fef667a17a9b2cf472f8819ddfcfae757f2442d6b077c11b6
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
@@ -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
 
@@ -19,7 +19,7 @@ module Mautic
19
19
  def owner=(hash)
20
20
  raise ArgumentError, "must be a hash !" unless hash.is_a?(Hash)
21
21
 
22
- @table["owner"] = hash["id"]
22
+ @table[:owner] = hash["id"]
23
23
  @owner = hash
24
24
  end
25
25
 
@@ -33,7 +33,7 @@ module Mautic
33
33
  # @see https://developer.mautic.org/#edit-contact set owner
34
34
  # @param [Integer] int
35
35
  def owner_id=(int)
36
- @table["owner"] = int
36
+ @table[:owner] = int
37
37
  end
38
38
 
39
39
  def assign_attributes(source = nil)
@@ -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!
@@ -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.6'
2
+ VERSION = '2.3.11'
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.6
4
+ version: 2.3.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukáš Pokorný
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-10 00:00:00.000000000 Z
11
+ date: 2020-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails