From 36954c595eca6dfd3d12385454a6564e9afab041 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Tue, 23 Feb 2016 16:51:24 +0100 Subject: [PATCH 1/4] Add UI SuperAdmin --- app/controllers/administrations_controller.rb | 39 +++++++++++++++++ app/controllers/root_controller.rb | 3 ++ .../sessions/sessions_controller.rb | 1 + app/mailers/new_admin_mailer.rb | 10 +++++ app/models/administration.rb | 5 +++ app/views/administrations/_list.html.haml | 25 +++++++++++ app/views/administrations/index.html.haml | 19 +++++++++ .../new_admin_mailer/new_admin_email.text.erb | 9 ++++ config/deploy.rb | 1 + config/locales/fr.yml | 38 +++++++++++++++++ config/routes.rb | 4 ++ ...223134354_devise_create_administrations.rb | 42 +++++++++++++++++++ db/schema.rb | 20 ++++++++- db/seeds/super_admin.rb | 1 + 14 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 app/controllers/administrations_controller.rb create mode 100644 app/mailers/new_admin_mailer.rb create mode 100644 app/models/administration.rb create mode 100644 app/views/administrations/_list.html.haml create mode 100644 app/views/administrations/index.html.haml create mode 100644 app/views/new_admin_mailer/new_admin_email.text.erb create mode 100644 db/migrate/20160223134354_devise_create_administrations.rb create mode 100644 db/seeds/super_admin.rb diff --git a/app/controllers/administrations_controller.rb b/app/controllers/administrations_controller.rb new file mode 100644 index 000000000..32b523b28 --- /dev/null +++ b/app/controllers/administrations_controller.rb @@ -0,0 +1,39 @@ +class AdministrationsController < ApplicationController + include SmartListing::Helper::ControllerExtensions + helper SmartListing::Helper + + before_action :authenticate_administration! + + def index + + @admins = smart_listing_create :admins, + Administrateur.all, + partial: "administrations/list", + array: true + + @admin = Administrateur.new + end + + def show + @admin = Administrateur.find(params[:id]) + end + + def create + admin = Administrateur.new create_administrateur_params + + if admin.save + flash.notice = "Administrateur créé" + NewAdminMailer.new_admin_email(admin, params[:administrateur][:password]).deliver_now! + else + flash.alert = admin.errors.full_messages.join('
').html_safe + end + + redirect_to administrations_path + end + + private + + def create_administrateur_params + params.require(:administrateur).permit(:email, :password) + end +end diff --git a/app/controllers/root_controller.rb b/app/controllers/root_controller.rb index 7020767e5..8ea836b49 100644 --- a/app/controllers/root_controller.rb +++ b/app/controllers/root_controller.rb @@ -10,6 +10,9 @@ class RootController < ApplicationController elsif administrateur_signed_in? redirect_to admin_procedures_path + elsif administration_signed_in? + redirect_to administrations_path + else @latest_release = Github::Releases.latest render 'landing' diff --git a/app/controllers/sessions/sessions_controller.rb b/app/controllers/sessions/sessions_controller.rb index e5d5023a8..d288616c3 100644 --- a/app/controllers/sessions/sessions_controller.rb +++ b/app/controllers/sessions/sessions_controller.rb @@ -6,5 +6,6 @@ class Sessions::SessionsController < Devise::SessionsController sign_out :user if user_signed_in? sign_out :gestionnaire if gestionnaire_signed_in? sign_out :administrateur if administrateur_signed_in? + sign_out :administration if administration_signed_in? end end diff --git a/app/mailers/new_admin_mailer.rb b/app/mailers/new_admin_mailer.rb new file mode 100644 index 000000000..22c0428fb --- /dev/null +++ b/app/mailers/new_admin_mailer.rb @@ -0,0 +1,10 @@ +class NewAdminMailer < ApplicationMailer + def new_admin_email admin, password + + @admin = admin + @password = password + + mail(from: "tech@apientreprise.fr", to: 'tech@apientreprise.fr', + subject: "Création d'un compte Admin TPS") + end +end diff --git a/app/models/administration.rb b/app/models/administration.rb new file mode 100644 index 000000000..bab810519 --- /dev/null +++ b/app/models/administration.rb @@ -0,0 +1,5 @@ +class Administration < ActiveRecord::Base + # Include default devise modules. Others available are: + # :confirmable, :lockable, :timeoutable and :omniauthable + devise :database_authenticatable, :rememberable, :trackable, :validatable +end diff --git a/app/views/administrations/_list.html.haml b/app/views/administrations/_list.html.haml new file mode 100644 index 000000000..a08ac983f --- /dev/null +++ b/app/views/administrations/_list.html.haml @@ -0,0 +1,25 @@ +- unless smart_listing.empty? + %table.table + %thead + %th.col-md-4.col-lg-4= smart_listing.sortable 'Email', :email + %th.col-md-4.col-lg-4= smart_listing.sortable 'API Token', :api_token + %th.col-md-4.col-lg-4= smart_listing.sortable 'Date de dernière connexion', :last_sign_in_at + + - @admins.each do |admin| + %tr + %td + = admin.email + %td + = admin.api_token + %td + - unless admin.last_sign_in_at.nil? + = time_ago_in_words(l(admin.last_sign_in_at, format: "%d/%m/%Y %H:%M UTC +02:00")) + ( + = admin.last_sign_in_at.to_date.strftime('%d/%m/%Y') + ) + + = smart_listing.paginate + = smart_listing.pagination_per_page_links +- else + %h4.center + Aucun administrateur créé diff --git a/app/views/administrations/index.html.haml b/app/views/administrations/index.html.haml new file mode 100644 index 000000000..b0f2e40b7 --- /dev/null +++ b/app/views/administrations/index.html.haml @@ -0,0 +1,19 @@ +%br +%br + += form_for @admin, url: {controller: 'administrations', action: :create} do |f| + .form-group.form-inline.center + = f.text_field :email, placeholder: :email, class: 'form-control' + = f.text_field :password, placeholder: :password, class: 'form-control' + + = f.submit 'Valider', class: 'btn btn-success', id: 'submit_new_administrateur' + +%br + += smart_listing_render :admins + +%br +%br + +.center + =link_to 'Deconnexion', '/administrations/sign_out', method: :delete \ No newline at end of file diff --git a/app/views/new_admin_mailer/new_admin_email.text.erb b/app/views/new_admin_mailer/new_admin_email.text.erb new file mode 100644 index 000000000..8b4d474df --- /dev/null +++ b/app/views/new_admin_mailer/new_admin_email.text.erb @@ -0,0 +1,9 @@ +Un nouvel administrateur a été créé sur TPS. + +Plateforme : <%= TPS::Application::URL %> + +Login : <%= @admin.email %> +Password : <%= @password %> + +--- +L'équipe TPS - tps@apientreprise.fr \ No newline at end of file diff --git a/config/deploy.rb b/config/deploy.rb index 486a752e9..6379bc505 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -127,6 +127,7 @@ task :deploy => :environment do invoke :'deploy:link_shared_paths' invoke :'bundle:install' invoke :'rails:db_migrate' + invoke :'rails:db_seed' invoke :'rails:assets_precompile' to :launch do diff --git a/config/locales/fr.yml b/config/locales/fr.yml index f38143913..9054844ae 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -140,3 +140,41 @@ fr: connexion: "Erreur lors de la connexion à France Connect." extension_white_list_error: "Le format de fichier de la pièce jointe n'est pas valide." + datetime: + distance_in_words: + about_x_hours: + one: environ une heure + other: environ %{count} heures + about_x_months: + one: environ un mois + other: environ %{count} mois + about_x_years: + one: environ un an + other: environ %{count} ans + almost_x_years: + one: presqu'un an + other: presque %{count} ans + half_a_minute: une demi-minute + less_than_x_minutes: + zero: moins d'une minute + one: moins d'une minute + other: moins de %{count} minutes + less_than_x_seconds: + zero: moins d'une seconde + one: moins d'une seconde + other: moins de %{count} secondes + over_x_years: + one: plus d'un an + other: plus de %{count} ans + x_days: + one: 1 jour + other: "%{count} jours" + x_minutes: + one: 1 minute + other: "%{count} minutes" + x_months: + one: 1 mois + other: "%{count} mois" + x_seconds: + one: 1 seconde + other: "%{count} secondes" \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index db5e5f748..47bb7f06b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,7 @@ Rails.application.routes.draw do + devise_for :administrations, skip: [:password, :registrations] + devise_for :administrateurs, controllers: { sessions: 'administrateurs/sessions' }, skip: [:password, :registrations] @@ -106,6 +108,8 @@ Rails.application.routes.draw do resources :commentaires, only: [:create] end + resources :administrations + namespace :api do namespace :v1 do resources :procedures, only: [:index, :show] do diff --git a/db/migrate/20160223134354_devise_create_administrations.rb b/db/migrate/20160223134354_devise_create_administrations.rb new file mode 100644 index 000000000..4d139ee28 --- /dev/null +++ b/db/migrate/20160223134354_devise_create_administrations.rb @@ -0,0 +1,42 @@ +class DeviseCreateAdministrations < ActiveRecord::Migration + def change + create_table(:administrations) 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 :administrations, :email, unique: true + add_index :administrations, :reset_password_token, unique: true + # add_index :administrations, :confirmation_token, unique: true + # add_index :administrations, :unlock_token, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index b5d316ccf..3547f6476 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160204155519) do +ActiveRecord::Schema.define(version: 20160223134354) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -35,6 +35,24 @@ ActiveRecord::Schema.define(version: 20160204155519) do 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 "administrations", 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 "administrations", ["email"], name: "index_administrations_on_email", unique: true, using: :btree + add_index "administrations", ["reset_password_token"], name: "index_administrations_on_reset_password_token", unique: true, using: :btree + create_table "cadastres", force: :cascade do |t| t.string "surface_intersection" t.float "surface_parcelle" diff --git a/db/seeds/super_admin.rb b/db/seeds/super_admin.rb new file mode 100644 index 000000000..309eb8252 --- /dev/null +++ b/db/seeds/super_admin.rb @@ -0,0 +1 @@ +Administration.create(email: SUPERADMIN.email, password: SUPERADMIN.password) \ No newline at end of file From 774ad95cc695c5703ff0e4648a640834d5f9a958 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Wed, 24 Feb 2016 10:51:23 +0100 Subject: [PATCH 2/4] Add shared link for super_admin initializers --- config/deploy.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/deploy.rb b/config/deploy.rb index 6379bc505..aa81b70cf 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -66,6 +66,7 @@ set :shared_paths, [ 'config/initializers/secret_token.rb', "config/environments/#{ENV['to']}.rb", "config/initializers/token.rb", + "config/initializers/super_admin.rb", "config/unicorn.rb", "config/initializers/raven.rb", 'config/france_connect.yml', From e1d1e356e56573ecb139f51f7f53ecff5fec8d10 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Wed, 24 Feb 2016 11:12:18 +0100 Subject: [PATCH 3/4] fix deploy rake process --- config/deploy.rb | 57 +++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/config/deploy.rb b/config/deploy.rb index aa81b70cf..8f377a78a 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -1,7 +1,7 @@ require 'mina/bundler' require 'mina/rails' require 'mina/git' -require 'mina/rbenv' # for rbenv support. (http://rbenv.org) +require 'mina/rbenv' # for rbenv support. (http://rbenv.org) # require 'mina/rvm' # for rvm support. (http://rvm.io) # Basic settings: @@ -20,7 +20,7 @@ print "Deploy to #{ENV['to']} environment branch #{branch}\n" # set :domain, '5.135.190.60' set :domain, ENV['domain'] -set :repository,'https://github.com/sgmap/tps.git' +set :repository, 'https://github.com/sgmap/tps.git' set :port, 2200 set :deploy_to, '/var/www/tps_dev' @@ -33,7 +33,7 @@ if ENV["to"] == "staging" set :branch, ENV['branch'] end set :deploy_to, '/var/www/tps_dev' - set :user, 'tps_dev' # Username in the server to SSH to. + set :user, 'tps_dev' # Username in the server to SSH to. appname = 'tps_dev' elsif ENV["to"] == "production" if ENV['branch'].nil? @@ -42,7 +42,7 @@ elsif ENV["to"] == "production" set :branch, ENV['branch'] end set :deploy_to, '/var/www/tps' - set :user, 'tps' # Username in the server to SSH to. + set :user, 'tps' # Username in the server to SSH to. appname = 'tps' end @@ -54,24 +54,24 @@ set :rails_env, ENV["to"] # Manually create these paths in shared/ (eg: shared/config/database.yml) in your server. # They will be linked in the 'deploy:link_shared_paths' step. set :shared_paths, [ - 'log', - 'bin', - 'uploads', - 'tmp/pids', - 'tmp/cache', - 'tmp/sockets', - 'public/system', - 'public/uploads', - 'config/database.yml', - 'config/initializers/secret_token.rb', - "config/environments/#{ENV['to']}.rb", - "config/initializers/token.rb", - "config/initializers/super_admin.rb", - "config/unicorn.rb", - "config/initializers/raven.rb", - 'config/france_connect.yml', - 'config/initializers/mailjet.rb' -] + 'log', + 'bin', + 'uploads', + 'tmp/pids', + 'tmp/cache', + 'tmp/sockets', + 'public/system', + 'public/uploads', + 'config/database.yml', + 'config/initializers/secret_token.rb', + "config/environments/#{ENV['to']}.rb", + "config/initializers/token.rb", + "config/initializers/super_admin.rb", + "config/unicorn.rb", + "config/initializers/raven.rb", + 'config/france_connect.yml', + 'config/initializers/mailjet.rb' + ] set :rbenv_path, "/usr/local/rbenv/bin/rbenv" @@ -79,7 +79,7 @@ set :rbenv_path, "/usr/local/rbenv/bin/rbenv" # Optional settings: # set :user, 'foobar' # Username in the server to SSH to. # set :port, '30000' # SSH port number. - set :forward_agent, true # SSH forward_agent. +set :forward_agent, true # SSH forward_agent. # This task is the environment that is loaded for most commands, such as # `mina deploy` or `mina rake`. @@ -109,13 +109,13 @@ task :setup => :environment do queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/config"] queue! %[touch "#{deploy_to}/shared/config/database.yml"] - queue %[echo "-----> Be sure to edit 'shared/config/database.yml'."] + queue %[echo "-----> Be sure to edit 'shared/config/database.yml'."] queue! %[touch "#{deploy_to}/shared/environments/production.rb"] - queue %[echo "-----> Be sure to edit 'shared/environments/production.rb'."] + queue %[echo "-----> Be sure to edit 'shared/environments/production.rb'."] queue! %[touch "#{deploy_to}/shared/environments/staging.rb"] - queue %[echo "-----> Be sure to edit 'shared/environments/staging.rb'."] + queue %[echo "-----> Be sure to edit 'shared/environments/staging.rb'."] end desc "Deploys the current version to the server." @@ -128,11 +128,14 @@ task :deploy => :environment do invoke :'deploy:link_shared_paths' invoke :'bundle:install' invoke :'rails:db_migrate' - invoke :'rails:db_seed' invoke :'rails:assets_precompile' to :launch do queue "/etc/init.d/#{user} upgrade " + + queue "cd #{deploy_to}/#{current_path}/" + queue "bundle exec rake db:seed RAILS_ENV=#{rails_env}" + queue %[echo "-----> Rake Seeding Completed."] end end end From a316165792c2b8bbba601bf22700feb6f8fbf93b Mon Sep 17 00:00:00 2001 From: Xavier J Date: Thu, 25 Feb 2016 11:50:09 +0100 Subject: [PATCH 4/4] !!FIX BUG : values does not appear on info dossier --- app/decorators/champ_decorator.rb | 1 + spec/views/dossiers/_infos_dossier_spec.rb | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/app/decorators/champ_decorator.rb b/app/decorators/champ_decorator.rb index be8fcb54d..b6613426b 100644 --- a/app/decorators/champ_decorator.rb +++ b/app/decorators/champ_decorator.rb @@ -4,6 +4,7 @@ class ChampDecorator < Draper::Decorator if type_champ == 'checkbox' return object.value == 'on' ? 'Oui' : 'Non' end + object.value end def type_champ diff --git a/spec/views/dossiers/_infos_dossier_spec.rb b/spec/views/dossiers/_infos_dossier_spec.rb index 5e934ce9c..cb77000e1 100644 --- a/spec/views/dossiers/_infos_dossier_spec.rb +++ b/spec/views/dossiers/_infos_dossier_spec.rb @@ -4,6 +4,11 @@ describe 'dossiers/_infos_dossier.html.haml', type: :view do let(:dossier) { create(:dossier, :with_entreprise, procedure: create(:procedure, :with_api_carto, :with_type_de_champ)) } before do + champs.each do |champ| + champ.value = ((0...8).map { (65 + rand(26)).chr }.join) + champ.save + end + assign(:facade, DossierFacades.new(dossier.id, dossier.user.email)) render end