jumpserver/apps/users/tasks.py

115 lines
3.1 KiB
Python
Raw Normal View History

2017-12-04 12:15:47 +00:00
# -*- coding: utf-8 -*-
#
2016-11-10 15:54:21 +00:00
import sys
2016-11-10 15:54:21 +00:00
from celery import shared_task
from django.conf import settings
from ops.celery.utils import (
create_or_update_celery_periodic_tasks, disable_celery_periodic_task
)
from ops.celery.decorator import after_app_ready_start
from common.utils import get_logger
2019-03-04 03:36:10 +00:00
from .models import User
from .utils import (
send_password_expiration_reminder_mail, send_user_expiration_reminder_mail
)
from settings.utils import LDAPServerUtil, LDAPImportUtil
logger = get_logger(__file__)
2016-11-10 15:54:21 +00:00
@shared_task
def check_password_expired():
2020-07-28 09:53:01 +00:00
users = User.objects.filter(source=User.SOURCE_LOCAL).exclude(role=User.ROLE.APP)
for user in users:
if not user.is_valid:
continue
if not user.password_will_expired:
continue
msg = "The user {} password expires in {} days"
logger.info(msg.format(user, user.password_expired_remain_days))
send_password_expiration_reminder_mail(user)
@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)
@shared_task
def check_user_expired():
2020-07-28 09:53:01 +00:00
users = User.objects.exclude(role=User.ROLE.APP)
for user in users:
if not user.is_valid:
continue
if not user.will_expired:
continue
msg = "The user {} will expires in {} days"
logger.info(msg.format(user, user.expired_remain_days))
send_user_expiration_reminder_mail(user)
@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
@shared_task
def import_ldap_user():
logger.info("Start import ldap user task")
util_server = LDAPServerUtil()
util_import = LDAPImportUtil()
users = util_server.search()
errors = util_import.perform_import(users)
if errors:
logger.error("Imported LDAP users errors: {}".format(errors))
else:
logger.info('Imported {} users successfully'.format(len(users)))
@shared_task
@after_app_ready_start
def import_ldap_user_periodic():
if not settings.AUTH_LDAP:
return
if not settings.AUTH_LDAP_SYNC_IS_PERIODIC:
task_name = sys._getframe().f_code.co_name
disable_celery_periodic_task(task_name)
return
interval = settings.AUTH_LDAP_SYNC_INTERVAL
if isinstance(interval, int):
interval = interval * 3600
else:
interval = None
crontab = settings.AUTH_LDAP_SYNC_CRONTAB
tasks = {
'import_ldap_user_periodic': {
'task': import_ldap_user.name,
'interval': interval,
'crontab': crontab,
'enabled': True,
}
}
create_or_update_celery_periodic_tasks(tasks)