- 论坛徽章:
- 0
|
public boolean createUser(String machineryPath, String user,
String password, String mail, String mobile, String usertype,
String managemachinery, String displayname, String validateCode,
String address,String postalcode,String telphone,String shengshi,
String nvsex,String vc_Uip,String startupuser,String maxnum) {
DirContext dctx=null;
try {
dctx = (DirContext) myLdapConnection.get();//调用线程中现有的连接
if (dctx == null) {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
env.put(Context.PROVIDER_URL, MY_HOST);
env.put(Context.SECURITY_AUTHENTICATION, MY_AUTH);
env.put(Context.SECURITY_PRINCIPAL, MY_DN);
env.put(Context.SECURITY_CREDENTIALS, MY_PW);
dctx = new InitialDirContext(env);
myLdapConnection.set(dctx);
}
//检查用户名是否已注册
Attributes SearchAttrs = new BasicAttributes(true);
SearchAttrs.put(new BasicAttribute("uid", user));
NamingEnumeration result = dctx.search(machineryPath, SearchAttrs);
//判断用户信息是否全,如果为空转为空值
if (password == null)
{
password = "";
}
else if (mail == null)
{
mail = "";
}
else if (mobile == null)
{
mobile = "";
}
else if (displayname == null)
{
displayname = "";
}
else if (validateCode == null)
{
validateCode = "";
}
else if (address == null)
{
address = "";
}
else if (postalcode == null)
{
postalcode = "";
}
else if (telphone == null)
{
telphone = "";
}
else if (shengshi == null)
{
shengshi = "";
}
else if (nvsex == null)
{
nvsex = "";
}
else if (vc_Uip == null)
{
vc_Uip = "";
}
else if (startupuser == null)
{
startupuser = "";
}
else if (maxnum == null)
{
maxnum = "1";
}
if (result.hasMore())
{
//用户名已注册
return false;
}
else
{
//用户名没有注册
Attributes attrs = new BasicAttributes(true);
//写用户名
Attribute id = new BasicAttribute("uid");
id.add(user);
Attribute cn = new BasicAttribute("cn");
cn.add(user);
Attribute sn = new BasicAttribute("sn");
sn.add(user);
//写email
Attribute email = new BasicAttribute("mail");
email.add(mail);
//写口令
Attribute pw = new BasicAttribute("userPassword");
pw.add(password);
//写手机号
Attribute mob = new BasicAttribute("mobile");
mob.add(mobile);
//写用户类型
Attribute type = new BasicAttribute("l");
type.add(usertype);
//写机构管理员管理的机构路径
Attribute manage = new BasicAttribute("description");
manage.add(managemachinery);
//写名字
Attribute nam = new BasicAttribute("displayName");
nam.add(displayname);
Attribute mes = new BasicAttribute("title");
mes.add(validateCode);
//USERCODE
Attribute machinery = new BasicAttribute("employeeType");
machinery.add(machineryPath);
//部门
Attribute objectclass = new BasicAttribute("objectclass");
objectclass.add("inetOrgPerson");
//以下是扩展
//写联系地址
Attribute addr = new BasicAttribute("x121Address");
addr.add(address);
//写邮编
Attribute post = new BasicAttribute("postalCode");
post.add(postalcode);
//写固定电话
Attribute tel = new BasicAttribute("telephoneNumber");
tel.add(telphone);
//写生日
Attribute sr = new BasicAttribute("o");
sr.add(shengshi);
//写性别
Attribute nn = new BasicAttribute("audio");
nn.add(nvsex);
//写ip
Attribute ip = new BasicAttribute("seeAlso");
ip.add(vc_Uip);
//写开启状态
Attribute stuts = new BasicAttribute("initials");
stuts.add(startupuser);
//写开启状态
Attribute num = new BasicAttribute("roomNumber");
num.add(maxnum);
attrs.put(id);
attrs.put(cn);
attrs.put(sn);
attrs.put(pw);
attrs.put(email);
attrs.put(nam);
attrs.put(mob);
attrs.put(type);
attrs.put(manage);
attrs.put(mes);
attrs.put(machinery);
attrs.put(objectclass);
//以下是扩展
attrs.put(addr);
attrs.put(post);
attrs.put(tel);
attrs.put(sr);
attrs.put(nn);
attrs.put(ip);
attrs.put(stuts);
attrs.put(num);
String AddDn = "uid=" + user + "," + machineryPath;
dctx.createSubcontext(AddDn, attrs);
//创建User 同时在条目下创建Application//用户所属应用
//Attributes AppAttrs1 = new BasicAttributes(true);
//Attribute AppOu1 = new BasicAttribute("ou");
//AppOu1.add("Power");
Attribute AppObjectclass = new BasicAttribute("objectclass");
AppObjectclass.add("organizationalunit");
//AppAttrs1.put(AppOu1);
//AppAttrs1.put(AppObjectclass);
//String AddAppDn1 = "ou=Power,uid=" + user + "," + machineryPath;
//dctx.createSubcontext(AddAppDn1, AppAttrs1);
//创建User 同时在条目下创建Application//用户所属角色
Attributes AppAttrs = new BasicAttributes(true);
Attribute AppOu = new BasicAttribute("ou");
AppOu.add("Role");
AppAttrs.put(AppOu);
AppAttrs.put(AppObjectclass);
String AddAppDn = "ou=Role,uid=" + user + "," + machineryPath;
dctx.createSubcontext(AddAppDn, AppAttrs);
return true;
} //if
} catch (NamingException e) {
e.printStackTrace();
System.out.println("DomainManager.createUser() fail...,"
+ e.toString());
return false;
}
} |
|