package com.gtiit.shr.utils;

import java.util.Properties;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.ldap.Control;
import javax.naming.ldap.InitialLdapContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;




@Component
public class ADUtils {
    @Value("${api.ad.address}")
    private String address;
    @Value("${api.ad.username}")
    private String username;
    @Value("${api.ad.password}")
    private String passWord;

    public DirContext getDirContext() throws NamingException {
        Properties env = new Properties();
        env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
        env.put("java.naming.security.authentication", "simple");
        env.put("com.sun.jndi.ldap.connect.timeout", "3000");
        env.put("java.naming.security.principal", this.username);
        env.put("java.naming.security.credentials", this.passWord);
        env.put("java.naming.provider.url", this.address);

        try {
            DirContext dc = new InitialLdapContext(env, (Control[])null);
            System.out.println("AD域服务连接认证成功!");
            return dc;
        } catch (NamingException var4) {
            String message = var4.getMessage();
            System.out.println("认证出错：");
            throw new NamingException("AD域服务连接认证失败:" + message);
        }
    }

    public ADUtils() {
    }

    public String getAddress() {
        return this.address;
    }

    public String getUsername() {
        return this.username;
    }

    public String getPassWord() {
        return this.passWord;
    }

    public void setAddress(final String address) {
        this.address = address;
    }

    public void setUsername(final String username) {
        this.username = username;
    }

    public void setPassWord(final String passWord) {
        this.passWord = passWord;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof ADUtils)) {
            return false;
        } else {
            ADUtils other = (ADUtils)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label47: {
                    Object this$address = this.getAddress();
                    Object other$address = other.getAddress();
                    if (this$address == null) {
                        if (other$address == null) {
                            break label47;
                        }
                    } else if (this$address.equals(other$address)) {
                        break label47;
                    }

                    return false;
                }

                Object this$username = this.getUsername();
                Object other$username = other.getUsername();
                if (this$username == null) {
                    if (other$username != null) {
                        return false;
                    }
                } else if (!this$username.equals(other$username)) {
                    return false;
                }

                Object this$passWord = this.getPassWord();
                Object other$passWord = other.getPassWord();
                if (this$passWord == null) {
                    if (other$passWord != null) {
                        return false;
                    }
                } else if (!this$passWord.equals(other$passWord)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof ADUtils;
    }

    public int hashCode() {
        boolean PRIME = true;
        int result = 1;
        Object $address = this.getAddress();
        result = result * 59 + ($address == null ? 43 : $address.hashCode());
        Object $username = this.getUsername();
        result = result * 59 + ($username == null ? 43 : $username.hashCode());
        Object $passWord = this.getPassWord();
        result = result * 59 + ($passWord == null ? 43 : $passWord.hashCode());
        return result;
    }

    public String toString() {
        return "ADUtils(address=" + this.getAddress() + ", username=" + this.getUsername() + ", passWord=" + this.getPassWord() + ")";
    }
}
