@login_requireddefad_user(request):if request.method != "POST":return render(request,"index.html")try:

FirstName= request.POST.get('first-name') #姓

LastName = request.POST.get('last-name') #名

LognName = request.POST.get('logn-name') #登录名

LognPassword = request.POST.get('logn-password') #登录密码

OuName = request.POST.get('OU') #厂区部门

GroupsName = request.POST.get('GROUPS') #工作组

EmpNo = request.POST.get("logn-empno") #工號

except:return render(request,"index.html")#用戶提交表單處理---------------------------------------------------------------------

FirstName = FirstName.replace(" ", '') #去除字符串中的空格

LastName = LastName.replace(" ", '') #去除字符串中的空格

LognName = LognName.replace(" ", '') #去除字符串中的空格

LognPassword = LognPassword.replace(" ", '') #去除字符串中的空格

#GroupsName = GroupsName.replace(" ", '') #去除字符串中的空格

EmpNo = EmpNo.replace(" ", '') #去除字符串中的空格

if FirstName and LastName and LognName and LognPassword andGroupsName:pass

else:return render(request, "error.html", {'error_message':'提交了非法字符串,無法建立,請重試'})#根據OU代碼在數據庫中查找相對應的OU名

try:

OuNameDatabase= OuSwitch.objects.get(ou_code=OuName)except:return render(request, "error.html",{'error_message':'無法識別的組織單位!'})#OU處理-----------------------------------------------------------------------------------------------

allName = FirstName+LastName

User= 'CN=' + allName + ','OuPath= User +OuNameDatabase.ou_name

DisplayName= LognName+'('+ allName +')' #XXX(XXX)

UserPrincipalName = LognName + '@Company.com.cn' #後綴

print('組織路徑:'+OuPath)print('用戶組ID:'+GroupsName)print('工號:'+EmpNo)#Group工作組處理-----------------------------------------------------------------------------------------------

GroupsName = GroupsName.split(',') ## 將一個或多個工作組轉換成列表

groups =[]for group inGroupsName:try:

groups.append(GroupSwitch.objects.get(id=group)) #查找用戶組OU

except:pass

if groups == []: #如果遇到用戶提交的數據出現無法找到的情況,將檢測這組數據是否全不達標

return render(request, "error.html",{'error_message':'無法識別的工作組!'})#print("查詢到的數據:"+ groups )

#LDAP主程序---------------------------------------------------------------------------------------------------

try:

server= Server("ldaps://Company.com.cn:636", use_ssl=True)

conn= Connection(server, user="Company\\user_registration", password="whoareyou2009", authentication=NTLM, auto_bind=True)except:return render(request, "error.html",{'error_message':'無法連接至域控,請稍候再試!'})ifconn.bind():#-------------------------添加用戶前檢測用戶是否存在---------------------------#

dn_look_path = 'ou=Company,dc=Company,dc=com,dc=cn'

if conn.search(dn_look_path, '(cn={})'.format(allName)):

conn.unbind()

conn.closedreturn render(request, "error.html",{'error_message':'{}用戶名已存在,請更換其他名稱后再試'.format(allName)})if conn.search(dn_look_path, '(sAMAccountName={})'.format(LognName)):

conn.unbind()

conn.closedreturn render(request, "error.html",{'error_message':'{}登錄名已存在,請更換其他登錄名后再試'.format(LognName)})#-------------------------添加用戶---------------------------#

msg_add = conn.add(OuPath,'inetOrgPerson',{'displayName':DisplayName, #顯示名

'givenName':LastName, #名

'sn':FirstName, #姓

'sAMAccountName':LognName, #登錄名

'userPrincipalName':UserPrincipalName, #完整名

'Description':EmpNo, #工號

'scriptPath':'user'}) #用戶登錄腳本

#-------------------------設置用戶初始密碼---------------------------#

print(msg_add)ifmsg_add:

msg_jh= conn.extend.microsoft.modify_password(OuPath,new_password=LognPassword, old_password=None)#-------------------------將用戶啟用---------------------------#

ifmsg_jh:try:

changeUACattribute= {'userAccountControl': [('MODIFY_REPLACE', [512])]}

conn.modify(OuPath, changes=changeUACattribute)except:return render(request, "error.html", {'error_message': '{}用戶啟用失敗!'.format(allName)})#-------------------------設置用戶下次需更改密碼---------------------------#

try:

changeTime= {'pwdLastSet': [('MODIFY_REPLACE', [0])]}

conn.modify(OuPath, changes=changeTime)except:print('設置用戶下次更改密碼失敗')return render(request, "error.html", {'error_message': '{}設置用戶下次更改密碼失敗'.format(allName)})#-------------------------加入工作組------------------------------------#

for group_cache ingroups:#groupdn = 'CN=資訊部,OU=資訊,OU=總管理處,OU=Company,DC=Company,DC=com,DC=cn', 'CN=jobs,OU=人事,OU=總管理處,OU=Company,DC=Company,DC=com,DC=cn'

try:print(group_cache.group_name)

conn.extend.microsoft.add_members_to_groups(OuPath, groups='{}'.format(group_cache.group_name))except:#return render(request, "error.html", {'error_message': '{}創建成功,但尝试加入用戶組失敗'.format(User[3:])})

pass

