class User < ActiveRecord::Base
    belongs_to :user_group
    def self.authenticate(username="", login_password="")
      user = User.where('ad_user is null').find_by_username(username)

      if user && user.match_password(login_password)
        return user
      else
        return false
      end
    end   
    def match_password(login_password="")
      password == Digest::SHA1.hexdigest("#{login_password}#{salt}")
    end
    
    def self.ad_auth(username="", login_password="")
        ad_params={}
        LdapParameter.all.each do |param|
            ad_params[param.parameter]=param.value
        end
		Net::LDAP.open(:host => ad_params['server'],:port => ad_params['port'],:base => ad_params['base'],:auth => { :username => "#{username}@#{ad_params['domain']}",:password => login_password,:method => :simple}) do |ldap|
            user = User.where(:ad_user => 1).find_by_username(username)
            if user
                search_param = username
			    search_filter = Net::LDAP::Filter.eq("sAMAccountName", search_param)
			    ldap.search(:filter => search_filter)  do |item|  
                    item.memberof.as_json.each do |member|
                        if member.split(',')[0].split('=')[1] == LdapUserGroup.where(:user_group_id => user.user_group_id).first[:ldap_group_name]
                            return user
                        end 
                           
                    end
                    new_group = update_ad_user_group(item,user)
                    if new_group
                        return user
                    end
                end
            else
                search_param = username
			    search_filter = Net::LDAP::Filter.eq("sAMAccountName", search_param)
			    ldap.search(:filter => search_filter)  do |item|  
                    group_id=0
			        LdapUserGroup.order('order_id ASC').all.each do |group|
                        item.memberof.as_json.each do |member|
                            if member.split(',')[0].split('=')[1] == group[:ldap_group_name]
                                group_id = group[:user_group_id]
                                break
                            end
                            
                        end
                        if group_id > 0
                            break
                        end 
                    end
                    if group_id == 0
                        return false
                    else
                        user=User.create(:username => username, :password => 'ad_auth', :salt => 'ad_auth', :user_group_id => group_id, :from_date => DateTime.now, :to_date => DateTime.now, :ad_user => 1)
                        return user
                    end
                end
            end
                
		end
        
        return false
    end
    
    def update_ad_user_group(ad_groups,user)
        group_id=0
        LdapUserGroup.order('order_id ASC').all.each do |group|
            ad_groups.memberof.as_json.each do |member|
                if member.split(',')[0].split('=')[1] == group[:ldap_group_name]
                    group_id = group[:user_group_id]
                    break
                end
                
            end
            if group_id > 0
                break
            end 
        end
        if group_id > 0
            user.update(:user_group_id => group_id)
            return true
        else
            return false
        end
    end
end
