mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 09:36:19 -05:00
FEATURE: Webhook for user creation and approval
This commit is contained in:
parent
fd9056973a
commit
c463cf63d4
9 changed files with 57 additions and 10 deletions
|
@ -87,6 +87,7 @@ class User < ActiveRecord::Base
|
||||||
after_create :ensure_in_trust_level_group
|
after_create :ensure_in_trust_level_group
|
||||||
after_create :automatic_group_membership
|
after_create :automatic_group_membership
|
||||||
after_create :set_default_categories_preferences
|
after_create :set_default_categories_preferences
|
||||||
|
after_create :trigger_user_created_event
|
||||||
|
|
||||||
before_save :update_username_lower
|
before_save :update_username_lower
|
||||||
before_save :ensure_password_is_hashed
|
before_save :ensure_password_is_hashed
|
||||||
|
@ -266,7 +267,12 @@ class User < ActiveRecord::Base
|
||||||
|
|
||||||
self.approved_at = Time.now
|
self.approved_at = Time.now
|
||||||
|
|
||||||
send_approval_email if save and send_mail
|
if result = save
|
||||||
|
send_approval_email if send_mail
|
||||||
|
DiscourseEvent.trigger(:user_approved, self)
|
||||||
|
end
|
||||||
|
|
||||||
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.email_hash(email)
|
def self.email_hash(email)
|
||||||
|
@ -1007,6 +1013,11 @@ class User < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def trigger_user_created_event
|
||||||
|
DiscourseEvent.trigger(:user_created, self)
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def previous_visit_at_update_required?(timestamp)
|
def previous_visit_at_update_required?(timestamp)
|
||||||
|
|
|
@ -68,6 +68,12 @@ class WebHook < ActiveRecord::Base
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
%i(user_created user_approved).each do |event|
|
||||||
|
DiscourseEvent.on(event) do |user|
|
||||||
|
WebHook.enqueue_hooks(:user, user_id: user.id, event_name: event.to_s)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# == Schema Information
|
# == Schema Information
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
class WebHookEventType < ActiveRecord::Base
|
class WebHookEventType < ActiveRecord::Base
|
||||||
TOPIC = 1
|
TOPIC = 1
|
||||||
POST = 2
|
POST = 2
|
||||||
|
USER = 3
|
||||||
|
|
||||||
has_and_belongs_to_many :web_hooks
|
has_and_belongs_to_many :web_hooks
|
||||||
|
|
||||||
|
|
|
@ -2467,12 +2467,9 @@ en:
|
||||||
post_event:
|
post_event:
|
||||||
name: "Post Event"
|
name: "Post Event"
|
||||||
details: "When there is a new reply, edit, deleted or recovered."
|
details: "When there is a new reply, edit, deleted or recovered."
|
||||||
invitation_event:
|
|
||||||
name: "Invitation Event"
|
|
||||||
details: "When a invitation is sent or accepted."
|
|
||||||
user_event:
|
user_event:
|
||||||
name: "User Event"
|
name: "User Creation Event"
|
||||||
details: "When there is a user is created or changed."
|
details: "When a user is created or approved."
|
||||||
delivery_status:
|
delivery_status:
|
||||||
title: "Delivery Status"
|
title: "Delivery Status"
|
||||||
inactive: "Inactive"
|
inactive: "Inactive"
|
||||||
|
|
|
@ -7,3 +7,8 @@ WebHookEventType.seed do |b|
|
||||||
b.id = WebHookEventType::POST
|
b.id = WebHookEventType::POST
|
||||||
b.name = "post"
|
b.name = "post"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
WebHookEventType.seed do |b|
|
||||||
|
b.id = WebHookEventType::USER
|
||||||
|
b.name = "user"
|
||||||
|
end
|
||||||
|
|
|
@ -64,6 +64,7 @@ describe PostCreator do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "success" do
|
context "success" do
|
||||||
|
before { creator }
|
||||||
|
|
||||||
it "doesn't return true for spam" do
|
it "doesn't return true for spam" do
|
||||||
creator.create
|
creator.create
|
||||||
|
@ -71,6 +72,7 @@ describe PostCreator do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "triggers extensibility events" do
|
it "triggers extensibility events" do
|
||||||
|
creator # bypass a user_created event, can be removed when there is a UserCreator
|
||||||
DiscourseEvent.expects(:trigger).with(:before_create_post, anything).once
|
DiscourseEvent.expects(:trigger).with(:before_create_post, anything).once
|
||||||
DiscourseEvent.expects(:trigger).with(:validate_post, anything).once
|
DiscourseEvent.expects(:trigger).with(:validate_post, anything).once
|
||||||
DiscourseEvent.expects(:trigger).with(:topic_created, anything, anything, user).once
|
DiscourseEvent.expects(:trigger).with(:topic_created, anything, anything, user).once
|
||||||
|
|
|
@ -102,7 +102,7 @@ describe UserBadgesController do
|
||||||
|
|
||||||
it 'will trigger :user_badge_granted' do
|
it 'will trigger :user_badge_granted' do
|
||||||
log_in :admin
|
log_in :admin
|
||||||
|
user
|
||||||
DiscourseEvent.expects(:trigger).with(:user_badge_granted, anything, anything).once
|
DiscourseEvent.expects(:trigger).with(:user_badge_granted, anything, anything).once
|
||||||
xhr :post, :create, badge_id: badge.id, username: user.username
|
xhr :post, :create, badge_id: badge.id, username: user.username
|
||||||
end
|
end
|
||||||
|
@ -126,6 +126,7 @@ describe UserBadgesController do
|
||||||
|
|
||||||
it 'will trigger :user_badge_removed' do
|
it 'will trigger :user_badge_removed' do
|
||||||
log_in :admin
|
log_in :admin
|
||||||
|
|
||||||
DiscourseEvent.expects(:trigger).with(:user_badge_removed, anything, anything).once
|
DiscourseEvent.expects(:trigger).with(:user_badge_removed, anything, anything).once
|
||||||
xhr :delete, :destroy, id: user_badge.id
|
xhr :delete, :destroy, id: user_badge.id
|
||||||
end
|
end
|
||||||
|
|
|
@ -58,6 +58,12 @@ describe User do
|
||||||
user.approve(admin)
|
user.approve(admin)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'triggers a extensibility event' do
|
||||||
|
user && admin # bypass the user_created event
|
||||||
|
DiscourseEvent.expects(:trigger).with(:user_approved, user).once
|
||||||
|
user.approve(admin)
|
||||||
|
end
|
||||||
|
|
||||||
context 'after approval' do
|
context 'after approval' do
|
||||||
before do
|
before do
|
||||||
user.approve(admin)
|
user.approve(admin)
|
||||||
|
@ -153,8 +159,13 @@ describe User do
|
||||||
expect(subject.approved_by_id).to be_blank
|
expect(subject.approved_by_id).to be_blank
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'triggers an extensibility event' do
|
||||||
|
DiscourseEvent.expects(:trigger).with(:user_created, subject).once
|
||||||
|
subject.save!
|
||||||
|
end
|
||||||
|
|
||||||
context 'after_save' do
|
context 'after_save' do
|
||||||
before { subject.save }
|
before { subject.save! }
|
||||||
|
|
||||||
it "has correct settings" do
|
it "has correct settings" do
|
||||||
expect(subject.email_tokens).to be_present
|
expect(subject.email_tokens).to be_present
|
||||||
|
|
|
@ -103,9 +103,10 @@ describe WebHook do
|
||||||
let!(:post_hook) { Fabricate(:web_hook) }
|
let!(:post_hook) { Fabricate(:web_hook) }
|
||||||
let!(:topic_hook) { Fabricate(:topic_web_hook) }
|
let!(:topic_hook) { Fabricate(:topic_web_hook) }
|
||||||
let(:user) { Fabricate(:user) }
|
let(:user) { Fabricate(:user) }
|
||||||
|
let(:admin) { Fabricate(:admin) }
|
||||||
let(:topic) { Fabricate(:topic, user: user) }
|
let(:topic) { Fabricate(:topic, user: user) }
|
||||||
let(:post) { Fabricate(:post, topic: topic) }
|
let(:post) { Fabricate(:post, topic: topic, user: user) }
|
||||||
let(:post2) { Fabricate(:post, topic: topic) }
|
let(:post2) { Fabricate(:post, topic: topic, user: user) }
|
||||||
|
|
||||||
it 'should enqueue the right hooks for topic events' do
|
it 'should enqueue the right hooks for topic events' do
|
||||||
WebHook.expects(:enqueue_topic_hooks).once
|
WebHook.expects(:enqueue_topic_hooks).once
|
||||||
|
@ -119,6 +120,7 @@ describe WebHook do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should enqueue the right hooks for post events' do
|
it 'should enqueue the right hooks for post events' do
|
||||||
|
user # bypass a user_created event
|
||||||
WebHook.expects(:enqueue_hooks).once
|
WebHook.expects(:enqueue_hooks).once
|
||||||
PostCreator.create(user, { raw: 'post', topic_id: topic.id, reply_to_post_number: 1, skip_validations: true })
|
PostCreator.create(user, { raw: 'post', topic_id: topic.id, reply_to_post_number: 1, skip_validations: true })
|
||||||
|
|
||||||
|
@ -128,5 +130,16 @@ describe WebHook do
|
||||||
WebHook.expects(:enqueue_hooks).once
|
WebHook.expects(:enqueue_hooks).once
|
||||||
PostDestroyer.new(user, post2).recover
|
PostDestroyer.new(user, post2).recover
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should enqueue the right hooks for user creation events' do
|
||||||
|
WebHook.expects(:enqueue_hooks).once
|
||||||
|
user
|
||||||
|
|
||||||
|
WebHook.expects(:enqueue_hooks).once
|
||||||
|
admin
|
||||||
|
|
||||||
|
WebHook.expects(:enqueue_hooks).once
|
||||||
|
user.approve(admin)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue