This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
forked from hms-networks/discourse-private-topic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.rb
56 lines (45 loc) · 1.66 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# name: private-topics
# about: Searchable private category topics
# version: 0.1
# authors: Jordan Seanor
# url: https://github.com/HMSAB/discourse-private-topic.git
enabled_site_setting :private_topics_enabled
register_asset("stylesheets/privatetopics.scss", :desktop)
after_initialize do
load File.expand_path("../controllers/privatetopic_controller.rb", __FILE__)
Discourse::Application.routes.prepend do
post 'privatetopic/control_access' => 'privatetopic#control_access'
end
Topic.register_custom_field_type('topic_restricted_access', :boolean)
add_to_serializer(:topic_view, :custom_fields, false) {object.topic.custom_fields}
module ::TopicLocked
def self.access_restricted(guardian, topic, user)
if !user.nil?
return false if guardian.is_admin? || user.id == topic.user_id
if topic.custom_fields["topic_restricted_access"]
if user.id != topic.user_id
return true
end
end
else
return true if topic.custom_fields["topic_restricted_access"] && user.nil?
end
end
class NoAccessLocked < StandardError; end
end
require_dependency 'topic_view'
class ::TopicView
alias_method :old_check_and_raise_exceptions, :check_and_raise_exceptions
def check_and_raise_exceptions
if SiteSetting.private_topics_enabled
raise ::TopicLocked::NoAccessLocked.new if TopicLocked.access_restricted(@guardian, @topic, @user)
end
end
end
require_dependency 'application_controller'
class ::ApplicationController
rescue_from ::TopicLocked::NoAccessLocked do
rescue_discourse_actions(:invalid_access, 403, include_ember: true)
end
end
end