Skip to content

Commit

Permalink
Merge pull request #1428 from TencentBlueKing/feat_sms
Browse files Browse the repository at this point in the history
minor: 限制短信邀请次数; 移除 meta 敏感信息 --story=119850966
  • Loading branch information
benero authored Oct 9, 2024
2 parents 8e5cde5 + 0d9629d commit 1100b45
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 9 deletions.
3 changes: 3 additions & 0 deletions config/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,3 +965,6 @@ def redirect_func(request):
NOTICE_IGNORE_LIST = os.getenv("BKAPP_NOTICE_IGNORE_LIST", [])
if isinstance(NOTICE_IGNORE_LIST, str):
NOTICE_IGNORE_LIST = [i.lower().strip() for i in NOTICE_IGNORE_LIST.split(",")]

# SMS 邀请评价限额
TICKET_INVITE_SMS_COUNT = int(os.getenv("BKAPP_TICKET_INVITE_SMS_COUNT", 10))
7 changes: 7 additions & 0 deletions itsm/ticket/models/ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
from platform_config import BaseTicket

from .basic import Model
from ..utils import filter_sensitive_info
from ...auth_iam.utils import IamRequest


Expand Down Expand Up @@ -1399,6 +1400,12 @@ class Meta:
def __unicode__(self):
return "{}({})".format(self.title, self.sn)

def get_meta(self, is_filter_sensitive_info=True):
"""获取单据 meta 信息"""
if not is_filter_sensitive_info:
return self.meta
return filter_sensitive_info(self.meta)

@property
def task_schemas(self):
# todo 测试后删除
Expand Down
11 changes: 5 additions & 6 deletions itsm/ticket/serializers/ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
TaskFieldSerializer,
)
from itsm.ticket.tasks import remark_notify
from itsm.ticket.utils import compute_list_difference, get_user_profile
from itsm.ticket.utils import compute_list_difference, get_user_profile, filter_sensitive_info
from itsm.ticket.validators import CreateTicketValidator, StateOperateValidator
from itsm.ticket_status.models import TicketStatus
from itsm.workflow.models import WorkflowVersion
Expand Down Expand Up @@ -675,7 +675,7 @@ def to_client_representation(self):
)
supervisors = supervisors.split(",") if supervisors else []
real_supervisors = supervisors + [inst["creator"]]
inst["meta"] = real_ticket["meta"]
inst["meta"] = filter_sensitive_info(real_ticket["meta"])

current_status = real_ticket["current_status"]
# 等待审批的条件为在审批节点 并且 单据处在非挂起状态
Expand Down Expand Up @@ -874,16 +874,15 @@ def to_representation(self, inst):
# 当前步骤、单据状态、优先级来源母单
master_ticket = inst.get_master_ticket()
master_or_self_ticket = master_ticket if master_ticket else inst

if "headers" in master_or_self_ticket.meta:
master_or_self_ticket.meta.pop("headers")

meta = master_or_self_ticket.get_meta()

data.update(
current_status=master_or_self_ticket.current_status,
current_status_display=master_or_self_ticket.current_status_display,
current_steps=master_or_self_ticket.brief_current_steps,
priority_name=master_or_self_ticket.priority_name,
meta=master_or_self_ticket.meta,
meta=meta
)

can_comment = inst.can_comment(username) or is_email_invite_token
Expand Down
9 changes: 9 additions & 0 deletions itsm/ticket/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import copy
import json
from typing import Dict

import jmespath
import requests
Expand Down Expand Up @@ -224,3 +226,10 @@ def get_user_profile(username):
except Exception:
profile = {"name": "", "phone": "", "departments": []}
return profile


def filter_sensitive_info(ticket_meta: Dict):
meta = copy.deepcopy(ticket_meta)
meta.pop("headers", None)
meta.pop("callback_url", None)
return meta
12 changes: 10 additions & 2 deletions itsm/ticket/validators/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""

from django.conf import settings
from django.utils.translation import ugettext as _
from rest_framework import serializers

from common.log import logger
from itsm.component.utils.basic import Regex
from itsm.component.utils.client_backend_query import get_bk_users
from itsm.role.models import UserRole
from itsm.ticket.models import Ticket, TicketComment
from itsm.ticket.models import Ticket, TicketComment, TicketCommentInvite


def days_validate(days):
Expand Down Expand Up @@ -102,6 +102,14 @@ def sms_invite_validate(ticket, numbers, invitor):

if not ticket.can_comment(invitor):
raise serializers.ValidationError(_('抱歉,您无权发送评价邀请'))

if settings.TICKET_INVITE_SMS_COUNT:
if len(numbers) > settings.TICKET_INVITE_SMS_COUNT:
raise serializers.ValidationError(_("SMS 发送评价邀请超过限额"))

invite_count = TicketCommentInvite.objects.filter(comment__ticket__id=ticket.id).count()
if invite_count > settings.TICKET_INVITE_SMS_COUNT:
raise serializers.ValidationError(_("SMS 发送评价邀请超过限额"))

for number in numbers:
try:
Expand Down
2 changes: 1 addition & 1 deletion itsm/ticket/views/ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ def send_sms(self, request, *args, **kwargs):
if fail_numbers
else "success",
"data": links,
"code": "SEND_SMS_FAILED",
"code": "OK" if len(fail_numbers) == 0 else "SEND_SMS_FAILED",
}
)

Expand Down

0 comments on commit 1100b45

Please sign in to comment.