From 259ee494661ecd9ed03122a3db67bbbdbe780449 Mon Sep 17 00:00:00 2001 From: kikito Date: Thu, 1 Oct 2015 14:17:11 +0200 Subject: [PATCH 1/7] Dashboard for management --- app/controllers/management/dashboard_controller.rb | 6 ++++++ config/routes.rb | 1 + 2 files changed, 7 insertions(+) create mode 100644 app/controllers/management/dashboard_controller.rb diff --git a/app/controllers/management/dashboard_controller.rb b/app/controllers/management/dashboard_controller.rb new file mode 100644 index 000000000..abb605341 --- /dev/null +++ b/app/controllers/management/dashboard_controller.rb @@ -0,0 +1,6 @@ +class Management::DashboardController < Management::BaseController + + def index + end + +end diff --git a/config/routes.rb b/config/routes.rb index 2103c0fce..11b69dc4b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -164,6 +164,7 @@ Rails.application.routes.draw do end namespace :management do + root to: "dashboard#index" end From 0be4a966488c7e1b521b54a5987067bf598d4de0 Mon Sep 17 00:00:00 2001 From: kikito Date: Thu, 1 Oct 2015 16:42:16 +0200 Subject: [PATCH 2/7] adds management dashboard index view --- .../management/app/views/management/dashboard/index.html.erb | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 app/views/management/app/views/management/dashboard/index.html.erb diff --git a/app/views/management/app/views/management/dashboard/index.html.erb b/app/views/management/app/views/management/dashboard/index.html.erb new file mode 100644 index 000000000..8fc1b6100 --- /dev/null +++ b/app/views/management/app/views/management/dashboard/index.html.erb @@ -0,0 +1,3 @@ +
+

<%= t("management.dashboard.index.title") %>