#-------------------------釋放與服務器的連接---------------------------#

conn.unbind()

conn.closedreturn render(request, "succeed.html", {'username':allName,'lognname':LognName,'lognpw':LognPassword,'oupath':OuNameDatabase,'groups':groups})else:return render(request, "error.html", {'error_message': '登錄名{},註冊失敗!'.format(LognName)})else:return render(request, "error.html", {'error_message': '與服務連接失敗,請聯繫資訊,或稍候再試'})

python ldap3 创建用户_基于python LDAP3的AD域账号注册系统相关推荐

  1. 通过python添加ad域账号_基于python LDAP3的AD域账号注册系统

    @login_requireddefad_user(request):if request.method != "POST":return render(request," ...

  2. python读ad域日志_基于python LDAP3的AD域账号注册系统

    @login_requireddefad_user(request):if request.method != "POST":return render(request," ...

  3. 华润大学ldap账号是什么_认证配置 - 接入 LDAP / AD 域账号(Beta) - 《KubeSphere v2.1 使用手册》 - 书栈网 · BookStack...

    接入 LDAP / AD 域账号(Beta) 如果您的企业使用 LDAP 作为用户认证系统,您可以在 KubeSphere 中通过脚本配置内置的 OpenLDAP 接入您的 LDAP 用户系统,从而允 ...

  4. python电影推荐算法_基于Python的电影推荐算法

    原标题:基于Python的电影推荐算法 第一步:收集和清洗数据 数据链接:https://grouplens.org/datasets/movielens/ 下载文件:ml-latest-small ...

  5. python交互式数据可视化_基于Python实现交互式数据可视化的工具,你用过几种?...

    作者:Alark Joshi 翻译:陈雨琳 来源:数据派THU(ID:DatapiTHU) 我教授了一门关于数据可视化的数据科学硕士课程.我们的数据科学硕士项目是一个为期15个月的强化项目,这个项目已 ...

  6. autocad python二次开发_基于Python AutoCAD ActiveX 二次开发,pyautocad应用技术

    AutoCAD应用程序开发系列 基于Python AutoCAD ActiveX 二次开发技术 主要库:pyautocad==0.2.0 内容#:基于Python AutoCAD ActiveX 二次 ...

  7. python参数化建模加工图_基于Python的ABAQUS层压板参数化建模

    唐维 康泽毓 杨婷 曾凤 蒋莉 摘要:为了提高层压板在ABAQUS仿真中建模的效率与准确性,提出利用Python语言对ABAQUS二次开发进行层压板参数化建模的方法.基于ABAQUS有限元软件,采用P ...

  8. 用python做炒股软件-python程序源码_基于python的炒股软件

    股票模拟交易系统设计与实现 不但能够进行界面的设计,还可以实现各个窗口的关联,通过WPF实现和其余窗口的关联,而且WPF中的类不但能够和其中一个窗口进行关联,还可以跟许多功能操作接口,WPF在对窗口对 ...

  9. python怎么创建列表_用Python将一个列表分割成小列表的实例讲解 Python 如何创建一个带小数的列表...

    python里有一个列表,列表里有几个小列表,小列表#冒泡排序:scoreList = [['a',98],['c',45],['b',70],['d',85],['h',85],['f',92],[ ...

最新文章

  1. Java基础之Comparable接口和Comparator接口的比较
  2. LifseaOS 悄然来袭,一款为云原生而生的 OS
  3. pcap封包修改服务器数据,tcprewrite批量修改pcap数据报文
  4. Java web (JSP)入门
  5. chrome浏览器加载css、js等静态资源文件的坑
  6. 【月径流预测】基于matlab未来搜索算法算法优化BP神经网络月径流预测【含Matlab源码 2001期】
  7. 汇编语言程序设计基础知识
  8. 天使、A轮、B轮……公司不同阶段估值方法大全
  9. 简单理解网络分层模型--向计算机网络迈一小jio;还会有后续;
  10. Win软件 - 教你正确使用 Win10 专业版系统自带远程桌面(免费内网穿透)
  11. 用JavaBean封装数据库操作
  12. 【毕业设计-课程设计】-单片机电子密码锁设计
  13. Git学习二、GitLab官网,安装自己的一个私有Gitlab代码管理库
  14. linux三维动画软件,Blender2.9免费版下载
  15. Linux下更改服务器名称
  16. Android自定义带标题边框的Layout
  17. kubernetes 部署 rook (ceph集群)
  18. 用于光导耦合的倾斜光栅的分析
  19. 【微信群助手机器人】好不好用?可以实现哪些功能?
  20. 《乘风破浪的姐姐2》开播大热,姐姐们的.fans相关域名速来抢!

热门文章

  1. 扫雷-下载-规则介绍
  2. 卷帘曝光和全局曝光的差别
  3. 【总结】AWS的(助理)架构师认证体系详解
  4. 【Base】ping-pong buffer
  5. [java]干支纪年法(循环练习)
  6. 操作系统课程课程笔记
  7. 1212淘宝精选店铺红包
  8. 【Java】int a = 2; 输出 a+++1 的结果是多少? 以及输出之后a的值是多少?
  9. 如何在互联网存量时代做电商?
  10. Linux中ls -l、ls -d和ls -ld的显示内容与区别