-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplugin.rb
187 lines (151 loc) · 4.99 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# name: procourse-teaser
# about: Provides methods for teasing the content behind a secured category.
# version: 0.1
# authors: Joe Buhlig
# url: https://github.com/procourse/procourse-teaser
enabled_site_setting :procourse_teaser_enabled
register_asset 'stylesheets/procourse-teaser.scss'
after_initialize do
register_svg_icon "shield" if respond_to?(:register_svg_icon)
class ::Category
def self.reset_teasing_cache
@allowed_teasing_cache["allowed"] =
begin
Set.new(
CategoryCustomField
.where(name: "enable_topic_teasing", value: "true")
.pluck(:category_id)
)
end
end
@allowed_teasing_cache = DistributedCache.new("allowed_topic_teasing")
def self.can_tease?(category_id)
return false unless SiteSetting.procourse_teaser_enabled
unless set = @allowed_teasing_cache["allowed"]
set = reset_teasing_cache
end
set.include?(category_id)
end
after_save :reset_teasing_cache
protected
def reset_teasing_cache
::Category.reset_teasing_cache
end
end
require_dependency 'guardian'
module ::CategoryGuardian
alias_method :super_allowed_category_ids, :allowed_category_ids
def allowed_category_ids
ids = super_allowed_category_ids
if SiteSetting.procourse_teaser_enabled
teased_ids = CategoryCustomField.where(name: "enable_topic_teasing").where(value: "true").pluck(:category_id)
ids += teased_ids
end
return ids
end
alias_method :super_secure_category_ids, :secure_category_ids
def secure_category_ids
ids = super_secure_category_ids
if SiteSetting.procourse_teaser_enabled
teased_ids = CategoryCustomField.where(name: "enable_topic_teasing").where(value: "true").pluck(:category_id)
ids += teased_ids
end
return ids
end
end
require_dependency 'topic'
class ::Topic
def teased?(user)
return false unless SiteSetting.procourse_teaser_enabled
if self.archetype == "private_message"
return false
else
if !user
group_access = false
elsif (user && self.category && self.category.category_groups && self.category.category_groups.pluck(:group_id).length > 0)
group_access = (self.category.category_groups.pluck(:group_id) & user.groups.pluck(:id)).length > 0
else
group_access = true
end
category = Category.find(category_id)
if category.custom_fields && category.custom_fields["enable_topic_teasing"] && category.custom_fields["enable_topic_teasing"] == "true"
category_teasing = true
else
category_teasing = false
end
end
category_teasing && !group_access
end
def topic_teasing_url
if category_id && defined?(category_id)
Category.find(category_id).custom_fields["topic_teasing_url"] || "/"
else
""
end
end
def topic_teasing_icon
if category_id
Category.find(category_id).custom_fields["topic_teasing_icon"] || "shield"
else
""
end
end
end
require_dependency 'topic_list_item_serializer'
class ::TopicListItemSerializer
attributes :teased, :topic_teasing_url, :topic_teasing_icon
def teased
object.teased?(scope.user)
end
def topic_teasing_url
object.topic_teasing_url
end
def topic_teasing_icon
object.topic_teasing_icon
end
end
require_dependency 'topic_view_serializer'
class ::TopicViewSerializer
attributes :teased, :topic_teasing_url, :topic_teasing_icon
def teased
object.topic.teased?(scope.user)
end
def topic_teasing_url
object.topic.topic_teasing_url
end
def topic_teasing_icon
object.topic.topic_teasing_icon
end
end
require_dependency 'topics_controller'
class ::TopicsController
before_action :check_teaser, only: :show
def check_teaser
topic_view = TopicView.new(params[:id] || params[:topic_id], current_user)
if topic_view.topic.category && topic_view.topic.category.custom_fields["topic_teasing_url"] == "/login"
cookies[:teaser_url] = "/t/#{topic_view.topic.id}"
end
if topic_view.topic.category
url = topic_view.topic.category.custom_fields["topic_teasing_url"] || "/"
redirect_to url if topic_view.topic.teased?(current_user)
end
end
end
require_dependency 'session_controller'
class ::SessionController
before_action :check_teaser_on_login, only: :create
before_action :check_teaser_on_sso_redirect, only: :sso
def check_teaser_on_login
if cookies[:teaser_url]
cookies[:destination_url] = cookies[:teaser_url]
cookies.delete(:teaser_url)
end
end
def check_teaser_on_sso_redirect
if cookies[:teaser_url]
params[:return_path] = cookies[:teaser_url]
cookies.delete(:teaser_url)
end
end
end
end