+
From 742ea1bd2e8d619fa4b4f3bfb0b7170597d4a354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Thu, 1 Oct 2015 17:53:15 +0200 Subject: [PATCH 3/7] adds management/sessions controller --- .../management/sessions_controller.rb | 17 +++++++++++ app/models/manager.rb | 4 +-- config/routes.rb | 3 ++ .../management/sessions_controller_spec.rb | 28 +++++++++++++++++++ spec/models/manager_spec.rb | 12 ++++---- 5 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 app/controllers/management/sessions_controller.rb create mode 100644 spec/controllers/management/sessions_controller_spec.rb diff --git a/app/controllers/management/sessions_controller.rb b/app/controllers/management/sessions_controller.rb new file mode 100644 index 000000000..47aa3508f --- /dev/null +++ b/app/controllers/management/sessions_controller.rb @@ -0,0 +1,17 @@ +class Management::SessionsController < ActionController::Base + + def create + destroy_session + if manager = Manager.valid_manager(params[:login], params[:clave_usuario]) + session["manager_id"] = manager.id + redirect_to management_root_path + else + raise ActionController::RoutingError.new('Not Found') + end + end + + private + def destroy_session + session["manager_id"] = nil + end +end \ No newline at end of file diff --git a/app/models/manager.rb b/app/models/manager.rb index 1cfc3e545..6dc5cceea 100644 --- a/app/models/manager.rb +++ b/app/models/manager.rb @@ -4,9 +4,9 @@ class Manager < ActiveRecord::Base has_secure_password - def self.valid_auth?(username = nil, password = nil) + def self.valid_manager(username = nil, password = nil) return false unless username.present? && password.present? - Manager.find_by(username: username).try(:authenticate, password).present? + Manager.find_by(username: username).try(:authenticate, password) end end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 11b69dc4b..391bc5415 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -166,6 +166,9 @@ Rails.application.routes.draw do namespace :management do root to: "dashboard#index" + get 'sign_in', to: 'sessions#create' + + resources :sessions, only: :create end # Example of regular route: diff --git a/spec/controllers/management/sessions_controller_spec.rb b/spec/controllers/management/sessions_controller_spec.rb new file mode 100644 index 000000000..e895849a7 --- /dev/null +++ b/spec/controllers/management/sessions_controller_spec.rb @@ -0,0 +1,28 @@ +require 'rails_helper' + +describe Management::SessionsController do + + before(:all) do + create(:manager, username: "supermanager" , password: "secret") + end + + describe 'Sign up' do + it "should return 404 if not username/password" do + expect { get :create }.to raise_error "Not Found" + end + + it "should return 404 if wrong username" do + expect { get :create, login: "nonexistent" , clave_usuario: "secret" }.to raise_error "Not Found" + end + + it "should return 404 if wrong password" do + expect { get :create, login: "supermanager" , clave_usuario: "wrong" }.to raise_error "Not Found" + end + + it "should redirect to management root path if right credentials" do + get :create, login: "supermanager" , clave_usuario: "secret" + expect(response).to be_redirect + end + end + +end \ No newline at end of file diff --git a/spec/models/manager_spec.rb b/spec/models/manager_spec.rb index 8e08e1224..a6a89427c 100644 --- a/spec/models/manager_spec.rb +++ b/spec/models/manager_spec.rb @@ -20,26 +20,26 @@ describe Manager do end end - describe "self.valid_auth?" do + describe "self.valid_manager" do before(:all) { create(:manager, username: "Silvia" ,password: "supersecret") } it "is false when username is blank" do - expect(Manager.valid_auth?(nil, "supersecret")).to be false + expect(Manager.valid_manager(nil, "supersecret")).to be_blank end it "is false when password is blank" do - expect(Manager.valid_auth?("Silvia", nil)).to be false + expect(Manager.valid_manager("Silvia", nil)).to be_blank end it "is false if manager unexistent" do - expect(Manager.valid_auth?("Manager", "supersecret")).to be false + expect(Manager.valid_manager("Manager", "supersecret")).to be_blank end it "is false if wrong password unexistent" do - expect(Manager.valid_auth?("Silvia", "wrong")).to be false + expect(Manager.valid_manager("Silvia", "wrong")).to be_blank end it "is true if right username/password combination" do - expect(Manager.valid_auth?("Silvia", "supersecret")).to be true + expect(Manager.valid_manager("Silvia", "supersecret")).to be_present end end From f77ea6722cecc7939d1fb960132f6f570c0b34ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Thu, 1 Oct 2015 17:53:37 +0200 Subject: [PATCH 4/7] fixes missing templates --- app/views/management/_menu.html.erb | 0 .../{app/views/management => }/dashboard/index.html.erb | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 app/views/management/_menu.html.erb rename app/views/management/{app/views/management => }/dashboard/index.html.erb (100%) diff --git a/app/views/management/_menu.html.erb b/app/views/management/_menu.html.erb new file mode 100644 index 000000000..e69de29bb diff --git a/app/views/management/app/views/management/dashboard/index.html.erb b/app/views/management/dashboard/index.html.erb similarity index 100% rename from app/views/management/app/views/management/dashboard/index.html.erb rename to app/views/management/dashboard/index.html.erb From fe9cd8dc440cb19ca605b0932181093b78e2904c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Thu, 1 Oct 2015 17:59:01 +0200 Subject: [PATCH 5/7] adds i18n file for management --- config/i18n-tasks.yml | 1 + config/locales/management.en.yml | 5 +++++ config/locales/management.es.yml | 5 +++++ 3 files changed, 11 insertions(+) create mode 100644 config/locales/management.en.yml create mode 100644 config/locales/management.es.yml diff --git a/config/i18n-tasks.yml b/config/i18n-tasks.yml index b1fa6d362..da441e691 100644 --- a/config/i18n-tasks.yml +++ b/config/i18n-tasks.yml @@ -24,6 +24,7 @@ data: - config/locales/%{locale}.yml - config/locales/admin.%{locale}.yml - config/locales/moderation.%{locale}.yml + - config/locales/management.%{locale}.yml - config/locales/verification.%{locale}.yml - config/locales/mailers.%{locale}.yml - config/locales/pages.%{locale}.yml diff --git a/config/locales/management.en.yml b/config/locales/management.en.yml new file mode 100644 index 000000000..92e8475a5 --- /dev/null +++ b/config/locales/management.en.yml @@ -0,0 +1,5 @@ +en: + management: + dashboard: + index: + title: Management Dashboard \ No newline at end of file diff --git a/config/locales/management.es.yml b/config/locales/management.es.yml new file mode 100644 index 000000000..de4e58f9f --- /dev/null +++ b/config/locales/management.es.yml @@ -0,0 +1,5 @@ +es: + management: + dashboard: + index: + title: Gestión \ No newline at end of file From 20bf9d718659c1af47b14514cc27d994aeab0d1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Thu, 1 Oct 2015 17:59:36 +0200 Subject: [PATCH 6/7] adds auth before filter to verify manager --- app/controllers/management/base_controller.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/controllers/management/base_controller.rb b/app/controllers/management/base_controller.rb index 2a5aba4e6..2ca542954 100644 --- a/app/controllers/management/base_controller.rb +++ b/app/controllers/management/base_controller.rb @@ -6,6 +6,11 @@ class Management::BaseController < ActionController::Base private def verify_manager + raise ActionController::RoutingError.new('Not Found') unless current_manager.present? + end + + def current_manager + @current_manager ||= Manager.find(session["manager_id"]) if session["manager_id"] end end From 7b3d3243b8f0a819f34d61daa50b52d352fd98a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Tue, 6 Oct 2015 14:32:01 +0200 Subject: [PATCH 7/7] adds management layout --- app/controllers/management/base_controller.rb | 2 +- app/views/layouts/management.html.erb | 64 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 app/views/layouts/management.html.erb diff --git a/app/controllers/management/base_controller.rb b/app/controllers/management/base_controller.rb index 2ca542954..49a9eaf5d 100644 --- a/app/controllers/management/base_controller.rb +++ b/app/controllers/management/base_controller.rb @@ -1,5 +1,5 @@ class Management::BaseController < ActionController::Base - layout 'admin' + layout 'management' before_action :verify_manager diff --git a/app/views/layouts/management.html.erb b/app/views/layouts/management.html.erb new file mode 100644 index 000000000..431ee27c0 --- /dev/null +++ b/app/views/layouts/management.html.erb @@ -0,0 +1,64 @@ + + + + + + + + <%= content_for?(:title) ? yield(:title) : "Admin" %> + <%= stylesheet_link_tag "application" %> + <%= javascript_include_tag "vendor/modernizr" %> + <%= javascript_include_tag "application", 'data-turbolinks-track' => true %> + <%= content_for :head %> + <%= csrf_meta_tags %> + <%= favicon_link_tag "favicon.ico" %> + + + +
+ + +
+ +
+
+
+ +
+ <%= render "/management/menu" %> +
+ +
+ <% if notice %> +
+ ">× + <%= notice %> +
+ <% end %> + + <% if alert %> +
+ ">× + <%= alert %> +
+ <% end %> + + <%= yield %> +
+
+ +