This repository has been archived by the owner on May 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.rb
135 lines (103 loc) · 3.45 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
# name: discourse-persona-mozillians
# about: persona login provider with some mozillians magic sprinkled on top
# version: 1.1
# author: Vikhyat Korrapati, Leo McArdle
# url: https://github.com/mozilla-cit/discourse-persona-mozillians
gem 'omniauth-browserid-discourse', '0.0.2', require_name: 'omniauth-browserid'
class PersonaAuthenticator < ::Auth::Authenticator
def name
"persona"
end
def add_to_group(user, group_name)
group = Group.where(name: group_name).first
if group
if group.group_users.where(user_id: user.id).first.nil?
group.group_users.create(user_id: user.id, group_id: group.id)
end
end
end
def remove_from_group(user, group_name)
group = Group.where(name: group_name).first
if group
if not group.group_users.where(user_id: user.id).first.nil?
group.group_users.where(user_id: user.id).destroy_all
end
end
end
def purge_from_groups(user)
group_prefix = SiteSetting.mozillians_group_prefix
remove_from_group(user, group_prefix)
groups = Group.where("name LIKE '#{group_prefix}*_%' ESCAPE '*'")
groups.each do |group|
remove_from_group(user, group.name)
end
end
def mozillians_magic(user)
return unless SiteSetting.mozillians_enabled
mozillians_url = SiteSetting.mozillians_url
app_name = SiteSetting.mozillians_app_name
app_key = SiteSetting.mozillians_app_key
email = user.email
begin
uri = URI.parse("#{mozillians_url}/api/v1/users/?app_name=#{app_name}&app_key=#{app_key}&email=#{email}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if SiteSetting.mozillians_enable_ssl
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
if response.code.to_i == 200
res = JSON.parse(response.body)
total_count = res["meta"]["total_count"]
if total_count.to_i == 1
is_vouched = !!res["objects"].first["is_vouched"]
group_prefix = SiteSetting.mozillians_group_prefix
add_to_group(user, group_prefix)
if is_vouched
remove_from_group(user, "#{group_prefix}_unvouched")
add_to_group(user, "#{group_prefix}_vouched")
else
remove_from_group(user, "#{group_prefix}_vouched")
add_to_group(user, "#{group_prefix}_unvouched")
end
else
purge_from_groups(user)
end
else
purge_from_groups(user)
end
rescue Exception => details
puts "Failed to query API: #{details.message}"
purge_from_groups(user)
end
end
def after_authenticate(auth_token)
result = Auth::Result.new
result.email = auth_token[:info][:email]
result.email_valid = true
result.user = User.find_by_email(result.email)
mozillians_magic(result.user) if result.user.try(:id)
result
end
def after_create_account(user, auth)
mozillians_magic(user)
end
def register_middleware(omniauth)
omniauth.provider :browser_id, name: "persona"
end
end
auth_provider authenticator: PersonaAuthenticator.new
register_asset "javascripts/persona.js"
register_css <<CSS
.btn-social.persona {
background: #606060 !important;
}
.btn-social.persona:before {
content: "";
background-image: url("https://d1v2u343srvgst.cloudfront.net/persona-login-v2.png");
background-size: auto 15px;
background-repeat: no-repeat;
width: 20px;
height: 15px;
display: inline-block;
margin-bottom: -3px;
}
CSS