Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change auth to use projects. #1649

Merged
merged 5 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions dongtai_common/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.utils.translation import gettext_lazy as _

from dongtai_common.models.department import Department
from dongtai_conf.patch import patch_point


class PermissionsMixin(models.Model):
Expand Down Expand Up @@ -109,3 +110,11 @@ def get_using_department(self):
if self.using_department:
return self.using_department
return self.get_department()

def get_projects(self) -> QuerySet:
from dongtai_common.models.project import IastProject

queryset = IastProject.objects.none()
if self.is_system_admin:
return IastProject.objects.all()
return patch_point(queryset)
16 changes: 7 additions & 9 deletions dongtai_web/aggr_vul/app_vul_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ def post(self, request):
}
ser = AggregationArgsSerializer(data=request.data)
# 获取用户权限
departments = request.user.get_relative_department()
queryset = IastVulnerabilityModel.objects.filter(
is_del=0, project_id__gt=0, project__department__in=departments
)
projects = request.user.get_projects()
queryset = IastVulnerabilityModel.objects.filter(is_del=0, project_id__gt=0, project__in=projects)

try:
if ser.is_valid(True):
Expand Down Expand Up @@ -148,7 +146,7 @@ def post(self, request):
order_list.append(order_type_desc + order_type)
es_query["order"] = order_type_desc + order_type
if ELASTICSEARCH_STATE:
vul_data = get_vul_list_from_elastic_search(departments, page=page, page_size=page_size, **es_query)
vul_data = get_vul_list_from_elastic_search(projects, page=page, page_size=page_size, **es_query)
else:
vul_data = queryset.values(*tuple(fields)).order_by(*tuple(order_list))[begin_num:end_num]
except ValidationError as e:
Expand Down Expand Up @@ -214,7 +212,7 @@ def set_vul_inetration(end: dict[str, Any], user_id: int) -> None:


def get_vul_list_from_elastic_search(
departments,
projects,
project_ids=None,
project_version_ids=None,
hook_type_ids=None,
Expand Down Expand Up @@ -246,9 +244,9 @@ def get_vul_list_from_elastic_search(

from dongtai_common.models.strategy import IastStrategyModel

department_ids = list(departments.values_list("id", flat=True))
auth_project_ids = list(project_ids.values_list("id", flat=True))
must_query = [
Q("terms", department_id=department_ids),
Q("terms", bind_project_id=auth_project_ids),
Q("terms", is_del=[0]),
Q("range", bind_project_id={"gt": 0}),
Q("range", strategy_id={"gt": 0}),
Expand Down Expand Up @@ -290,7 +288,7 @@ def get_vul_list_from_elastic_search(
a = Q("bool", must=must_query)
hashkey = make_hash(
[
department_ids,
auth_project_ids,
project_ids,
project_version_ids,
hook_type_ids,
Expand Down
5 changes: 2 additions & 3 deletions dongtai_web/views/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from rest_framework.serializers import ValidationError

from dongtai_common.endpoint import R, UserEndPoint
from dongtai_common.models.project import IastProject, ProjectStatus
from dongtai_common.models.project import ProjectStatus
from dongtai_web.serializers.project import (
ProjectSerializer,
get_agent_count,
Expand Down Expand Up @@ -69,8 +69,7 @@ def get(self, request):
except ValidationError as e:
return R.failure(data=e.detail)

department = request.user.get_relative_department()
queryset = IastProject.objects.filter(department__in=department).order_by("-latest_time")
queryset = request.user.get_projects().order_by("-latest_time")
if name:
queryset = queryset.filter(name__icontains=name)
if status is not None:
Expand Down
1 change: 1 addition & 0 deletions dongtai_web/views/sca_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class ScaSummary(UserEndPoint):
tags=[_("Component")],
summary=_("Component Summary (with project)"),
description=_("Use the specified project information to get the corresponding component summary"),
deprecated=True,
response_schema=_ResponseSerializer,
)
def post(self, request):
Expand Down
1 change: 1 addition & 0 deletions dongtai_web/views/scas.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class ScaList(UserEndPoint):
tags=[_("Component")],
summary=_("Component List (with project)"),
description=_("use the specified project information to obtain the corresponding component."),
deprecated=True,
response_schema=_ResponseSerializer,
)
def post(self, request):
Expand Down
1 change: 0 additions & 1 deletion dongtai_web/views/vul_count_for_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def get(self, request):
agent_name = request.query_params.get("name")
departmenttoken = request.query_params.get("departmenttoken", "")
projectname = request.query_params.get("projectname", "")
request.user.get_relative_department()
if not agent_name:
return R.failure(msg=_("Please input agent name."))
departmenttoken = departmenttoken.replace("GROUP", "")
Expand Down
12 changes: 6 additions & 6 deletions dongtai_web/views/vul_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ def parse_request(method, uri, query_param, protocol, header, data):
def parse_response(header, body):
return f"{header}\n\n{body}"

def get_vul(self, department):
vul = IastVulnerabilityModel.objects.filter(id=self.vul_id, project__department__in=department).first()
def get_vul(self, projects):
vul = IastVulnerabilityModel.objects.filter(id=self.vul_id, project__in=projects).first()
hook_type = HookType.objects.filter(pk=vul.hook_type_id).first() if vul is not None else None
hook_type_name = hook_type.name if hook_type else None
strategy = IastStrategyModel.objects.filter(pk=vul.strategy_id).first()
Expand Down Expand Up @@ -408,11 +408,11 @@ def get(self, request, id):
:return:
"""
self.vul_id = id
self.departments = request.user.get_relative_department()
projects = request.user.get_projects()
try:
return R.success(
data={
"vul": self.get_vul(self.departments),
"vul": self.get_vul(projects),
"server": self.get_server(),
"strategy": self.get_strategy(),
}
Expand Down Expand Up @@ -464,10 +464,10 @@ def get(
id,
):
self.vul_id = id
self.departments = request.user.get_relative_department()
projects = request.user.get_projects()
try:
data = {
"vul": self.get_vul(self.departments),
"vul": self.get_vul(projects),
"server": self.get_server(),
"strategy": self.get_strategy(),
}
Expand Down
1 change: 0 additions & 1 deletion dongtai_web/views/vul_list_for_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ def get(self, request):
agent_name = request.query_params.get("name", None)
departmenttoken = request.query_params.get("departmenttoken", "")
projectname = request.query_params.get("projectname", "")
request.user.get_relative_department()
if not agent_name:
return R.failure(msg=_("Please input agent name."))
departmenttoken = departmenttoken.replace("GROUP", "")
Expand Down
4 changes: 2 additions & 2 deletions dongtai_web/views/vul_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ def post(self, request):
status_id = request.data.get("status_id")
user = request.user
user_id = user.id
department = request.user.get_relative_department()
projects = request.user.get_projects()
if not (isinstance(vul_id, int) or isinstance(vul_ids, list)):
return R.failure()
if not vul_ids:
vul_ids = [vul_id]
queryset = IastVulnerabilityModel.objects.filter(is_del=0, project__department__in=department)
queryset = IastVulnerabilityModel.objects.filter(is_del=0, project__in=projects)
vul_status = IastVulnerabilityStatus.objects.filter(pk=status_id).first()
if vul_status:
queryset_status = queryset.filter(id__in=vul_ids)
Expand Down