account_base_service.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. import json
  2. import time
  3. import jwt
  4. from django.conf import settings
  5. from django.contrib.auth import authenticate
  6. from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
  7. from django.contrib.auth.hashers import make_password
  8. from django.db.models import Q
  9. from apps.account.models import User, UserRole, Dept, Role, UserDept, Application
  10. from service.util.archive_service import ArchiveService
  11. from service.base_service import BaseService
  12. from service.common.constant_service import constant_service_ins
  13. from service.common.log_service import auto_log
  14. class AccountBaseService(BaseService):
  15. """
  16. account
  17. """
  18. @classmethod
  19. @auto_log
  20. def get_token_by_app_name(cls, app_name: str) -> tuple:
  21. """
  22. get app's call token by app_name
  23. :param app_name:
  24. :return:
  25. """
  26. app_token_obj = Application.objects.filter(app_name=app_name).first()
  27. return True, app_token_obj
  28. @classmethod
  29. @auto_log
  30. def get_user_by_username(cls, username: str) -> tuple:
  31. """
  32. get user info by username
  33. :return:
  34. """
  35. result = User.objects.filter(username=username).first()
  36. if result:
  37. return True, result
  38. else:
  39. return False, 'username: {} is not existed or has been deleted'.format(username)
  40. @classmethod
  41. @auto_log
  42. def get_user_by_email(cls, email: str) -> tuple:
  43. """
  44. get user info by email
  45. :return:
  46. """
  47. result = User.objects.filter(email=email).first()
  48. if result:
  49. return True, result
  50. else:
  51. return False, 'user: {} is not existed or has been deleted'.format(email)
  52. @classmethod
  53. @auto_log
  54. def get_user_list_by_usernames(cls, usernames: list) -> tuple:
  55. """
  56. get user info by username
  57. :return:
  58. """
  59. result = User.objects.filter(username__in=usernames).all()
  60. if result:
  61. return True, result
  62. else:
  63. return False, 'usernames: {} is not existed or has been deleted'.format(usernames)
  64. @classmethod
  65. @auto_log
  66. def get_user_by_user_id(cls, user_id: int) -> tuple:
  67. """
  68. get user by user id
  69. :param user_id:
  70. :return:
  71. """
  72. result = User.objects.filter(id=user_id).first()
  73. if result:
  74. return True, result
  75. else:
  76. return False, 'user_id: {} is not existed or has been deleted'.format(user_id)
  77. @auto_log
  78. def get_user_format_by_user_id(self, user_id):
  79. """
  80. get user's format info
  81. :param user_id:
  82. :return:
  83. """
  84. flag, result = self.get_user_by_user_id(user_id)
  85. if flag is not False:
  86. user_result = result.get_dict()
  87. user_dept_list = UserDept.objects.filter(user_id=user_id)
  88. user_dept_info_list = []
  89. for user_dept in user_dept_list:
  90. user_dept_info_list.append(
  91. dict(name=user_dept.dept.name, id=user_dept.dept.id))
  92. user_result['department'] = user_dept_info_list
  93. return flag, user_result
  94. return flag, result
  95. @classmethod
  96. @auto_log
  97. def get_user_name_list_by_id_list(cls, user_id_list: list) -> tuple:
  98. """
  99. get username list by user id list
  100. 根据用户id的数组获取用户名的list
  101. :param user_id_list:
  102. :return:
  103. """
  104. user_queryset = User.objects.filter(id__in=user_id_list).all()
  105. if not user_queryset:
  106. return False, 'user id is not existed or has been deleted'
  107. username_list = [user_query.username for user_query in user_queryset]
  108. return True, dict(username_list=username_list)
  109. @classmethod
  110. @auto_log
  111. def get_user_role_id_list(cls, username: str) -> tuple:
  112. """
  113. get user's role id list by username
  114. :param username:
  115. :return:
  116. """
  117. user_obj = User.objects.filter(username=username).first()
  118. if not user_obj:
  119. return False, 'user is not existed or has been deleted'
  120. user_role_queryset = UserRole.objects.filter(user_id=user_obj.id).all()
  121. user_role_id_list = [user_role.role_id for user_role in user_role_queryset]
  122. return True, user_role_id_list
  123. @classmethod
  124. @auto_log
  125. def get_user_role_info_by_user_id(cls, user_id: int, search_value: str = 0, page: int = 1,
  126. per_page: int = 10) -> tuple:
  127. """
  128. get user's role info list by user's id and query params: role name、page、per_page
  129. :param user_id:
  130. :param search_value:
  131. :param page:
  132. :param per_page:
  133. :return:
  134. """
  135. user_role_queryset = UserRole.objects.filter(user_id=user_id).all()
  136. user_role_id_list = [user_role.role_id for user_role in user_role_queryset]
  137. query_params = Q(is_deleted=False, id__in=user_role_id_list)
  138. if search_value:
  139. query_params &= Q(name__contains=search_value)
  140. role_info_queryset = Role.objects.filter(query_params).all()
  141. paginator = Paginator(role_info_queryset, per_page)
  142. try:
  143. role_info_result_paginator = paginator.page(page)
  144. except PageNotAnInteger:
  145. role_info_result_paginator = paginator.page(1)
  146. except EmptyPage:
  147. # If page is out of range (e.g. 9999), deliver last page of results
  148. role_info_result_paginator = paginator.page(paginator.num_pages)
  149. role_result_list = role_info_result_paginator.object_list
  150. role_result_format_list = []
  151. for role_info in role_result_list:
  152. role_result_format_list.append(dict(id=role_info.id, name=role_info.name, description=role_info.description,
  153. label=json.dumps(role_info.label) if role_info.label else {},
  154. creator=role_info.creator, gmt_created=str(role_info.gmt_created)[:19]))
  155. return True, dict(role_result_format_list=role_result_format_list,
  156. paginator_info=dict(per_page=per_page, page=page, total=paginator.count))
  157. @classmethod
  158. @auto_log
  159. def get_role_user_info_by_role_id(cls, role_id: int, search_value: str = '', page: int = 1,
  160. per_page: int = 10) -> tuple:
  161. """
  162. get role's user info list by role_id
  163. :param role_id:
  164. :param search_value:
  165. :param page:
  166. :param per_page:
  167. :return:
  168. """
  169. user_role_queryset = UserRole.objects.filter(role_id=role_id).all()
  170. role_user_id_list = [user_role.user_id for user_role in user_role_queryset]
  171. query_params = Q(is_deleted=False, id__in=role_user_id_list)
  172. if search_value:
  173. query_params &= Q(username__contains=search_value) | Q(alias__contains=search_value)
  174. user_info_queryset = User.objects.filter(query_params).all()
  175. paginator = Paginator(user_info_queryset, per_page)
  176. try:
  177. user_info_result_paginator = paginator.page(page)
  178. except PageNotAnInteger:
  179. user_info_result_paginator = paginator.page(1)
  180. except EmptyPage:
  181. # If page is out of range (e.g. 9999), deliver last page of results
  182. user_info_result_paginator = paginator.page(paginator.num_pages)
  183. user_result_list = user_info_result_paginator.object_list
  184. user_result_format_list = []
  185. for user_info in user_result_list:
  186. user_result_format_list.append(user_info.get_dict())
  187. return True, dict(user_result_format_list=user_result_format_list,
  188. paginator_info=dict(per_page=per_page, page=page, total=paginator.count))
  189. @classmethod
  190. @auto_log
  191. def get_user_up_dept_id_list(cls, username: str) -> tuple:
  192. """
  193. get user's department id list by username, include parent department
  194. :param username:
  195. :return:
  196. """
  197. dept_id_list = []
  198. user_obj = User.objects.filter(username=username).first()
  199. if not user_obj:
  200. return False, 'user is not existed or has been deleted'
  201. def iter_dept(dept_id):
  202. dept_obj = Dept.objects.filter(id=dept_id).first()
  203. if dept_obj:
  204. dept_id_list.append(dept_obj.id)
  205. if dept_obj.parent_dept_id:
  206. iter_dept(dept_obj.parent_dept_id)
  207. user_dept_queryset = UserDept.objects.filter(user_id=user_obj.id).all()
  208. user_dept_id_list = [user_dept.dept_id for user_dept in user_dept_queryset]
  209. for user_dept_id in user_dept_id_list:
  210. iter_dept(user_dept_id)
  211. dept_id_list = list(set(dept_id_list))
  212. return True, dept_id_list
  213. @classmethod
  214. @auto_log
  215. def get_user_dept_approver(cls, username: str, dept_id: int = 0) -> tuple:
  216. """
  217. get user's department approver, Preferential access to the approver, without taking tl(team leader)
  218. :param username:
  219. :param dept_id: 用于用户可能属于多个部门的情况
  220. :return:
  221. """
  222. user_obj = User.objects.filter(username=username).first()
  223. if dept_id:
  224. if UserDept.objects.filter(user_id=user_obj.id, dept_id=dept_id).first():
  225. loon_dept_obj = Dept.objects.filter(id=dept_id).first()
  226. if loon_dept_obj.approver:
  227. return True, loon_dept_obj.approver
  228. else:
  229. return True, loon_dept_obj.leader
  230. else:
  231. return False, 'dept_id is invalid'
  232. else:
  233. # no dept id specified, get all user dept's approver
  234. user_dept_queryset = UserDept.objects.filter(user_id=user_obj.id)
  235. approver_list = []
  236. for user_dept in user_dept_queryset:
  237. if user_dept.dept.approver:
  238. approver_list.extend(user_dept.dept.approver.split(','))
  239. else:
  240. approver_list.append(user_dept.dept.leader)
  241. return True, ','.join(list(set(approver_list)))
  242. @classmethod
  243. @auto_log
  244. def get_user_dept_info(cls, username: str = '', user_id: int = 0) -> tuple:
  245. """
  246. get user dept info
  247. :param username:
  248. :param user_id:
  249. :return:
  250. """
  251. if username:
  252. user_obj = User.objects.filter(username=username).first()
  253. user_id = user_obj.id
  254. user_dept_queryset = UserDept.objects.filter(user_id=user_id).all()
  255. user_dept_info = {}
  256. user_dept_id_list = []
  257. user_dept_name_list = []
  258. for user_dept in user_dept_queryset:
  259. user_dept_id_list.append(str(user_dept.dept_id))
  260. user_dept_name_list.append(user_dept.dept.name)
  261. user_dept_info['id'] = ','.join(user_dept_id_list)
  262. user_dept_info['name'] = ','.join(user_dept_name_list)
  263. return True, user_dept_info
  264. @classmethod
  265. @auto_log
  266. def get_dept_sub_dept_id_list(cls, dept_id: int) -> tuple:
  267. """
  268. get department's all subordinate department
  269. :param dept_id:
  270. :return:
  271. """
  272. dept_id_list = []
  273. dept_obj = Dept.objects.filter(id=dept_id).first()
  274. if dept_obj:
  275. dept_id_list.append(dept_obj.id)
  276. else:
  277. return True, []
  278. def iter_dept_id_list(new_dept_id):
  279. new_dept_obj = Dept.objects.filter(id=new_dept_id).first()
  280. if new_dept_obj:
  281. sub_dept_queryset = Dept.objects.filter(parent_dept_id=new_dept_obj.id).all()
  282. for sub_dept in sub_dept_queryset:
  283. if sub_dept:
  284. dept_id_list.append(sub_dept.id)
  285. iter_dept_id_list(sub_dept.id)
  286. iter_dept_id_list(dept_id)
  287. return True, dept_id_list
  288. @classmethod
  289. @auto_log
  290. def get_dept_username_list(cls, dept_id: object) -> tuple:
  291. """
  292. get department's all username list
  293. :param dept_id: int or str
  294. :return:
  295. """
  296. if type(dept_id) == str:
  297. dept_id_str_list = dept_id.split(',') # 用于支持多部门
  298. dept_id_list = [int(dept_id_str) for dept_id_str in dept_id_str_list]
  299. else:
  300. dept_id_list = [dept_id]
  301. sub_dept_id_list_total = []
  302. for dept_id in dept_id_list:
  303. flag, sub_dept_id_list = cls.get_dept_sub_dept_id_list(dept_id)
  304. if flag is False:
  305. return False, sub_dept_id_list
  306. sub_dept_id_list_total = sub_dept_id_list_total + sub_dept_id_list
  307. user_dept_queryset = UserDept.objects.filter(dept_id__in=sub_dept_id_list_total).all()
  308. user_id_list = [user_dept.user_id for user_dept in user_dept_queryset]
  309. user_queryset = User.objects.filter(id__in=user_id_list).all()
  310. user_name_list = [user.username for user in user_queryset]
  311. return True, user_name_list
  312. @classmethod
  313. @auto_log
  314. def get_role_username_list(cls, role_id: int) -> tuple:
  315. """
  316. get role's username list by role_id
  317. :param role_id:
  318. :return:
  319. """
  320. user_role_queryset = UserRole.objects.filter(role_id=role_id).all()
  321. user_id_list = []
  322. for user_role in user_role_queryset:
  323. user_id_list.append(user_role.user_id)
  324. if not user_id_list:
  325. return True, []
  326. username_queryset = User.objects.filter(id__in=user_id_list).all()
  327. username_list = []
  328. for username_obj in username_queryset:
  329. username_list.append(username_obj.username)
  330. return True, username_list
  331. @classmethod
  332. @auto_log
  333. def get_dept_by_id(cls, dept_id: int) -> tuple:
  334. """
  335. get department's info by dept_id
  336. :param dept_id:
  337. :return:
  338. """
  339. return True, Dept.objects.filter(id=dept_id, is_deleted=False).first()
  340. @classmethod
  341. @auto_log
  342. def get_dept_by_ids(cls, dept_ids: str) -> tuple:
  343. """
  344. get department's queryset by dept_ids
  345. :param dept_ids:
  346. :return:
  347. """
  348. if dept_ids:
  349. dept_id_list = dept_ids.split(',')
  350. return True, Dept.objects.filter(id__in=dept_id_list, is_deleted=False).all()
  351. @classmethod
  352. @auto_log
  353. def get_role_by_id(cls, role_id: int) -> tuple:
  354. """
  355. get role's info by role_id
  356. :param role_id:
  357. :return:
  358. """
  359. return True, Role.objects.filter(id=role_id, is_deleted=False).first()
  360. @classmethod
  361. @auto_log
  362. def app_workflow_permission_list(cls, app_name: str) -> tuple:
  363. """
  364. get app's authorised workflow_id list by app_name
  365. :param app_name:
  366. :return:
  367. """
  368. if not app_name:
  369. return False, 'app_name is not provided'
  370. if app_name == 'loonflow':
  371. # loonflow有权限访问所有workflow
  372. from apps.workflow.models import Workflow
  373. workflow_query_set = Workflow.objects.filter(is_deleted=0).all()
  374. workflow_id_list = []
  375. for workflow_obj in workflow_query_set:
  376. workflow_id_list.append(workflow_obj.id)
  377. return True, dict(workflow_id_list=workflow_id_list)
  378. app_token_obj = Application.objects.filter(app_name=app_name).first()
  379. if not app_token_obj:
  380. return False, 'appname is unauthorized'
  381. @classmethod
  382. @auto_log
  383. def app_workflow_permission_check(cls, app_name: str, workflow_id: int) -> tuple:
  384. """
  385. appname has permission for workflow check by app_name and workflow_id
  386. :param app_name:
  387. :param workflow_id:
  388. :return:
  389. """
  390. if app_name == 'loonflow':
  391. return True, ''
  392. from service.workflow.workflow_permission_service import workflow_permission_service_ins
  393. flag, result = workflow_permission_service_ins.get_workflow_id_list_by_permission('api', 'app', app_name)
  394. if flag and result.get('workflow_id_list') and workflow_id in result.get('workflow_id_list'):
  395. return True, ''
  396. else:
  397. return False, 'the app has no permission to the workflow_id'
  398. @classmethod
  399. @auto_log
  400. def app_ticket_permission_check(cls, app_name: str, ticket_id: int) -> tuple:
  401. """
  402. appname has permission to ticket check by app_name and ticket_id
  403. :param app_name:
  404. :param ticket_id:
  405. :return:
  406. """
  407. from service.ticket.ticket_base_service import ticket_base_service_ins
  408. flag, ticket_obj = ticket_base_service_ins.get_ticket_by_id(ticket_id)
  409. if not flag:
  410. return False, ticket_obj
  411. workflow_id = ticket_obj.workflow_id
  412. from service.workflow.workflow_permission_service import workflow_permission_service_ins
  413. permission_check, msg = workflow_permission_service_ins.workflow_id_permission_check(workflow_id, 'api', 'app',
  414. app_name)
  415. if not permission_check:
  416. return False, msg
  417. return True, ''
  418. @classmethod
  419. @auto_log
  420. def get_user_list(cls, search_value: str, department_id: int, page: int = 1, per_page: int = 10) -> tuple:
  421. """
  422. get user restful info list by query params: search_value, page, per_page
  423. :param search_value: support user's username, and user's alias. fuzzy query
  424. :param department_id:
  425. :param page:
  426. :param per_page:
  427. :return:
  428. """
  429. query_params = Q()
  430. if search_value:
  431. query_params &= Q(username__contains=search_value) | Q(alias__contains=search_value)
  432. if department_id:
  433. query_params &= Q(dept__id__in=Dept.objects.filter(id=department_id))
  434. user_objects = User.objects.filter(query_params).order_by("id")
  435. paginator = Paginator(user_objects, per_page)
  436. try:
  437. user_result_paginator = paginator.page(page)
  438. except PageNotAnInteger:
  439. user_result_paginator = paginator.page(1)
  440. except EmptyPage:
  441. # If page is out of range (e.g. 9999), deliver last page of results
  442. user_result_paginator = paginator.page(paginator.num_pages)
  443. user_result_object_list = user_result_paginator.object_list
  444. user_result_object_format_list = []
  445. user_id_list = [user_result_object.id for user_result_object in user_result_object_list]
  446. user_dept_list = UserDept.objects.filter(user_id__in=user_id_list)
  447. for user_result_object in user_result_object_list:
  448. user_result_format_dict = user_result_object.get_dict()
  449. user_dept_info_list = []
  450. for user_dept in user_dept_list:
  451. if user_result_object.id == user_dept.user_id:
  452. user_dept_info_list.append(
  453. dict(name=user_dept.dept.name, id=user_dept.dept.id))
  454. user_result_format_dict['department'] = user_dept_info_list
  455. user_result_object_format_list.append(user_result_format_dict)
  456. return True, dict(user_result_object_format_list=user_result_object_format_list,
  457. paginator_info=dict(per_page=per_page, page=page, total=paginator.count))
  458. @classmethod
  459. @auto_log
  460. def add_user(cls, name: str, alias: str, email: str, phone: str, dept_id_list: list, role_id_list, type: str,
  461. status: str, avatar: str, lang: str, creator_id: int, password: str = '', tenant_id: int = 1) -> tuple:
  462. """
  463. add user record
  464. :param name:
  465. :param alias:
  466. :param email:
  467. :param phone:
  468. :param dept_id_list:
  469. :param role_id_list:
  470. :param type:
  471. :param status:
  472. :param avatar:
  473. :param lang:
  474. :param creator:
  475. :param password:
  476. :return:
  477. """
  478. password_str = make_password(password, None, 'pbkdf2_sha256')
  479. user_obj = User(name=name, alias=alias, email=email, phone=phone,
  480. status=status, type=type, avatar=avatar, lang=lang,
  481. creator_id=creator_id, password=password_str, tenant_id=tenant_id)
  482. user_obj.save()
  483. queryset_list = []
  484. for dept_id in dept_id_list:
  485. queryset_list.append(UserDept(user_id=user_obj.id, dept_id=dept_id))
  486. UserDept.objects.bulk_create(queryset_list)
  487. for role_id in role_id_list:
  488. queryset_list.append(UserRole(user_id=user_obj.id, role_id=role_id))
  489. UserDept.objects.bulk_create(queryset_list)
  490. return True, dict(user_id=user_obj.id)
  491. @classmethod
  492. @auto_log
  493. def edit_user(cls, user_id: int, name: str, alias: str, email: str, phone: str, dept_id_list: list, role_id_list, type: str,
  494. status: str, avatar: str, lang: str, creator_id: int, tenant_id: int = 1) -> tuple:
  495. """
  496. update user
  497. :param user_id:
  498. :param name:
  499. :param alias:
  500. :param email:
  501. :param phone:
  502. :param dept_id_list:
  503. :param role_id_list:
  504. :param type:
  505. :param status:
  506. :param avatar:
  507. :param lang:
  508. :param creator_id:
  509. :param tenant_id:
  510. :return:
  511. """
  512. user_obj = User.objects.filter(id=user_id)
  513. user_obj.update(name=name, alias=alias, email=email, phone=phone,
  514. status=status, type=type, avatar=avatar, lang=lang,
  515. creator_id=creator_id, tenant_id=tenant_id)
  516. # update dept info
  517. user_id = user_obj.first().id
  518. user_dept_queryset = UserDept.objects.filter(user_id=user_id).all()
  519. user_dept_id_exist = [user_dept.dept_id for user_dept in user_dept_queryset]
  520. need_add_list = [dept_id_int for dept_id_int in dept_id_list if dept_id_int not in user_dept_id_exist]
  521. need_delete_list = [user_dept_id for user_dept_id in user_dept_id_exist if user_dept_id not in dept_id_list]
  522. add_queryset = []
  523. for need_add in need_add_list:
  524. add_queryset.append(UserDept(user_id=user_id, dept_id=need_add))
  525. UserDept.objects.bulk_create(add_queryset)
  526. UserDept.objects.filter(user_id=user_id, dept_id__in=need_delete_list).update(is_deleted=1)
  527. return True, {}
  528. @classmethod
  529. @auto_log
  530. def delete_user(cls, user_id: int, operator_id: int) -> tuple:
  531. """
  532. delete user
  533. :param user_id:
  534. :param operator_id:
  535. :return:
  536. """
  537. user_obj = User.objects.get(id=user_id)
  538. return ArchiveService.archive_record('User', user_obj, operator_id)
  539. @classmethod
  540. @auto_log
  541. def delete_user_list(cls, user_id_list: list, operator_id: int) -> tuple:
  542. """
  543. delete user list
  544. :param user_id_list:
  545. :param operator_id:
  546. :return:
  547. """
  548. if user_id_list:
  549. user_queryset = User.objects.filter(id__in=user_id_list).all()
  550. return ArchiveService.archive_record_list("User", user_queryset, operator_id)
  551. else:
  552. return False, "user_id_list can not be a blank list"
  553. @classmethod
  554. @auto_log
  555. def get_role_list(cls, search_value: str, page: int = 1, per_page: int = 10) -> tuple:
  556. """
  557. 获取角色列表
  558. get role restful list by search params
  559. :param search_value: role name or role description Support fuzzy queries
  560. :param page:
  561. :param per_page:
  562. :return:
  563. """
  564. query_params = Q(is_deleted=False)
  565. if search_value:
  566. query_params &= Q(name__contains=search_value) | Q(description__contains=search_value)
  567. user_objects = Role.objects.filter(query_params)
  568. paginator = Paginator(user_objects, per_page)
  569. try:
  570. role_result_paginator = paginator.page(page)
  571. except PageNotAnInteger:
  572. role_result_paginator = paginator.page(1)
  573. except EmptyPage:
  574. # If page is out of range (e.g. 9999), deliver last page of results
  575. role_result_paginator = paginator.page(paginator.num_pages)
  576. role_result_object_list = role_result_paginator.object_list
  577. role_result_object_format_list = []
  578. for role_result_object in role_result_object_list:
  579. role_result_object_format_list.append(role_result_object.get_dict())
  580. return True, dict(role_result_object_format_list=role_result_object_format_list,
  581. paginator_info=dict(per_page=per_page, page=page, total=paginator.count))
  582. @classmethod
  583. @auto_log
  584. def add_role(cls, name: str, description: str, label: str, creator: str) -> tuple:
  585. """
  586. add role
  587. :param name:
  588. :param description:
  589. :param label:
  590. :param creator:
  591. :return:
  592. """
  593. role_obj = Role(name=name, description=description, label=label, creator=creator)
  594. role_obj.save()
  595. return True, dict(role_id=role_obj.id)
  596. @classmethod
  597. @auto_log
  598. def add_role_user(cls, role_id: int, user_id: int, creator: str) -> tuple:
  599. """
  600. add role's user
  601. :param role_id:
  602. :param user_id:
  603. :param creator:
  604. :return:
  605. """
  606. # 去重下
  607. role_user_queryset = UserRole.objects.filter(user_id=user_id, role_id=role_id)
  608. if role_user_queryset:
  609. return False, 'user has been existed in this role'
  610. role_user_obj = UserRole(user_id=user_id, role_id=role_id, creator=creator)
  611. role_user_obj.save()
  612. return True, dict(role_user_id=role_user_obj.id)
  613. @classmethod
  614. @auto_log
  615. def delete_role_user(cls, user_id: int) -> tuple:
  616. """
  617. 删除角色用户
  618. :param user_id:
  619. :return:
  620. """
  621. role_user_obj = UserRole.objects.filter(user_id=user_id)
  622. if not role_user_obj:
  623. return False, 'record is not existed or has been deleted'
  624. role_user_obj.update(is_deleted=1)
  625. return True, ''
  626. @classmethod
  627. @auto_log
  628. def update_role(cls, role_id: int, name: str, description: str, label: str) -> tuple:
  629. """
  630. update role
  631. 更新角色
  632. :param role_id:
  633. :param name:
  634. :param description:
  635. :param label:
  636. :return:
  637. """
  638. role_queryset = Role.objects.filter(id=role_id)
  639. if not role_queryset:
  640. return False, 'role record is not existed'
  641. role_queryset.update(name=name, description=description, label=label)
  642. return True, {}
  643. @classmethod
  644. @auto_log
  645. def delete_role(cls, role_id: int) -> tuple:
  646. """
  647. delete role record
  648. 删除角色
  649. :param role_id:
  650. :return:
  651. """
  652. role_queryset = Role.objects.filter(id=role_id)
  653. if not role_queryset:
  654. return False, 'role record is not existed'
  655. role_queryset.update(is_deleted=1)
  656. return True, {}
  657. @classmethod
  658. @auto_log
  659. def get_dept_list(cls, search_value: str, page: int = 1, per_page: int = 10, simple=False) -> tuple:
  660. """
  661. get dept restful list by search params
  662. :param search_value: department name or department description Support fuzzy queries
  663. :param page:
  664. :param per_page:
  665. :param simple: 只返回部分数据
  666. :return:
  667. """
  668. query_params = Q(is_deleted=False)
  669. if search_value:
  670. query_params &= Q(name__contains=search_value) | Q(label__contains=search_value)
  671. dept_objects = Dept.objects.filter(query_params)
  672. paginator = Paginator(dept_objects, per_page)
  673. try:
  674. dept_result_paginator = paginator.page(page)
  675. except PageNotAnInteger:
  676. dept_result_paginator = paginator.page(1)
  677. except EmptyPage:
  678. # If page is out of range (e.g. 9999), deliver last page of results
  679. dept_result_paginator = paginator.page(paginator.num_pages)
  680. dept_result_object_list = dept_result_paginator.object_list
  681. dept_result_object_format_list = []
  682. for dept_result_object in dept_result_object_list:
  683. result_dict = dept_result_object.get_dict()
  684. if simple:
  685. simple_result_dict = dict()
  686. simple_result_dict['id'] = result_dict['id']
  687. simple_result_dict['name'] = result_dict['name']
  688. simple_result_dict['parent_dept_info'] = result_dict['parent_dept_info']
  689. dept_result_object_format_list.append(result_dict)
  690. return True, dict(dept_result_object_format_list=dept_result_object_format_list,
  691. paginator_info=dict(per_page=per_page, page=page, total=paginator.count))
  692. @classmethod
  693. @auto_log
  694. def add_dept(cls, name: str, parent_dept_id: int, leader: str, approver: str, label: str, creator: str) -> tuple:
  695. """
  696. add department
  697. 新增部门
  698. :param name:
  699. :param parent_dept_id:
  700. :param leader:
  701. :param approver:
  702. :param label:
  703. :param creator:
  704. :return:
  705. """
  706. dept_obj = Dept(name=name, parent_dept_id=parent_dept_id, leader=leader, approver=approver, label=label,
  707. creator=creator)
  708. dept_obj.save()
  709. return True, dict(dept_id=dept_obj.id)
  710. @classmethod
  711. @auto_log
  712. def update_dept(cls, dept_id: int, name: str, parent_dept_id: int, leader: str, approver: str, label: str) -> tuple:
  713. """
  714. update department record
  715. 更新部门
  716. :param dept_id:
  717. :param name:
  718. :param parent_dept_id:
  719. :param leader:
  720. :param approver:
  721. :param label:
  722. :return:
  723. """
  724. dept_queryset = Dept.objects.filter(id=dept_id)
  725. if not dept_queryset:
  726. return False, 'dept is not existed or has been deleted'
  727. dept_queryset.update(name=name, parent_dept_id=parent_dept_id, leader=leader, approver=approver, label=label)
  728. return True, ''
  729. @classmethod
  730. @auto_log
  731. def delete_dept(cls, dept_id: int) -> tuple:
  732. """
  733. delete department record
  734. :param dept_id:
  735. :return:
  736. """
  737. dept_queryset = Dept.objects.filter(id=dept_id)
  738. if not dept_queryset:
  739. return False, 'dept is not existed or has been deleted'
  740. dept_queryset.update(is_deleted=1)
  741. return True, ''
  742. @classmethod
  743. @auto_log
  744. def get_token_list(cls, search_value: str, page: int = 1, per_page: int = 10, simple=False) -> tuple:
  745. """
  746. get app permission token list
  747. :param search_value: support app name fuzzy queries
  748. :param page:
  749. :param per_page:
  750. :param simple: 返回简易数据,排除敏感信息
  751. :return:
  752. """
  753. query_params = Q(is_deleted=False)
  754. if search_value:
  755. query_params &= Q(app_name__contains=search_value)
  756. token_objects = Application.objects.filter(query_params)
  757. paginator = Paginator(token_objects, per_page)
  758. try:
  759. token_result_paginator = paginator.page(page)
  760. except PageNotAnInteger:
  761. token_result_paginator = paginator.page(1)
  762. except EmptyPage:
  763. # If page is out of range (e.g. 9999), deliver last page of results
  764. token_result_paginator = paginator.page(paginator.num_pages)
  765. token_result_object_list = token_result_paginator.object_list
  766. token_result_object_format_list = []
  767. for token_result_object in token_result_object_list:
  768. app_list = [token_result_object.app_name for token_result_object in token_result_object_list]
  769. # todo: get token permission workflow list
  770. from service.workflow.workflow_permission_service import workflow_permission_service_ins
  771. flag, result = workflow_permission_service_ins.get_record_list_by_app_list(app_list)
  772. permission_list = result.get('permission_query_set')
  773. token_result_data = token_result_object.get_dict()
  774. token_workflow_list = []
  775. if simple:
  776. token_result_data.pop('token')
  777. else:
  778. for permission in permission_list:
  779. if permission.user == token_result_data.get('app_name'):
  780. token_workflow_list.append(str(permission.workflow_id))
  781. token_result_data['workflow_ids'] = ','.join(token_workflow_list)
  782. token_result_object_format_list.append(token_result_data)
  783. return True, dict(token_result_object_format_list=token_result_object_format_list,
  784. paginator_info=dict(per_page=per_page, page=page, total=paginator.count))
  785. @classmethod
  786. @auto_log
  787. def add_token_record(cls, app_name: str, ticket_sn_prefix: str, workflow_ids: str, username: str) -> tuple:
  788. """
  789. add app token record
  790. :param app_name:
  791. :param ticket_sn_prefix:
  792. :param workflow_ids:
  793. :param username:
  794. :return:
  795. """
  796. import uuid
  797. token = uuid.uuid1()
  798. query_result = Application.objects.filter(app_name=app_name)
  799. if query_result:
  800. return False, 'app_name existed,please alter app_name'
  801. app_token_obj = Application(app_name=app_name, ticket_sn_prefix=ticket_sn_prefix,
  802. token=token, creator=username)
  803. app_token_obj.save()
  804. from apps.workflow.models import WorkflowUserPermission
  805. permission_sql_list = []
  806. if workflow_ids:
  807. for workflow_id in workflow_ids.split(','):
  808. permission_sql_list.append(
  809. WorkflowUserPermission(workflow_id=int(workflow_id), permission='api', user_type='app',
  810. user=app_name))
  811. WorkflowUserPermission.objects.bulk_create(permission_sql_list)
  812. return True, dict(app_token_id=app_token_obj.id)
  813. @classmethod
  814. @auto_log
  815. def update_token_record(cls, app_token_id: int, ticket_sn_prefix: str, workflow_ids: str) -> tuple:
  816. """
  817. update token record
  818. :param app_token_id:
  819. :param ticket_sn_prefix:
  820. :param workflow_ids:
  821. :return:
  822. """
  823. app_token_obj = Application.objects.filter(id=app_token_id).first()
  824. if not app_token_obj:
  825. return False, 'record is not exist or has been deleted'
  826. app_token_obj.ticket_sn_prefix = ticket_sn_prefix
  827. app_token_obj.save()
  828. from service.workflow.workflow_permission_service import workflow_permission_service_ins
  829. workflow_permission_service_ins.update_app_permission(app_token_obj.app_name, workflow_ids)
  830. return True, ''
  831. @classmethod
  832. @auto_log
  833. def del_token_record(cls, app_token_id: int) -> tuple:
  834. """
  835. del app token record
  836. :param app_token_id:
  837. :return:
  838. """
  839. app_token_obj = Application.objects.filter(id=app_token_id).first()
  840. if not app_token_obj:
  841. return False, 'record is not exist or has been deleted'
  842. app_token_obj.is_deleted = True
  843. app_token_obj.save()
  844. from service.workflow.workflow_permission_service import workflow_permission_service_ins
  845. workflow_permission_service_ins.del_app_permission(app_token_obj.app_name)
  846. return True, ''
  847. @classmethod
  848. @auto_log
  849. def admin_permission_check(cls, username: str = '', user_id: int = 0) -> tuple:
  850. """
  851. admin permission check
  852. :param username:
  853. :param user_id:
  854. :return:
  855. """
  856. if username:
  857. flag, result = cls.get_user_by_username(username)
  858. elif user_id:
  859. flag, result = cls.get_user_by_user_id(user_id)
  860. else:
  861. return False, 'username or user_id is needed'
  862. if flag is False:
  863. return False, result
  864. if result.type_id == constant_service_ins.ACCOUNT_TYPE_SUPER_ADMIN:
  865. return True, 'user is admin'
  866. else:
  867. return False, 'user is not admin'
  868. @classmethod
  869. @auto_log
  870. def workflow_admin_permission_check(cls, username: str = '', user_id: int = 0) -> tuple:
  871. """
  872. workflow admin permission check
  873. :param username:
  874. :param user_id:
  875. :return:
  876. """
  877. if username:
  878. flag, result = cls.get_user_by_username(username)
  879. elif user_id:
  880. flag, result = cls.get_user_by_username(username)
  881. else:
  882. return False, 'username or user_id is needed'
  883. if flag is False:
  884. return False, result
  885. if result.type_id == constant_service_ins.ACCOUNT_TYPE_WORKFLOW_ADMIN:
  886. return True, 'user is workflow admin'
  887. if result.type_id == constant_service_ins.ACCOUNT_TYPE_SUPER_ADMIN:
  888. return True, 'user is admin'
  889. else:
  890. return False, 'user is not admin or workflow admin'
  891. @classmethod
  892. @auto_log
  893. def admin_or_workflow_admin_check(cls, username: str = '', user_id: int = 0) -> tuple:
  894. """
  895. admin or workflow admin check
  896. :param username:
  897. :param user_id:
  898. :return:
  899. """
  900. if username:
  901. flag, result = cls.get_user_by_username(username)
  902. elif user_id:
  903. flag, result = cls.get_user_by_username(username)
  904. else:
  905. return False, 'username or user_id is needed'
  906. if flag is False:
  907. return False, result
  908. if result.type_id in (
  909. constant_service_ins.ACCOUNT_TYPE_SUPER_ADMIN, constant_service_ins.ACCOUNT_TYPE_WORKFLOW_ADMIN):
  910. return True, 'user is admin or workflow admin'
  911. else:
  912. return False, 'user is not admin or workflow admin'
  913. @classmethod
  914. @auto_log
  915. def user_type_check(cls, email: str = "", user_id: int = 0, types: str = "") -> tuple:
  916. """
  917. user type check
  918. :param email:
  919. :param user_id:
  920. :param types:
  921. :return:
  922. """
  923. if email:
  924. flag, result = cls.get_user_by_email(email)
  925. elif user_id:
  926. flag, result = cls.get_user_by_user_id(user_id)
  927. else:
  928. return False, 'username or user_id is needed'
  929. if flag is False:
  930. return False, result
  931. if result.type in (types.split(',')):
  932. return True, 'user type matched'
  933. else:
  934. return False, 'user type is not match'
  935. @classmethod
  936. @auto_log
  937. def reset_password(cls, username: str = '', user_id: int = 0) -> tuple:
  938. """
  939. reset user's password
  940. just admin or workflow admin need login loonflow's admin,so just admin and workflow admin can rest password
  941. :param username:
  942. :param user_id:
  943. :return:
  944. """
  945. flag, result = False, ''
  946. if username:
  947. flag, result = cls.get_user_by_username(username)
  948. if user_id:
  949. flag, result = cls.get_user_by_user_id(user_id)
  950. if flag:
  951. user_obj = result
  952. # if user_obj.type_id in (constant_service_ins.ACCOUNT_TYPE_SUPER_ADMIN, constant_service_ins.ACCOUNT_TYPE_WORKFLOW_ADMIN):
  953. password_str = make_password('123456', None, 'pbkdf2_sha256')
  954. user_obj.password = password_str
  955. user_obj.save()
  956. return True, 'password has been reset to 123456'
  957. # else:
  958. # return False, 'just admin or workflow admin can be reset password'
  959. else:
  960. return False, result
  961. @classmethod
  962. @auto_log
  963. def get_user_jwt(cls, email: str) -> tuple:
  964. """
  965. get user's jwt
  966. :param email:
  967. :return:
  968. """
  969. flag, user_obj = cls.get_user_by_email(email)
  970. if flag is False:
  971. return False, user_obj
  972. user_info = user_obj.get_dict()
  973. user_info.pop('last_login')
  974. user_info.pop('created_at')
  975. user_info.pop('updated_at')
  976. user_info.pop('creator')
  977. jwt_salt = settings.JWT_SALT
  978. jwt_info = jwt.encode(
  979. {
  980. 'exp': int(time.time()) + 24*60,
  981. 'iat': int(time.time()),
  982. 'data': user_info}, jwt_salt, algorithm='HS256')
  983. return True, jwt_info
  984. @classmethod
  985. @auto_log
  986. def change_password(cls, username: str, source_password: str, new_password: str) -> tuple:
  987. """
  988. 修改密码
  989. :param username:
  990. :param source_password:
  991. :param new_password:
  992. :return:
  993. """
  994. flag, user_obj = cls.get_user_by_username(username)
  995. if flag is False:
  996. return False, user_obj
  997. user = authenticate(username=username, password=source_password)
  998. if user is None:
  999. return False, '原密码输入错误,不允许修改密码'
  1000. new_password_format = make_password(new_password, None, 'pbkdf2_sha256')
  1001. user_obj.password = new_password_format
  1002. user_obj.save()
  1003. return True, '密码修改成功'
  1004. account_base_service_ins = AccountBaseService()