diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb new file mode 100644 index 000000000..9061f7063 --- /dev/null +++ b/app/controllers/admin_controller.rb @@ -0,0 +1,6 @@ +class AdminController < ApplicationController + + def index + redirect_to(controller: '/administrateurs/sessions', action: :new) unless administrateur_signed_in? + end +end diff --git a/app/controllers/administrateurs/sessions_controller.rb b/app/controllers/administrateurs/sessions_controller.rb new file mode 100644 index 000000000..b1fe69832 --- /dev/null +++ b/app/controllers/administrateurs/sessions_controller.rb @@ -0,0 +1,15 @@ +class Administrateurs::SessionsController < Devise::SessionsController + + def new + @administrateur = Administrateur.new + end + + def create + super + end + + def after_sign_in_path_for(resource) + # stored_location_for(resource) || + admin_path + end +end diff --git a/app/models/administrateur.rb b/app/models/administrateur.rb new file mode 100644 index 000000000..4166b6099 --- /dev/null +++ b/app/models/administrateur.rb @@ -0,0 +1,6 @@ +class Administrateur < ActiveRecord::Base + # Include default devise modules. Others available are: + # :confirmable, :lockable, :timeoutable and :omniauthable + devise :database_authenticatable, :registerable, + :recoverable, :rememberable, :trackable, :validatable +end diff --git a/app/views/admin/index.html.haml b/app/views/admin/index.html.haml new file mode 100644 index 000000000..928564dbc --- /dev/null +++ b/app/views/admin/index.html.haml @@ -0,0 +1 @@ +%p Coucou \ No newline at end of file diff --git a/app/views/administrateurs/sessions/new.html.haml b/app/views/administrateurs/sessions/new.html.haml new file mode 100644 index 000000000..646e33fe4 --- /dev/null +++ b/app/views/administrateurs/sessions/new.html.haml @@ -0,0 +1,10 @@ +%h1 Bienvenue sur TPS - Administration +%br +#gestionnaire_login + = form_for @administrateur, url: {controller: 'administrateurs/sessions', action: :create } do |f| + .form-group-lg + .form-group + = f.text_field :email, class: 'form-control', placeholder: 'Email' + .form-group + = f.password_field :password, class: 'form-control', placeholder: 'Mot de passe' + = f.submit 'Se connecter', class: %w(btn btn-lg btn-success), data: { disable_with: 'Connexion', submit: true } \ No newline at end of file diff --git a/app/views/gestionnaires/sessions/new.html.haml b/app/views/gestionnaires/sessions/new.html.haml index 553d80524..b32ac2c06 100644 --- a/app/views/gestionnaires/sessions/new.html.haml +++ b/app/views/gestionnaires/sessions/new.html.haml @@ -1,4 +1,4 @@ -%h1 Bienvenue sur TPS +%h1 Bienvenue sur TPS - Gestionnaire %br #gestionnaire_login = form_for @gestionnaire, url: {controller: 'gestionnaires/sessions', action: :create } do |f| diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 12c6b712e..4537abe85 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -17,6 +17,11 @@ %div{style: 'decorate:none; box-shadow:none; float:right; margin-top:8px'} = current_gestionnaire.email = link_to "Déconnexion", '/gestionnaires/sign_out', method: :delete, :class => 'btn btn-md' + -elsif administrateur_signed_in? + %div{style: 'decorate:none; box-shadow:none; float:right; margin-top:8px'} + = current_administrateur.email + = link_to "Déconnexion", '/administrateurs/sign_out', method: :delete, :class => 'btn btn-md' + - elsif user_signed_in? %div.user{style: 'decorate:none; box-shadow:none; float:right; margin-top:8px'} -if current_user.loged_in_with_france_connect diff --git a/config/routes.rb b/config/routes.rb index 9b767f907..6c2540a50 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,13 +1,17 @@ Rails.application.routes.draw do - devise_for :users, controllers: { - sessions: 'users/sessions' - } + devise_for :administrateurs, controllers: { + sessions: 'administrateurs/sessions' + }, skip: [:password, :registrations] devise_for :gestionnaires, controllers: { sessions: 'gestionnaires/sessions' }, skip: [:password, :registrations] + devise_for :users, controllers: { + sessions: 'users/sessions' + } + root 'users/dossiers#index' get 'france_connect' => 'france_connect#login' @@ -35,23 +39,11 @@ Rails.application.routes.draw do resource :dossiers end + get 'admin' => 'admin#index' - # resources :dossiers do - - - # # get '/carte/position' => 'carte#get_position' - # # get '/carte' => 'carte#show' - # # post '/carte' => 'carte#save_ref_api_carto' - - # # get '/description' => 'description#show' - # # get '/description/error' => 'description#error' - # # post 'description' => 'description#create' - - - # post '/commentaire' => 'commentaires#create' - - # end - + namespace :admin do + get 'sign_in' => '/administrateurs/sessions#new' + end get 'backoffice' => 'backoffice#index' @@ -63,6 +55,7 @@ Rails.application.routes.draw do end resources :commentaires, only: [:create] end + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/db/migrate/20151023132121_devise_create_administrateurs.rb b/db/migrate/20151023132121_devise_create_administrateurs.rb new file mode 100644 index 000000000..35979387e --- /dev/null +++ b/db/migrate/20151023132121_devise_create_administrateurs.rb @@ -0,0 +1,42 @@ +class DeviseCreateAdministrateurs < ActiveRecord::Migration + def change + create_table(:administrateurs) do |t| + ## Database authenticatable + t.string :email, null: false, default: "" + t.string :encrypted_password, null: false, default: "" + + ## Recoverable + t.string :reset_password_token + t.datetime :reset_password_sent_at + + ## Rememberable + t.datetime :remember_created_at + + ## Trackable + t.integer :sign_in_count, default: 0, null: false + t.datetime :current_sign_in_at + t.datetime :last_sign_in_at + t.inet :current_sign_in_ip + t.inet :last_sign_in_ip + + ## Confirmable + # t.string :confirmation_token + # t.datetime :confirmed_at + # t.datetime :confirmation_sent_at + # t.string :unconfirmed_email # Only if using reconfirmable + + ## Lockable + # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts + # t.string :unlock_token # Only if unlock strategy is :email or :both + # t.datetime :locked_at + + + t.timestamps + end + + add_index :administrateurs, :email, unique: true + add_index :administrateurs, :reset_password_token, unique: true + # add_index :administrateurs, :confirmation_token, unique: true + # add_index :administrateurs, :unlock_token, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index e34568214..238f3a38d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,11 +11,29 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20151008090835) do +ActiveRecord::Schema.define(version: 20151023132121) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "administrateurs", force: :cascade do |t| + t.string "email", default: "", null: false + t.string "encrypted_password", default: "", null: false + t.string "reset_password_token" + t.datetime "reset_password_sent_at" + t.datetime "remember_created_at" + t.integer "sign_in_count", default: 0, null: false + t.datetime "current_sign_in_at" + t.datetime "last_sign_in_at" + t.inet "current_sign_in_ip" + t.inet "last_sign_in_ip" + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "administrateurs", ["email"], name: "index_administrateurs_on_email", unique: true, using: :btree + add_index "administrateurs", ["reset_password_token"], name: "index_administrateurs_on_reset_password_token", unique: true, using: :btree + create_table "cerfas", force: :cascade do |t| t.string "content" t.integer "dossier_id" diff --git a/spec/models/administrateur_spec.rb b/spec/models/administrateur_spec.rb new file mode 100644 index 000000000..fa8a5d093 --- /dev/null +++ b/spec/models/administrateur_spec.rb @@ -0,0 +1,18 @@ +require 'rails_helper' + +RSpec.describe Administrateur, type: :model do + describe 'database column' do + it { is_expected.to have_db_column(:email) } + it { is_expected.to have_db_column(:encrypted_password) } + it { is_expected.to have_db_column(:reset_password_token) } + it { is_expected.to have_db_column(:reset_password_sent_at) } + it { is_expected.to have_db_column(:remember_created_at) } + it { is_expected.to have_db_column(:sign_in_count) } + it { is_expected.to have_db_column(:current_sign_in_at) } + it { is_expected.to have_db_column(:last_sign_in_at) } + it { is_expected.to have_db_column(:current_sign_in_ip) } + it { is_expected.to have_db_column(:last_sign_in_ip) } + it { is_expected.to have_db_column(:created_at) } + it { is_expected.to have_db_column(:updated_at) } + end +end