FEATURE: Experimental support for group membership via google auth (#14835)

This commit introduces a new site setting "google_oauth2_hd_groups". If enabled, group information will be fetched from Google during authentication, and stored in the Discourse database. These 'associated groups' can be connected to a Discourse group via the "Membership" tab of the group preferences UI. 

The majority of the implementation is generic, so we will be able to add support to more authentication methods in the near future.

https://meta.discourse.org/t/managing-group-membership-via-authentication/175950
This commit is contained in:
Angus McLeod
2021-12-09 20:30:27 +08:00
committed by GitHub
parent 347669ef04
commit df3886d6e5
44 changed files with 926 additions and 16 deletions

View File

@ -21,7 +21,8 @@ class Auth::Result
:omniauth_disallow_totp,
:failed,
:failed_reason,
:failed_code
:failed_code,
:associated_groups
]
attr_accessor *ATTRIBUTES
@ -36,7 +37,8 @@ class Auth::Result
:name,
:authenticator_name,
:extra_data,
:skip_email_validation
:skip_email_validation,
:associated_groups
]
def [](key)
@ -94,6 +96,29 @@ class Auth::Result
change_made
end
def apply_associated_attributes!
if authenticator&.provides_groups? && !associated_groups.nil?
associated_group_ids = []
associated_groups.uniq.each do |associated_group|
begin
associated_group = AssociatedGroup.find_or_create_by(
name: associated_group[:name],
provider_id: associated_group[:id],
provider_name: extra_data[:provider]
)
rescue ActiveRecord::RecordNotUnique
retry
end
associated_group_ids.push(associated_group.id)
end
user.update(associated_group_ids: associated_group_ids)
AssociatedGroup.where(id: associated_group_ids).update_all("last_used = CURRENT_TIMESTAMP")
end
end
def can_edit_name
!SiteSetting.auth_overrides_name
end
@ -167,6 +192,10 @@ class Auth::Result
username || name || email
end
def authenticator
@authenticator ||= Discourse.enabled_authenticators.find { |a| a.name == authenticator_name }
end
def resolve_username
if staged_user
if !username.present? || UserNameSuggester.fix_username(username) == staged_user.username