diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 227d4a1..5752c2c 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,46 +1,71 @@ class Admin::UsersController < Admin::BaseController # GET /admin/users def index @baculized_host_names = Hash.new { |h, k| h[k] = [] } @non_baculized_host_names = Hash.new { |h, k| h[k] = [] } @unverified_host_names = Hash.new { |h, k| h[k] = [] } @users = User.all.includes(:hosts) @users = @users.admin if params[:type] == 'admin' @users = @users.vima if params[:type] == 'vima' @users.each do |user| user.hosts.each do |host| if host.deployed? || host.updated? || host.dispatched? || host.for_removal? @baculized_host_names[user.id] << host.name else @non_baculized_host_names[user.id] << host.name @unverified_host_names[user.id] << host.name if !host.verified? end end end end + # GET /admin/users/new + def new + @user = User.new + end + + # POST /admin/users + def create + @user = User.new(fetch_params) + + @user.user_type = :admin + if @user.add_password(@user.password) + flash[:success] = 'User created' + redirect_to admin_users_path + else + flash[:error] = 'User was not created' + render 'new' + end + end + # PATCH /admin/users/1/ban def ban @user = User.find(params[:id]) if @user.ban flash[:success] = 'User banned' else flash[:error] = 'User NOT banned' end redirect_to admin_users_path end # PATCH /admin/users/1/unban def unban @user = User.find(params[:id]) if @user.unban flash[:success] = 'User enabled' else flash[:error] = 'User NOT enabled' end redirect_to admin_users_path end + + private + + def fetch_params + params.require(:user).permit(:username, :email, :password, :retype_password) + end end diff --git a/app/views/admin/users/_form.html.erb b/app/views/admin/users/_form.html.erb new file mode 100644 index 0000000..df81432 --- /dev/null +++ b/app/views/admin/users/_form.html.erb @@ -0,0 +1,15 @@ +<%= bootstrap_form_for(@user, url: admin_users_path, method: :post, layout: :horizontal, + label_col: 'col-xs-3', control_col: 'col-xs-8') do |f| %> + <%= f.text_field :username, required: true %> + <%= f.password_field :password, required: true %> + <%= f.password_field :retype_password, required: true %> + <%= f.email_field :email, required: true %> + +
+
+ <%= f.submit class: 'btn btn-success' %> +
+
+<% end %> + +<%= link_to 'Cancel', admin_users_path, class: 'btn btn-danger', role: 'button' %> diff --git a/app/views/admin/users/index.html.erb b/app/views/admin/users/index.html.erb index 5d1520f..d0fa88c 100644 --- a/app/views/admin/users/index.html.erb +++ b/app/views/admin/users/index.html.erb @@ -1,23 +1,34 @@ +
+ <%= link_to new_admin_user_path, class: "btn btn-default", role: "button" do %> + + New Admin + <% end %> +
+

Users

-
- - - - - - - - - - - - - - +
+
+
+
idusernameemailuser typecreated atclientsunverified hostspending hostsactions
+ + + + + + + + + + + + + - - <%= render partial: 'user', collection: @users %> - -
idusernameemailuser typecreated atclientsunverified hostspending hostsactions
+ + <%= render partial: 'user', collection: @users %> + + +
+ diff --git a/app/views/admin/users/new.html.erb b/app/views/admin/users/new.html.erb new file mode 100644 index 0000000..010faa0 --- /dev/null +++ b/app/views/admin/users/new.html.erb @@ -0,0 +1,11 @@ +
+
+
+
+

New Admin

+ + <%= render partial: 'form' %> +
+
+
+
diff --git a/config/routes.rb b/config/routes.rb index 5f840c9..eedbac6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,81 +1,81 @@ Rails.application.routes.draw do root 'application#index' post 'grnet' => 'application#grnet' match 'vima', to: 'application#vima', :via => [:get, :post] get 'logout' => 'application#logout' resources :clients, only: [:index, :show] do member do get :jobs get :logs get :stats post :stats get :users get :restore post :run_restore end collection do post :index end end resources :hosts, only: [:new, :create, :show, :edit, :update, :destroy] do member do post :submit_config post :disable delete :revoke end resources :jobs, only: [:new, :create, :show, :edit, :update, :destroy] do member do patch :toggle_enable post :backup_now end end resources :filesets, only: [:show, :new, :create, :destroy] resources :schedules, only: [:show, :new, :edit, :create, :update, :destroy] end namespace :admin do match '/', to: 'base#index', via: [:get, :post] get '/login' => 'base#login', as: :login resources :settings, only: [:index, :new, :create, :edit, :update] do member do delete :reset end end resources :clients, only: [:index, :show] do member do get :jobs get :logs get :stats post :stats get :configuration post :disable delete :revoke end end resources :hosts, only: [:show] do collection do get :unverified end member do post :verify end end - resources :users, only: [:index] do + resources :users, only: [:index, :new, :create] do member do patch :ban patch :unban end end end end diff --git a/spec/routing/admin/users_routing_spec.rb b/spec/routing/admin/users_routing_spec.rb index ee6f272..ea0505b 100644 --- a/spec/routing/admin/users_routing_spec.rb +++ b/spec/routing/admin/users_routing_spec.rb @@ -1,17 +1,27 @@ require 'spec_helper' describe Admin::UsersController do it 'routes GET /admin/users' do expect(get('/admin/users')).to route_to(controller: 'admin/users', action: 'index') end + it 'routes GET /admin/users/new' do + expect(get('/admin/users/new')). + to route_to(controller: 'admin/users', action: 'new') + end + + it 'routes POST /admin/users' do + expect(post('/admin/users')). + to route_to(controller: 'admin/users', action: 'create') + end + it 'routes PATCH /admin/users/1/ban' do expect(patch('/admin/users/1/ban')). to route_to(controller: 'admin/users', action: 'ban', id: '1') end it 'routes PATCH /admin/users/1/unban' do expect(patch('/admin/users/1/unban')). to route_to(controller: 'admin/users', action: 'unban', id: '1') end end