Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added dialup administration, not as a module [#19 state:resolved]
- Loading branch information
Jonathan Hoyt
authored and
Jonathan Hoyt
committed
Dec 22, 2008
1 parent
a8b9bad
commit d770f21
Showing
21 changed files
with
810 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Install hook code here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.