import ast
import json
import time
from urllib import request, parse
from itapi.exapi import *
import pytz
import tzlocal
from django.http import HttpResponse, JsonResponse
import datetime
# Create your views here.
from django.views import View
from ldap3 import Server, Connection, ALL, MODIFY_DELETE, MODIFY_REPLACE, MODIFY_ADD, MODIFY_INCREMENT, ObjectDef, \
AttrDef, Reader, Writer, SEQUENCE_TYPES, SUBTREE
from dbinfo.views import insert_log, selectindexmessagedb, getskey
from itops.settings import ldap3RESTARTABLE, ladp3search_base
from permission.views import Userperm
#替换()*
from sendmailapi.sendmail import send_email_by_template
def repeace(message):
promessage=message.replace('(',r'\28').replace(')',r'\29').replace('*',r'2a')
return promessage
def repeacelist(message):
a = list()
for i in message:
a.append(i.replace('(',r'\28').replace(')',r'\29').replace('*',r'2a'))
return a
def iisurl():
cusr='http://'+getskey()['iisserver']+':'+getskey()['iisport']
return cusr
def date_handler(obj):
if hasattr(obj, 'isoformat'):
return obj.isoformat()
else:
raise TypeError
#AD账号条件模糊查找/ 传入 usernane
class GetConMessage(View):
def get(self, request):
usernammse = request.session.get('username')
username = request.GET.get('username')
try:
username = repeace(username)
with ldap3RESTARTABLE as conn:
conn.search(
search_base=ladp3search_base,
search_filter= '(&(anr='+username+')(|(&(objectCategory=person)(objectClass=user))(objectCategory=group)))',
attributes=['sAMAccountName', "distinguishedName", 'proxyAddresses','description', 'displayName', 'name', 'objectClass',
'userAccountControl'],
)
result_id = conn.result
response_id = conn.response
if result_id['result'] == 0:
message = []
for i in response_id:
if i.get('attributes'):
usemessage = i['attributes']
message.append(dict(usemessage))
result = {'isSuccess': True, 'message': message}
else:
result = {'isSuccess': False, "message": result_id}
except Exception as e:
result = {'isSuccess': False, "message": str(e)}
insert_log(usernammse, request, str(result['isSuccess']), str(result), 'AD账号条件模糊查找')
response = HttpResponse()
response['Content-Type'] = "application/json"
response.write(json.dumps(result).encode("UTF-8"))
return response
def post(self, request):
usernammse = request.session.get('username')
username = request.POST.get('username')
mode = request.POST.get('mode')
try:
username=repeace(username)
if mode =='computer':
search='(&(anr='+username+')(&(objectClass=computer)))'
elif mode=='user':
search = '(|(anr=' + username + ')(&(|(&(objectCategory=person)(objectClass=user))(objectCategory=group)(objectCategory=contact))(|(wWWHomePage=' + username + ')(physicalDeliveryOfficeName=' + username + '))))'
else:
search='(&(anr='+username+')(|(objectCategory=group)(&(objectCategory=person)(objectClass=user))(objectCategory=contact)))'
with ldap3RESTARTABLE as conn:
conn.search(
search_base=ladp3search_base,
search_filter=search,
search_scope='SUBTREE',
attributes=['sAMAccountName',"distinguishedName" ,'description','displayName', 'name','objectClass','userAccountControl','lockoutTime'], )
result_id = conn.result
response_id = conn.response
if result_id['result']==0:
message=[]
for i in response_id:
if i.get('attributes'):
usemessage=i['attributes']
if 'computer' in usemessage['objectClass']:
usemessage['objectClass']='计算机'
userAccountCon = bin(usemessage['userAccountControl'])[-2]
if userAccountCon=='0':
usemessage['userAccountConte']='启用'
usemessage['icon'] = '<img src="/static/zTreeStyle/img/Computer.png"> '
else:
usemessage['userAccountConte'] = '禁用'
usemessage['icon'] = '<img src="/static/zTreeStyle/img/computer2.png"> '
elif 'organizationalUnit' in usemessage['objectClass']:
usemessage['objectClass'] = '组织单位'
usemessage['icon'] = '<img src="/static/zTreeStyle/img/ou.png"> '
elif 'container' in usemessage['objectClass']:
usemessage['objectClass'] = '容器'
usemessage['icon'] = '<img src="/static/zTreeStyle/img/ou01.png"> '
elif 'group' in usemessage['objectClass']:
usemessage['objectClass'] = '组'
usemessage['icon'] = '<img src="/static/zTreeStyle/img/group.png"> '
elif 'user' in usemessage['objectClass'] and 'person' in usemessage['objectClass']:
usemessage['objectClass'] = '用户'
userAccountCon = bin(usemessage['userAccountControl'])[-2]
if userAccountCon=='0':
usemessage['userAccountConte']='启用'
usemessage['icon'] = '<img src="/static/zTreeStyle/img/user.png"> '
else:
usemessage['userAccountConte'] = '禁用'
usemessage['icon'] = '<img src="/static/zTreeStyle/img/user2.png"> '
elif 'contact' in usemessage['objectClass']:
usemessage['objectClass'] = '联系人'
usemessage['icon'] = '<img src="/static/zTreeStyle/img/user.png"> '
else:
usemessage['objectClass'] = '其他'
usemessage['icon'] = '<img src="/static/zTreeStyle/img/weizi.png"> '
message.append(dict(usemessage))
result = {'isSuccess': True,'count':len(response_id)-3,'message': message}
else:
result = {'isSuccess': False, "message": result_id}
except Exception as e:
result = {'isSuccess': False, "message": str(e)}
insert_log(usernammse, request, str(result['isSuccess']), str(result), 'AD账号条件模糊查找')
response = HttpResponse()
response['Content-Type'] = "application/json"
response.write(json.dumps(result,default=date_handler).encode("UTF-8"))
return response
#AD账号条件模糊查找/ 传入 usernane 仅仅查询用户
class GetOnlyConMessage(View):
def get(self, request):
usernammse = request.session.get('username')
username = request.GET.get('username')
try:
username = repeace(username)
with ldap3RESTARTABLE as conn:
conn.search(
search_base=ladp3search_base,
search_filter= '(&(anr='+username+')(&(objectCategory=person)(objectClass=user)))',
attributes=['sAMAccountName', "distinguishedName", 'description'