2017-12-04 12:15:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2016-11-10 15:54:21 +00:00
|
|
|
|
|
|
|
from celery import shared_task
|
2019-09-27 10:30:13 +00:00
|
|
|
from django.conf import settings
|
2022-06-28 09:23:20 +00:00
|
|
|
from django.utils import timezone
|
2018-11-22 10:02:12 +00:00
|
|
|
|
2021-08-24 06:20:54 +00:00
|
|
|
from users.notifications import PasswordExpirationReminderMsg
|
2019-11-11 10:50:05 +00:00
|
|
|
from ops.celery.utils import (
|
|
|
|
create_or_update_celery_periodic_tasks, disable_celery_periodic_task
|
|
|
|
)
|
2019-07-31 08:57:21 +00:00
|
|
|
from ops.celery.decorator import after_app_ready_start
|
2018-11-22 10:02:12 +00:00
|
|
|
from common.utils import get_logger
|
2022-04-01 07:33:14 +00:00
|
|
|
from orgs.models import Organization
|
2019-03-04 03:36:10 +00:00
|
|
|
from .models import User
|
2021-08-24 06:20:54 +00:00
|
|
|
from users.notifications import UserExpirationReminderMsg
|
2019-11-11 08:41:32 +00:00
|
|
|
from settings.utils import LDAPServerUtil, LDAPImportUtil
|
2018-11-22 10:02:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
2016-11-10 15:54:21 +00:00
|
|
|
|
|
|
|
|
2018-11-22 10:02:12 +00:00
|
|
|
@shared_task
|
|
|
|
def check_password_expired():
|
2022-02-17 12:13:31 +00:00
|
|
|
users = User.get_nature_users().filter(source=User.Source.local)
|
2018-11-22 10:02:12 +00:00
|
|
|
for user in users:
|
2019-07-30 04:52:45 +00:00
|
|
|
if not user.is_valid:
|
|
|
|
continue
|
2018-11-22 10:02:12 +00:00
|
|
|
if not user.password_will_expired:
|
|
|
|
continue
|
2019-07-30 04:52:45 +00:00
|
|
|
msg = "The user {} password expires in {} days"
|
|
|
|
logger.info(msg.format(user, user.password_expired_remain_days))
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
|
|
PasswordExpirationReminderMsg(user).publish_async()
|
2018-11-22 10:02:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
|
@after_app_ready_start
|
|
|
|
def check_password_expired_periodic():
|
|
|
|
tasks = {
|
|
|
|
'check_password_expired_periodic': {
|
|
|
|
'task': check_password_expired.name,
|
|
|
|
'interval': None,
|
|
|
|
'crontab': '0 10 * * *',
|
|
|
|
'enabled': True,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
create_or_update_celery_periodic_tasks(tasks)
|
2019-03-04 02:39:44 +00:00
|
|
|
|
|
|
|
|
2019-07-31 08:57:21 +00:00
|
|
|
@shared_task
|
|
|
|
def check_user_expired():
|
2022-06-28 09:23:20 +00:00
|
|
|
date_expired_lt = timezone.now() + timezone.timedelta(days=User.DATE_EXPIRED_WARNING_DAYS)
|
|
|
|
users = User.get_nature_users()\
|
|
|
|
.filter(source=User.Source.local)\
|
|
|
|
.filter(date_expired__lt=date_expired_lt)
|
|
|
|
|
2019-07-31 08:57:21 +00:00
|
|
|
for user in users:
|
|
|
|
if not user.is_valid:
|
|
|
|
continue
|
|
|
|
if not user.will_expired:
|
|
|
|
continue
|
2020-03-13 04:33:09 +00:00
|
|
|
msg = "The user {} will expires in {} days"
|
|
|
|
logger.info(msg.format(user, user.expired_remain_days))
|
2021-08-24 06:20:54 +00:00
|
|
|
UserExpirationReminderMsg(user).publish_async()
|
2019-07-31 08:57:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
|
@after_app_ready_start
|
|
|
|
def check_user_expired_periodic():
|
|
|
|
tasks = {
|
|
|
|
'check_user_expired_periodic': {
|
|
|
|
'task': check_user_expired.name,
|
|
|
|
'interval': None,
|
|
|
|
'crontab': '0 14 * * *',
|
|
|
|
'enabled': True,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
create_or_update_celery_periodic_tasks(tasks)
|
2019-03-04 03:36:10 +00:00
|
|
|
|
2019-09-27 06:04:10 +00:00
|
|
|
|
|
|
|
@shared_task
|
2019-11-11 08:41:32 +00:00
|
|
|
def import_ldap_user():
|
|
|
|
logger.info("Start import ldap user task")
|
|
|
|
util_server = LDAPServerUtil()
|
|
|
|
util_import = LDAPImportUtil()
|
|
|
|
users = util_server.search()
|
2022-04-20 08:05:33 +00:00
|
|
|
if settings.XPACK_ENABLED:
|
|
|
|
org_id = settings.AUTH_LDAP_SYNC_ORG_ID
|
|
|
|
default_org = None
|
|
|
|
else:
|
|
|
|
# 社区版默认导入Default组织
|
|
|
|
org_id = Organization.DEFAULT_ID
|
|
|
|
default_org = Organization.default()
|
|
|
|
org = Organization.get_instance(org_id, default=default_org)
|
2022-04-01 07:33:14 +00:00
|
|
|
errors = util_import.perform_import(users, org)
|
2019-11-11 08:41:32 +00:00
|
|
|
if errors:
|
|
|
|
logger.error("Imported LDAP users errors: {}".format(errors))
|
|
|
|
else:
|
|
|
|
logger.info('Imported {} users successfully'.format(len(users)))
|
2019-09-27 06:04:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
|
@after_app_ready_start
|
2019-11-11 08:41:32 +00:00
|
|
|
def import_ldap_user_periodic():
|
2019-09-27 06:04:10 +00:00
|
|
|
if not settings.AUTH_LDAP:
|
|
|
|
return
|
2021-09-09 06:00:50 +00:00
|
|
|
task_name = 'import_ldap_user_periodic'
|
2019-09-27 10:19:19 +00:00
|
|
|
if not settings.AUTH_LDAP_SYNC_IS_PERIODIC:
|
2019-11-11 10:50:05 +00:00
|
|
|
disable_celery_periodic_task(task_name)
|
2019-09-27 10:19:19 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
interval = settings.AUTH_LDAP_SYNC_INTERVAL
|
|
|
|
if isinstance(interval, int):
|
|
|
|
interval = interval * 3600
|
|
|
|
else:
|
|
|
|
interval = None
|
|
|
|
crontab = settings.AUTH_LDAP_SYNC_CRONTAB
|
2022-04-20 08:30:41 +00:00
|
|
|
if crontab:
|
|
|
|
# 优先使用 crontab
|
|
|
|
interval = None
|
2019-09-27 06:04:10 +00:00
|
|
|
tasks = {
|
2021-09-09 06:00:50 +00:00
|
|
|
task_name: {
|
2019-11-11 08:41:32 +00:00
|
|
|
'task': import_ldap_user.name,
|
2019-09-27 10:19:19 +00:00
|
|
|
'interval': interval,
|
|
|
|
'crontab': crontab,
|
2019-09-27 06:04:10 +00:00
|
|
|
'enabled': True,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
create_or_update_celery_periodic_tasks(tasks)
|