Project

General

Profile

Patch #5690 » ldappassword.patch

change user ldap password - Oskar H, 2010-06-15 14:18

View differences:

app/controllers/my_controller.rb
# Redmine - project management software
# Redmine - project management software
# Copyright (C) 2006-2009 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
......
end
if request.post?
if @user.check_password?(params[:password])
@user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
if @user.save
flash[:notice] = l(:notice_account_password_updated)
redirect_to :action => 'account'
if @user.isExternal?
if @user.changeExternalPassword(params[:password],params[:new_password], params[:new_password_confirmation])
flash[:notice] = l(:notice_account_password_updated)
redirect_to :action => 'account'
else
flash[:error] = l(:notice_external_password_error)
end
else
@user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
if @user.save
flash[:notice] = l(:notice_account_password_updated)
redirect_to :action => 'account'
end
end
else
flash[:error] = l(:notice_account_wrong_password)
app/models/auth_source_ldap.rb
require 'net/ldap'
require 'iconv'
require 'digest'
require 'base64'
class AuthSourceLdap < AuthSource
validates_presence_of :host, :port, :attr_login
......
validates_length_of :account, :base_dn, :maximum => 255, :allow_nil => true
validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30, :allow_nil => true
validates_numericality_of :port, :only_integer => true
validates_columns :password_encryption
before_validation :strip_ldap_attributes
def after_initialize
self.port = 389 if self.port == 0
self.port = 389 if self.port == 0
end
def authenticate(login, password)
......
def auth_method_name
"LDAP"
end
def allow_password_changes?
return self.enabled_passwd
end
def encode_password(clear_password)
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
salt = ''
10.times { |i| salt << chars[rand(chars.size-1)] }
if self.password_encryption == :MD5
logger.debug "Encode as md5"
return "{MD5}"+Base64.encode64(Digest::MD5.digest(clear_password)).chomp!
end
if self.password_encryption == :SSHA
logger.debug "Encode as ssha"
return "{SSHA}"+Base64.encode64(Digest::SHA1.digest(clear_password+salt)+salt).chomp!
end
if self.password_encryption == :CLEAR
logger.debug "Encode as cleartype"
return clear_password
end
end
# change password
def change_password(login,password,newPassword)
begin
attrs = get_user_dn(login)
if attrs
if self.account.blank? || self.account_password.blank?
logger.debug "Binding with user account"
ldap_con = initialize_ldap_con(attrs[:dn], password)
else
logger.debug "Binding with administrator account"
ldap_con = initialize_ldap_con(self.account, self.account_password)
end
return ldap_con.replace_attribute attrs[:dn], :userPassword, encode_password(newPassword)
end
rescue
return false
end
return false
end
private
def strip_ldap_attributes
app/models/user.rb
anonymous_user
end
def isExternal?
return auth_source_id.present?
end
def changeExternalPassword(password,newPassword,newPasswordConfirm)
return false if newPassword == "" || newPassword.length < 4
return false if newPassword != newPasswordConfirm
if (self.isExternal?)
return self.auth_source.change_password(self.login,password,newPassword)
end
return false
end
protected
def validate
app/views/ldap_auth_sources/_form.rhtml
<p><label for="auth_source_onthefly_register"><%=l(:field_onthefly)%></label>
<%= check_box 'auth_source', 'onthefly_register' %></p>
<p><label for="auth_source_enabled_passwd"><%=l(:field_enabled_passwd)%></label>
<%= check_box 'auth_source', 'enabled_passwd' %></p>
</div>
<fieldset class="box"><legend><%=l(:label_attribute_plural)%></legend>
......
<p><label for="auth_source_attr_mail"><%=l(:field_mail)%></label>
<%= text_field 'auth_source', 'attr_mail', :size => 20 %></p>
<p><label for="auth_source_password_encryption"><%=l(:field_password_encryption)%></label>
<%= input 'auth_source', 'password_encryption' %></p>
</fieldset>
<!--[eoform:auth_source]-->
config/locales/de.yml
general_pdf_encoding: ISO-8859-1
general_first_day_of_week: '1'
notice_external_password_error: Externes Konto konnte nicht aktualisiert werden.
notice_account_updated: Konto wurde erfolgreich aktualisiert.
notice_account_invalid_creditentials: Benutzer oder Kennwort ist ungültig.
notice_account_password_updated: Kennwort wurde erfolgreich aktualisiert.
notice_account_wrong_password: Falsches Kennwort.
notice_account_register_done: Konto wurde erfolgreich angelegt.
notice_account_unknown_email: Unbekannter Benutzer.
notice_can_t_change_password: Dieses Konto verwendet eine externe Authentifizierungs-Quelle. Unmöglich, das Kennwort zu ändern.
notice_can_t_change_password: Kennwort ändern ist gesperrt für diese externe Authentifizierungs-Quelle.
notice_account_lost_email_sent: Eine E-Mail mit Anweisungen, ein neues Kennwort zu wählen, wurde Ihnen geschickt.
notice_account_activated: Ihr Konto ist aktiviert. Sie können sich jetzt anmelden.
notice_successful_create: Erfolgreich angelegt
......
field_attr_lastname: Name-Attribut
field_attr_mail: E-Mail-Attribut
field_onthefly: On-the-fly-Benutzererstellung
field_password_encryption: Verschlüsselungsart
field_enabled_passwd: Password ändern erlauben
field_start_date: Beginn
field_done_ratio: % erledigt
field_auth_source: Authentifizierungs-Modus
config/locales/en.yml
general_pdf_encoding: ISO-8859-1
general_first_day_of_week: '7'
notice_external_password_error: External password changing goes wrong
notice_account_updated: Account was successfully updated.
notice_account_invalid_creditentials: Invalid user or password
notice_account_password_updated: Password was successfully updated.
......
field_attr_lastname: Lastname attribute
field_attr_mail: Email attribute
field_onthefly: On-the-fly user creation
field_password_encryption: Encyrption
field_enabled_passwd: Enabled password changing
field_start_date: Start
field_done_ratio: % Done
field_auth_source: Authentication mode
db/migrate/20100609104539_ldap_password_change.rb
class LdapPasswordChange < ActiveRecord::Migration
def self.up
add_column :auth_sources, :password_encryption, :enum, :limit => [:MD5, :SSHA, :CLEAR], :default => :MD5
add_column :auth_sources, :enabled_passwd, :boolean, :default => false
end
def self.down
remove_column :auth_sources, :password_encryption
remove_column :auth_sources, :enabled_passwd
end
end
vendor/plugins/enum-column/init.rb
require 'enum/enum_adapter'
require 'enum/mysql_adapter' if defined? ActiveRecord::ConnectionAdapters::MysqlAdapter
require 'enum/postgresql_adapter' if defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
require 'enum/sqlite3_adapter' if defined? ActiveRecord::ConnectionAdapters::SQLite3Adapter
require 'enum/schema_statements'
require 'enum/schema_definitions'
require 'enum/quoting'
require 'enum/validations'
require 'enum/active_record_helper'
vendor/plugins/enum-column/lib/enum/active_record_helper.rb
module ActionView
module Helpers
module FormHelper
# helper to create a select drop down list for the enumerated values. This
# is the default input tag.
def enum_select(object_name, method, options = {})
if RAILS_GEM_VERSION < '2.2.0'
tag = InstanceTag.new(object_name, method, self, nil, options.delete(:object))
else
tag = InstanceTag.new(object_name, method, self, options.delete(:object))
end
tag.to_enum_select_tag(options)
end
# Creates a set of radio buttons for all the enumerated values.
def enum_radio(object_name, method, options = {})
if RAILS_GEM_VERSION < '2.2.0'
tag = InstanceTag.new(object_name, method, self, nil, options.delete(:object))
else
tag = InstanceTag.new(object_name, method, self, options.delete(:object))
end
tag.to_enum_radio_tag(options)
end
end
class FormBuilder
def enum_select(method, options = { })
@template.enum_select(@object_name, method, options)
end
def enum_radio(method, options = { })
@template.enum_radio(@object_name, method, options)
end
end
class InstanceTag #:nodoc:
alias __to_tag_enum to_tag
# Add the enumeration tag support. Defaults using the select tag to
# display the options.
def to_tag(options = {})
if column_type == :enum
to_enum_select_tag(options)
else
__to_tag_enum(options)
end
end
# Create a select tag and one option for each of the
# enumeration values.
def to_enum_select_tag(options = {})
# Remove when we no longer support 1.1.
begin
v = value(object)
rescue ArgumentError
v = value
end
add_default_name_and_id(options)
tag_text = "<select"
tag_text << tag_options(options)
tag_text << ">"
values = enum_values
raise ArgumentError, "No values for enum select tag" unless values
if options[:include_blank]
tag_text << "<option value=\"\"></option>\n"
end
values.each do |enum|
tag_text << "<option value=\"#{enum}\""
tag_text << ' selected="selected"' if v and v == enum
tag_text << ">#{enum}</option>"
end
tag_text << "</select>"
end
# Creates a set of radio buttons and labels.
def to_enum_radio_tag(options = {})
# Remove when we no longer support 1.1.
begin
v = value(object)
rescue ArgumentError
v = value
end
add_default_name_and_id(options)
values = enum_values
raise ArgumentError, "No values for enum select tag" unless values
tag_text = ''
template = options.dup
template.delete('checked')
values.each do |enum|
opts = template.dup
opts['checked'] = 'checked' if v and v == enum
opts['id'] = "#{opts['id']}_#{enum}"
tag_text << "<label>#{enum}: "
tag_text << to_radio_button_tag(enum, opts)
tag_text << "</label>"
end
tag_text
end
# Gets the list of values for the column.
def enum_values
object.send("column_for_attribute", @method_name).values
end
end
end
end
vendor/plugins/enum-column/lib/enum/enum_adapter.rb
# This module provides all the column helper methods to deal with the
# values and adds the common type management code for the adapters.
module ActiveRecordEnumerations
module Column
# Add the values accessor to the column class.
def self.included(klass)
klass.module_eval <<-EOE
def values; @limit; end
EOE
end
# Add the type to the native database types. This will most
# likely need to be modified in the adapter as well.
def native_database_types
types = super
types[:enum] = { :name => "enum" }
types
end
# The new constructor with a values argument.
def initialize(name, default, sql_type = nil, null = true, values = nil)
super(name, default, sql_type, null)
@limit = values if type == :enum
end
# The class for enum is Symbol.
def klass
if type == :enum
Symbol
else
super
end
end
# Convert to a symbol.
def type_cast(value)
return nil if value.nil?
if type == :enum
ActiveRecordEnumerations::Column.value_to_symbol(value)
else
super
end
end
# Code to convert to a symbol.
def type_cast_code(var_name)
if type == :enum
"ActiveRecordEnumerations::Column.value_to_symbol(#{var_name})"
else
super
end
end
# The enum simple type.
def simplified_type(field_type)
if field_type =~ /enum/i
:enum
else
super
end
end
# Safely convert the value to a symbol.
def self.value_to_symbol(value)
case value
when Symbol
value
when String
value.empty? ? nil : value.intern
else
nil
end
end
end
end
vendor/plugins/enum-column/lib/enum/mysql_adapter.rb
module ActiveRecord
module ConnectionAdapters
class MysqlAdapter
alias __native_database_types_enum native_database_types
def native_database_types #:nodoc
types = __native_database_types_enum
types[:enum] = { :name => "enum" }
types
end
def columns(table_name, name = nil)#:nodoc:
sql = "SHOW FIELDS FROM #{table_name}"
columns = []
execute(sql, name).each { |field| columns << MysqlColumnWithEnum.new(field[0], field[4], field[1], field[2] == "YES") }
columns
end
end
class MysqlColumnWithEnum < MysqlColumn
include ActiveRecordEnumerations::Column
def initialize(name, default, sql_type = nil, null = true)
if sql_type =~ /^enum/i
values = sql_type.sub(/^enum\('([^)]+)'\)/i, '\1').split("','").map { |v| v.intern }
default = default.intern if default and !default.empty?
end
super(name, default, sql_type, null, values)
end
end
end
end
vendor/plugins/enum-column/lib/enum/postgresql_adapter.rb
module ActiveRecord
module ConnectionAdapters
class PostgreSQLAdapter
alias __native_database_types_enum native_database_types
def native_database_types #:nodoc
types = __native_database_types_enum
types[:enum] = { :name => "character varying(32)" }
types
end
def columns(table_name, name = nil)#:nodoc:
column_definitions(table_name).collect do |name, type, default, notnull, consrc|
values = nil
if consrc and consrc =~ /ANY \(\(ARRAY(\[[^]]+\])/o and type == native_database_types[:enum][:name]
values = eval $1.gsub(/::character varying/, '')
type = 'enum'
end
# typmod now unused as limit, precision, scale all handled by superclass
PostgreSQLColumnWithEnum.new(name, default_value(default),
translate_field_type(type), notnull == "f",
values)
end
end
# Add constraints to the list of columns. This will only pick up check constraints.
# We'll filter the constraint for the column type and ANY ((ARRAY[ type.
def column_definitions(table_name)
query <<-end_sql
SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull, c.consrc
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
LEFT JOIN pg_constraint c ON a.attrelid = c.conrelid AND
c.contype = 'c' AND c.conkey[1] = a.attnum
WHERE a.attrelid = '#{table_name}'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
end_sql
end
def add_column_options!(sql, options)
unless sql =~ /\(32\)\('[^']+'/
super(sql, options)
else
sql.gsub!(/("[^"]+")([^3]+32\))(.+)/, '\1\2 CHECK(\1 in \3)')
super(sql, options)
end
end
end
class PostgreSQLColumnWithEnum < Column
include ActiveRecordEnumerations::Column
def initialize(name, default, sql_type = nil, null = true, values = nil)
if values
values = values.map { |v| v.intern }
default = default.intern if default and !default.empty?
end
super(name, default, sql_type, null, values)
end
end
end
end
vendor/plugins/enum-column/lib/enum/quoting.rb
module ActiveRecord
module ConnectionAdapters # :nodoc:
module Quoting
alias __quote_enum quote
# Quote a symbol as a normal string. This will support quoting of
# enumerated values.
def quote(value, column = nil)
if !value.is_a? Symbol
__quote_enum(value, column)
else
ActiveRecord::Base.send(:quote_bound_value, value.to_s)
end
end
end
end
end
vendor/plugins/enum-column/lib/enum/schema_definitions.rb
module ActiveRecord
module ConnectionAdapters
class TableDefinition
def enum(*args)
options = args.extract_options!
column_names = args
column_names.each { |name| column(name, 'enum', options) }
end
end
end
end
vendor/plugins/enum-column/lib/enum/schema_statements.rb
module ActiveRecord
module ConnectionAdapters # :nodoc:
module SchemaStatements
alias __type_to_sql_enum type_to_sql
# Add enumeration support for schema statement creation. This
# will have to be adapted for every adapter if the type requires
# anything by a list of allowed values. The overrides the standard
# type_to_sql method and chains back to the default. This could
# be done on a per adapter basis, but is generalized here.
#
# will generate enum('a', 'b', 'c') for :limit => [:a, :b, :c]
def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc:
if type == :enum
native = native_database_types[type]
column_type_sql = native[:name] || 'enum'
column_type_sql << "(#{limit.map { |v| quote(v) }.join(',')})"
column_type_sql
else
# Edge rails fallback for Rails 1.1.6. We can remove the
# rescue once everyone has upgraded to 1.2.
begin
__type_to_sql_enum(type, limit, precision, scale)
rescue ArgumentError
__type_to_sql_enum(type, limit)
end
end
end
end
end
end
vendor/plugins/enum-column/lib/enum/sqlite3_adapter.rb
module ActiveRecord
module ConnectionAdapters
class SQLite3Adapter
alias __native_database_types_enum native_database_types
def native_database_types #:nodoc
types = __native_database_types_enum
types[:enum] = { :name => "varchar(32)" }
types
end
def columns(table_name, name = nil)#:nodoc:
constraints = { }
@connection.execute "SELECT sql FROM sqlite_master WHERE name = '#{table_name}'" do |row|
sql = row[0]
sql.scan(/, \"(\w+)\" varchar\(32\) CHECK\(\"\w+\" in \(([^\)]+)\)/i) do |column, constraint|
constraints[column] = constraint
end
end
table_structure(table_name).map do |field|
name = field['name']
type = field['type']
if (const = constraints[name])
type = "enum(#{const.strip})"
end
SQLite3ColumnWithEnum.new(name, field['dflt_value'], type, field['notnull'] == "0")
end
end
def add_column_options!(sql, options)
unless sql =~ /\(32\)\('[^']+'/
super(sql, options)
else
sql.gsub!(/("[^"]+")([^3]+32\))(.+)/, '\1\2 CHECK(\1 in \3)')
super(sql, options)
end
end
end
class SQLite3ColumnWithEnum < SQLiteColumn
include ActiveRecordEnumerations::Column
def initialize(name, default, sql_type = nil, null = true)
if sql_type =~ /^enum/i
values = sql_type.sub(/^enum\('([^)]+)'\)/i, '\1').split("','").map { |v| v.intern }
default = default.intern if default and !default.empty?
end
super(name, default, sql_type, null, values)
end
end
end
end
vendor/plugins/enum-column/lib/enum/validations.rb
module ActiveRecord
module Validations
module ClassMethods
# Automatically validates the column against the schema definition
# for nullability, format, and enumerations. Handles integers, floats,
# enumerations, and string limits.
#
# Usage: validates_columns :severity, :name
def validates_columns(*column_names)
begin
cols = columns_hash
column_names.each do |name|
col = cols[name.to_s]
raise ArgumentError, "Cannot find column #{name}" unless col
# test for nullability
validates_presence_of(name) if !col.null
# Test various known types.
case col.type
when :enum
validates_inclusion_of name, :in => col.values, :allow_nil => true
when :integer, :float
validates_numericality_of name, :allow_nil => true
when :string
if col.limit
validates_length_of name, :maximum => col.limit, :allow_nil => true
end
end
end
rescue ActiveRecord::StatementInvalid=>e
raise e unless e.message.include?("42S02") # swallow the exception if its for a missing table
end
end
end
end
end
vendor/plugins/enum-column/readme.html
<html>
<head>
<title>Ruby on Rails Enumerated Column Constraints</title>
<style>
body {
font-family: "Lucida Sans", Verdana, sans-serif;
font-size: small;
}
h1 { font-size: 175%; }
.indent {
margin-left: 4em;
}
</style>
</head>
<body>
<h1>Ruby on Rails Enumerated Column Constraints</h1>
<h2>Overview</h2>
<p>This plugin is an ROR extension to support enumerations in the database schema using the enum type in MySQL or column constraints in other databases. Currently only MySQL has been implemented, but other connection adapters can easily be extended to check for column constraints and then parse out the possible values.</p>
<p>This makes it very easy to implement and maintain controlled vocabularies in the database schema without having to change the validations and the view. I have made an effort to make the modifications without modifying the core classes, but I have not been extreamly successful. I have had to make some modifications that are version dependent and then put some exception handlers to make the backward compatible. </p>
<p>This is not the ideal solution and could cause some performance issues in the helpers. The core code still works with both 1.1 and 1.2 without modification.</p>
<h2>Installation</h2>
<p>To install using the plugin script:</p>
<pre>
script/plugin install enum-column
</pre>
<p>To manually install using Subversion, do the following. In your <code>vendor/plugins</code> directory,
type the following:<p>
<pre>
svn checkout svn://rubyforge.org/var/svn/enum-column
</pre>
<p>Otherwize download the latest zip or tarball and unpack it in your <code>vendor/plugins</code> directory.</p>
<h2>How to use it.</h2>
<h3>In your schema</h3>
<p>When you create your schema, specify the constraint as a limit:</p>
<pre>
create_table :enumerations, :force => true do |t|
t.column :severity, :enum, :limit =&gt; [:low, :medium, :high, :critical],
:default => :medium
t.column :color, :enum, :limit =&gt; [:red, :blue, :green, :yellow]
...
end
</pre>
<h3>In the model</h3>
<p>You can then automatically validate this column using:</p>
<pre>
validates_columns :severity, :color
</pre>
<p>The rest will be handled for you. All enumerated values will be given as symbols.</p>
<pre>
@e = Enumeration.new
@e.severity = :low
</pre>
<h3>In the views.</h3>
<p>In the controller:</p>
<pre>
@e = Enumeration.new
</pre>
<p>The enumerates list of values will be specified as follows:</p>
<pre>
&lt;%= input 'e', 'severity' %&gt;
</pre>
<p>Will create a select/option list:</p>
<pre>
&lt;select id="e_severity" name="e[severity]"&gt;
&lt;option value="low"&gt;low&lt;/option&gt;
&lt;option value="medium" selected="selected"&gt;medium&lt;/option&gt;
&lt;option value="high"&gt;high&lt;/option&gt;
&lt;option value="critical"&gt;critical&lt;/option&gt;
&lt;/select&gt;
</pre>
<div class="indent">
<select id="e_severity" name="e[severity]">
<option value="low">low</option>
<option value="medium" selected="selected">medium</option>
<option value="high">high</option>
<option value="critical">critical</option>
</select>
</div>
<p>You can also create a set of radio buttons using the following helper:</p>
<pre>
&lt;%= enum_radio('e', 'severity') %=&gt;
</pre>
<p>Will produce the following group of radio buttons:</p>
<pre>
&lt;label&gt;low: &lt;input id="test_severity_low" name="test[severity]" type="radio" value="low" /&gt;&lt;/label&gt;
&lt;label&gt;medium: &lt;input checked="checked" id="test_severity_medium" name="test[severity]" type="radio" value="medium" /&gt;&lt;/label&gt;
&lt;label&gt;high: &lt;input id="test_severity_high" name="test[severity]" type="radio" value="high" /&gt;&lt;/label&gt;
&lt;label&gt;critical: &lt;input id="test_severity_critical" name="test[severity]" type="radio" value="critical" /&gt;&lt;/label&gt;
</pre>
<div class="indent">
<label>low: <input id="test_severity_low" name="test[severity]" type="radio" value="low" /></label>
<label>medium: <input checked="checked" id="test_severity_medium" name="test[severity]" type="radio" value="medium" /></label>
<label>high: <input id="test_severity_high" name="test[severity]" type="radio" value="high" /></label>
<label>critical: <input id="test_severity_critical" name="test[severity]" type="radio" value="critical" /></label>
</div>
<p>You can always use the column reflection to get the list of possible values from the database column. </p>
<pre>
Enumeration.columns_hash['color'].values
Will yield: [:red, :blue, :green, :yellow]
</pre>
vendor/plugins/enum-column/test/db/schema.rb
ActiveRecord::Schema.define do
create_table :enumerations, :force => true do |t|
t.column :severity, :enum, :limit => [:low, :medium, :high, :critical],
:default => :medium
t.column :color, :enum, :limit => [:red, :blue, :green, :yellow]
t.column :string_field, :string, :limit => 8, :null => false
t.column :int_field, :integer
end
create_table :basic_enums, :force => true do |t|
t.column :value, :enum, :limit => [:good, :working]
end
create_table :basic_default_enums, :force => true do |t|
t.column :value, :enum, :limit => [:good, :working],:default => :working
end
create_table :nonnull_enums, :force => true do |t|
t.column :value, :enum, :limit => [:good, :working],:null => false
end
create_table :nonnull_default_enums, :force => true do |t|
t.column :value, :enum, :limit => [:good, :working],:null => false,:default => :working
end
end
vendor/plugins/enum-column/test/enum_mysql_test.rb
require File.dirname(__FILE__) + '/test_helper'
require 'fixtures/enumeration'
class EnumerationsTest < Test::Unit::TestCase
class EnumController < ActionController::Base
def test1
@test = Enumeration.new
render :inline => "<%= input('test', 'severity')%>"
end
def test2
@test = Enumeration.new
render :inline => "<%= enum_radio('test', 'severity')%>"
end
end
def setup
Enumeration.connection.execute 'DELETE FROM enumerations'
end
def test_column_values
columns = Enumeration.columns_hash
color_column = columns['color']
assert color_column
assert_equal [:red, :blue, :green, :yellow], color_column.values
severity_column = columns['severity']
assert severity_column
assert_equal [:low, :medium, :high, :critical], severity_column.values
assert_equal :medium, severity_column.default
end
def test_insert_enum
row = Enumeration.new
row.color = :blue
row.string_field = 'test'
assert_equal :medium, row.severity
assert row.save
db_row = Enumeration.find(row.id)
assert db_row
assert :blue, row.color
assert :medium, row.severity
end
# Uses the automatic validates_columns to create automatic validation rules
# for columns based on the schema information.
def test_bad_value
row = Enumeration.new
row.color = :violet
row.string_field = 'test'
assert !row.save
assert row.errors
assert_equal 'is not included in the list', row.errors['color']
end
def test_other_types
row = Enumeration.new
row.string_field = 'a' * 10
assert !row.save
assert_equal 'is too long (maximum is 8 characters)', row.errors['string_field']
row = Enumeration.new
assert !row.save
assert_equal 'can\'t be blank', row.errors['string_field']
row = Enumeration.new
row.string_field = 'test'
row.int_field = 'aaaa'
assert !row.save
assert_equal 'is not a number', row.errors['int_field']
row = Enumeration.new
row.string_field = 'test'
row.int_field = '500'
assert row.save
end
def test_view_helper
request = ActionController::TestRequest.new
response = ActionController::TestResponse.new
request.action = 'test1'
body = EnumController.process(request, response).body
assert_equal '<select id="test_severity" name="test[severity]"><option value="low">low</option><option value="medium" selected="selected">medium</option><option value="high">high</option><option value="critical">critical</option></select>', body
end
def test_radio_helper
request = ActionController::TestRequest.new
response = ActionController::TestResponse.new
request.action = 'test2'
body = EnumController.process(request, response).body
assert_equal '<label>low: <input id="test_severity_low" name="test[severity]" type="radio" value="low" /></label><label>medium: <input checked="checked" id="test_severity_medium" name="test[severity]" type="radio" value="medium" /></label><label>high: <input id="test_severity_high" name="test[severity]" type="radio" value="high" /></label><label>critical: <input id="test_severity_critical" name="test[severity]" type="radio" value="critical" /></label>', body
end
# Basic tests
def test_create_basic_default
assert (object = BasicEnum.create)
assert_nil object.value,
"Enum columns without explicit default, default to null if allowed"
assert !object.new_record?
end
def test_create_basic_good
assert (object = BasicEnum.create(:value => :good))
assert_equal :good, object.value
assert !object.new_record?
assert (object = BasicEnum.create(:value => :working))
assert_equal :working, object.value
assert !object.new_record?
end
def test_create_basic_null
assert (object = BasicEnum.create(:value => nil))
assert_nil object.value
assert !object.new_record?
end
def test_create_basic_bad
assert (object = BasicEnum.create(:value => :bad))
assert object.new_record?
end
# Basic w/ Default
######################################################################
def test_create_basic_wd_default
assert (object = BasicDefaultEnum.create)
assert_equal :working, object.value, "Explicit default ignored columns"
assert !object.new_record?
end
def test_create_basic_wd_good
assert (object = BasicDefaultEnum.create(:value => :good))
assert_equal :good, object.value
assert !object.new_record?
assert (object = BasicDefaultEnum.create(:value => :working))
assert_equal :working, object.value
assert !object.new_record?
end
def test_create_basic_wd_null
assert (object = BasicDefaultEnum.create(:value => nil))
assert_nil object.value
assert !object.new_record?
end
def test_create_basic_wd_bad
assert (object = BasicDefaultEnum.create(:value => :bad))
assert object.new_record?
end
# Nonnull
######################################################################
def test_create_nonnull_default
assert (object = NonnullEnum.create)
# assert_equal :good, object.value,
# "Enum columns without explicit default, default to first value if null not allowed"
assert object.new_record?
end
def test_create_nonnull_good
assert (object = NonnullEnum.create(:value => :good))
assert_equal :good, object.value
assert !object.new_record?
assert (object = NonnullEnum.create(:value => :working))
assert_equal :working, object.value
assert !object.new_record?
end
def test_create_nonnull_null
assert (object = NonnullEnum.create(:value => nil))
assert object.new_record?
end
def test_create_nonnull_bad
assert (object = NonnullEnum.create(:value => :bad))
assert object.new_record?
end
# Nonnull w/ Default
######################################################################
def test_create_nonnull_wd_default
assert (object = NonnullDefaultEnum.create)
assert_equal :working, object.value
assert !object.new_record?
end
def test_create_nonnull_wd_good
assert (object = NonnullDefaultEnum.create(:value => :good))
assert_equal :good, object.value
assert !object.new_record?
assert (object = NonnullDefaultEnum.create(:value => :working))
assert_equal :working, object.value
assert !object.new_record?
end
def test_create_nonnull_wd_null
assert (object = NonnullDefaultEnum.create(:value => nil))
assert object.new_record?
end
def test_create_nonnull_wd_bad
assert (object = NonnullDefaultEnum.create(:value => :bad))
assert object.new_record?
end
def test_quoting
value = ActiveRecord::Base.send(:sanitize_sql, ["value = ? ", :"'" ] )
assert_equal "value = '\\'' ", value
end
end
vendor/plugins/enum-column/test/fixtures/db_definitions/mysql.drop.sql
DROP TABLE accounts;
DROP TABLE funny_jokes;
DROP TABLE companies;
DROP TABLE topics;
DROP TABLE developers;
DROP TABLE projects;
DROP TABLE developers_projects;
DROP TABLE customers;
DROP TABLE orders;
DROP TABLE movies;
DROP TABLE subscribers;
DROP TABLE booleantests;
DROP TABLE auto_id_tests;
DROP TABLE entrants;
DROP TABLE colnametests;
DROP TABLE mixins;
DROP TABLE people;
DROP TABLE readers;
DROP TABLE binaries;
DROP TABLE computers;
DROP TABLE tasks;
DROP TABLE posts;
DROP TABLE comments;
DROP TABLE authors;
DROP TABLE categories;
DROP TABLE categories_posts;
DROP TABLE fk_test_has_fk;
DROP TABLE fk_test_has_pk;
DROP TABLE keyboards;
DROP TABLE enumerations;
DROP TABLE legacy_things;
DROP TABLE numeric_data;
vendor/plugins/enum-column/test/fixtures/db_definitions/mysql.sql
CREATE TABLE `accounts` (
`id` int(11) NOT NULL auto_increment,
`firm_id` int(11) default NULL,
`credit_limit` int(5) default NULL,
PRIMARY KEY (`id`)
) TYPE=InnoDB;
CREATE TABLE `funny_jokes` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) default NULL,
PRIMARY KEY (`id`)
) TYPE=InnoDB;
CREATE TABLE `companies` (
`id` int(11) NOT NULL auto_increment,
`type` varchar(50) default NULL,
`ruby_type` varchar(50) default NULL,
`firm_id` int(11) default NULL,
`name` varchar(50) default NULL,
`client_of` int(11) default NULL,
`rating` int(11) default NULL default 1,
PRIMARY KEY (`id`)
) TYPE=InnoDB;
CREATE TABLE `topics` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(255) default NULL,
`author_name` varchar(255) default NULL,
`author_email_address` varchar(255) default NULL,
`written_on` datetime default NULL,
`bonus_time` time default NULL,
`last_read` date default NULL,
`content` text,
`approved` tinyint(1) default 1,
`replies_count` int(11) default 0,
`parent_id` int(11) default NULL,
`type` varchar(50) default NULL,
PRIMARY KEY (`id`)
) TYPE=InnoDB;
CREATE TABLE `developers` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) default NULL,
`salary` int(11) default 70000,
`created_at` datetime default NULL,
`updated_at` datetime default NULL,
PRIMARY KEY (`id`)
) TYPE=InnoDB;
CREATE TABLE `projects` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) default NULL,
`type` VARCHAR(255) default NULL,
PRIMARY KEY (`id`)
) TYPE=InnoDB;
CREATE TABLE `developers_projects` (
`developer_id` int(11) NOT NULL,
`project_id` int(11) NOT NULL,
`joined_on` date default NULL,
`access_level` smallint default 1
) TYPE=InnoDB;
CREATE TABLE `orders` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) default NULL,
`billing_customer_id` int(11) default NULL,
`shipping_customer_id` int(11) default NULL,
PRIMARY KEY (`id`)
) TYPE=InnoDB;
CREATE TABLE `customers` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) default NULL,
`balance` int(6) default 0,
`address_street` varchar(100) default NULL,
`address_city` varchar(100) default NULL,
`address_country` varchar(100) default NULL,
`gps_location` varchar(100) default NULL,
PRIMARY KEY (`id`)
) TYPE=InnoDB;
CREATE TABLE `movies` (
`movieid` int(11) NOT NULL auto_increment,
`name` varchar(100) default NULL,
PRIMARY KEY (`movieid`)
) TYPE=InnoDB;
CREATE TABLE `subscribers` (
`nick` varchar(100) NOT NULL,
`name` varchar(100) default NULL,
PRIMARY KEY (`nick`)
) TYPE=InnoDB;
CREATE TABLE `booleantests` (
`id` int(11) NOT NULL auto_increment,
`value` integer default NULL,
PRIMARY KEY (`id`)
) TYPE=InnoDB;
CREATE TABLE `auto_id_tests` (
`auto_id` int(11) NOT NULL auto_increment,
`value` integer default NULL,
PRIMARY KEY (`auto_id`)
) TYPE=InnoDB;
CREATE TABLE `entrants` (
`id` INTEGER NOT NULL auto_increment PRIMARY KEY,
`name` VARCHAR(255) NOT NULL,
`course_id` INTEGER NOT NULL
);
CREATE TABLE `colnametests` (
`id` int(11) NOT NULL auto_increment,
`references` int(11) NOT NULL,
PRIMARY KEY (`id`)
) TYPE=InnoDB;
CREATE TABLE `mixins` (
`id` int(11) NOT NULL auto_increment,
`parent_id` int(11) default NULL,
`pos` int(11) default NULL,
`created_at` datetime default NULL,
`updated_at` datetime default NULL,
`lft` int(11) default NULL,
`rgt` int(11) default NULL,
`root_id` int(11) default NULL,
`type` varchar(40) default NULL,
PRIMARY KEY (`id`)
) TYPE=InnoDB;
CREATE TABLE `people` (
`id` INTEGER NOT NULL auto_increment PRIMARY KEY,
`first_name` VARCHAR(40) NOT NULL,
`lock_version` INTEGER NOT NULL DEFAULT 0
) TYPE=InnoDB;
CREATE TABLE `readers` (
`id` int(11) NOT NULL auto_increment PRIMARY KEY,
`post_id` INTEGER NOT NULL,
`person_id` INTEGER NOT NULL
) TYPE=InnoDB;
CREATE TABLE `binaries` (
`id` int(11) NOT NULL auto_increment,
`data` mediumblob,
PRIMARY KEY (`id`)
) TYPE=InnoDB;
CREATE TABLE `computers` (
`id` INTEGER NOT NULL auto_increment PRIMARY KEY,
`developer` INTEGER NOT NULL,
`extendedWarranty` INTEGER NOT NULL
) TYPE=InnoDB;
CREATE TABLE `posts` (
`id` INTEGER NOT NULL auto_increment PRIMARY KEY,
`author_id` INTEGER,
`title` VARCHAR(255) NOT NULL,
`body` TEXT NOT NULL,
`type` VARCHAR(255) NOT NULL
) TYPE=InnoDB;
CREATE TABLE `comments` (
`id` INTEGER NOT NULL auto_increment PRIMARY KEY,
`post_id` INTEGER NOT NULL,
`body` TEXT NOT NULL,
`type` VARCHAR(255) NOT NULL
) TYPE=InnoDB;
CREATE TABLE `authors` (
`id` INTEGER NOT NULL auto_increment PRIMARY KEY,
`name` VARCHAR(255) NOT NULL
) TYPE=InnoDB;
CREATE TABLE `tasks` (
`id` int(11) NOT NULL auto_increment,
`starting` datetime NOT NULL default '0000-00-00 00:00:00',
`ending` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) TYPE=InnoDB;
CREATE TABLE `categories` (
`id` int(11) NOT NULL auto_increment,
`name` VARCHAR(255) NOT NULL,
`type` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
) TYPE=InnoDB;
CREATE TABLE `categories_posts` (
`category_id` int(11) NOT NULL,
`post_id` int(11) NOT NULL
) TYPE=InnoDB;
CREATE TABLE `fk_test_has_pk` (
`id` INTEGER NOT NULL auto_increment PRIMARY KEY
) TYPE=InnoDB;
CREATE TABLE `fk_test_has_fk` (
`id` INTEGER NOT NULL auto_increment PRIMARY KEY,
`fk_id` INTEGER NOT NULL,
FOREIGN KEY (`fk_id`) REFERENCES `fk_test_has_pk`(`id`)
) TYPE=InnoDB;
CREATE TABLE `keyboards` (
`key_number` int(11) NOT NULL auto_increment primary key,
`name` varchar(50) default NULL
);
<<<<<<< .mine
CREATE TABLE `enumerations` (
`id` INTEGER NOT NULL auto_increment PRIMARY KEY,
`severity` enum('low', 'medium', 'high', 'critical') DEFAULT 'medium',
`color` enum('red', 'blue', 'green', 'yellow'),
`string_field` varchar(8) NOT NULL,
`int_field` int(11)
) TYPE=InnoDB;
--Altered lock_version column name.
=======
-- Altered lock_version column name.
>>>>>>> .r3595
CREATE TABLE `legacy_things` (
`id` int(11) NOT NULL auto_increment,
`tps_report_number` int(11) default NULL,
`version` int(11) NOT NULL default 0,
PRIMARY KEY (`id`)
) TYPE=InnoDB;
<<<<<<< .mine
=======
CREATE TABLE `numeric_data` (
`id` INTEGER NOT NULL auto_increment PRIMARY KEY,
`bank_balance` decimal(10,2),
`big_bank_balance` decimal(15,2),
`world_population` decimal(10),
`my_house_population` decimal(2),
`decimal_number_with_default` decimal(3,2) DEFAULT 2.78
) TYPE=InnoDB;
>>>>>>> .r4885
vendor/plugins/enum-column/test/fixtures/enumeration.rb
class Enumeration < ActiveRecord::Base
validates_columns :color, :severity, :string_field, :int_field
end
class BasicEnum < ActiveRecord::Base
validates_columns :value
end
class BasicDefaultEnum < ActiveRecord::Base
validates_columns :value
end
class NonnullEnum < ActiveRecord::Base
validates_columns :value
end
class NonnullDefaultEnum < ActiveRecord::Base
validates_columns :value
end
vendor/plugins/enum-column/test/test_helper.rb
# =============================================================================
# Include the files required to test Engines.
# Load the default rails test helper - this will load the environment.
require File.dirname(__FILE__) + '/../../../../test/test_helper'
plugin_path = File::dirname(__FILE__) + '/..'
schema_file = plugin_path + "/test/db/schema.rb"
load(schema_file) if File.exist?(schema_file)
# set up the fixtures location to use your engine's fixtures
fixture_path = File.dirname(__FILE__) + "/fixtures/"
Test::Unit::TestCase.fixture_path = fixture_path
$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
$LOAD_PATH.unshift(File.dirname(__FILE__))
# =============================================================================
(1-1/7)