Skip to content

Commit

Permalink
Added dialup administration, not as a module [#19 state:resolved]
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Hoyt authored and Jonathan Hoyt committed Dec 22, 2008
1 parent a8b9bad commit d770f21
Show file tree
Hide file tree
Showing 21 changed files with 810 additions and 2 deletions.
38 changes: 38 additions & 0 deletions app/controllers/radchecks_controller.rb
@@ -0,0 +1,38 @@
class RadchecksController < ApplicationController
before_filter :login_required
layout nil

def create
@radcheck = Radcheck.new(params[:radcheck])
if @radcheck.save
flash[:notice] = "Dialup account created successfully!"
redirect_to :back
else
flash[:notice] = "Suite could not create this dialup account."
redirect_to :back
end
end

def update
@radcheck = Radcheck.find(params[:id])
if @radcheck.update_attributes(params[:radcheck])
flash[:notice] = "Dialup account updated successfully!"
redirect_to :back
else
flash[:notice] = "Suite could not update this dialup account."
redirect_to :back
end
end

def destroy
@radcheck = Radcheck.find(params[:id])
if @radcheck.destroy
flash[:notice] = "Dialup account deleted successfully."
redirect_to :back
else
flash[:notice] = "Dialup account could not be deleted."
redirect_to :back
end
end

end
10 changes: 10 additions & 0 deletions app/models/client.rb
Expand Up @@ -13,6 +13,8 @@ class Client < ActiveRecord::Base
has_many :phones, :dependent => :destroy
has_many :emails, :dependent => :destroy
has_many :addresses, :dependent => :destroy

has_one :radcheck, :dependent => :destroy

validates_associated :phones, :emails, :addresses

Expand Down Expand Up @@ -135,5 +137,13 @@ def primary_address
def open_tickets
Ticket.find(:all, :conditions => {:archived_on => nil, :client_id => self.id})
end

def active_dialup_user?
if self.radcheck && self.radcheck.value[0..8] != "disabled_"
return true
else
return false
end
end

end
2 changes: 1 addition & 1 deletion app/models/device.rb
Expand Up @@ -12,7 +12,7 @@ class Device < ActiveRecord::Base
after_create :create_checklists

def create_service_tag
if self.service_tag == ""
if self.service_tag.blank?
recent_device = Device.find(:first, :order => 'created_at DESC', :conditions => {:created_at.gt => DateTime.now.beginning_of_day, :created_at.lt => DateTime.now.end_of_day, :device_type_id => self.device_type_id})
device_type = DeviceType.find(self.device_type_id)
if recent_device != nil
Expand Down
16 changes: 16 additions & 0 deletions app/models/radcheck.rb
@@ -0,0 +1,16 @@
class Radcheck < ActiveRecord::Base
use_db :prefix => "freeradius_"
set_table_name "radcheck"

belongs_to :client

validates_presence_of :username, :attribute, :value
validates_uniqueness_of :username

# this won't work for some reason
before_create :set_attribute
def set_attribute
self.attribute = "User-Password"
end

end
61 changes: 61 additions & 0 deletions app/views/clients/_dialup.html.erb
@@ -0,0 +1,61 @@
<% !@client.radcheck.blank? ? @radcheck = @client.radcheck : @radcheck = Radcheck.new %>
<div id="dialup">
<% if @client.radcheck.blank? %>
<ul class="toolbar">
<li><a href="#" class="add">Add Dialup</a></li>
</ul>
<% else %>
<ul class="toolbar">
<% if @client.radcheck.value[0..8] == "disabled_" %>
<li><a href="#" class="enable">Enable Dialup Account</a></li>
<% else %>
<li><a href="#" class="disable">Disable Dialup Account</a></li>
<% end %>
<li><%= link_to "Delete Dialup Account", { :controller => "radchecks", :action => "destroy", :id => @radcheck }, :confirm => "Are you sure you want to delete this dialup account?", :method => :delete %></li>
</ul>
<% end %>
<div id="dialup_form" class="toolbox hide">
<% form_for @radcheck do |f| %>
<table class="m">
<tr>
<td class="first">Username:</td>
<td><%= f.text_field :username %></td>
</tr>
<tr>
<td class="first">Password:</td>
<td><%= f.text_field :value %></td>
</tr>
</table>
<%= f.hidden_field :attribute, :value => "User-Password" %>
<%= f.hidden_field :client_id, :value => @client.id %>
<%= f.submit "Save" %>
<% end %>
<br />
</div>
</div>

<script>
<% if @client.active_dialup_user? %>
$("div#dialup_form").removeClass("hide");
$("div#dialup a.disable").click(function(){
var password = $("input#radcheck_value").val();
password = "disabled_" + password;
$("input#radcheck_value").val(password);
$("div#dialup_form form input[type=submit]").click();
});
<% else %>
$("div#dialup a.enable").click(function(){
var password = $("input#radcheck_value").val();
password = password.split("disabled_");
password = password[1];
$("input#radcheck_value").val(password);
$("div#dialup_form form input[type=submit]").click();
});
<% end %>

$("div#dialup > ul.toolbar > li > a.add").click(function(){
$("div#dialup_form").slideDown(500);
});


</script>
3 changes: 2 additions & 1 deletion app/views/shared/_new_device.html.erb
@@ -1,6 +1,7 @@
<h3>New Device</h3>
<% form_for Device.new(), :url => "" do |f| -%>
<%= f.hidden_field :client_id, :value => @ticket.client.id %>
<% @client.blank? ? @client_id = @ticket.client.id : @client_id = @client.id %>
<%= f.hidden_field :client_id, :value => @client_id %>
<table id="device_details" class="m">
<tr>
<td class="first">Service Tag:</td><td><%= f.text_field :service_tag %> Auto-Generated</td>
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Expand Up @@ -43,6 +43,8 @@

map.resources :things

map.resources :radchecks

map.device_details '/tickets/:ticket_id/devices/:id/details', :controller => 'devices', :action => 'details'
map.add_to_ticket '/tickets/:ticket_id/devices/:id/add_to_ticket', :controller => 'devices', :action => 'add_to_ticket'
map.remove_device_from_ticket '/tickets/:ticket_id/devices/:id/remove_from_ticket', :controller => 'devices', :action => 'remove_from_ticket'
Expand Down
20 changes: 20 additions & 0 deletions vendor/plugins/use_db/MIT-LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2007 [name of plugin creator]

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
86 changes: 86 additions & 0 deletions vendor/plugins/use_db/README
@@ -0,0 +1,86 @@
UseDb
by David Stevenson
ds@elctech.com
=====
USAGE

This plugin allows you to use multiple databases in your rails application.
You can switch the database for a model in the following manner:

class MyModel < ActiveRecord::Base
use_db :prefix => "secdb_", :suffix => "_cool"
end

"use_db" takes a prefix and a suffix (only 1 of which is required) which are prepended and appended onto the current RAILS_ENV.
In the above example, I would have to make the following database entries to my database.yml:

secdb_development_cool:
adapter: mysql
database: secdb_dev_db
...

secdb_test_cool:
adapater: mysql
database: secdb_test_db
...

It's often useful to create a single abstract model which all models using a different database extend from:

class SecdbBase < ActiveRecord::Base
use_db :prefix => "secdb_"
self.abstract_class = true
end

class MyModel < SecdbBase
# this model will use a different database automatically now
end

==========
MIGRATIONS

To write a migration which executes on a different database, add the following method to your
migration:

class MyMigration < ActiveRecord::Migration
def self.database_model
return "SecdbBase"
end

def self.up
...
end

...
end

The "self.database_model" call must return a string which is the name of the model whose connection
you want to borrow when performing the migration. If this method is undefined, the default ActiveRecord::Base
connection is used.

=======
TESTING

In order to test multiple databases, you must invoke a task which clones the development database
structure and copies it into the test database, clearing out the existing test data. There is a single
helper method which executes this task and you invoke it as follows:

UseDbTest.prepare_test_db(:prefix => "secdb_")

Even though it might not be the best place for it, I often place a call to this in my test helper.
You don't want it to execute for every test, so put the following guards around it:

unless defined?(CLONED_SEC_DB_FOR_TEST)
UseDbTest.prepare_test_db(:prefix => "secdb_")
CLONED_SEC_DB_FOR_TEST = true
end

========
FIXTURES

Fixtures will automatically be loaded into the correct database as long as the fixture name corresponds
to the name of a model. For example, if I have a model called SecdbUser who uses a different database and
I create a fixture file called secdb_users.yml, the fixture loader will use whatever database connection
belongs to hte SecdbUser model.

There is currently no other way to force a fixture to use a specific database (sorry, no join tables yet),
like there is for migrations.
22 changes: 22 additions & 0 deletions vendor/plugins/use_db/Rakefile
@@ -0,0 +1,22 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the use_db plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the use_db plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'UseDb'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
13 changes: 13 additions & 0 deletions vendor/plugins/use_db/init.rb
@@ -0,0 +1,13 @@
# Include hook code here

require "use_db"
require "use_db_test"
require 'active_record/fixtures'
require "override_fixtures"
require 'active_record/migration'
require 'override_test_case'
require "migration"

ActiveRecord::Base.extend(UseDbPlugin)
#Fixtures.send(:extend, OverrideFixtures::ClassMethods)
#Fixtures.send(:include, OverrideFixtures::InstanceMethods)
1 change: 1 addition & 0 deletions vendor/plugins/use_db/install.rb
@@ -0,0 +1 @@
# Install hook code here
21 changes: 21 additions & 0 deletions vendor/plugins/use_db/lib/migration.rb
@@ -0,0 +1,21 @@
module ActiveRecord
class Migration
class << self
def method_missing(method, *arguments, &block)
say_with_time "#{method}(#{arguments.map { |a| a.inspect }.join(", ")})" do
arguments[0] = Migrator.proper_table_name(arguments.first) unless arguments.empty? || method == :execute
if (self.respond_to?(:database_model))
write "Using custom database model's connection (#{self.database_model}) for this migration"
eval("#{self.database_model}.connection.send(method, *arguments, &block)")
else
ActiveRecord::Base.connection.send(method, *arguments, &block)
end
end
end

def uses_db?
true
end
end
end
end
32 changes: 32 additions & 0 deletions vendor/plugins/use_db/lib/override_fixtures.rb
@@ -0,0 +1,32 @@
# Override the rails fixtures to borrow the proper model's connection when inserting or deleting
# data whenever possible.

class Fixtures
alias_method :rails_delete_existing_fixtures, :delete_existing_fixtures
def delete_existing_fixtures
m = get_model
#puts "Model: #{m}, class_name: #{@class_name}"
return rails_delete_existing_fixtures unless m && m.respond_to?(:uses_db?) && m.uses_db?
connection = m.connection
connection.delete "DELETE FROM #{m.table_name}", 'Fixture Delete'
end

alias_method :rails_insert_fixtures, :insert_fixtures
def insert_fixtures
m = get_model
return rails_insert_fixtures unless m && m.respond_to?(:uses_db?) && m.uses_db?
connection = m.connection
values.each do |fixture|
#puts "Inserting fixtures into custom DB for #{connection.current_database}.#{m.table_name}: INSERT INTO #{m.table_name} (#{fixture.key_list}) VALUES (#{fixture.value_list})"
connection.execute("INSERT INTO #{m.table_name} (#{fixture.key_list}) VALUES (#{fixture.value_list})", 'Fixture Insert')
end
end

private
def get_model
klass = eval(@class_name)
return klass
rescue
return nil
end
end

0 comments on commit d770f21

Please sign in to comment.