Skip to content

Instantly share code, notes, and snippets.

@akinsgre
Created April 20, 2014 05:26
Show Gist options
  • Save akinsgre/11105858 to your computer and use it in GitHub Desktop.
Save akinsgre/11105858 to your computer and use it in GitHub Desktop.
class Employee < User
set_primary_key :employee_id
has_one :user, :foreign_key => :employee_id
has_many :goals, :conditions => ["goals.year = ?", Phase.year]
has_many :approved_goals, :class_name => "Goal" , :conditions => ["state_id in (?) and goals.year = ?", State.approved_states, Phase.year], :order => :title
has_many :employee_pending_goals, :class_name => "Goal" , :conditions => ["state_id in (?) and goals.year = ?", State.employee_pending_states, Phase.year], :order => :title
has_many :supervisor_pending_goals, :class_name => "Goal" , :conditions => ["state_id in (?) and goals.year =?", State.supervisor_pending_states, Phase.year], :order => :title
has_many :abandoned_goals, :class_name => "DeletedGoal", :conditions => ["deleted_goals.year = ?", Phase.year], :conditions => ["updated_at >= ?", Phase.find_by_description("Phase II").date], :order => :title
has_many :assessment, :conditions => ["assessments.year = ?", Phase.year]
has_many :evaluation
has_many :review, :conditions => ["supervisor_id is null and reviews.year = ?", Phase.year]
has_many :supervisor_review, :class_name => 'Review', :conditions => ["supervisor_id is not null and reviews.year = ?", Phase.year]
has_one :career_objective
has_many :career_office_relocation
has_one :user_option, :dependent => :destroy
has_one :supervisor_option, :dependent => :destroy
has_many :comment
has_many :secondary_supervisor, :dependent => :destroy
has_many :custom_group_membership, :dependent => :destroy
has_many :successions, :class_name => 'Succession', :foreign_key => :replacement_employee_id, :order => :succession_range_id
has_many :successors, :class_name => 'Succession', :order => :succession_range_id
alias :raw_successions :successions
def successions(manager_id = nil)
grouped_successions = Hash.new
successions = manager_id.nil? ? raw_successions : raw_successions.find_all_by_manager_id(manager_id)
successions.each do |s|
grouped_successions[s.succession_range] = grouped_successions[s.succession_range].to_a.concat [s]
end
grouped_successions
end
alias :raw_successors :successors
def successors(manager_id)
grouped_successors = Hash.new
raw_successors.find_all_by_manager_id(manager_id).each do |s|
grouped_successors[s.succession_range] = grouped_successors[s.succession_range].to_a.concat [s]
end
grouped_successors
end
def current_year_eval
self.evaluation.find(:all, :conditions => {:year => Phase.year})
end
# Set the current Account State
def change_account_state(state_id)
self.account_state = AccountState.find(state_id)
self.save
end
# Return all of the goals for a user
def historic_goals
Goal.find_all_by_employee_id(self.employee_id)
end
# Returns the current year's goals - Saved for future use.
def current_year_goals
self.goals.find(:all, :conditions => {:year => Phase.year})
#Goal.find_all_by_employee_id_and_year(self.employee_id, Phase.year)
end
# Returns the number of goals for a given year
def goals_for_year(year)
goals = Goal.find_all_by_employee_id_and_year(self.employee_id, year)
goals.count
end
# Finds the years in which a user participated in gaimplan by searching for their goals
# and collecting all the goal.year into an array.
def find_years
goals = self.historic_goals
years = []
i = 0
goals.each do |goal|
years[i] = goal.year
i = i + 1
end
years.uniq
end
# Returns a hash where the keys are each year an employee participated in GAIMPlan and
# the values are the number of goals for that year
def goals_per_year
years = self.find_years
goals_per_year_var = {}
years.each do |year|
goals_per_year_var[year] = self.goals_for_year(year)
end
return goals_per_year_var
end
# Returns the assessments for the current year - Saved for future use
def current_year_assessments
self.assessments.find(:all, :conditions => {:year => Phase.year})
end
# Returns the employee pending goals for the current year - Saved for future use
def current_year_employee_pending_goals
self.employee_pending_goals.find(:all, :conditions => {:year => Phase.year})
end
# Returns the supervisor pending goals for the current year - Saved for future use
def current_year_supervisor_pending_goals
self.supervisor_pending_goals.find(:all, :conditions => {:year => Phase.year})
end
# Returns the approved goals for the current year - Saved for future use
def current_year_approved_goals
self.approved_goals.find(:all, :conditions => {:year => Phase.year})
end
def current_year_eval_state
self.current_year_eval.length == 0 ? "Not Started" : self.current_year_eval.first.state.description
end
def current_eval_items(type)
unless self.current_year_eval.length == 0
if type.nil?
self.current_year_eval.first.evaluation_item
else
self.current_year_eval.first.evaluation_item.find(:all, :conditions => {:evaluation_type_id => EvaluationType.find_by_description(type).id})
end
else
return []
end
end
def admin_goalassessments_by_states(states)
assessments = self.assessment.find(:all, :include => :goal,
:conditions => ["assessments.source_id = ? AND assessments.state_id in (?) AND goals.id IS NOT NULL", Source.find_by_description("Goal"), states],
:order => 'goals.title')
self.admin_goals.each do |a|
ga = self.assessment.find(:first, :conditions => ["source_id = ? AND goal_id = ? and state_id in (?)",
Source.find_by_description("AdminGoal"), a.id, states])
assessments << ga unless ga.nil? #if self.assessment.exists?(["source_id = ? AND goal_id = ? and state_id in (?)",
# Source.find_by_description("AdminGoal"), a.id, State.supervisor_pending_states])
end
assessments
end
def supervisor_pending_assessments
admin_goalassessments_by_states(State.supervisor_pending_states)
end
def completed_assessments
admin_goalassessments_by_states(State.approved_states)
end
def employee_pending_assessments
emp_pending_goals = self.goals.find(:all, :include => :assessment,
:conditions => ["(assessments.state_id in (?) OR assessments.state_id is null) AND goals.state_id in (?)",
State.employee_pending_states, State.approved_states],
:order => 'goals.title')
emp_pending = Array.new
emp_pending_goals.each do |g|
if g.assessment.nil?
a = Assessment.new
a.goal_id = g.id
a.source_id = Source.find_by_description(g.class.to_s).id
emp_pending << a
else
emp_pending << g.assessment
end
end
self.admin_goals.each do |a|
ga = self.assessment.find(:first, :conditions => ["source_id = ? AND goal_id = ? and (state_id in (?) OR state_id is null)",
Source.find_by_description("AdminGoal"), a.id, State.employee_pending_states])
if self.assessment.exists?(["source_id = ? AND goal_id = ? and state_id in (?)",
Source.find_by_description("AdminGoal"),
a.id,
State.approved_states.concat(State.supervisor_pending_states)])
elsif ga.nil?
as = Assessment.new
as.goal_id = a.id
as.source_id = Source.find_by_description(a.class.to_s).id
emp_pending << as
else
emp_pending << ga unless emp_pending.include?(ga)
end
end
emp_pending
end
def admin_goalreviews_by_states(states)
reviews = self.review.find(:all, :include => :goal,
:conditions => ["reviews.source_id = ? AND reviews.state_id in (?) AND goals.id IS NOT NULL", Source.find_by_description("Goal"), states],
:order => 'goals.title')
self.admin_goals.each do |a|
ga = self.review.find(:first, :conditions => ["source_id = ? AND goal_id = ? and state_id in (?)",
Source.find_by_description("AdminGoal"), a.id, states])
reviews << ga unless ga.nil? #if self.review.exists?(["source_id = ? AND goal_id = ? and state_id in (?)",
# Source.find_by_description("AdminGoal"), a.id, State.supervisor_pending_states])
end
reviews
end
def supervisor_pending_reviews
admin_goalreviews_by_states(State.supervisor_pending_states)
end
def completed_reviews
admin_goalreviews_by_states(State.approved_states)
end
def employee_pending_reviews
emp_pending_goals = self.goals.find(:all, :include => :review,
:conditions => ["(reviews.state_id in (?) OR reviews.state_id is null) AND goals.state_id in (?)",
State.employee_pending_states, State.approved_states],
:order => 'goals.title')
emp_pending = Array.new
emp_pending_goals.each do |g|
if g.review.nil?
a = Review.new
a.goal_id = g.id
a.source_id = Source.find_by_description(g.class.to_s).id
emp_pending << a
else
emp_pending << g.review
end
end
self.admin_goals.each do |a|
ga = self.review.find(:first, :conditions => ["source_id = ? AND goal_id = ? and (state_id in (?) OR state_id is null)",
Source.find_by_description("AdminGoal"), a.id, State.employee_pending_states])
if self.review.exists?(["source_id = ? AND goal_id = ? and state_id in (?)",
Source.find_by_description("AdminGoal"),
a.id,
State.approved_states.concat(State.supervisor_pending_states)])
elsif ga.nil?
as = Review.new
as.goal_id = a.id
as.source_id = Source.find_by_description(a.class.to_s).id
emp_pending << as
else
emp_pending << ga unless emp_pending.include?(ga)
end
end
emp_pending
end
def custom_groups
self.custom_group_membership.collect {|c| c.custom_group_id}
end
def secondary_supervisors
self.secondary_supervisor.collect {|s| s.supervisor_id}
end
def load_relationships
self.supervisor
self.employees
end
def supervisor_override
User.find_employee(SupervisorOverride.find_by_employee_id(self.employee_id).supervisor_id).employee unless SupervisorOverride.find_by_employee_id(self.employee_id).nil? || SupervisorOverride.find_by_employee_id(self.employee_id).supervisor_id.blank?
end
def supervisor
supervisor = supervisor_override || User.find_employee(self.ivantage_employee.SupervisorEmpID).employee
SupervisorOption.find_or_create_by_employee_id(supervisor.employee_id) unless supervisor.nil?
supervisor
end
def supervisors
supervisors = Array.new
begin
supervisors << supervisor
rescue
end
supervisors = supervisors.concat SecondarySupervisor.find_all_by_employee_id(self.employee_id).collect {|s| s.supervisor}
end
def employees
employees = Array.new
IvantageEmployee.find_all_by_SupervisorEmpID(self.employee_id, :order => :EmployeeFullName).each do |e|
begin
employees << User.find_employee(e.EMPNO).employee if e.employee.supervisor_override.nil?
UserOption.find_or_create_by_employee_id(e.EMPNO)
rescue
end
end
employees = employees.concat SupervisorOverride.find_all_by_supervisor_id(self.employee_id).collect {|s| s.employee unless s.employee.ivantage_employee.nil?}.compact
employees = employees.concat SecondarySupervisor.find_all_by_supervisor_id(self.employee_id).collect {|s| s.employee unless s.employee.ivantage_employee.nil?}.compact
employees.compact
end
def retirement_employees
retirement_age = SuccessionPlanConfig.find_by_attribute_name("Succession Planning Age").value.to_i
employees = Array.new
employees.concat descendants.collect{|e| e.employee if !e.nil? and e.BirthDate <= retirement_age.years.ago}.compact
employees.concat SupervisorOverride.find_all_by_supervisor_id(self.employee_id).collect {|s| s.employee if s.employee.ivantage_employee.nil? and s.employee.age >= retirement_age}
employees.concat SecondarySupervisor.find_all_by_supervisor_id(self.employee_id).collect {|s| s.employee if s.employee.ivantage_employee.nil? and s.employee.age >= retirement_age}
end
# Employees by activity
# Returns a has where the keys are 'Active' and 'Inactive' and the values are
# Arrays of users who are active or inactive
def employees_by_activity
employees = self.employees
@employees_by_activity = {'active' => [], 'inactive' => []}
for e in employees
if e.account_state == AccountState.find(1)
@employees_by_activity['active'].push(e)
else
@employees_by_activity['inactive'].push(e)
end
end
@employees_by_activity
end
def is_department_head
!self.ivantage_employee.nil? && self.ivantage_employee.ancestors.first.DepartmentCode != self.ivantage_employee.DepartmentCode && self.employees.length > 0
end
def is_section_head
!self.ivantage_employee.nil? && self.ivantage_employee.ancestors.first.SectionCode != self.ivantage_employee.SectionCode && self.employees.length > 0
end
def is_company_head
!self.ivantage_employee.nil? && self.ivantage_employee.ancestors.first.nil? && self.employees.length > 0
end
def admin_goals
@admin_goals = Array.new
OrganizationType.find(:all).each do |type|
@admin_goals.concat AdminGoal.find_all_by_organization_type_id_and_specifier_and_year(
OrganizationType.find_by_description(type.description).id,
(self.ivantage_employee.has_attribute?(type.employee_field_code) ?
self.ivantage_employee.attributes[type.employee_field_code] :
(eval "self.#{type.employee_class.tableize}")),
Phase.year
)
end
@admin_goals.compact
end
def visions
@visions = Array.new
OrganizationType.find(:all).each do |type|
@visions.concat CompanyVision.find_all_by_organization_type_id_and_vision_specifier_and_year(
OrganizationType.find_by_description(type.description).id,
(self.ivantage_employee.has_attribute?(type.employee_field_code) ?
self.ivantage_employee.attributes[type.employee_field_code] :
(eval "self.#{type.employee_class.tableize}")),
Phase.year
)
end
@visions.compact
end
def employee_specifiers
es = Hash.new
OrganizationType.find(:all).each do |type|
es[type.description] = (self.ivantage_employee.has_attribute?(type.employee_field_code) ?
self.ivantage_employee.attributes[type.employee_field_code] :
(eval "self.#{type.employee_class.tableize}"))
end
es
end
def employee_specifier_string
specifiers = []
self.employee_specifiers.each {|t,s| specifiers << "#{t}:#{s}" unless s.blank?}
specifiers.join(',').gsub(/\s/,'_')
end
def self.search_employees(attribute, value)
Employee.find(:all, :conditions => ["#{attribute} like ?", "#{value}%"])
end
def phase_ii_exempt?
DateTime.parse(self.created_at.to_s) >= Phase.phase_ii_cutoff
end
def photo
base_dir = Pathname.new("#{RAILS_ROOT}/public")
names = self.full_name.gsub(/,/,'').split(' ')
photos = Photo.find("thumb*" + names[0] + "*" + names[1] + "*")
photos = photos.empty? ? Photo.find("thumb*" + names[0] + "*" + self.ivantage_employee.EmployeeNickname + "*") : photos
photos.empty? ? '/images/Photo_Not_Available.JPG' : '/' + Pathname.new(photos.last).relative_path_from(base_dir)
end
def resume
base_dir = Pathname.new("#{RAILS_ROOT}/public")
names = self.display_name.gsub(/,/,'').split(' ')
resumes = Resume.find("*" + names[1] + "*" + names[0] + "*")
if resumes.empty?
names = self.full_name.gsub(/,/,'').split(' ')
resumes = Resume.find("*" + names[0] + "*" + names[1] + "*")
end
resumes.empty? ? '' : '/' + Pathname.new(resumes[0]).relative_path_from(base_dir)
end
def resumeText
Resume.resumeText(self.resume)
end
def resumeFieldText(heading)
Resume.resumeText(self.resume)[heading].to_a
end
def age
((Time.now - self.ivantage_employee.BirthDate) / 60 /60 / 24 / 365).to_i
end
def descendants_slow
emps = self.employees.dup
emps.each {|child| emps.concat(child.descendants)}
emps
end
def descendants
@ivantage_employees = IvantageEmployee.find(:all)#, :select => "EMPNO, SupervisorEmpID, BirthDate")
get_descendants(self.ivantage_employee)
end
def get_descendants(manager)
emps = @ivantage_employees.select {|v| v.SupervisorEmpID == manager.EMPNO}
emps.each {|iv_emp| emps.concat(get_descendants(iv_emp))}
emps
end
end
class Goal < ActiveRecord::Base
belongs_to :goal_type
belongs_to :state
belongs_to :employee
belongs_to :source
has_many :comment, :order => :created_at, :conditions => ["phase_id = ?", Phase.find_by_description("Phase I").id]
has_many :goal_attachment
has_many :changed_goal
has_one :assessment, :dependent => :destroy
has_one :review, :conditions => {:supervisor_id => nil}, :dependent => :destroy
has_one :supervisor_review, :class_name => 'Review', :conditions => ["supervisor_id is not null"]
before_save :check_changes
before_save :set_restore_id
before_update :write_changed
after_save :notify_employee
before_destroy :write_deleted
attr_accessor :restore_id
def set_restore_id
self.id = self.restore_id unless self.restore_id.nil?
end
def title
read_attribute(:title).blank? ? '[ #' + self.id.to_s + ' ]' : read_attribute(:title)
end
def check_changes
return if self.id.nil? || self.state_id.nil? || self.state_id == 0
old_state_id = Goal.find(self.id).state_id
StateChange.create(
:employee_id => User.current_user.employee_id,
:goal_id => self.id,
:old_state_id => old_state_id,
:new_state_id => self.state_id
) if old_state_id != self.state_id && old_state_id.to_i != 0 && !User.current_user.nil?
self.assessment.change_state("Further Review") unless Phase.current.id < 2 || self.assessment.nil?
true
end
def change_state(state_description)
self.state_id = State.find_by_description(state_description).id
self.save
end
def notify_employee
return if self.id.nil? || self.state_id.nil? || self.state_id == 0
message = "According to your notification options, you have chosen to receive notifications for added/deleted goals."
message += " To change your notification options, visit GAIM Plan and choose the Notification Settings option"
message += " from the Supervisor Options section of the left navigation menu."
UserNotify.deliver_daily_notification(self.employee.supervisor, message, [], [], [self], [], [], [], []) if !self.employee.supervisor.nil? && self.employee.supervisor.supervisor_option.add_delete_goals_flag && !self.description.blank?
end
def write_deleted
return if self.state_id == 0
deleted_goal = DeletedGoal.new(self.attributes)
deleted_goal.goal_id = self.id
deleted_goal.save
end
def write_changed
if Phase.current.id > 1
changed_goal = ChangedGoal.new(Goal.find(self.id).attributes)
changed_goal.goal_id = self.id
changed_goal.save
end
end
end
require 'digest/sha1'
require 'ldap'
class User < ActiveRecord::Base
belongs_to :account_state
belongs_to :perm
belongs_to :ivantage_employee, :foreign_key => :employee_id
belongs_to :ivantage_employee_ex, :foreign_key => :employee_id
belongs_to :employee, :foreign_key => :employee_id
before_create :check_group_membership
after_save :load_options
before_destroy :remove_options
cattr_accessor :current_user
# has_and_belongs_to_many :inactivities
def user
self
end
def remove_options
self.employee.user_option.destroy unless Employee.count(self.employee_id) > 1
end
def load_options
UserOption.find_or_create_by_employee_id(self.employee_id)
SupervisorOption.find_or_create_by_employee_id(self.employee_id)
end
def check_group_membership
# self.perm_id = Perm.find_by_description("Accounting").id if self.is_member_of('Company_Security') and self.perm_id != Perm.find_by_description("Administrator").id
self.perm_id = Perm.find_by_description("Administrator").id if self.has_membership(""Company_ecurity") || self.has_membership("GompanyIT_IvantageAdmins_Security")
end
def self.find_employee(employee_id)
self.find_by_employee_id(employee_id) || self.find_by_employee_id(employee_id.to_i) || (self.exists_in_ad(employee_id) ? self.find_in_ad(employee_id) : nil) || self.find_in_vision(employee_id) || User.new
end
def self.find_in_vision(employee_id)
employee_id = "%05d" % employee_id.to_i
vision_user = VisionEmployee.find_by_Employee(employee_id)
ivantage_user = IvantageEmployee.find_by_EMPNO(employee_id)
if User.exists?(:employee_id => employee_id)
User.find_by_employee_id(employee_id)
elsif !vision_user.nil? && !ivantage_user.nil?
user = User.new
user.employee_id = employee_id
user.login = vision_user.Username
user.display_name = ivantage_user.EmployeeDisplayName
user.full_name = ivantage_user.EmployeeFullName
user.given_name = ivantage_user.EmployeeFirstName
user.email = ivantage_user.CompanyEmailAddress
user.custom_flag = true
user.save
user
else
nil
end
end
def self.exists_in_ad(employee_id)
!self.find_in_ad(employee_id, false).nil?
end
def self.find_in_ad(employee_id, save_record = true)
@db_user = User.new
unless VisionEmployee.find_by_user_id(employee_id).nil?
login = VisionEmployee.find_by_user_id(employee_id).Username
config = YAML::load(File.open("#{RAILS_ROOT}/config/active_directory.yml"))
@host = config["host"]
@port = config["port"]
@domain = config["domain"]
@dn = config["dn"]
@username = config["user"]
@password = config["password"]
begin
email = login + "@" + @domain
user = "company" + '\\' + @username
connection = LDAP::Conn.new(@host, @port)
connection.set_option( LDAP::LDAP_OPT_PROTOCOL_VERSION, 3 )
connection.bind( user, @password )
connection.search( @dn, LDAP::LDAP_SCOPE_SUBTREE, "sAMAccountName=#{login}") do |ad_user|
db_user = User.new
db_user.login = login
db_user.email = ad_user.vals("mail").to_s
db_user.display_name = ad_user.vals("displayName").to_s
db_user.given_name = ad_user.vals("givenName").to_s
# db_user.last_login_at = Time.new
db_user.employee_id = VisionEmployee.find_by_Username(db_user.login).Employee #Authenticator.getEmployeeId(login)
db_user.full_name = IvantageEmployee.find(db_user.employee_id).EmployeeFullName
db_user.save if save_record
@db_user = db_user
end
end
end
@db_user.login.nil? ? nil : @db_user
end
def self.find_by_email_in_ad(email)
config = YAML::load(File.open("#{RAILS_ROOT}/config/active_directory.yml"))
db_user = nil
db_user = User.find_by_email(email)
begin
user = "company" + '\\' + config["user"]
connection = LDAP::Conn.new(config["host"], config["port"])
connection.set_option( LDAP::LDAP_OPT_PROTOCOL_VERSION, 3 )
connection.bind( user, config["password"] )
connection.search( config["dn"], LDAP::LDAP_SCOPE_SUBTREE, "proxyAddresses=*#{email}") do |ad_user|
db_user ||= User.new
db_user.login = ad_user.vals("samAccountName").to_s
db_user.email = ad_user.vals("mail").to_s
db_user.display_name = ad_user.vals("displayName").to_s
db_user.given_name = ad_user.vals("givenName").to_s
# db_user.last_login_at = Time.new
db_user.employee_id = VisionEmployee.find_by_Username(db_user.login).Employee #Authenticator.getEmployeeId(login)
db_user.save
@db_user = db_user
end
end
@db_user
end
def self.find_by_display_name_in_ad(display_name)
config = YAML::load(File.open("#{RAILS_ROOT}/config/active_directory.yml"))
db_user = nil
db_user = User.find_by_display_name(display_name)
begin
user = "company" + '\\' + config["user"]
connection = LDAP::Conn.new(config["host"], config["port"])
connection.set_option( LDAP::LDAP_OPT_PROTOCOL_VERSION, 3 )
connection.bind( user, config["password"] )
connection.search( config["dn"], LDAP::LDAP_SCOPE_SUBTREE, "displayName=#{display_name}") do |ad_user|
db_user ||= User.new
db_user.login = ad_user.vals("samAccountName").to_s
db_user.email = ad_user.vals("mail").to_s
db_user.display_name = ad_user.vals("displayName").to_s
db_user.given_name = ad_user.vals("givenName").to_s
# db_user.last_login_at = Time.new
db_user.employee_id = VisionEmployee.find_by_Username(db_user.login).Employee #Authenticator.getEmployeeId(login)
db_user.save
@db_user = db_user
end
end
@db_user
end
def self.find_by_common_name_in_ad(common_name)
config = YAML::load(File.open("#{RAILS_ROOT}/config/active_directory.yml"))
@db_user = nil
diag = ""
begin
user = "company" + '\\' + config["user"]
connection = LDAP::Conn.new(config["host"], config["port"])
connection.set_option( LDAP::LDAP_OPT_PROTOCOL_VERSION, 3 )
connection.bind( user, config["password"] )
connection.search( config["dn"], LDAP::LDAP_SCOPE_SUBTREE, "CN=#{common_name}") do |ad_user|
diag = ad_user.vals("samAccountName").to_s
@db_user = User.find_employee(VisionEmployee.find_by_Username(ad_user.vals("samAccountName").to_s).Employee) #Authenticator.getEmployeeId(login)
end
rescue RuntimeError
return nil
rescue
return nil
end
@db_user
end
def is_member_of(group_cn)
User.get_members(group_cn).include?(self.user)
end
def has_membership(group_cn)
config = YAML::load(File.open("#{RAILS_ROOT}/config/active_directory.yml"))
@is_member = false
begin
user = "company" + '\\' + config["user"]
connection = LDAP::Conn.new(config["host"], config["port"])
connection.set_option( LDAP::LDAP_OPT_PROTOCOL_VERSION, 3 )
connection.bind( user, config["password"] )
connection.search( config["group_dn"], LDAP::LDAP_SCOPE_SUBTREE, "CN=*#{group_cn}") do |ad_group|
ad_group.vals("member").each do |member|
begin
@group = member.match(/.*OU=_Security Groups,DC=Company,DC=local$/)
@service_account = member.match(/.*OU=_Service Accounts,DC=Company,DC=local$/)
#if @group.nil? and (self.id == User.find_by_common_name_in_ad(member.match(/CN=([^,]*),/)[1]).id)
if @group.nil? and @service_account.nil? and (self.display_name == member.match(/CN=([^,]*),/)[1])
@is_member = true
break
else
@is_member = self.is_member_of(member.match(/CN=([^,]*),/)[1])
break if @is_member
end
rescue RuntimeError
end
end
end
end
@is_member
end
def highest_authority
@auth_levels = PurchasingAuthorityLevel.find(:all, :order => :position)
@auth_levels.reverse_each do |auth_level|
if self.is_member_of(auth_level.security_group)
return auth_level
end
end
nil
end
def self.get_members(group_cn)
config = YAML::load(File.open("#{RAILS_ROOT}/config/active_directory.yml"))
@members = nil
@members = Array.new
begin
user = "company" + '\\' + config["user"]
connection = LDAP::Conn.new(config["host"], config["port"])
connection.set_option( LDAP::LDAP_OPT_PROTOCOL_VERSION, 3 )
connection.bind( user, config["password"] )
connection.search( config["group_dn"], LDAP::LDAP_SCOPE_SUBTREE, "CN=*#{group_cn}") do |ad_group|
ad_group.vals("member").each do |member|
@group = member.match(/.*OU=_Security Groups,DC=Company,DC=local$/)
if @group.nil?
user = User.find_by_common_name_in_ad(member.match(/CN=([^,]*),/)[1])
@members << user unless user.nil?
else
@members += get_members(member.match(/CN=([^,]*),/)[1])
end
end
end
end
@members
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment