def update(self, request, *args, **kwargs): switch = { 'close': self.__close, 'process': self.__processing, 'reject': self.__reject, 'distribution': self.__distribution, 'complete': self.__complete, 'default':'default' } self.request.POST._mutable = True _action = request.data.get('action', 'default') try: ret = switch.get(_action, 'default')(request, *args, **kwargs) except Exception as e: logger.critical(e) return Response({'detail': str(e)}, status=status.HTTP_400_BAD_REQUEST) # instance = self.get_object() # old_processor_id = instance.current_processor.id # old_status = instance.status # ret = super(WorkOrderViewset, self).update(request, *args, **kwargs) # instance = self.get_object() # 更新实例 # # 当工单分发给其他人员时需要邮件通知 # if instance.current_processor.id != old_processor_id: # send_change_process_order_mail(self.request.user, instance) # # # 当工单的状态为失败、成功、拒绝的时候需要发送邮件通知工单发起人 # if instance.status != old_status: # if instance.status == 2 or instance.status == 3 or instance.status == 4: # send_result_order_mail(instance) return ret
def get_object(self): lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]} uid = filter_kwargs.get('pk') search_filter = '(uid={})'.format(uid) ldap_conn.search(search_base=LADPAPI['LDAP_BASE_DN'], search_filter=search_filter, attributes=[ 'sn', 'givenName', 'displayName', 'uid', 'userPassword', 'mobile', 'mail', 'postalAddress' ]) if ldap_conn.result['result'] != 0 or not ldap_conn.entries: logger.critical(str(ldap_conn.result)) raise LdapError attributes = json.loads( ldap_conn.entries[0].entry_to_json())['attributes'] data = {} for key, val in attributes.items(): data[key] = val[0] return data
def destroy(self, request, *args, **kwargs): uid = kwargs.get('pk') user_dn = 'cn={},{}'.format(uid, LADPAPI['LDAP_BASE_DN']) ldap_conn.delete(user_dn) if ldap_conn.result['result'] != 0: logger.critical(str(ldap_conn.result)) raise LdapError return Response(status=status.HTTP_204_NO_CONTENT)
def perform_create(self, serializer): attributes = dict(serializer.data) attributes['objectClass'] = [ 'shadowAccount', 'person', 'organizationalPerson', 'inetOrgPerson' ] user_dn = 'cn={},{}'.format(attributes['uid'], LADPAPI['LDAP_BASE_DN']) ldap_conn.add(user_dn, attributes=attributes) if ldap_conn.result['result'] != 0: logger.critical(str(ldap_conn.result)) raise LdapError
def get_choices_type(self, request): ''' 返回工单类型选择 :param request: :return: ''' try: data = self.__get_choices('TYPE') except Exception as e: logger.critical('获取工单类型选项失败') return Response({'detail': str(e)}, status=status.HTTP_404_NOT_FOUND) return Response(data)
def change_password(self, request, pk=None): user_info = oaapi.get_userinfo(request.user.username) password = request.data.get('password', 'hello1234') if user_info: uid = user_info.get('pinyin') user_dn = 'cn={},{}'.format(uid, LADPAPI['LDAP_BASE_DN']) ldap_conn.modify(user_dn, {'userPassword': [(MODIFY_REPLACE, [password])]}) if ldap_conn.result['result'] != 0: logger.critical(str(ldap_conn.result)) raise LdapError else: return Response({'detail': '修改密码失败'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response({'detail': '修改密码成功'})
def update(self, request, *args, **kwargs): # 因为get_object返回的不是model对象,需要手动获取serializer attributes = request.data.copy() serializer = self.get_serializer(data=attributes) user_dn = 'cn={},{}'.format(attributes['uid'], LADPAPI['LDAP_BASE_DN']) serializer.is_valid(raise_exception=True) for key, val in attributes.items(): attributes[key] = [(MODIFY_REPLACE, [val])] ldap_conn.modify(user_dn, attributes) if ldap_conn.result['result'] != 0: logger.critical(str(ldap_conn.result)) raise LdapError return Response(data=serializer.data)
def get_token(self, prefix='/login'): ''' 登录获取token ''' data = { "username": self.__username, "password": self.__password, "eauth": "pam" } headers = {'Accept': 'application/json'} url = '{}{}'.format(self.__url, prefix) try: req = requests.post(url, headers=headers, data=data, verify=False, timeout=self.__timeout) logger.debug(JSON.dumps(req.json(), indent=4)) if req.status_code != 200: return {'code': req.status_code, 'detail': '请求异常'} req = req.json() self.__token = req['return'][0]['token'] self.__token_expire = req['return'][0]['expire'] start = req['return'][0]['start'] cache.set('salt-token', { 'token': self.__token, 'start': start, 'expire': self.__token_expire }, timeout=86400) except Exception as e: logger.critical(e) raise e
def activate_ldap(self, request, pk=None): user_info = oaapi.get_userinfo(request.user.username) password = request.data.get('password', 'hello1234') attributes = {} if user_info: attributes['sn'] = user_info.get('pinyinhead') attributes['givenName'] = user_info.get('pinyinhead') attributes['displayName'] = user_info.get('name') attributes['uid'] = user_info.get('pinyin') attributes['userPassword'] = password attributes['mobile'] = user_info.get('telNumber') attributes['mail'] = user_info.get('emailAddress') attributes['postalAddress'] = user_info.get('address') attributes['objectClass'] = [ 'shadowAccount', 'person', 'organizationalPerson', 'inetOrgPerson' ] user_dn = 'cn={},{}'.format(attributes['uid'], LADPAPI['LDAP_BASE_DN']) ldap_conn.add(user_dn, attributes=attributes) if ldap_conn.result['result'] != 0: logger.critical(str(ldap_conn.result)) raise LdapError # 设置数据库标记 properties = json.loads(request.user.properties) properties['activate_ldap'] = True request.user.properties = json.dumps(properties) request.user.save() else: return Response({'detail': '获取OA信息失败'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response( {'detail': 'LDAP已经激活, 登录用户:{}'.format(attributes['uid'])})
__author__ = 'singo' __datetime__ = '2019/4/17 6:09 PM ' from django.conf import settings from pymongo import MongoClient from common.utils import logger try: URI = 'mongodb://{}:{}@{}:{}/'.format( settings.SQLAUDIT.get('USERNAME'), settings.SQLAUDIT.get('PASSWORD'), settings.SQLAUDIT.get('HOST'), settings.SQLAUDIT.get('PORT'), ) mongodb_client = MongoClient(URI) except Exception as e: mongodb_client = None logger.critical(e)