def template_roles_remove_view(request, template_id): try: if request.method == 'POST': data = request.POST.dict() target = data.get('target') role_id = data.get('role_id') if target == 'user': status, msg = UserAndTeamRoleAPI.delete_role_from_user( request.user, int(data.get('user_id')), int(role_id)) if not status: return HttpResponse(msg) return HttpResponseRedirect( reverse('project:template_roles', kwargs={'template_id': template_id})) elif target == 'team': status, msg = UserAndTeamRoleAPI.delete_role_from_team( request.user, int(data.get('team_id')), int(role_id)) if not status: return HttpResponse(msg) return HttpResponseRedirect( reverse('project:template_roles', kwargs={'template_id': template_id})) else: return HttpResponse('404') except Exception as e: return HttpResponse(str(e))
def template_roles_view(request, template_id): try: status, msg, tem = TemplateAPI.get(request.user, template_id) if not status: return HttpResponse(msg) users = tem.users users_with_roles = [] for user in users: status, msg, roles = UserAndTeamRoleAPI.get_user_roles_on_resource( request.user, user.id, RS_TEM, template_id) if status: users_with_roles.append([user, roles]) teams = tem.teams teams_with_roles = [] for team in teams: status, msg, roles = UserAndTeamRoleAPI.get_team_roles_on_resource( request.user, team.id, RS_TEM, template_id) if status: teams_with_roles.append([team, roles]) all_users = tem.organization.users context = { 'app': template_app, 'template': tem, 'pm': InternalAPI.get_user_permissions_on_resource( request.user, RS_TEM, tem.id), 'users_with_roles': users_with_roles, 'teams_with_roles': teams_with_roles, 'all_users': all_users, 'all_teams': tem.organization.team_set.all(), 'roles': tem.roles.filter(resource_type=RS_TEM), } return render(request, 'project/template_roles.html', context) except Exception as e: return HttpResponse(str(e))
def project_roles_view(request, project_id): try: status, errmsg, pro = ProjectAPI.get(request.user, project_id) if not status: return render(request, 'error.html', {ARK_ERRMSG: errmsg}) users = pro.users users_with_roles = list() for user in users: status, errmsg, roles = \ UserAndTeamRoleAPI.get_user_roles_on_resource( user=request.user, target_user_id=user.id, resource_type=RS_PROJECT, resource_id=project_id ) if status: users_with_roles.append([user, roles]) teams = pro.teams teams_with_roles = list() for team in teams: status, errmsg, roles = \ UserAndTeamRoleAPI.get_team_roles_on_resource( user=request.user, target_team_id=team.id, resource_type=RS_PROJECT, resource_id=project_id ) if status: teams_with_roles.append([team, roles]) all_users = pro.organization.users all_teams = pro.organization.team_set.all() pm_list = InternalAPI.get_user_permissions_on_resource( user=request.user, resource_type=RS_PROJECT, resource_id=pro.id) context = { 'app': project_app, 'project': pro, 'pm': pm_list, 'users_with_roles': users_with_roles, 'teams_with_roles': teams_with_roles, 'all_users': all_users, 'all_teams': all_teams, 'roles': pro.roles.filter(resource_type=RS_PROJECT), } return render(request, 'project/project_roles.html', context) except Exception as e: return render(request, 'error.html', {ARK_ERRMSG: str(e)})
def inventory_roles_view(request, inventory_id): try: status, msg, inv = InventoryAPI.get(request.user, inventory_id) if not status: return HttpResponse(msg) users = inv.users users_with_roles = [] for user in users: status, msg, roles = UserAndTeamRoleAPI.get_user_roles_on_resource( request.user, user.id, RS_INV, inventory_id ) if status: users_with_roles.append([user, roles]) teams = inv.teams teams_with_roles = [] for team in teams: status, msg, roles = UserAndTeamRoleAPI.get_team_roles_on_resource( request.user, team.id, RS_INV, inventory_id ) if status: teams_with_roles.append([team, roles]) all_users = inv.organization.users context = { 'app': app, 'inventory': inv, 'pm': InternalAPI.get_user_permissions_on_resource( request.user, RS_INV, inv.id ), 'users_with_roles': users_with_roles, 'teams_with_roles': teams_with_roles, 'all_users': all_users, 'all_teams': inv.organization.team_set.all(), 'roles': inv.roles.filter(resource_type=RS_INV), } return render(request, 'inventory/inventory_roles.html', context) except Exception as e: return HttpResponse(str(e))
def user_role_view(request, target_user_id): context = dict() user = request.user try: if request.method == 'GET': status, errmsg, result = UserAndTeamRoleAPI.get_user_role( user=user, target_user_id=target_user_id ) if not status: context = { 'errmsg': errmsg } return render(request, 'error.html', context) status, errmsg, target_user = UserAPI.get( user=user, target_user_id=target_user_id ) if not status: context = { 'errmsg': errmsg } return render(request, 'error.html', context) is_login_user_system_admin = Helper.is_system_admin(user) is_target_user_system_admin = Helper.is_system_admin(target_user) pm_list = InternalAPI.get_user_permissions_on_resource( user=user, resource_type=RS_SYS ) if PM_ADD_SYSTEM_ROLE in pm_list: pm_add_system_role = pm_list[PM_ADD_SYSTEM_ROLE] else: pm_add_system_role = False pm_add_organization_role = False for org in user.organizations: org_pm_list = InternalAPI.get_user_permissions_on_resource( user=user, resource_type=RS_ORGANIZATION, resource_id=org.id ) if PM_ADD_ORGANIZATION_ROLE in org_pm_list: pm_add_organization_role = org_pm_list[PM_ADD_ORGANIZATION_ROLE] else: pm_add_organization_role = False context = { 'user_role': result, 'target_user': target_user, 'is_login_user_system_admin': is_login_user_system_admin, 'is_target_user_system_admin': is_target_user_system_admin, 'pm_add_system_role': pm_add_system_role, 'pm_add_organization_role': pm_add_organization_role } return render(request, 'user/user_info_role.html', context) except Exception as e: context[ARK_ERRMSG] = str(e) return render(request, 'error.html', context)
def inventory_roles_add_view(request, inventory_id): try: if request.method == 'POST': data = request.POST target = data.get('target') role_id = data.get('role_id') if target == 'user': user_id_list = data.getlist('user_id') for user_id in user_id_list: status, msg = UserAndTeamRoleAPI.add_role_to_user( request.user, int(user_id), int(role_id) ) if not status: return HttpResponse(msg) return HttpResponseRedirect( reverse( 'inventory:inventory_roles', kwargs={'inventory_id': inventory_id} ) ) elif target == 'team': team_id_list = data.getlist('team_id') for team_id in team_id_list: status, msg = UserAndTeamRoleAPI.add_role_to_team( request.user, int(team_id), int(role_id) ) if not status: return HttpResponse(msg) return HttpResponseRedirect( reverse( 'inventory:inventory_roles', kwargs={'inventory_id': inventory_id} ) ) else: return HttpResponse('404') except Exception as e: return HttpResponse(str(e))
def team_info_user_add_view(request, team_id): ''' 向团队内添加用户的view :param request: :param team_id: :return: ''' user = request.user try: if request.method == 'GET': status, errmsg, team = TeamAPI.get(user=user, team_id=team_id) if not status: return HttpResponse(errmsg) org_users = team.organization.users # 列出该组织内没有团队的用户 all_teams = Team.objects.all() org_users_not_has_team = org_users.exclude( roles__team__in=all_teams).all() pm_list = InternalAPI.get_user_permissions_on_resource( user=user, resource_type=RS_TEAM, resource_id=team.id) if PM_ADD_TEAM_ROLE not in pm_list: pm_add_team_role = False else: pm_add_team_role = pm_list[PM_ADD_TEAM_ROLE] if not pm_add_team_role: return HttpResponse(ARK_ERRMSG_CONTENT[1201]) context = { 'app': app, 'organization': team.organization, 'organization_id': team.organization.id, 'team': team, 'org_users_not_has_team': org_users_not_has_team, } return render(request, 'organization/team_info_user_add.html', context) else: if request.is_ajax() and request.method == 'POST': ajax_data = request.POST.get('data') data = json.loads(ajax_data) user_ids = data.get('user_ids') team_role = int(data.get('team_role')) for user_id in user_ids: status, errmsg = UserAndTeamRoleAPI.add_role_to_user( user=user, target_user_id=int(user_id), role_id=team_role) if not status: return JsonResponse({ ARK_STATUS: False, ARK_ERRMSG: errmsg }) else: return JsonResponse({ARK_STATUS: True}) except Exception as e: return HttpResponse(str(e))
def project_roles_remove_view(request, project_id): try: if request.is_ajax(): if request.method == 'POST': ajax_data = request.POST.get('data') data = json.loads(ajax_data) target = data.get('target') role_id = int(data.get('role_id')) if target == 'user': status, errmsg = UserAndTeamRoleAPI.delete_role_from_user( user=request.user, target_user_id=int(data.get('user_id')), role_id=role_id) if not status: return JsonResponse({ ARK_STATUS: False, ARK_ERRMSG: errmsg }) return JsonResponse({ARK_STATUS: True}) elif target == 'team': status, errmsg = UserAndTeamRoleAPI.delete_role_from_team( user=request.user, target_team_id=int(data.get('team_id')), role_id=role_id) if not status: return JsonResponse({ ARK_STATUS: False, ARK_ERRMSG: errmsg }) return JsonResponse({ARK_STATUS: True}) else: return JsonResponse({ ARK_STATUS: False, ARK_ERRMSG: '既不是User也不是Team' }) else: return render(request, 'error.html', {ARK_ERRMSG: 'Method is not post'}) except Exception as e: return JsonResponse({ARK_STATUS: False, ARK_ERRMSG: str(e)})
def organization_user_add_view(request, organization_id): user = request.user try: if request.method == 'GET': status, errmsg, organization = OrganizationAPI.get( user=user, organization_id=organization_id) if not status: return HttpResponse(errmsg) org_users = organization.users pm_list = InternalAPI.get_user_permissions_on_resource( user=user, resource_type=RS_ORG, resource_id=organization.id) if PM_ADD_ORGANIZATION_ROLE not in pm_list: pm_add_organization_role = False else: pm_add_organization_role = pm_list[PM_ADD_ORGANIZATION_ROLE] # 列出没有组织的普通用户 users_not_has_org = None all_orgs = Organization.objects.all() if pm_add_organization_role: users_not_has_org = User.objects.\ exclude(roles__organization__in=all_orgs).\ exclude(roles__name=RO_SYS_ADMIN).all().distinct() else: return HttpResponse(ARK_ERRMSG_CONTENT[1201]) context = { 'app': app, 'organization': organization, 'organization_id': organization_id, 'org_users': org_users, 'users_not_has_org': users_not_has_org } return render(request, 'organization/organization_info_user_add.html', context) else: if request.is_ajax() and request.method == 'POST': ajax_data = request.POST.get('data') data = json.loads(ajax_data) user_ids = data.get('user_ids') org_role = int(data.get('org_role')) for user_id in user_ids: status, errmsg, = UserAndTeamRoleAPI.add_role_to_user( user=user, target_user_id=int(user_id), role_id=org_role) if not status: return JsonResponse({ ARK_STATUS: False, ARK_ERRMSG: errmsg }) else: return JsonResponse({ARK_STATUS: True}) except Exception as e: return HttpResponse(str(e))
def project_roles_add_view(request, project_id): try: if request.method == 'POST': data = request.POST target = data.get('target') role_id = data.get('role_id') if target == 'user': user_id_list = data.getlist('user_id') for user_id in user_id_list: status, errmsg = UserAndTeamRoleAPI.add_role_to_user( request.user, int(user_id), int(role_id)) if not status: return render(request, 'error.html', {ARK_ERRMSG: errmsg}) return HttpResponseRedirect( reverse('project:project_roles', kwargs={'project_id': project_id})) elif target == 'team': team_id_list = data.getlist('team_id') for team_id in team_id_list: status, errmsg = UserAndTeamRoleAPI.add_role_to_team( request.user, int(team_id), int(role_id)) if not status: return render(request, 'error.html', {ARK_ERRMSG: errmsg}) return HttpResponseRedirect( reverse('project:project_roles', kwargs={'project_id': project_id})) else: return HttpResponse('404') except Exception as e: return render(request, 'error.html', {ARK_ERRMSG: str(e)})
def organization_user_role_remove_view(request): try: if request.is_ajax() and request.method == 'POST': user = request.user ajax_data = request.POST.get('data') data = json.loads(ajax_data) target_user_id = int(data.get('user_id')) role_id = int(data.get('role_id')) status, errmsg = UserAndTeamRoleAPI.delete_role_from_user( user=user, target_user_id=target_user_id, role_id=role_id) if not status: return JsonResponse({ARK_STATUS: False, ARK_ERRMSG: errmsg}) return JsonResponse({ARK_STATUS: True}) except Exception as e: return JsonResponse({ARK_STATUS: False, ARK_ERRMSG: str(e)})
def team_info_role_remove_view(request): user = request.user try: if request.is_ajax(): if request.method == 'POST': ajax_data = request.POST.get('data') data = json.loads(ajax_data) target_team_id = int(data.get('target_team_id')) role_id = int(data.get('role_id')) status, errmsg = UserAndTeamRoleAPI.delete_role_from_team( user=user, target_team_id=target_team_id, role_id=role_id) if not status: return JsonResponse({ ARK_STATUS: False, ARK_ERRMSG: '权限不足' }) return JsonResponse({ARK_STATUS: True}) except Exception as e: return JsonResponse({ARK_STATUS: False, ARK_ERRMSG: str(e)})
def team_info_role_view(request, team_id): context = dict() user = request.user try: if request.method == 'GET': status, errmsg, result = UserAndTeamRoleAPI.get_team_role( user=user, team_id=team_id) if not status: context = {'errmsg': errmsg} return render(request, 'error.html', context) status, errmsg, team = TeamAPI.get(user=user, team_id=team_id) if not status: context = {'errmsg': errmsg} return render(request, 'error.html', context) # is_system_admin = Helper.is_system_admin(target_user) pm_list = InternalAPI.get_user_permissions_on_resource( user=user, resource_type=RS_SYS) if PM_ADD_SYSTEM_ROLE in pm_list: pm_add_system_role = pm_list[PM_ADD_SYSTEM_ROLE] else: pm_add_system_role = False pm_add_team_role = False team_pm_list = InternalAPI.get_user_permissions_on_resource( user=user, resource_type=RS_TEAM, resource_id=team_id) if PM_ADD_TEAM_ROLE in team_pm_list: pm_add_team_role = team_pm_list[PM_ADD_TEAM_ROLE] context = { 'app': app, 'team_role': result, 'team': team, 'pm_add_system_role': pm_add_system_role, 'pm_add_team_role': pm_add_team_role } return render(request, 'organization/team_info_role.html', context) except Exception as e: context[ARK_ERRMSG] = str(e) return render(request, 'error.html', context)
def user_role_add_view(request, target_user_id): user = request.user context = dict() try: if request.method == 'GET': status, errmsg, target_user = UserAPI.get( user=user, target_user_id=target_user_id ) if not status: context = { 'errmsg': errmsg } return render(request, 'error.html', context) # 项目 status, errmsg, projects = ProjectAPI.all(user) if not status: context = { 'errmsg': errmsg } return render(request, 'error.html', context) for org in target_user.organizations: projects = projects.filter(organization=org).distinct() # 仓库 status, errmsg, inventories = InventoryAPI.all(user) if not status: context = { 'errmsg': errmsg } return render(request, 'error.html', context) for org in target_user.organizations: inventories = inventories.filter(organization=org).distinct() # 模板 status, errmsg, templates = TemplateAPI.all(user) if not status: context = { 'errmsg': errmsg } return render(request, 'error.html', context) for org in target_user.organizations: templates = templates.filter(project__organization=org).distinct() # 系统 is_system_admin = Helper.is_system_admin(user) context = { 'is_system_admin': is_system_admin, 'projects': projects, 'inventories': inventories, 'templates': templates, 'target_user': target_user } return render(request, 'user/user_info_role_add.html', context) if request.is_ajax(): if request.method == 'POST': status, errmsg, target_user = UserAPI.get( user=user, target_user_id=target_user_id ) if not status: return JsonResponse({ARK_STATUS: False, ARK_ERRMSG: errmsg}) ajax_data = request.POST.get('data') data = json.loads(ajax_data) project_ids = data.get('project_ids') project_role = data.get('project_role') for project_id in project_ids: role = InternalAPI.get_role( resource_type=RS_PROJECT, resource_name=project_role, resource_id=int(project_id) ) status, errmsg = UserAndTeamRoleAPI.add_role_to_user( user, target_user_id, role.id ) if not status: return JsonResponse( {ARK_STATUS: False, ARK_ERRMSG: errmsg} ) inventory_ids = data.get('inventory_ids') inventory_role = data.get('inventory_role') for inventory_id in inventory_ids: role = InternalAPI.get_role( resource_type=RS_INVENTORY, resource_name=inventory_role, resource_id=int(inventory_id) ) status, errmsg = UserAndTeamRoleAPI.add_role_to_user( user, target_user_id, role.id ) if not status: return JsonResponse( {ARK_STATUS: False, ARK_ERRMSG: errmsg} ) template_ids = data.get('template_ids') template_role = data.get('template_role') for template_id in template_ids: role = InternalAPI.get_role( resource_type=RS_TEMPLATE, resource_name=template_role, resource_id=int(template_id) ) status, errmsg = UserAndTeamRoleAPI.add_role_to_user( user, target_user_id, role.id ) if not status: return JsonResponse( {ARK_STATUS: False, ARK_ERRMSG: errmsg} ) system_admin = bool(data.get('system_admin')) if system_admin: role = InternalAPI.get_role( resource_type=RS_SYSTEM, resource_name=RO_SYSTEM_ADMIN ) status, errmsg = UserAndTeamRoleAPI.add_role_to_user( user, target_user_id, role.id ) if not status: return JsonResponse( {ARK_STATUS: False, ARK_ERRMSG: errmsg} ) return JsonResponse({ARK_STATUS: True}) except Exception as e: context['errmsg'] = str(e) return render(request, 'error.html', context)
def team_info_role_add_view(request, team_id): user = request.user context = dict() try: if request.method == 'GET': status, errmsg, team = TeamAPI.get(user=user, team_id=team_id) if not status: context = {'errmsg': errmsg} return render(request, 'error.html', context) # 项目 status, errmsg, projects = ProjectAPI.all(user) if not status: context = {'errmsg': errmsg} return render(request, 'error.html', context) projects = projects.filter( organization=team.organization).distinct() # 仓库 status, errmsg, inventories = InventoryAPI.all(user) if not status: context = {'errmsg': errmsg} return render(request, 'error.html', context) inventories = inventories.filter(organization=team.organization).\ distinct() # 模板 status, errmsg, templates = TemplateAPI.all(user) if not status: context = {'errmsg': errmsg} return render(request, 'error.html', context) templates = templates.filter(project__organization=team.organization).\ distinct() context = { 'app': app, 'projects': projects, 'inventories': inventories, 'templates': templates, 'team': team } return render(request, 'organization/team_info_role_add.html', context) if request.is_ajax(): if request.method == 'POST': ajax_data = request.POST.get('data') data = json.loads(ajax_data) project_ids = data.get('project_ids') project_role = data.get('project_role') for project_id in project_ids: role = InternalAPI.get_role(resource_type=RS_PROJECT, resource_name=project_role, resource_id=int(project_id)) status, errmsg = UserAndTeamRoleAPI.add_role_to_team( user, team_id, role.id) if not status: return JsonResponse({ ARK_STATUS: False, ARK_ERRMSG: errmsg }) inventory_ids = data.get('inventory_ids') inventory_role = data.get('inventory_role') for inventory_id in inventory_ids: role = InternalAPI.get_role(resource_type=RS_INVENTORY, resource_name=inventory_role, resource_id=int(inventory_id)) status, errmsg = UserAndTeamRoleAPI.add_role_to_team( user, team_id, role.id) if not status: return JsonResponse({ ARK_STATUS: False, ARK_ERRMSG: errmsg }) template_ids = data.get('template_ids') template_role = data.get('template_role') for template_id in template_ids: role = InternalAPI.get_role(resource_type=RS_TEMPLATE, resource_name=template_role, resource_id=int(template_id)) status, errmsg = UserAndTeamRoleAPI.add_role_to_team( user, team_id, role.id) if not status: return JsonResponse({ ARK_STATUS: False, ARK_ERRMSG: errmsg }) return JsonResponse({ARK_STATUS: True}) except Exception as e: context['errmsg'] = str(e) return render(request, 'error.html', context)