From 77e2ff61ba4a0dcee6d8404c35514b8caa976464 Mon Sep 17 00:00:00 2001 From: DmitryIlin <339966289@mail.ru> Date: Wed, 27 Jun 2018 10:00:25 +0300 Subject: [PATCH] admin controller, #326, without tests --- .../{ => admin}/canonical_items_controller.rb | 16 +-- .../organizations_controller.rb} | 24 ++--- app/controllers/admin/users_controller.rb | 40 +++++++ app/controllers/admin_controller.rb | 9 ++ app/controllers/application_controller.rb | 2 +- app/controllers/organizations_controller.rb | 20 +--- app/controllers/users_controller.rb | 34 ------ app/models/user.rb | 2 +- .../_canonical_item_row.html.erb | 4 +- .../canonical_items/_dropdown.html.erb | 0 .../canonical_items/_form.html.erb | 2 +- .../{ => admin}/canonical_items/edit.html.erb | 2 +- .../canonical_items/index.html.erb | 1 + .../{ => admin}/canonical_items/new.html.erb | 2 +- .../{ => admin}/canonical_items/show.html.erb | 2 +- .../organizations/_organization_row.html.erb | 12 +++ .../organizations}/edit.html.erb | 4 +- app/views/admin/organizations/index.html.erb | 50 +++++++++ .../organizations}/new.html.erb | 4 +- .../organizations}/show.html.erb | 4 +- app/views/admin/users/index.html.erb | 69 ++++++++++++ app/views/admin/users/new.html.erb | 43 ++++++++ app/views/admins/_organization_row.html.erb | 13 --- app/views/admins/index.html.erb | 47 -------- app/views/layouts/_admin_navbar.html.erb | 71 ------------ app/views/layouts/_lte_navbar.html.erb | 36 ++++--- app/views/layouts/_lte_sidebar.html.erb | 61 ++++------- app/views/layouts/_navbar.html.erb | 101 ------------------ app/views/users/index.html.erb | 13 +-- 29 files changed, 299 insertions(+), 389 deletions(-) rename app/controllers/{ => admin}/canonical_items_controller.rb (67%) rename app/controllers/{admins_controller.rb => admin/organizations_controller.rb} (59%) create mode 100644 app/controllers/admin/users_controller.rb create mode 100644 app/controllers/admin_controller.rb rename app/views/{ => admin}/canonical_items/_canonical_item_row.html.erb (58%) rename app/views/{ => admin}/canonical_items/_dropdown.html.erb (100%) rename app/views/{ => admin}/canonical_items/_form.html.erb (84%) rename app/views/{ => admin}/canonical_items/edit.html.erb (90%) rename app/views/{ => admin}/canonical_items/index.html.erb (99%) rename app/views/{ => admin}/canonical_items/new.html.erb (89%) rename app/views/{ => admin}/canonical_items/show.html.erb (95%) create mode 100644 app/views/admin/organizations/_organization_row.html.erb rename app/views/{admins => admin/organizations}/edit.html.erb (92%) create mode 100644 app/views/admin/organizations/index.html.erb rename app/views/{admins => admin/organizations}/new.html.erb (88%) rename app/views/{admins => admin/organizations}/show.html.erb (94%) create mode 100644 app/views/admin/users/index.html.erb create mode 100644 app/views/admin/users/new.html.erb delete mode 100644 app/views/admins/_organization_row.html.erb delete mode 100644 app/views/admins/index.html.erb delete mode 100644 app/views/layouts/_admin_navbar.html.erb delete mode 100644 app/views/layouts/_navbar.html.erb diff --git a/app/controllers/canonical_items_controller.rb b/app/controllers/admin/canonical_items_controller.rb similarity index 67% rename from app/controllers/canonical_items_controller.rb rename to app/controllers/admin/canonical_items_controller.rb index e057a2b6e3..d0d5e64d3b 100644 --- a/app/controllers/canonical_items_controller.rb +++ b/app/controllers/admin/canonical_items_controller.rb @@ -1,6 +1,4 @@ -class CanonicalItemsController < ApplicationController - before_action :authorize_user - +class Admin::CanonicalItemsController < AdminController def edit @canonical_item = CanonicalItem.find(params[:id]) end @@ -8,7 +6,7 @@ def edit def update @canonical_item = CanonicalItem.find(params[:id]) if @canonical_item.update(canonical_item_params) - redirect_to canonical_items_path, notice: "Updated canonical item!" + redirect_to admin_canonical_items_path, notice: "Updated canonical item!" else flash[:error] = "Failed to update this canonical item." render :edit @@ -26,7 +24,7 @@ def new def create @canonical_item = CanonicalItem.create(canonical_item_params) if @canonical_item.save - redirect_to canonical_items_path, notice: "Canonical Item added!" + redirect_to admin_canonical_items_path, notice: "Canonical Item added!" else flash[:error] = "Failed to create Canonical Item." render :new @@ -41,18 +39,14 @@ def show def destroy @canonical_item = CanonicalItem.includes(:items).find(params[:id]) if !@canonical_item.items.empty? && @canonical_item.destroy - redirect_to canonical_items_path, notice: "Canonical Item deleted!" + redirect_to admin_canonical_items_path, notice: "Canonical Item deleted!" else - redirect_to admins_path, alert: "Failed to delete Canonical Item. Are there still items attached?" + redirect_to admin_canonical_items_path, alert: "Failed to delete Canonical Item. Are there still items attached?" end end private - def authorize_user - verboten! unless current_user.organization_admin - end - def canonical_item_params params.require(:canonical_item).permit(:name, :key, :category) end diff --git a/app/controllers/admins_controller.rb b/app/controllers/admin/organizations_controller.rb similarity index 59% rename from app/controllers/admins_controller.rb rename to app/controllers/admin/organizations_controller.rb index ea2da58022..451b687bc5 100644 --- a/app/controllers/admins_controller.rb +++ b/app/controllers/admin/organizations_controller.rb @@ -1,16 +1,14 @@ -class AdminsController < ApplicationController - before_action :authorize_user - +class Admin::OrganizationsController < AdminController def edit @organization = Organization.find(params[:id]) end def update @organization = Organization.find(params[:id]) - if @organization.update(organization_params) - redirect_to admins_path, notice: "Updated organization!" + if @organization.update_attributes(organization_params) + redirect_to admin_organizations_path, notice: 'Updated organization!' else - flash[:error] = "Failed to update this organization." + flash[:error] = 'Failed to update this organization.' render :edit end end @@ -21,7 +19,7 @@ def index def invite_user User.invite!(email: params[:email], name: params[:name], organization_id: params[:org]) - redirect_to admins_path, notice: "User invited to organization!" + redirect_to admin_organizations_path, notice: 'User invited to organization!' end def new @@ -32,7 +30,7 @@ def create @organization = Organization.create(organization_params) if @organization.save Organization.seed_items(@organization) - redirect_to admins_path, notice: "Organization added!" + redirect_to admin_organizations_path, notice: "Organization added!" else flash[:error] = "Failed to create Organization." render :new @@ -46,19 +44,15 @@ def show def destroy @organization = Organization.find(params[:id]) if @organization.destroy - redirect_to admins_path, notice: "Organization deleted!" + redirect_to admin_organizations_path, notice: "Organization deleted!" else - redirect_to admins_path, alert: "Failed to delete Organization." + redirect_to admin_organizations_path, alert: "Failed to delete Organization." end end private - def authorize_user - verboten! unless current_user.organization_admin - end - def organization_params - params.require(:organization).permit(:name, :short_name, :street, :city, :state, :zipcode, :email, :url, :logo) + params.require(:organization).permit(:name, :short_name, :street, :city, :state, :zipcode, :email, :url, :logo, :intake_location) end end diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb new file mode 100644 index 0000000000..0f47053063 --- /dev/null +++ b/app/controllers/admin/users_controller.rb @@ -0,0 +1,40 @@ +class Admin::UsersController < AdminController + def index + @users = User.all + end + + def update; end + + def new + @user = User.new + @organizations = Organization.all + end + + def create + @user = User.new(user_params) + + if @user.save + @user.invite!(@user) + redirect_to admin_users_path, notice: "Created a new user!" + else + flash[:error] = "Failed to create user" + render 'admin/users/new' + end + end + + def destroy + @user = User.find_by(id: params[:id]) + if @user.present? + @user.destroy + redirect_to admin_users_path, notice: "Deleted that user" + else + redirect_to admin_users_path, flash: { error: "Couldn't find that user, sorry" } + end + end + + private + + def user_params + params.require(:user).permit(:name, :organization_id, :email, :password, :password_confirmation) + end +end diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb new file mode 100644 index 0000000000..de896878c7 --- /dev/null +++ b/app/controllers/admin_controller.rb @@ -0,0 +1,9 @@ +class AdminController < ApplicationController + before_action :require_admin + + def require_admin + unless current_user.is_superadmin? + redirect_to root_path, flash: { error: "Access Denied. Only for SuperAdmin." } + end + end +end \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1c4d4d12ca..64e52c926e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -31,7 +31,7 @@ def default_url_options(options = {}) end def authorize_user - verboten! unless params[:controller].include?("devise") || current_organization.id == current_user.organization_id + verboten! unless params[:controller].include?("devise") || params[:controller].include?("admin") || current_organization.id == current_user.organization_id end def not_found! diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index 2a12478095..749e18b07f 100644 --- a/app/controllers/organizations_controller.rb +++ b/app/controllers/organizations_controller.rb @@ -1,21 +1,5 @@ class OrganizationsController < ApplicationController - def edit - @organization = current_organization - end - - def update - @organization = current_organization - if @organization.update(organization_params) - redirect_to edit_organization_path(organization_id: current_organization.to_param), notice: "Updated organization!" - else - flash[:error] = "Failed to update organization" - render :edit - end - end - - private - - def organization_params - params.require(:organization).permit(:name, :short_name, :street, :city, :state, :zipcode, :email, :url, :logo, :intake_location) + def show + render 'admin/organizations/show' end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 2484acead7..8bba17c377 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -2,38 +2,4 @@ class UsersController < ApplicationController def index @users = current_organization.users end - - def update; end - - def new - @user = User.new - end - - def create - @user = User.new(user_params.merge(organization_id: current_organization.id)) - - if @user.save - @user.invite!(@user) - redirect_to users_path, notice: "Created a new user!" - else - flash[:error] = "Failed to create user" - render :new - end - end - - def destroy - @user = current_organization.users.find_by(id: params[:id]) - if @user.present? - @user.destroy - redirect_to users_path, notice: "Deleted that user" - else - redirect_to users_path, flash: { error: "Couldn't find that user, sorry" } - end - end - - private - - def user_params - params.require(:user).permit(:name, :email, :password, :password_confirmation) - end end diff --git a/app/models/user.rb b/app/models/user.rb index bcbc4d74fd..e8ae7bdeb0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -40,6 +40,6 @@ class User < ApplicationRecord validates :name, :email, presence: true def is_superadmin? - false + superadmin end end diff --git a/app/views/canonical_items/_canonical_item_row.html.erb b/app/views/admin/canonical_items/_canonical_item_row.html.erb similarity index 58% rename from app/views/canonical_items/_canonical_item_row.html.erb rename to app/views/admin/canonical_items/_canonical_item_row.html.erb index 7e9b26f0bb..aef4220b27 100644 --- a/app/views/canonical_items/_canonical_item_row.html.erb +++ b/app/views/admin/canonical_items/_canonical_item_row.html.erb @@ -3,8 +3,8 @@ <%= canonical_item_row.category %> <%= canonical_item_row.item_count %> - <%= link_to "View", canonical_item_row, class: "btn btn-primary btn-xs" %> - <%= link_to edit_canonical_item_path(canonical_item_row), class: "btn btn-info btn-xs" do %> + <%= link_to "View", admin_canonical_item_path(canonical_item_row), class: "btn btn-primary btn-xs" %> + <%= link_to edit_admin_canonical_item_path(canonical_item_row), class: "btn btn-info btn-xs" do %> Edit <% end %> diff --git a/app/views/canonical_items/_dropdown.html.erb b/app/views/admin/canonical_items/_dropdown.html.erb similarity index 100% rename from app/views/canonical_items/_dropdown.html.erb rename to app/views/admin/canonical_items/_dropdown.html.erb diff --git a/app/views/canonical_items/_form.html.erb b/app/views/admin/canonical_items/_form.html.erb similarity index 84% rename from app/views/canonical_items/_form.html.erb rename to app/views/admin/canonical_items/_form.html.erb index e0ed4f5127..b14414983a 100644 --- a/app/views/canonical_items/_form.html.erb +++ b/app/views/admin/canonical_items/_form.html.erb @@ -1,7 +1,7 @@ <% submit_text ||= form.submit_text %>
- <%= simple_form_for form, html: { class: 'form-horizontal' } do |f| %> + <%= simple_form_for form, html: { class: 'form-horizontal' }, url: admin_canonical_item_path do |f| %> <%= f.input :name, label: "Name", wrapper: :vertical_input_group do %> <%= f.input_field :name, class: "form-control" %> diff --git a/app/views/canonical_items/edit.html.erb b/app/views/admin/canonical_items/edit.html.erb similarity index 90% rename from app/views/canonical_items/edit.html.erb rename to app/views/admin/canonical_items/edit.html.erb index 9f6a016cb0..4959c3cf72 100644 --- a/app/views/canonical_items/edit.html.erb +++ b/app/views/admin/canonical_items/edit.html.erb @@ -8,7 +8,7 @@ Home <% end %> -
  • <%= link_to "All Canonical Items", (canonical_items_path) %>
  • +
  • <%= link_to "All Canonical Items", (admin_canonical_items_path) %>
  • Editing <%= @canonical_item.name %>
  • diff --git a/app/views/canonical_items/index.html.erb b/app/views/admin/canonical_items/index.html.erb similarity index 99% rename from app/views/canonical_items/index.html.erb rename to app/views/admin/canonical_items/index.html.erb index ea21e5134c..680fa6c3df 100644 --- a/app/views/canonical_items/index.html.erb +++ b/app/views/admin/canonical_items/index.html.erb @@ -36,4 +36,5 @@
    + diff --git a/app/views/canonical_items/new.html.erb b/app/views/admin/canonical_items/new.html.erb similarity index 89% rename from app/views/canonical_items/new.html.erb rename to app/views/admin/canonical_items/new.html.erb index d9d6a85317..baccb4ad8a 100644 --- a/app/views/canonical_items/new.html.erb +++ b/app/views/admin/canonical_items/new.html.erb @@ -8,7 +8,7 @@ Home <% end %> -
  • <%= link_to "All Canonical Items", (canonical_items_path) %>
  • +
  • <%= link_to "All Canonical Items", (admin_canonical_items_path) %>
  • New Canonical Item
  • diff --git a/app/views/canonical_items/show.html.erb b/app/views/admin/canonical_items/show.html.erb similarity index 95% rename from app/views/canonical_items/show.html.erb rename to app/views/admin/canonical_items/show.html.erb index 43bc75ae24..9ddc3702f9 100644 --- a/app/views/canonical_items/show.html.erb +++ b/app/views/admin/canonical_items/show.html.erb @@ -9,7 +9,7 @@ Home <% end %> -
  • <%= link_to "Canonical Items", (canonical_items_path) %>
  • +
  • <%= link_to "Canonical Items", (admin_canonical_items_path) %>
  • <%= @canonical_item.name %>
  • diff --git a/app/views/admin/organizations/_organization_row.html.erb b/app/views/admin/organizations/_organization_row.html.erb new file mode 100644 index 0000000000..f00b8f1239 --- /dev/null +++ b/app/views/admin/organizations/_organization_row.html.erb @@ -0,0 +1,12 @@ + + <%= organization_row.name %> + <%= link_to organization_row.email, "mailto:#{organization_row.email}" %> + + <%= link_to edit_admin_organization_path(organization_row.id), class: "btn btn-info btn-xs" do %> + <%= fa_icon "edit" %> Edit + <% end %> + <%= link_to admin_organization_path(organization_row.id), method: :delete, data: { confirm: confirm_delete_msg(organization_row.name) }, class: "btn btn-danger btn-xs" do %> + <%= fa_icon "trash" %> Delete + <% end unless (Organization.count <= 1) %> + + diff --git a/app/views/admins/edit.html.erb b/app/views/admin/organizations/edit.html.erb similarity index 92% rename from app/views/admins/edit.html.erb rename to app/views/admin/organizations/edit.html.erb index 93c58014b8..269b1a61bf 100644 --- a/app/views/admins/edit.html.erb +++ b/app/views/admin/organizations/edit.html.erb @@ -9,7 +9,7 @@ Editing Home <% end %> -
  • <%= link_to "Administration", (admins_path(organization_id: current_user.organization)) %>
  • +
  • <%= link_to "Administration", (admin_organizations_path) %>
  • Editing <%= @organization.name %>
  • @@ -23,7 +23,7 @@ Editing

    Update record for <%= current_organization.name %>

    -<%= simple_form_for @organization, url: admin_path do |f| %> +<%= simple_form_for @organization, url: admin_organization_path do |f| %>
    <%= f.input :name, required: true, autofocus: true %> diff --git a/app/views/admin/organizations/index.html.erb b/app/views/admin/organizations/index.html.erb new file mode 100644 index 0000000000..f4b3d09fb7 --- /dev/null +++ b/app/views/admin/organizations/index.html.erb @@ -0,0 +1,50 @@ +
    + <% content_for :title, "Admin - Organizations" %> +

    + All Diaperbase Organizations + +

    + +
    + + +
    + +
    +
    +
    + <%= link_to new_admin_organization_path, class: "btn btn-success" do %> + <%= fa_icon "plus" %> Add New Organization + <% end %> +
    +
    +
    + +
    + + + + + + + + + + + <%= render partial: "organization_row", collection: @organizations %> + +
    OrganizationContact E-mailActions
    + +
    + +
    +
    +
    +
    +
    diff --git a/app/views/admins/new.html.erb b/app/views/admin/organizations/new.html.erb similarity index 88% rename from app/views/admins/new.html.erb rename to app/views/admin/organizations/new.html.erb index 2352acdd88..36152b4e63 100644 --- a/app/views/admins/new.html.erb +++ b/app/views/admin/organizations/new.html.erb @@ -9,7 +9,7 @@ New Organization Home <% end %> -
  • <%= link_to "Administration", (admins_path(organization_id: current_user.organization)) %>
  • +
  • <%= link_to "Administration", new_admin_organization_path %>
  • New Organization
  • @@ -23,7 +23,7 @@ New Organization

    Add New Diaperbase Organization

    -<%= simple_form_for @organization, url: admins_path do |f| %> +<%= simple_form_for @organization, url: admin_organizations_path do |f| %>
    <%= f.input :name, required: true, autofocus: true %> diff --git a/app/views/admins/show.html.erb b/app/views/admin/organizations/show.html.erb similarity index 94% rename from app/views/admins/show.html.erb rename to app/views/admin/organizations/show.html.erb index f86f01f24c..78214d154b 100644 --- a/app/views/admins/show.html.erb +++ b/app/views/admin/organizations/show.html.erb @@ -9,7 +9,6 @@ Home <% end %> -
  • <%= link_to "Administration", (admins_path(organization_id: current_user.organization)) %>
  • <%= @organization.name %>
  • @@ -49,7 +48,7 @@
    -<% end %> \ No newline at end of file +
    \ No newline at end of file diff --git a/app/views/layouts/_lte_sidebar.html.erb b/app/views/layouts/_lte_sidebar.html.erb index e036e22a93..344d8e3c49 100644 --- a/app/views/layouts/_lte_sidebar.html.erb +++ b/app/views/layouts/_lte_sidebar.html.erb @@ -1,5 +1,3 @@ -<% if controller.controller_name != "admins" %> - -<% else %> - - -<% end %> diff --git a/app/views/layouts/_navbar.html.erb b/app/views/layouts/_navbar.html.erb deleted file mode 100644 index 10fb8b33b8..0000000000 --- a/app/views/layouts/_navbar.html.erb +++ /dev/null @@ -1,101 +0,0 @@ - diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index d09358400b..a7ef5da776 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -33,7 +33,6 @@ Name Email - @@ -41,17 +40,6 @@ <%= user.name %> <%= user.email %> - - <% if user != current_user %> - <%= link_to user_path(user), method: :delete, confirm: "Are you sure you want to permanently remove this user?", class: "btn btn-danger btn-xs" do %> - <%= fa_icon "trash" %> Delete - <% end %> - <% else %> - <%= link_to edit_user_registration_path(organization_id: nil), class: "btn btn-info btn-xs" do %> - <%= fa_icon "edit" %> Edit - <% end %> - <% end %> - <% end %> @@ -62,4 +50,5 @@
    +