Skip to content

Commit

Permalink
Gestion de la sélection d'une académie
Browse files Browse the repository at this point in the history
  • Loading branch information
tnicolas1 committed Feb 14, 2025
1 parent 2594ae5 commit 3aae3ab
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 9 deletions.
11 changes: 11 additions & 0 deletions app/controllers/academic/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Academic
class UsersController < ApplicationController
before_action :infer_page_title

def select_academy
@academic_user = current_user
end
end
end
9 changes: 6 additions & 3 deletions app/controllers/concerns/developer_oidc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ def provider_info(attrs)
end

def extra_info(attrs)
uai = attrs["info"]["uai"]

info = role(attrs) == :dir ? responsibility_hash(attrs, uai) : authorised_hash(attrs, uai)
if attrs["info"]["uai"].nil?
info = { AplyproAcademieResp: attrs["info"]["academy_code"] }
else
uai = attrs["info"]["uai"]
info = role(attrs) == :dir ? responsibility_hash(attrs, uai) : authorised_hash(attrs, uai)
end

{
extra: {
Expand Down
14 changes: 11 additions & 3 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,27 @@ def oidc
choose_redirect_page!
end

def academic
def academic # rubocop:disable Metrics/AbcSize
parse_identity

@academic_login = true
@academic_user = Academic::User.from_oidc(auth_hash).tap(&:save!)

add_auth_breadcrumb(data: { user_id: @academic_user.id }, message: "Successfully parsed academic user")

raise IdentityMappers::Errors::NoLimitedAccessError if @mapper.attributes["AplyproAcademieResp"].nil?
@academies = @mapper.aplypro_academies

raise IdentityMappers::Errors::NoLimitedAccessError if @academies.empty?

sign_in(:academic_user, @academic_user)

redirect_to academic_home_path
if @academies.many?
redirect_to academic_user_select_path(@academic_user)
else
@academic_user.update!(selected_academy: @academies.first)

redirect_to academic_home_path, notice: t("auth.success")
end
end

def asp
Expand Down
6 changes: 5 additions & 1 deletion app/models/academic/user.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Academic
class User < ApplicationRecord
class User < User
devise :authenticatable

validates :uid, :provider, :name, :email, presence: true
Expand All @@ -21,6 +21,10 @@ def from_oidc(attrs)
end
end

def academies
establishments.distinct.pluck(:academy_code)
end

def to_s
name
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/concerns/identity_mappers/fim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ def responsibility_uais
def aplypro_responsibilities
Array(attributes["AplyproResp"]).compact
end

def aplypro_academies
Array(attributes["AplyproAcademieResp"]).compact
end
end
end
12 changes: 12 additions & 0 deletions app/views/academic/users/select_academy.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.fr-grid-row
.fr-col-md-7
%p
Veuillez sélectionner l'académie que vous désirez piloter dans la liste ci-dessous.

.fr-select-group.fr-col-md-7
= form_with url: academic_user_select_academy_path, builder: DsfrFormBuilder do |form|
.fr-input-group
= form.label :selected_academy, "Académie", class: 'fr-label'
= form.select :selected_academy, @academic_user.academies, {}, { class: 'fr-select' }

= form.submit "Continuez avec cette académie", class: 'fr-btn'
2 changes: 1 addition & 1 deletion config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
provider :developer,
name: :academic_developer,
path_prefix: "/auth",
fields: %i[uai email]
fields: %i[academy_code email]
end

provider :openid_connect, {
Expand Down
2 changes: 2 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ fr:
application:
login: Connexion à APLyPro
home : Accueil
users:
select_academy: Choix de l'académie
stats:
index: Statistiques
school_years:
Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
namespace :academic do
get "home", to: "application#home"

resources :users, only: [] do
get "select_academy"
end

devise_for :users, skip: :all, class_name: "Academic::User"
end

Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20250214124041_add_selected_academy_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddSelectedAcademyToUsers < ActiveRecord::Migration[8.0]
def change
add_column :users, :selected_academy, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3aae3ab

Please sign in to comment.