Skip to content

Commit

Permalink
minor changes in ldap code, mostly formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mackuba committed Jan 17, 2011
1 parent 12071b1 commit 7823f4d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
41 changes: 21 additions & 20 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class User
property :id, Serial
property :name, String, :required => true
property :type, Discriminator, :index => true
property :login, String, :required => true, :index => true, :format => LOGIN_REGEXP, :unique => true
property :login, String, :required => true, :index => true,
:format => LOGIN_REGEXP, :unique => true
property :ldap_login, String, :format => LOGIN_REGEXP, :unique => true, :index => true
property :email, String, :required => true, :format => :email_address
property :active, Boolean, :required => true, :default => true
Expand Down Expand Up @@ -72,24 +73,8 @@ def self.with_activities_for_client(client)
active.all('activities.project.client_id' => client.id, :unique => true)
end

def recent_projects
self.projects.active.sort_by { |p| self.last_activity_in_project(p).date }.reverse.first(RECENT_ACTIVITIES_NUM)
end

def last_activity_in_project(project)
self.activities.first(:project_id => project.id, :order => [:date.desc])
end

def authenticated?(password)
crypted_password == encrypt(password) && active
end

def authenticated_with_ldap?(login, password)
Auth::LDAP.authenticate(login, password) && active
end

def self.authenticate(login, password)
u = User.all(:login => login).first
u = User.first(:login => login)
if u && u.authenticated?(password)
u
else
Expand All @@ -98,7 +83,7 @@ def self.authenticate(login, password)
end

def self.authenticate_with_ldap(login, password)
u= User.all(:ldap_login => login).first
u = User.first(:ldap_login => login)
u && u.authenticated_with_ldap?(login, password)? u : nil
end

Expand All @@ -108,7 +93,23 @@ def self.authenticate_with_token(token)
user.remember_me_token_expiration > DateTime.now ? user : nil
end
end


def recent_projects
self.projects.active.sort_by { |p| self.last_activity_in_project(p).date }.reverse.first(RECENT_ACTIVITIES_NUM)
end

def last_activity_in_project(project)
self.activities.first(:project_id => project.id, :order => [:date.desc])
end

def authenticated?(password)
active && crypted_password == encrypt(password)
end

def authenticated_with_ldap?(login, password)
active && Auth::LDAP.authenticate(login, password)
end

def is_admin?
!self.is_client_user? && self.admin?
end
Expand Down
24 changes: 11 additions & 13 deletions lib/auth/ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@
module Auth
module LDAP

LDAP_CONFIG_FILE=File.join(Merb.root, 'config', 'ldap.yml')
LDAP_CONFIG_FILE = File.join(Merb.root, 'config', 'ldap.yml')

def self.isLDAP?
File.exist?(LDAP_CONFIG_FILE)

end

def self.settings
@settings ||=
YAML.load_file(LDAP_CONFIG_FILE)
@settings ||= YAML.load_file(LDAP_CONFIG_FILE)
end


def self.authenticate(login, password)
ldap = Net::LDAP.new({
:host => settings[:host],
:base => settings[:base],
:port => settings[:port],
:auth => {
:method => :simple,
:username => "#{settings[:attr]}=#{login},#{settings[:base]}",
:password => password
}})
:host => settings[:host],
:base => settings[:base],
:port => settings[:port],
:auth => {
:method => :simple,
:username => "#{settings[:attr]}=#{login},#{settings[:base]}",
:password => password
}})
ldap.bind
end

Expand Down

0 comments on commit 7823f4d

Please sign in to comment.