Browse Source

Add login/logout access to config

master
Norore 3 years ago
parent
commit
a7b5517959
  1. 3
      app/assets/javascripts/access.coffee
  2. 3
      app/assets/stylesheets/access.scss
  3. 11
      app/controllers/application_controller.rb
  4. 1
      app/controllers/groups_controller.rb
  5. 19
      app/controllers/site_controller.rb
  6. 1
      app/controllers/sites_controller.rb
  7. 1
      app/controllers/templates_controller.rb
  8. 2
      app/helpers/access_helper.rb
  9. 21
      app/views/config/index.html.erb
  10. 18
      app/views/site/_form.html.erb
  11. 5
      app/views/site/login.html.erb
  12. 5
      config/routes.rb
  13. 5
      spec/controllers/access_controller_spec.rb
  14. 15
      spec/helpers/access_helper_spec.rb

3
app/assets/javascripts/access.coffee

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

3
app/assets/stylesheets/access.scss

@ -0,0 +1,3 @@
// Place all the styles related to the access controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

11
app/controllers/application_controller.rb

@ -1,3 +1,14 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def authenticated?
session[:authenticated] == true
end
def must_be_authenticated
unless authenticated?
session[:redirect_to] = request.path
redirect_to login_path
end
end
end

1
app/controllers/groups_controller.rb

@ -1,5 +1,6 @@
class GroupsController < ApplicationController
before_action :set_group, only: %i[edit update destroy]
before_action :must_be_authenticated, only: %i[new create edit update destroy]
def new
@group = Group.new

19
app/controllers/site_controller.rb

@ -0,0 +1,19 @@
class SiteController < ApplicationController
def login
end
def auth
if params[:username] == ENV["username"] && params[:password] == ENV["password"]
session[:authenticated] = true
redirect_to session[:redirect_to] || diffs_path
else
render :login
end
end
def logout
session[:authenticated] = false
redirect_to :login
end
end

1
app/controllers/sites_controller.rb

@ -1,5 +1,6 @@
class SitesController < ApplicationController
before_action :set_site, only: %i[show edit update destroy]
before_action :must_be_authenticated, only: %i[new create edit update destroy]
def index
@sites = Site.all.includes(:group).order(:group_id, :url)

1
app/controllers/templates_controller.rb

@ -1,5 +1,6 @@
class TemplatesController < ApplicationController
before_action :set_template, only: %i[edit update destroy]
before_action :must_be_authenticated, only: %i[new create edit update destroy]
def new
@template = ::Template.new

2
app/helpers/access_helper.rb

@ -0,0 +1,2 @@
module AccessHelper
end

21
app/views/config/index.html.erb

@ -12,8 +12,11 @@
<ul class="unstyled">
<% @groups.each do |group| %>
<li><%= link_to (group.name||group.id), edit_group_path(group) %> |
<%= link_to :remove, group, method: :delete, data: { confirm: "Are you sure you want to remove this group ("+(group.name||group.id)+") and all related data? This cannot be revert!" } %>
<li><%= link_to (group.name||group.id), edit_group_path(group) %>
<% if session[:authenticated] %>
|
<%= link_to :remove, group, method: :delete, data: { confirm: "Are you sure you want to remove this group ("+(group.name||group.id)+") and all related data? This cannot be revert!" } %>
<% end %>
</li>
<% if group.targets %>
<ul>
@ -64,8 +67,11 @@
<ul class="unstyled">
<% @templates.each do |template| %>
<li>
<%= link_to (template.name||template.id), edit_template_path(template) %> |
<%= link_to :remove, template, method: :delete, data: { confirm: "Are you sure you want to remove this template ("+(template.name||template.id)+") and all related data? This cannot be revert!" } %>
<%= link_to (template.name||template.id), edit_template_path(template) %>
<% if session[:authenticated] %>
|
<%= link_to :remove, template, method: :delete, data: { confirm: "Are you sure you want to remove this template ("+(template.name||template.id)+") and all related data? This cannot be revert!" } %>
<% end %>
</li>
<% unless template.targets.empty? %>
<ul>
@ -99,8 +105,11 @@
<% @sites.each do |site| %>
<li>
<%= link_to (site.name||site.url), edit_site_path(site) %> |
<%= link_to :show, site %> |
<%= link_to :remove, site, method: :delete, data: { confirm: "Are you sure you want to remove this site ("+(site.name||site.id)+") and all related data? This cannot be revert!" } %>
<%= link_to :show, site %>
<% if session[:authenticated] %>
|
<%= link_to :remove, site, method: :delete, data: { confirm: "Are you sure you want to remove this site ("+(site.name||site.id)+") and all related data? This cannot be revert!" } %>
<% end %>
</li>
<% unless site.targets.empty? %>
<ul>

18
app/views/site/_form.html.erb

@ -0,0 +1,18 @@
<%= form_with(local: true, class: "mts") do |form| %>
<fieldset class="mbs pas block">
<legend class="h4-like"><%= :login %></legend>
<div class="auto-grid has-gutter mbs">
<%= form.label :username, :username, class: 'txtright' %>:
<%= form.text_field :username %>
<%= form.label :password, :password, class: 'txtright' %>:
<%= form.password_field :password %>
</div>
</fieldset>
<div class="actions">
<%= form.submit :submit %>
</div>
<% end %>

5
app/views/site/login.html.erb

@ -0,0 +1,5 @@
<h1 class="txtcenter"><%= :login %></h1>
<%= render 'form' %>
<%= link_to :back, diffs_path %>

5
config/routes.rb

@ -4,4 +4,9 @@ Rails.application.routes.draw do
resources :config, only: %i[index]
resources :groups, only: %i[new create edit update destroy]
resources :templates, only: %i[new create edit update destroy]
# resources :access, only: %i[new create]
get '/login', to: 'site#login'
post '/login', to: 'site#auth'
get '/logout', to: 'site#logout'
end

5
spec/controllers/access_controller_spec.rb

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe AccessController, type: :controller do
end

15
spec/helpers/access_helper_spec.rb

@ -0,0 +1,15 @@
require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the AccessHelper. For example:
#
# describe AccessHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe AccessHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
Loading…
Cancel
Save