Пример #1
0
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))
Пример #2
0
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))
Пример #3
0
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))
Пример #4
0
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)})
Пример #5
0
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)