diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb new file mode 100644 index 0000000..d6ac2c0 --- /dev/null +++ b/app/controllers/admin/users_controller.rb @@ -0,0 +1,32 @@ +module Admin + class UsersController < ApplicationController + before_action :authenticate_user! + before_action :admin_only! + + # GET /users/orphans + def orphans + @users = User.orphans + end + + def update_groups + additions = 0 + + params.each_pair { |k, group_id| + next if !k.start_with?('orphan-') + + _, id = k.split('-', 2) + user = User.orphans.find_by_id(id) + next if !user + + group = Group.find_by_id(group_id) + next if !group + + user.groups << group + additions += 1 + } + + redirect_to :back, notice: "#{additions} users were assigned to groups" + end + + end +end diff --git a/app/models/user.rb b/app/models/user.rb index f2bc444..0fc6268 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,7 +1,9 @@ class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_many :memberships has_many :groups, through: :memberships + + scope :orphans, -> { includes(:memberships).where(:memberships => { user_id: nil }) } end diff --git a/app/views/admin/users/orphans.html.erb b/app/views/admin/users/orphans.html.erb new file mode 100644 index 0000000..c08cd7d --- /dev/null +++ b/app/views/admin/users/orphans.html.erb @@ -0,0 +1,10 @@ +
+

+ <%= bootstrap_form_tag(url: update_groups_admin_users_path, method: 'PUT', layout: :horizontal, label_col: 'col-sm-2', control_col: 'col-sm-4') do |f| %> + <% @users.each do |u| %> + <%= f.collection_select "orphan-#{u.id}", Group.all, :id, :name, label: u.email, prompt: 'Assign to group' %> + <% end %> + <%= f.submit 'Add', class: 'btn btn-primary' %> + <% end %> +

+
diff --git a/app/views/shared/_nav.html.erb b/app/views/shared/_nav.html.erb index 5f0b8b8..5aa2498 100644 --- a/app/views/shared/_nav.html.erb +++ b/app/views/shared/_nav.html.erb @@ -1,53 +1,54 @@ diff --git a/config/routes.rb b/config/routes.rb index 8b6db0a..94031d9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,48 +1,52 @@ Rails.application.routes.draw do # Override devise user removal devise_scope :users do delete :users, to: redirect('/') end devise_for :users root to: redirect('/domains') resources :groups, only: [:show] do get :search_member, to: 'groups#search_member', on: :member post :members, to: 'groups#create_member', as: :create_member, on: :member delete 'member/:user_id', to: 'groups#destroy_member', as: :destroy_member, on: :member end resources :domains do get :edit_dnssec, to: 'domains#edit_dnssec', on: :member resources :records, except: [:index, :show] do # Reuse records#update instead of introducing new controller actions # # rubocop:disable Style/AlignHash put :disable, to: 'records#update', on: :member, defaults: { record: { disabled: true } } put :enable, to: 'records#update', on: :member, defaults: { record: { disabled: false } } # rubocop:enable Style/AlignHash end end get '/records/search', to: 'records#search' # Admin namespace :admin do root to: redirect('/admin/groups') resources :groups, except: [:show] resources :jobs, only: [:index, :destroy] + resources :users, only: [] do + get :orphans, to: 'users#orphans', on: :collection + put :update_groups, to: 'users#update_groups', on: :collection + end end # Private put 'private/replace_ds', to: 'private#replace_ds' put 'private/trigger_event', to: 'private#trigger_event' end