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
|
# File 'app/controllers/concerns/account/oauth/omniauth_callbacks/controller_base.rb', line 13
def callback(class_name, team_id)
oauth_account_class = "Oauth::#{class_name}Account".constantize
oauth_accounts_attribute = :"oauth_#{class_name.underscore}_account"
integrations_installations_class = "::Integrations::#{class_name}Installation".constantize
integrations_installations_collection = :"integrations_#{class_name.underscore}_installations"
auth = request.env["omniauth.auth"]
if params[:denied]
message = t("omniauth.team.denied", provider: t(auth.provider))
path = if team_id
[:account, team, integrations_installations_collection]
elsif current_user
[:account, current_user]
else
new_user_registration_path
end
redirect_to path, notice: message
end
begin
oauth_account = oauth_account_class.find_or_create_by(uid: auth.uid)
oauth_account.update_from_oauth(auth)
rescue PG::UniqueViolation
retry
end
if team_id
authenticate_user!
unless (team = current_user.teams.find_by(id: team_id))
raise "user is adding an integration to a team they don't belong to?"
end
if can? :create, integrations_installations_class.new(:team => team, oauth_accounts_attribute => oauth_account)
if team.send(integrations_installations_collection).find_by(oauth_accounts_attribute => oauth_account)
message = t("omniauth.team.already_present", provider: t(auth.provider))
else
team.send(integrations_installations_collection).create(:name => oauth_account.name, oauth_accounts_attribute => oauth_account)
message = t("omniauth.team.connected", provider: t(auth.provider))
end
else
message = t("omniauth.team.not_allowed", provider: t(auth.provider))
end
redirect_to [:account, team, integrations_installations_collection], notice: message
elsif current_user
if oauth_account.user
message_key = (oauth_account.user == current_user) ? "omniauth.user.reconnected" : "omniauth.user.already_registered"
redirect_to edit_account_user_path(current_user), notice: t(message_key, provider: t(auth.provider))
else
oauth_account.update(user: current_user)
redirect_to edit_account_user_path(current_user), notice: t("omniauth.user.connected", provider: t(auth.provider))
end
elsif oauth_account.user
sign_in oauth_account.user
handle_outstanding_invitation
redirect_to account_dashboard_path
elsif show_sign_up_options?
email = auth.info.email.present? ? auth.info.email : "noreply@#{SecureRandom.hex}.example.com"
if User.find_by(email: email)
redirect_to new_user_session_path(user: {email: email}, email_exists: true), notice: "Sorry, there is already a user registered with the email address #{email}, but this #{t(auth.provider)} account isn't configured for login with that account. Please sign in using your password and then add this account."
else
password = Devise.friendly_token[0, 20]
user = User.create(email: email, password: password, password_confirmation: password)
oauth_account.update(user: user)
sign_in user
handle_outstanding_invitation
unless user.teams.any?
user.create_default_team
user.teams.first.send(integrations_installations_collection).create(:name => oauth_account.name, oauth_accounts_attribute => oauth_account)
end
redirect_to account_dashboard_path
end
else
redirect_to new_user_session_path, notice: t("omniauth.user.account_not_found", provider: t(auth.provider))
end
end
|