-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2994 from zendesk/gurney/default
Support setting a default user role for new users
- Loading branch information
Showing
5 changed files
with
166 additions
and
148 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
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 |
---|---|---|
|
@@ -11,27 +11,27 @@ def new | |
|
||
def github | ||
return show_login_restriction unless role_id = github_authorization.role_id | ||
login(role_id: role_id) | ||
login(role_id: custom_role_or_default(role_id)) | ||
end | ||
|
||
def google | ||
return show_login_restriction unless allowed_to_login | ||
login(role_id: Role::VIEWER.id) | ||
login(role_id: custom_role_or_default(Role::VIEWER.id)) | ||
end | ||
|
||
def ldap | ||
return show_login_restriction unless allowed_to_login | ||
login(role_id: Role::VIEWER.id) | ||
login(role_id: custom_role_or_default(Role::VIEWER.id)) | ||
end | ||
|
||
def gitlab | ||
return show_login_restriction unless allowed_to_login | ||
login(role_id: Role::VIEWER.id) | ||
login(role_id: custom_role_or_default(Role::VIEWER.id)) | ||
end | ||
|
||
def bitbucket | ||
return show_login_restriction unless allowed_to_login | ||
login(role_id: Role::VIEWER.id) | ||
login(role_id: custom_role_or_default(Role::VIEWER.id)) | ||
end | ||
|
||
def failure | ||
|
@@ -98,7 +98,7 @@ def login(options = {}) | |
uid = auth_hash.uid | ||
end | ||
|
||
user = User.create_or_update_from_hash(options.merge( | ||
user = find_or_create_user_from_hash(options.merge( | ||
external_id: "#{strategy.name}-#{uid}", | ||
name: auth_hash.info.name, | ||
email: auth_hash.info.email | ||
|
@@ -114,4 +114,15 @@ def login(options = {}) | |
|
||
redirect_to_origin_or_default | ||
end | ||
|
||
def find_or_create_user_from_hash(hash) | ||
# first user will be promoted to super admin | ||
hash[:role_id] = Role::SUPER_ADMIN.id unless User.where.not(email: '[email protected]').exists? | ||
|
||
User.create_with(hash).find_or_create_by(external_id: hash[:external_id].to_s) | ||
end | ||
|
||
def custom_role_or_default(default) | ||
Integer(ENV.fetch('DEFAULT_USER_ROLE', default)) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,29 +87,6 @@ def administrated_projects | |
scope | ||
end | ||
|
||
def self.create_or_update_from_hash(hash) | ||
user = User.where(external_id: hash[:external_id].to_s).first || User.new | ||
|
||
# attributes are always a string hash | ||
attributes = user.attributes.merge(hash.stringify_keys) do |attribute, old, new| | ||
if attribute == 'role_id' | ||
if !User.where.not(email: '[email protected]').exists? | ||
Role::SUPER_ADMIN.id # first user will be promoted to super admin | ||
elsif new && (user.new_record? || new >= old) | ||
new # existing users can upgrade | ||
else | ||
old | ||
end | ||
else | ||
old.presence || new | ||
end | ||
end | ||
|
||
user.attributes = attributes | ||
user.save | ||
user | ||
end | ||
|
||
def name | ||
super.presence || email | ||
end | ||
|
Oops, something went wrong.