2017-03-15 16:43:43 +00:00
|
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
|
from __future__ import absolute_import, unicode_literals
|
2020-09-27 08:02:44 +00:00
|
|
|
|
from datetime import timedelta
|
2021-09-24 02:21:56 +00:00
|
|
|
|
from collections import defaultdict
|
2017-03-15 16:43:43 +00:00
|
|
|
|
|
2020-09-29 05:34:55 +00:00
|
|
|
|
from django.db.transaction import atomic
|
2020-11-20 08:12:24 +00:00
|
|
|
|
from django.conf import settings
|
2017-03-15 16:43:43 +00:00
|
|
|
|
from celery import shared_task
|
2021-09-24 02:21:56 +00:00
|
|
|
|
|
|
|
|
|
from orgs.utils import tmp_to_root_org
|
2020-08-16 15:08:58 +00:00
|
|
|
|
from common.utils import get_logger
|
2021-10-20 11:45:37 +00:00
|
|
|
|
from common.utils.timezone import local_now, dt_formatter, dt_parser
|
2020-11-20 08:12:24 +00:00
|
|
|
|
from ops.celery.decorator import register_as_period_task
|
2021-09-24 02:21:56 +00:00
|
|
|
|
from perms.notifications import (
|
2021-10-22 12:06:16 +00:00
|
|
|
|
PermedAssetsWillExpireUserMsg, AssetPermsWillExpireForOrgAdminMsg,
|
2021-10-20 11:45:37 +00:00
|
|
|
|
PermedAppsWillExpireUserMsg, AppPermsWillExpireForOrgAdminMsg
|
2021-09-24 02:21:56 +00:00
|
|
|
|
)
|
|
|
|
|
from perms.models import AssetPermission, ApplicationPermission
|
2021-02-05 05:29:29 +00:00
|
|
|
|
from perms.utils.asset.user_permission import UserGrantedTreeRefreshController
|
2017-03-15 16:43:43 +00:00
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
|
|
2020-11-20 08:12:24 +00:00
|
|
|
|
@register_as_period_task(interval=settings.PERM_EXPIRED_CHECK_PERIODIC)
|
2021-07-14 06:58:40 +00:00
|
|
|
|
@shared_task()
|
2020-09-29 05:34:55 +00:00
|
|
|
|
@atomic()
|
2021-09-24 02:21:56 +00:00
|
|
|
|
@tmp_to_root_org()
|
2020-09-27 08:02:44 +00:00
|
|
|
|
def check_asset_permission_expired():
|
|
|
|
|
"""
|
|
|
|
|
这里的任务要足够短,不要影响周期任务
|
|
|
|
|
"""
|
2020-09-29 05:34:55 +00:00
|
|
|
|
from settings.models import Setting
|
|
|
|
|
|
|
|
|
|
setting_name = 'last_asset_perm_expired_check'
|
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
end = local_now()
|
2020-09-29 05:34:55 +00:00
|
|
|
|
default_start = end - timedelta(days=36000) # Long long ago in china
|
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
defaults = {'value': dt_formatter(default_start)}
|
2020-09-29 05:34:55 +00:00
|
|
|
|
setting, created = Setting.objects.get_or_create(
|
|
|
|
|
name=setting_name, defaults=defaults
|
|
|
|
|
)
|
|
|
|
|
if created:
|
|
|
|
|
start = default_start
|
|
|
|
|
else:
|
|
|
|
|
start = dt_parser(setting.value)
|
2021-10-20 11:45:37 +00:00
|
|
|
|
setting.value = dt_formatter(end)
|
2020-09-29 05:34:55 +00:00
|
|
|
|
setting.save()
|
|
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
|
asset_perm_ids = AssetPermission.objects.filter(
|
2020-09-29 05:34:55 +00:00
|
|
|
|
date_expired__gte=start, date_expired__lte=end
|
2020-09-27 08:02:44 +00:00
|
|
|
|
).distinct().values_list('id', flat=True)
|
2021-02-05 05:29:29 +00:00
|
|
|
|
asset_perm_ids = list(asset_perm_ids)
|
|
|
|
|
logger.info(f'>>> checking {start} to {end} have {asset_perm_ids} expired')
|
|
|
|
|
UserGrantedTreeRefreshController.add_need_refresh_by_asset_perm_ids_cross_orgs(asset_perm_ids)
|
2021-09-24 02:21:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@register_as_period_task(crontab='0 10 * * *')
|
|
|
|
|
@shared_task()
|
|
|
|
|
@atomic()
|
|
|
|
|
@tmp_to_root_org()
|
|
|
|
|
def check_asset_permission_will_expired():
|
2021-10-20 11:45:37 +00:00
|
|
|
|
start = local_now()
|
2021-09-24 02:21:56 +00:00
|
|
|
|
end = start + timedelta(days=3)
|
|
|
|
|
|
2022-06-14 08:03:42 +00:00
|
|
|
|
user_asset_remain_day_mapper = defaultdict(dict)
|
|
|
|
|
org_perm_remain_day_mapper = defaultdict(dict)
|
2021-09-24 02:21:56 +00:00
|
|
|
|
|
|
|
|
|
asset_perms = AssetPermission.objects.filter(
|
2021-10-20 11:45:37 +00:00
|
|
|
|
date_expired__gte=start,
|
|
|
|
|
date_expired__lte=end
|
2021-09-24 02:21:56 +00:00
|
|
|
|
).distinct()
|
|
|
|
|
|
|
|
|
|
for asset_perm in asset_perms:
|
2022-06-14 08:03:42 +00:00
|
|
|
|
date_expired = dt_parser(asset_perm.date_expired)
|
|
|
|
|
remain_days = (end - date_expired).days
|
|
|
|
|
|
|
|
|
|
org = asset_perm.org
|
2021-09-24 02:21:56 +00:00
|
|
|
|
# 资产授权按照组织分类
|
2022-06-14 08:03:42 +00:00
|
|
|
|
if org in org_perm_remain_day_mapper[remain_days]:
|
|
|
|
|
org_perm_remain_day_mapper[remain_days][org].add(asset_perm)
|
|
|
|
|
else:
|
2022-06-16 09:49:40 +00:00
|
|
|
|
org_perm_remain_day_mapper[remain_days][org] = {asset_perm, }
|
2021-09-24 02:21:56 +00:00
|
|
|
|
|
|
|
|
|
# 计算每个用户即将过期的资产
|
|
|
|
|
users = asset_perm.get_all_users()
|
|
|
|
|
assets = asset_perm.get_all_assets()
|
|
|
|
|
|
|
|
|
|
for u in users:
|
2022-06-14 08:03:42 +00:00
|
|
|
|
if u in user_asset_remain_day_mapper[remain_days]:
|
|
|
|
|
user_asset_remain_day_mapper[remain_days][u].update(assets)
|
|
|
|
|
else:
|
2022-06-16 09:49:40 +00:00
|
|
|
|
user_asset_remain_day_mapper[remain_days][u] = set(assets)
|
2021-09-24 02:21:56 +00:00
|
|
|
|
|
2022-06-14 08:03:42 +00:00
|
|
|
|
for day_count, user_asset_mapper in user_asset_remain_day_mapper.items():
|
|
|
|
|
for user, assets in user_asset_mapper.items():
|
|
|
|
|
PermedAssetsWillExpireUserMsg(user, assets, day_count).publish_async()
|
2021-09-24 02:21:56 +00:00
|
|
|
|
|
2022-06-14 08:03:42 +00:00
|
|
|
|
for day_count, org_perm_mapper in org_perm_remain_day_mapper.items():
|
|
|
|
|
for org, perms in org_perm_mapper.items():
|
|
|
|
|
org_admins = org.admins.all()
|
|
|
|
|
for org_admin in org_admins:
|
|
|
|
|
AssetPermsWillExpireForOrgAdminMsg(org_admin, perms, org, day_count).publish_async()
|
2021-09-24 02:21:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@register_as_period_task(crontab='0 10 * * *')
|
|
|
|
|
@shared_task()
|
|
|
|
|
@atomic()
|
|
|
|
|
@tmp_to_root_org()
|
|
|
|
|
def check_app_permission_will_expired():
|
2021-10-20 11:45:37 +00:00
|
|
|
|
start = local_now()
|
2021-09-24 02:21:56 +00:00
|
|
|
|
end = start + timedelta(days=3)
|
|
|
|
|
|
|
|
|
|
app_perms = ApplicationPermission.objects.filter(
|
2021-10-20 11:45:37 +00:00
|
|
|
|
date_expired__gte=start,
|
|
|
|
|
date_expired__lte=end
|
2021-09-24 02:21:56 +00:00
|
|
|
|
).distinct()
|
|
|
|
|
|
2022-06-14 08:03:42 +00:00
|
|
|
|
user_app_remain_day_mapper = defaultdict(dict)
|
|
|
|
|
org_perm_remain_day_mapper = defaultdict(dict)
|
2021-09-24 02:21:56 +00:00
|
|
|
|
|
|
|
|
|
for app_perm in app_perms:
|
2022-06-14 08:03:42 +00:00
|
|
|
|
date_expired = dt_parser(app_perm.date_expired)
|
|
|
|
|
remain_days = (end - date_expired).days
|
|
|
|
|
|
|
|
|
|
org = app_perm.org
|
|
|
|
|
if org in org_perm_remain_day_mapper[remain_days]:
|
|
|
|
|
org_perm_remain_day_mapper[remain_days][org].add(app_perm)
|
|
|
|
|
else:
|
2022-06-16 09:49:40 +00:00
|
|
|
|
org_perm_remain_day_mapper[remain_days][org] = {app_perm, }
|
2021-09-24 02:21:56 +00:00
|
|
|
|
|
|
|
|
|
users = app_perm.get_all_users()
|
|
|
|
|
apps = app_perm.applications.all()
|
|
|
|
|
for u in users:
|
2022-06-14 08:03:42 +00:00
|
|
|
|
if u in user_app_remain_day_mapper[remain_days]:
|
|
|
|
|
user_app_remain_day_mapper[remain_days][u].update(apps)
|
|
|
|
|
else:
|
2022-06-16 09:49:40 +00:00
|
|
|
|
user_app_remain_day_mapper[remain_days][u] = set(apps)
|
2022-06-14 08:03:42 +00:00
|
|
|
|
|
|
|
|
|
for day_count, user_app_mapper in user_app_remain_day_mapper.items():
|
|
|
|
|
for user, apps in user_app_mapper.items():
|
|
|
|
|
PermedAppsWillExpireUserMsg(user, apps, day_count).publish_async()
|
|
|
|
|
|
|
|
|
|
for day_count, org_perm_mapper in org_perm_remain_day_mapper.items():
|
|
|
|
|
for org, perms in org_perm_mapper.items():
|
|
|
|
|
org_admins = org.admins.all()
|
|
|
|
|
for org_admin in org_admins:
|
|
|
|
|
AppPermsWillExpireForOrgAdminMsg(org_admin, perms, org, day_count).publish_async()
|