mautic 2.3.5 → 2.3.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: af1f088355eb05f40c5b2d22ce36e6c6319ff7e0bc7b6e62b798762a2a400724
4
- data.tar.gz: 92867e40e6b9722094f2c174b4a40286cdac41e07c09553e11b38df4caaff4c6
3
+ metadata.gz: 5b00390acf94aa6294ef58fbfeab2d3a0198544c52b162c837134adf79e8c3ad
4
+ data.tar.gz: 3efd6bea9cdc79cd58b535489edc1a6cd11ae5539797b2246feaa1760662009f
5
5
  SHA512:
6
- metadata.gz: 361320378ad1c77a3bd8b0a3600ad30fe5c5a95fc7aab7a934992a1fd591c23fd872b3c5aa0ebaf30e54e87997763bc3775119747ef2fa0f449a8252fd3626d2
7
- data.tar.gz: 5a630110651bd5fd915fd05570953bdf326c513cdea05d20f0040a6628a6b9b159bf55af5e0ffca6153dab32061a2fdd9dc0f30bd4c7d82401ccf098e9f4072f
6
+ metadata.gz: cc3eb999f11d813f81047485875e194acd8c41f9de2910f87df449af5be27a90d577eab222651acbe3565c804e7c9510a458cfc0e0a7f643c9217f049f035e59
7
+ data.tar.gz: dc2eeb43ce4ad5f5805e92e4bc452f91abbd3fbd2e16df28a6b1abdc78a9fd1975ac9faa1bdd7756d118feb792d20c0eec4e405e257cffff1d46828ac5eb50a4
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
 
@@ -12,16 +12,49 @@ module Mautic
12
12
  "#{firstname} #{lastname}"
13
13
  end
14
14
 
15
+ # @param [Hash] hash
16
+ # option hash [Integer] :id
17
+ # option hash [String] :firstName
18
+ # option hash [String] :lastName
19
+ def owner=(hash)
20
+ raise ArgumentError, "must be a hash !" unless hash.is_a?(Hash)
21
+
22
+ @table[:owner] = hash["id"]
23
+ @owner = hash
24
+ end
25
+
26
+ # @return [Hash]
27
+ # @example {id: 12, firstName: "Joe", lastName: "Doe"}
28
+ def owner
29
+ @owner || {}
30
+ end
31
+
32
+ # Assign mautic User ID as owner - for example for update author of contact
33
+ # @see https://developer.mautic.org/#edit-contact set owner
34
+ # @param [Integer] int
35
+ def owner_id=(int)
36
+ @table[:owner] = int
37
+ end
38
+
15
39
  def assign_attributes(source = nil)
16
40
  super
41
+
17
42
  if source
43
+ self.owner = source['owner'] || {}
44
+ tags = (source['tags'] || []).map { |t| Mautic::Tag.new(self, t) }.sort_by(&:name)
18
45
  self.attributes = {
19
- tags: (source['tags'] || []).collect { |t| Mautic::Tag.new(@connection, t) }.sort_by(&:name),
46
+ tags: Tag::Collection.new(self, *tags),
20
47
  doNotContact: source['doNotContact'] || [],
48
+ owner: owner['id'],
21
49
  }
22
50
  end
23
51
  end
24
52
 
53
+ def to_mautic(data = @table)
54
+ data.delete(:doNotContact)
55
+ super(data)
56
+ end
57
+
25
58
  def events
26
59
  @proxy_events ||= Proxy.new(connection, "contacts/#{id}/events", klass: "Mautic::Event")
27
60
  end
@@ -32,6 +65,7 @@ module Mautic
32
65
  def do_not_contact?
33
66
  doNotContact.present?
34
67
  end
68
+
35
69
  alias dnc? do_not_contact?
36
70
 
37
71
  # @return [Array[Hash]]
@@ -62,8 +96,9 @@ module Mautic
62
96
  self.errors = e.errors
63
97
  end
64
98
 
65
- self.errors.blank?
99
+ errors.blank?
66
100
  end
101
+
67
102
  alias add_dnc do_not_contact!
68
103
 
69
104
  def remove_do_not_contact!
@@ -77,6 +112,7 @@ module Mautic
77
112
 
78
113
  self.errors.blank?
79
114
  end
115
+
80
116
  alias remove_dnc remove_do_not_contact!
81
117
 
82
118
  # !endgroup
@@ -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
@@ -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
 
@@ -38,6 +38,8 @@ module Mautic
38
38
  end
39
39
 
40
40
  attr_reader :connection
41
+ attr_accessor :errors
42
+ attr_writer :changed
41
43
 
42
44
  # @param [Mautic::Connection] connection
43
45
  def initialize(connection, hash = nil)
@@ -57,28 +59,37 @@ module Mautic
57
59
  end
58
60
 
59
61
  def update(force = false)
60
- return false if changes.blank?
62
+ return false unless changed?
63
+
61
64
  begin
62
- json = @connection.request((force && :put || :patch), "api/#{endpoint}/#{id}/edit", { body: to_mautic })
63
- self.attributes = json[endpoint.singularize]
65
+ json = @connection.request((force && :put || :patch), "api/#{endpoint}/#{id}/edit", body: to_mautic)
66
+ assign_attributes json[endpoint.singularize]
64
67
  clear_changes
65
68
  rescue ValidationError => e
66
69
  self.errors = e.errors
67
70
  end
68
71
 
69
- self.errors.blank?
72
+ errors.blank?
73
+ end
74
+
75
+ def update_columns(attributes = {})
76
+ json = @connection.request(:patch, "api/#{endpoint}/#{id}/edit", body: to_mautic(attributes))
77
+ assign_attributes json[endpoint.singularize]
78
+ clear_changes
79
+ rescue ValidationError => e
80
+ self.errors = e.errors
70
81
  end
71
82
 
72
83
  def create
73
84
  begin
74
- json = @connection.request(:post, "api/#{endpoint}/#{id && "#{id}/"}new", { body: to_mautic })
75
- self.attributes = json[endpoint.singularize]
85
+ json = @connection.request(:post, "api/#{endpoint}/#{id && "#{id}/"}new", body: to_mautic)
86
+ assign_attributes json[endpoint.singularize]
76
87
  clear_changes
77
88
  rescue ValidationError => e
78
89
  self.errors = e.errors
79
90
  end
80
91
 
81
- self.errors.blank?
92
+ errors.blank?
82
93
  end
83
94
 
84
95
  def destroy
@@ -95,6 +106,12 @@ module Mautic
95
106
  @table.changes
96
107
  end
97
108
 
109
+ def changed?
110
+ return @changed unless @changed.nil?
111
+
112
+ @changed = !changes.empty?
113
+ end
114
+
98
115
  def attributes
99
116
  @table.to_h
100
117
  end
@@ -106,15 +123,22 @@ module Mautic
106
123
  end
107
124
  end
108
125
 
109
- def to_mautic
110
- @table.each_with_object({}) do |(key,val), mem|
111
- mem[key] = val.is_a?(Array) ? val.join("|") : val
126
+ def to_mautic(data = @table)
127
+ data.each_with_object({}) do |(key, val), mem|
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
112
135
  end
113
136
  end
114
137
 
115
138
  private
116
139
 
117
140
  def clear_changes
141
+ @changed = nil
118
142
  @table.instance_variable_set(:@changes, nil)
119
143
  end
120
144
 
@@ -1,3 +1,3 @@
1
1
  module Mautic
2
- VERSION = '2.3.5'
2
+ VERSION = '2.3.10'
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.5
4
+ version: 2.3.10
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-03-03 00:00:00.000000000 Z
11
+ date: 2020-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails