Skip to content

Commit c708911

Browse files
committed
Merge branch '2.0.0'
2 parents c80a6dc + edb0bbc commit c708911

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+742
-1018
lines changed

.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: ruby
2+
rvm:
3+
- 1.8.7
4+
- 1.9.3
5+
- rbx-18mode
6+
- rbx-19mode
7+
env:
8+
- "rake=0.8"
9+
- "rake=0.9"
10+
script: "bundle exec rspec -t no_ad"
11+
notifications:
12+
email: false

Gemfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
adauth (1.2.1)
4+
adauth (2.0.0pre)
55
net-ldap
66

77
GEM
@@ -15,9 +15,9 @@ GEM
1515
rspec-expectations (~> 2.11.0)
1616
rspec-mocks (~> 2.11.0)
1717
rspec-core (2.11.1)
18-
rspec-expectations (2.11.1)
18+
rspec-expectations (2.11.2)
1919
diff-lcs (~> 1.1.3)
20-
rspec-mocks (2.11.1)
20+
rspec-mocks (2.11.2)
2121

2222
PLATFORMS
2323
ruby

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'rubygems'
12
require 'bundler'
23

34
Bundler::GemHelper.install_tasks

Readme.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Adauth
2+
[RDoc](http://rubydoc.info/github/Arcath/Adauth/master/frames) | [www](http://adauth.arcath.net) | [Gempage](http://rubygems.org/gems/adauth) | [![Status](https://secure.travis-ci.org/Arcath/Adauth.png?branch=master)](http://travis-ci.org/Arcath/Adauth)
3+
4+
Easy to use Active Directory Authentication for Rails.
5+
6+
## Install
7+
8+
Add the Adauth gem to your Gemfile:
9+
10+
gem 'adauth'
11+
12+
and run a bundle install
13+
14+
## Usage
15+
16+
First off create a new config file by running the config generator
17+
18+
rails g adauth:config
19+
20+
Fill out the config values in _config/initializers/adauth.rb_
21+
22+
### Joining a model to Adauth
23+
24+
If you want to link your user model to Adauth you can use this simple code:
25+
26+
class User < ActiveRecord::Base
27+
include Adauth::Rails::ModelBridge
28+
29+
AdauthMappings = {
30+
:login => :login
31+
:group_strings => :cn_groups
32+
}
33+
34+
AdauthSearchField = [:login, :login]
35+
end
36+
37+
This gives you a bridge between Adauth and your model. When you call `User.create_from_adauth(adauth_model)` it does:
38+
39+
u = User.new
40+
u.login = adauth_model.login
41+
u.group_strings = adauth_model.cn_groups
42+
u.save
43+
44+
This can be used for any model and anything that you pull over through adauth.
45+
46+
### SessionsController
47+
48+
TODO

Readme.rdoc

Lines changed: 0 additions & 66 deletions
This file was deleted.

adauth.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# -*- encoding: utf-8 -*-
1+
# -*- encoding: utf-8 -*-
22
$:.push File.expand_path("../lib", __FILE__)
33
require 'adauth/version'
44

@@ -19,4 +19,4 @@ Gem::Specification.new do |s|
1919
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
2020
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
2121
s.require_paths = ["lib"]
22-
end
22+
end

lib/adauth.rb

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,53 @@
1+
# Requires
12
require 'net/ldap'
23
require 'timeout'
4+
# Version
35
require 'adauth/version'
4-
require 'adauth/user'
6+
# Classes
7+
require 'adauth/ad_object'
8+
require 'adauth/authenticate'
59
require 'adauth/config'
6-
require 'adauth/helpers'
710
require 'adauth/connection'
8-
require 'adauth/group'
9-
require 'adauth/admin_connection'
10-
require 'adauth/authenticate'
11-
require 'adauth/user_model'
11+
# AdObjects
12+
require 'adauth/ad_objects/computer'
13+
require 'adauth/ad_objects/group'
14+
require 'adauth/ad_objects/ou'
15+
require 'adauth/ad_objects/user'
16+
# Rails
17+
require 'adauth/rails'
18+
require 'adauth/rails/helpers'
19+
require 'adauth/rails/model_bridge'
1220

13-
# The top level module
14-
#
15-
# For Adauths documentation please see the github wiki.
21+
# Adauth Container Module
1622
module Adauth
17-
18-
# Used to configure Adauth
19-
#
20-
# Called as
21-
# Adauth.configure do |c|
22-
# c.foo = "bar"
23-
# end
24-
#
25-
# Configures Adauth and is required for Adauth to work.
23+
# Yields a new config object and then sets it as the Adauth Config
2624
def self.configure
27-
@config = Config.new
28-
yield(@config)
25+
@config = Config.new
26+
yield(@config)
27+
end
28+
29+
# Returns Adauths current connection to ActiveDirectory
30+
def self.connection
31+
raise "Adauth needs configuring before use" if @config == nil
32+
connect unless @connection
33+
@connection
2934
end
3035

31-
# Returns the config object
32-
#
33-
# Allows access to the adauth config object so you can call the config values in your application
34-
def self.config
35-
@config
36+
# Connects to ActiveDirectory using the query user details
37+
def self.connect
38+
@connection = Adauth::Connection.new(connection_hash(@config.query_user, @config.query_password)).bind
3639
end
3740

38-
# Rails generators
39-
module Generators
41+
# Generates a hash for the connection class, takes a username and password
42+
def self.connection_hash(user, password)
43+
{
44+
:domain => @config.domain,
45+
:server => @config.server,
46+
:port => @config.port,
47+
:base => @config.base,
48+
:encryption => @config.encryption,
49+
:username => user,
50+
:password => password
51+
}
4052
end
41-
end
53+
end

lib/adauth/ad_object.rb

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
module Adauth
2+
# Active Directory Interface Object
3+
#
4+
# Objects inherit from this class.
5+
#
6+
# Provides all the common functions for Active Directory.
7+
class AdObject
8+
# Returns all objects which have the ObjectClass of the inherited class
9+
def self.all
10+
results = []
11+
Adauth.connection.search(:filter => self::ObjectFilter).each do |result|
12+
results.push self.new(result)
13+
end
14+
results
15+
end
16+
17+
# Returns all the objects which match the supplied query
18+
#
19+
# Uses ObjectFilter to restrict to the current object
20+
def self.where(field, value)
21+
results = []
22+
search_filter = Net::LDAP::Filter.eq(field, value)
23+
joined_filter = search_filter & self::ObjectFilter
24+
Adauth.connection.search(:filter => joined_filter).each do |result|
25+
results.push self.new(result)
26+
end
27+
results
28+
end
29+
30+
# Creates a new instance of the object and sets @ldap_object to the passed Net::LDAP entity
31+
def initialize(ldap_object)
32+
@ldap_object = ldap_object
33+
end
34+
35+
# Allows direct access to @ldap_object
36+
def ldap_object
37+
@ldap_object
38+
end
39+
40+
# Over rides method_missing and interacts with @ldap_object
41+
def method_missing(method, *args)
42+
if self.class::Fields.keys.include?(method)
43+
field = self.class::Fields[method]
44+
if field.is_a? Symbol
45+
return @ldap_object.send(field).to_s
46+
elsif field.is_a? Array
47+
@ldap_object.send(field.first).collect(&field.last)
48+
end
49+
else
50+
super
51+
end
52+
end
53+
54+
# Returns all the groups the object is a member of
55+
def groups
56+
unless @groups
57+
@groups = convert_to_objects(cn_groups)
58+
end
59+
@groups
60+
end
61+
62+
# Returns all the ous the object is in
63+
def ous
64+
unless @ous
65+
@ous = []
66+
@ldap_object.dn.split(/,/).each do |entry|
67+
@ous.push Adauth::AdObjects::OU.where('name', entry.gsub(/OU=/, '')).first if entry =~ /OU=/
68+
end
69+
end
70+
@ous
71+
end
72+
73+
# CSV Version of the ous list (can't be pulled over from AD)
74+
def dn_ous
75+
unless @dn_ous
76+
@dn_ous = []
77+
@ldap_object.dn.split(/,/).each do |entry|
78+
@dn_ous.push entry.gsub(/OU=/, '').gsub(/CN=/,'') if entry =~ /OU=/ or entry == "CN=Users"
79+
end
80+
end
81+
@dn_ous
82+
end
83+
84+
private
85+
86+
def convert_to_objects(array)
87+
out = []
88+
array.each do |entity|
89+
out.push convert_to_object(entity)
90+
end
91+
out
92+
end
93+
94+
def convert_to_object(entity)
95+
user = Adauth::AdObjects::User.where('sAMAccountName', entity).first
96+
group = Adauth::AdObjects::Group.where('sAMAccountName', entity).first
97+
(user || group)
98+
end
99+
end
100+
101+
# Container for Objects which inherit from Adauth::AdObject
102+
module AdObjects
103+
end
104+
end

lib/adauth/ad_objects/computer.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Adauth
2+
module AdObjects
3+
# Active Directory Computer Object
4+
#
5+
# Inherits from Adauth::AdObject
6+
class Computer < Adauth::AdObject
7+
# Field mapping
8+
#
9+
# Maps methods to LDAP fields e.g.
10+
#
11+
# :foo => :bar
12+
#
13+
# Becomes
14+
#
15+
# Computer.name
16+
#
17+
# Which calls .name on the LDAP object
18+
Fields = {
19+
:name => :name
20+
}
21+
22+
# Object Net::LDAP filter
23+
#
24+
# Used to restrict searches to just this object
25+
ObjectFilter = Net::LDAP::Filter.eq("objectClass", "computer")
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)