mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 13:53:45 +08:00
FEATURE: Webhook for user creation and approval
This commit is contained in:
parent
fd9056973a
commit
c463cf63d4
|
@ -87,6 +87,7 @@ class User < ActiveRecord::Base
|
|||
after_create :ensure_in_trust_level_group
|
||||
after_create :automatic_group_membership
|
||||
after_create :set_default_categories_preferences
|
||||
after_create :trigger_user_created_event
|
||||
|
||||
before_save :update_username_lower
|
||||
before_save :ensure_password_is_hashed
|
||||
|
@ -266,7 +267,12 @@ class User < ActiveRecord::Base
|
|||
|
||||
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
|
||||
|
||||
def self.email_hash(email)
|
||||
|
@ -1007,6 +1013,11 @@ class User < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
def trigger_user_created_event
|
||||
DiscourseEvent.trigger(:user_created, self)
|
||||
true
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def previous_visit_at_update_required?(timestamp)
|
||||
|
|
|
@ -68,6 +68,12 @@ class WebHook < ActiveRecord::Base
|
|||
)
|
||||
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
|
||||
|
||||
# == Schema Information
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
class WebHookEventType < ActiveRecord::Base
|
||||
TOPIC = 1
|
||||
POST = 2
|
||||
USER = 3
|
||||
|
||||
has_and_belongs_to_many :web_hooks
|
||||
|
||||
|
|
|
@ -2467,12 +2467,9 @@ en:
|
|||
post_event:
|
||||
name: "Post Event"
|
||||
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:
|
||||
name: "User Event"
|
||||
details: "When there is a user is created or changed."
|
||||
name: "User Creation Event"
|
||||
details: "When a user is created or approved."
|
||||
delivery_status:
|
||||
title: "Delivery Status"
|
||||
inactive: "Inactive"
|
||||
|
|
|
@ -7,3 +7,8 @@ WebHookEventType.seed do |b|
|
|||
b.id = WebHookEventType::POST
|
||||
b.name = "post"
|
||||
end
|
||||
|
||||
WebHookEventType.seed do |b|
|
||||
b.id = WebHookEventType::USER
|
||||
b.name = "user"
|
||||
end
|
||||
|
|
|
@ -64,6 +64,7 @@ describe PostCreator do
|
|||
end
|
||||
|
||||
context "success" do
|
||||
before { creator }
|
||||
|
||||
it "doesn't return true for spam" do
|
||||
creator.create
|
||||
|
@ -71,6 +72,7 @@ describe PostCreator do
|
|||
end
|
||||
|
||||
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(:validate_post, anything).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
|
||||
log_in :admin
|
||||
|
||||
user
|
||||
DiscourseEvent.expects(:trigger).with(:user_badge_granted, anything, anything).once
|
||||
xhr :post, :create, badge_id: badge.id, username: user.username
|
||||
end
|
||||
|
@ -126,6 +126,7 @@ describe UserBadgesController do
|
|||
|
||||
it 'will trigger :user_badge_removed' do
|
||||
log_in :admin
|
||||
|
||||
DiscourseEvent.expects(:trigger).with(:user_badge_removed, anything, anything).once
|
||||
xhr :delete, :destroy, id: user_badge.id
|
||||
end
|
||||
|
|
|
@ -58,6 +58,12 @@ describe User do
|
|||
user.approve(admin)
|
||||
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
|
||||
before do
|
||||
user.approve(admin)
|
||||
|
@ -153,8 +159,13 @@ describe User do
|
|||
expect(subject.approved_by_id).to be_blank
|
||||
end
|
||||
|
||||
it 'triggers an extensibility event' do
|
||||
DiscourseEvent.expects(:trigger).with(:user_created, subject).once
|
||||
subject.save!
|
||||
end
|
||||
|
||||
context 'after_save' do
|
||||
before { subject.save }
|
||||
before { subject.save! }
|
||||
|
||||
it "has correct settings" do
|
||||
expect(subject.email_tokens).to be_present
|
||||
|
|
|
@ -103,9 +103,10 @@ describe WebHook do
|
|||
let!(:post_hook) { Fabricate(:web_hook) }
|
||||
let!(:topic_hook) { Fabricate(:topic_web_hook) }
|
||||
let(:user) { Fabricate(:user) }
|
||||
let(:admin) { Fabricate(:admin) }
|
||||
let(:topic) { Fabricate(:topic, user: user) }
|
||||
let(:post) { Fabricate(:post, topic: topic) }
|
||||
let(:post2) { Fabricate(:post, topic: topic) }
|
||||
let(:post) { Fabricate(:post, topic: topic, user: user) }
|
||||
let(:post2) { Fabricate(:post, topic: topic, user: user) }
|
||||
|
||||
it 'should enqueue the right hooks for topic events' do
|
||||
WebHook.expects(:enqueue_topic_hooks).once
|
||||
|
@ -119,6 +120,7 @@ describe WebHook do
|
|||
end
|
||||
|
||||
it 'should enqueue the right hooks for post events' do
|
||||
user # bypass a user_created event
|
||||
WebHook.expects(:enqueue_hooks).once
|
||||
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
|
||||
PostDestroyer.new(user, post2).recover
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user