diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f6dc1eab7..8b1ef66c5 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,7 +3,7 @@ class ApplicationController < ActionController::Base before_action :authenticate_auth_user! - def check_admin + def render_unauthorized return false if helpers.admin? render 'unauthorized', status: :unauthorized diff --git a/app/controllers/skills_controller.rb b/app/controllers/skills_controller.rb index 543c09de0..25cbebd86 100644 --- a/app/controllers/skills_controller.rb +++ b/app/controllers/skills_controller.rb @@ -3,7 +3,7 @@ class SkillsController < CrudController include ExportController - before_action :check_admin, except: %i[index show unrated_by_person] + before_action :render_unauthorized, except: %i[index show unrated_by_person] self.permitted_attrs = %i[title radar portfolio default_set category_id] diff --git a/app/helpers/auth_helper.rb b/app/helpers/auth_helper.rb index 9d9a0a30f..393b3f2cf 100644 --- a/app/helpers/auth_helper.rb +++ b/app/helpers/auth_helper.rb @@ -2,7 +2,7 @@ module AuthHelper def session_path(_scope) - new_person_session_path + new_auth_user_session_path end def admin? diff --git a/app/models/auth_user.rb b/app/models/auth_user.rb index 9477ff4fe..aa1bf6a60 100644 --- a/app/models/auth_user.rb +++ b/app/models/auth_user.rb @@ -4,13 +4,13 @@ class AuthUser < ApplicationRecord devise :omniauthable, omniauth_providers: [:keycloak_openid] - class << self def from_omniauth(auth) - person = where(email: auth.info.email).first_or_create do |user| + person = where(uid: auth.uid).first_or_create do |user| user.name = auth.info.name - + user.email = auth.info.email end + person.last_login = Time.zone.now set_admin(person, auth) end diff --git a/app/models/person.rb b/app/models/person.rb index 32a7a6368..85d09d036 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -92,33 +92,4 @@ def picture_size errors.add(:picture, 'grösse kann maximal 10MB sein') end - - class << self - def from_omniauth(auth) # rubocop:disable Metrics/AbcSize - person = where(email: auth.info.email).first_or_create do |user| - user.name = auth.info.name - user.birthdate = DateTime.new(2020, 1, 1) - user.nationality = 'CH' - user.location = 'Schweiz' - user.title = 'Software Engineer' - user.marital_status = :single - user.company = Company.first - end - set_admin(person, auth) - end - - private - - def set_admin(person, auth) - person.is_admin = admin?(auth) - person.save - person - end - - def admin?(auth) - resources = auth.extra.raw_info.resource_access[AuthConfig.client_id] - resources.roles.include? AuthConfig.admin_role - end - - end end diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 4bb998ffb..fa0ada633 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -28,7 +28,7 @@ %pzsh-icon.scale-icon-08.text-gray(name="question-circle") %li.d-flex.align-items-center.cursor-pointer.border-start.border-end.h-100.ps-2.pe-2 - if auth_user_signed_in? - =link_to "Logout", destroy_person_session_path, data: { "turbo-method": :delete} + =link_to "Logout", destroy_auth_user_session_path, data: { "turbo-method": :delete} - elsif devise_mapping.omniauthable? =button_to "Login", omniauth_authorize_path(resource_name, resource_class.omniauth_providers.first), {data: { "turbo": false}, class: "btn btn-link"} %div diff --git a/db/migrate/20240229093827_add_is_admin_to_people.rb b/db/migrate/20240229093827_add_is_admin_to_people.rb deleted file mode 100644 index 52b75f75e..000000000 --- a/db/migrate/20240229093827_add_is_admin_to_people.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddIsAdminToPeople < ActiveRecord::Migration[7.0] - def change - add_column :people, :is_admin, :boolean, default: false, null: false - end -end diff --git a/db/schema.rb b/db/schema.rb index c77b3e1f3..6cb1c07d4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -147,7 +147,6 @@ t.string "email" t.integer "department_id" t.string "shortname" - t.boolean "is_admin", default: false, null: false t.index ["company_id"], name: "index_people_on_company_id" end diff --git a/db/seeds/support/person_seeder.rb b/db/seeds/support/person_seeder.rb index 353ea92b9..fdf0257bc 100644 --- a/db/seeds/support/person_seeder.rb +++ b/db/seeds/support/person_seeder.rb @@ -18,7 +18,6 @@ def seed_people(names) person.projects.each do |project| seed_project_technology(project.id) end - person.is_admin = false end end @@ -103,7 +102,6 @@ def seed_person(name) p.competence_notes = competence_notes p.email = Faker::Internet.email p.department_id = Department.all.pluck(:id).sample - p.is_admin = false end end