Commit 934741e3 authored by Cool Fire's avatar Cool Fire

Mostly able to approve/disapprove job requests.

parent 37eea382
#!/usr/bin/env ruby
# encoding: utf-8
require 'bcrypt'
puts BCrypt::Password.create ARGV[0]
\ No newline at end of file
class CommandsController < ApplicationController
include LoginHelper
before_action :checkauth
def new
end
......
class JobrequestsController < ApplicationController
include JobsHelper
include LoginHelper
before_action :checkauth
def index
@jobrequests = Jobrequest.all
end
def approve
jr = Jobrequest.find(params[:id])
# Make job out of request
@job = Job.new()
@job.server_id = jr.server_id
@job.command_id = jr.command_id
@job.args = "#{jr.user} #{jr.email} #{jr.args}"
@job.state = 2
@job.result = 'Waiting for results'
@job.timestampstarted = DateTime.now
@job.save
# Update state of request
jr.state = 0
jr.result = 'Job approved'
jr.timestampcompleted = DateTime.now
jr.save
# Fire job into rabbitmq
queue_job(@job)
redirect_to @job
end
def disapprove
if params[:disapprove_reason]
# Send email explaining disapproval
raise "SHOOP DA WHOOP"
end
end
def show
begin
@job = Jobrequest.find(params[:id])
rescue => e
flash[:error] = "No jobrequest with that ID."
flash[:error] = 'No jobrequest with that ID'
redirect_to jobrequests_path
end
end
......
class JobsController < ApplicationController
include JobsHelper
include LoginHelper
before_action :checkauth
def index
@jobs = Job.all
end
......
class LoginController < ApplicationController
def index
end
include LoginHelper
def index
# Check if parameters are set
if params[:login_form]
if params[:login_form][:username] && params[:login_form][:password]
# Check if username and password are correct
if params[:login_form][:username] =~ /admin/i && checkpasword( params[:login_form][:password] )
session[:authed] = true
flash[:notice] = "Login successful"
redirect_to jobs_path
else
flash[:error] = "Username or password incorrect"
end
end
end
end
def logout
session[:authed] = false
session.delete(:authed)
flash[:notice] = "Logged out"
redirect_to '/login'
end
end
class ServersController < ApplicationController
include LoginHelper
before_action :checkauth
def index
@servers = Server.all
end
......
module LoginHelper
require 'bcrypt'
def checkpasword( pass )
config = loadconfig()
return BCrypt::Password.new(config['admin_hash']) == pass
end
def checkauth()
if session[:authed]
return true
else
flash[:error] = "Need to be logged in to view this page"
redirect_to '/login'
return false
end
end
end
<div class='pagetitle'>
New job
</div>
<div class='form'>
<table>
<%= form_for :disapprove_reason, url: jobrequest_disapprove_path do |f| %>
<tr>
<td>
<%= f.label :reason, 'Reason for disapproval' %>
</td>
<td>
<%= f.text_field :reason %>
</td>
</tr>
<tr>
<td>
<%= f.submit %>
</td>
</tr>
<% end %>
</table>
</div>
\ No newline at end of file
......@@ -43,5 +43,9 @@
<td>Processed time</td>
<td><%= showtime(@job.timestampcompleted) %></td>
</tr>
<tr>
<td><%= link_to 'Approve', jobrequest_approve_path, :method => :post %></td>
<td><%= link_to 'Disapprove', jobrequest_disapprove_path, :method => :post %></td>
</tr>
</table>
</div>
\ No newline at end of file
<h1>Login#index</h1>
<p>Find me in app/views/login/index.html.erb</p>
<div class='pagetitle'>
Log in
</div>
<div class='form'>
<table>
<%= form_for :login_form, url: '/login' do |f| %>
<tr>
<td>
<%= f.label :username, 'Username' %>
</td>
<td>
<%= f.text_field :username %>
</td>
</tr>
<tr>
<td>
<%= f.label :password, 'Password' %>
</td>
<td>
<%= f.password_field :password %>
</td>
</tr>
<tr>
<td>
<%= f.submit 'Log in' %>
</td>
</tr>
<% end %>
</table>
</div>
\ No newline at end of file
......@@ -4,5 +4,6 @@
"pass":"rabbitmq_pass",
"private_keyfile":"config/private.pem",
"encryption_pass":"password_for_private.pem",
"api_pass":"some_secret_key"
"api_pass":"some_secret_key",
"admin_hash":"$2a$10$0If.T9avOScIhcRyjekeFu5L2iWVRdJ3RWEV/G7TT255yzY4mwRsu"
}
\ No newline at end of file
......@@ -7,6 +7,14 @@ Rails.application.routes.draw do
get 'api/index'
post 'api/index'
get '/login' => 'login#index'
post '/login' => 'login#index'
get '/logout' => 'login#logout'
post '/jobrequests/:id/approve', to: 'jobrequests#approve', as: 'jobrequest_approve'
post '/jobrequests/:id/disapprove', to: 'jobrequests#disapprove', as: 'jobrequest_disapprove'
resources :commands, :servers, :jobs, :jobrequests
root to: 'login#index'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment