-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'AllYourBot:main' into add-support-for-gemini
- Loading branch information
Showing
48 changed files
with
646 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
3.3.5 | ||
3.3.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
ruby 3.3.5 | ||
ruby 3.3.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
app/controllers/authentications/microsoft_graph_oauth_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
class Authentications::MicrosoftGraphOauthController < ApplicationController | ||
allow_unauthenticated_access | ||
|
||
# GET /auth/microsoft_graph/callback | ||
def create | ||
if params[:error] | ||
if Current.user | ||
redirect_to(edit_settings_person_path, alert: params[:error_description]) | ||
else | ||
redirect_to(login_path, alert: params[:error_description]) | ||
end | ||
return | ||
end | ||
|
||
if Current.user | ||
Current.user.microsoft_graph_credential&.destroy | ||
_, cred = add_person_credentials("MicrosoftGraphCredential") | ||
cred.save! && redirect_to(edit_settings_person_path, notice: "Saved") && return | ||
|
||
elsif (credential = MicrosoftGraphCredential.find_by(oauth_id: auth[:uid])) | ||
@person = credential.user.person | ||
|
||
elsif Feature.disabled?(:registration) | ||
redirect_to(root_path, alert: "Registration is disabled") && return | ||
|
||
elsif auth_email && (user = Person.find_by(email: auth_email)&.user) | ||
@person = init_for_user(user) | ||
|
||
elsif auth_email && (@person = Person.find_by(email: auth_email)) | ||
@person = init_for_person(@person) | ||
|
||
else | ||
@person = initialize_microsoft_person | ||
end | ||
|
||
if @person&.save | ||
login_as(@person, credential: @person.user.reload.microsoft_graph_credential) | ||
redirect_to root_path | ||
else | ||
@person&.errors&.delete :personable | ||
msg = @person.errors.full_messages.map { |m| m.gsub(/Personable |credentials /, "") }.to_sentence.capitalize | ||
if msg.downcase.include?("oauth refresh token can't be blank") | ||
msg += " " + helpers.link_to("Microsoft third-party connections", "https://account.microsoft.com/privacy/app-access", class: "underline") + " search for website, and delete all it's connections. Then try again." | ||
end | ||
|
||
redirect_to new_user_path, alert: msg | ||
end | ||
rescue => e | ||
warn e.message | ||
warn e.backtrace.join("\n") | ||
redirect_to edit_settings_person_path, alert: "Error. #{e.message}", status: :see_other | ||
end | ||
|
||
private | ||
|
||
def auth | ||
request.env["omniauth.auth"]&.deep_symbolize_keys || {} | ||
end | ||
|
||
def auth_email | ||
auth.dig(:info, :email) | ||
end | ||
|
||
def init_for_user(user) | ||
user.microsoft_graph_credential&.destroy | ||
|
||
user.first_name = auth[:info][:first_name] | ||
user.last_name = auth[:info][:last_name] | ||
@person = user.person | ||
add_person_credentials("MicrosoftGraphCredential").first | ||
end | ||
|
||
def init_for_person(person) | ||
@person.personable_type = "User" | ||
@person.personable_attributes = { | ||
first_name: auth[:info][:first_name], | ||
last_name: auth[:info][:last_name] | ||
} | ||
add_person_credentials("MicrosoftGraphCredential").first | ||
end | ||
|
||
def initialize_microsoft_person | ||
@person = Person.new({ | ||
personable_type: "User", | ||
email: auth_email, | ||
personable_attributes: { | ||
first_name: auth[:info][:first_name], | ||
last_name: auth[:info][:last_name], | ||
} | ||
}) | ||
add_person_credentials("MicrosoftGraphCredential").first | ||
end | ||
|
||
def add_person_credentials(type) | ||
p = Current.person || @person | ||
c = p.user.credentials.build( | ||
type: type, | ||
oauth_id: auth[:uid], | ||
oauth_email: auth[:info][:email], | ||
oauth_token: auth[:credentials][:token], | ||
oauth_refresh_token: auth[:credentials][:refresh_token], | ||
properties: auth[:credentials].except(:token, :refresh_token) | ||
) | ||
[p, c] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class MicrosoftGraphCredential < Credential | ||
alias_attribute :oauth_id, :external_id | ||
|
||
validates :oauth_token, presence: true | ||
validates :oauth_refresh_token, presence: true | ||
validates :oauth_id, presence: true, uniqueness: true | ||
validates :oauth_email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP } | ||
|
||
normalizes :oauth_email, with: -> email { email.downcase.strip } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.