-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.rb
More file actions
136 lines (120 loc) · 3.43 KB
/
plugin.rb
File metadata and controls
136 lines (120 loc) · 3.43 KB
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
# name: after-logout-endpoint
# about: An endpoint to redirect to after logout which performs additional functions to the user browser
# version: 0.0.2
# authors: Stackable Regiments pty ltd
# url: https://github.com/StackableRegiments/discourseLogout
enabled_site_setting :enhancedLogout_enabled
after_initialize do
module ::Enderpoint
PLUGIN_NAME = "enhanced_logout".freeze
class Engine < ::Rails::Engine
engine_name PLUGIN_NAME
isolate_namespace Enderpoint
end
end
require_dependency "application_controller"
class Enderpoint::EnderController < ::ApplicationController
skip_before_action :redirect_to_login_if_required, :check_xhr
def clearBrowserHistory
if SiteSetting.enhancedLogout_should_clear_cookies? then
<<~SCRIPT_CONTENT
console.log("clearing browser history");
SCRIPT_CONTENT
else
""
end
end
def clearCookies
if SiteSetting.enhancedLogout_should_clear_browser_session_history? then
<<~SCRIPT_CONTENT
var getDocumentCookie = function(){
return document.cookie.split(";");
};
var getDomainParts = function(){
return window.location.hostname.split(".");
};
var getPathParts = function(){
return window.location.pathname.split("/");
};
var cookies = getDocumentCookie();
console.log("clearingCookies",cookies);
var oneDay = 24 * 60 * 60 * 1000;
var expiringDate = new Date(new Date().getTime() - oneDay).toGMTString();
var domainParts = getDomainParts();
var pathParts = getPathParts();
for (var i = 0; i < cookies.length; i++){
var cookie = cookies[i];
var parts = cookie.split("=");
var name = parts[0];
var value = parts[1];
if (name !== ""){
var terminalCookie = name+"="+value+"; expires="+expiringDate+"; path=/;";
console.log("clearing base cookie: ",cookie,terminalCookie);
document.cookie = terminalCookie;
for (var di = domainParts.length - 1; di >= 0; di--){
for (var pi = 0; pi <= pathParts.length; pi++){
var domain = domainParts.slice(di,domainParts.length).join(".");
var path = "/"+pathParts.slice(0,pi).join("/");
var terminalCookie = name+"="+value+"; expires="+expiringDate+"; path="+path+"; domain="+domain+";"
console.log("clearing potential cookie: ",cookie,terminalCookie);
document.cookie = terminalCookie;
}
}
}
}
cookies = getDocumentCookie();
console.log("clearedCookies",cookies);
SCRIPT_CONTENT
else
""
end
end
def redirectAgain
if SiteSetting.enhancedLogout_should_redirect? && !SiteSetting.enhancedLogout_redirect_url.blank? then
<<~SCRIPT_CONTENT
var redirectionLocation = "#{SiteSetting.enhancedLogout_redirect_url}";
console.log("redirecting to",redirectionLocation);
window.location = redirectionLocation;
SCRIPT_CONTENT
else
""
end
end
def closeTab
if SiteSetting.enhancedLogout_should_close_tab? then
<<~SCRIPT_CONTENT
console.log("attempting to close tab");
window.open('','_self').close();
SCRIPT_CONTENT
else
""
end
end
def showContent
SiteSetting.enhancedLogout_custom_logout_page_html
end
def index
render inline: <<-HTML_CONTENT
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<script type="text/javascript">
#{clearBrowserHistory}
#{clearCookies}
#{redirectAgain}
#{closeTab}
</script>
</head>
<body>
#{showContent}
</body>
</html>
HTML_CONTENT
end
end
Enderpoint::Engine.routes.draw do
get "/" => "ender#index"
end
Discourse::Application.routes.append do
mount ::Enderpoint::Engine, at: "/enhanced-logout"
end
end