diff --git a/app/controllers/admins_controller.rb b/app/controllers/admins_controller.rb index 7ee7cc44..866c9d2e 100644 --- a/app/controllers/admins_controller.rb +++ b/app/controllers/admins_controller.rb @@ -205,17 +205,7 @@ class AdminsController < ApplicationController return redirect_to admin_roles_path end - # Create the new role with the second highest priority - # This means that it will only be more important than the user role - # This also updates the user role to have the highest priority - new_role = Role.create(name: new_role_name, provider: @user_domain) - user_role = Role.find_by(name: 'user', provider: @user_domain) - - new_role.priority = user_role.priority - user_role.priority += 1 - - new_role.save! - user_role.save! + new_role = Role.create_new_role(new_role_name, @user_domain) redirect_to admin_roles_path(selected_role: new_role.id) end @@ -283,6 +273,9 @@ class AdminsController < ApplicationController :colour ) + # Role is a default role so users can't change the name + role_params[:name] = role.name if Role::RESERVED_ROLE_NAMES.include?(role.name) + # Make sure if the user is updating the role name that the role name is valid if role.name != role_params[:name] && !Role.duplicate_name(role_params[:name], @user_domain) && !role_params[:name].strip.empty? diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index 125108b7..d05734cf 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -42,7 +42,7 @@ class UserMailer < ApplicationMailer @admin_url = url + "admins" @image = image @color = color - @role = role.name + @role = translated_role_name(role) mail to: user.email, subject: t('mailer.user.promoted.subtitle', role: translated_role_name(role)) end @@ -51,7 +51,7 @@ class UserMailer < ApplicationMailer @root_url = url @image = image @color = color - @role = role.name + @role = translated_role_name(role) mail to: user.email, subject: t('mailer.user.demoted.subtitle', role: translated_role_name(role)) end diff --git a/app/models/role.rb b/app/models/role.rb index a178fad4..6f0e932d 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -40,4 +40,20 @@ class Role < ApplicationRecord send_promoted_email: true, send_demoted_email: true, can_edit_site_settings: true, can_edit_roles: true, can_manage_users: true, colour: "#cd201f") end + + def self.create_new_role(role_name, provider) + # Create the new role with the second highest priority + # This means that it will only be more important than the user role + # This also updates the user role to have the highest priority + role = Role.create(name: role_name, provider: provider) + user_role = Role.find_by(name: 'user', provider: provider) + + role.priority = user_role.priority + user_role.priority += 1 + + role.save! + user_role.save! + + role + end end diff --git a/app/models/user.rb b/app/models/user.rb index 9f2a65b8..92ed8c74 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -248,7 +248,15 @@ class User < ApplicationRecord unless has_role?(role) role_provider = Rails.configuration.loadbalanced_configuration ? provider : "greenlight" - roles << Role.find_or_create_by(name: role, provider: role_provider) + new_role = Role.find_by(name: role, provider: role_provider) + + if new_role.nil? + return if Role.duplicate_name(role, role_provider) || role.strip.empty? + + new_role = Role.create_new_role(role, role_provider) + end + + roles << new_role save! end diff --git a/app/views/admins/components/_roles.html.erb b/app/views/admins/components/_roles.html.erb index b88548cd..9d8cf77b 100644 --- a/app/views/admins/components/_roles.html.erb +++ b/app/views/admins/components/_roles.html.erb @@ -15,12 +15,13 @@
+ <% current_role = current_user.highest_priority_role%>
<% @roles.each do |role| %> <%= link_to admin_roles_path(selected_role: role.id), - class: "#{"sort-disabled" if role.name == "user" || role.name == "admin" || role.priority <= current_user.highest_priority_role.priority } dropdown-item list-group-item list-group-item-action #{"active" if @selected_role.id == role.id}", + class: "#{"sort-disabled" if role.name == "user" || role.name == "admin" || role.priority <= current_role.priority } dropdown-item list-group-item list-group-item-action #{"active" if @selected_role.id == role.id}", id: dom_id(role) do %> <%= translated_role_name(role) %> <% end %> @@ -47,34 +48,34 @@
-