mirror of https://github.com/jumpserver/jumpserver
Merge pull request #9624 from jumpserver/pr@dev@change_ldap_task_to_setting
perf: 修改 ldap task 位置pull/9625/head
commit
d795139108
|
@ -82,5 +82,5 @@ class LDAPSettingSerializer(serializers.Serializer):
|
|||
|
||||
@staticmethod
|
||||
def post_save():
|
||||
from users.tasks import import_ldap_user_periodic
|
||||
from settings.tasks import import_ldap_user_periodic
|
||||
import_ldap_user_periodic()
|
||||
|
|
|
@ -1,14 +1,72 @@
|
|||
# coding: utf-8
|
||||
#
|
||||
from celery import shared_task
|
||||
from django.conf import settings
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from common.utils import get_logger
|
||||
from ..utils import LDAPSyncUtil
|
||||
|
||||
__all__ = ['sync_ldap_user']
|
||||
from ops.celery.decorator import after_app_ready_start
|
||||
from ops.celery.utils import (
|
||||
create_or_update_celery_periodic_tasks, disable_celery_periodic_task
|
||||
)
|
||||
from orgs.models import Organization
|
||||
from ..utils import LDAPSyncUtil, LDAPServerUtil, LDAPImportUtil
|
||||
|
||||
__all__ = ['sync_ldap_user', 'import_ldap_user_periodic', 'import_ldap_user']
|
||||
|
||||
logger = get_logger(__file__)
|
||||
|
||||
|
||||
def sync_ldap_user():
|
||||
LDAPSyncUtil().perform_sync()
|
||||
|
||||
|
||||
@shared_task(verbose_name=_('Import ldap user'))
|
||||
def import_ldap_user():
|
||||
logger.info("Start import ldap user task")
|
||||
util_server = LDAPServerUtil()
|
||||
util_import = LDAPImportUtil()
|
||||
users = util_server.search()
|
||||
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)
|
||||
errors = util_import.perform_import(users, org)
|
||||
if errors:
|
||||
logger.error("Imported LDAP users errors: {}".format(errors))
|
||||
else:
|
||||
logger.info('Imported {} users successfully'.format(len(users)))
|
||||
|
||||
|
||||
@shared_task(verbose_name=_('Periodic import ldap user'))
|
||||
@after_app_ready_start
|
||||
def import_ldap_user_periodic():
|
||||
if not settings.AUTH_LDAP:
|
||||
return
|
||||
task_name = 'import_ldap_user_periodic'
|
||||
disable_celery_periodic_task(task_name)
|
||||
if not settings.AUTH_LDAP_SYNC_IS_PERIODIC:
|
||||
return
|
||||
|
||||
interval = settings.AUTH_LDAP_SYNC_INTERVAL
|
||||
if isinstance(interval, int):
|
||||
interval = interval * 3600
|
||||
else:
|
||||
interval = None
|
||||
crontab = settings.AUTH_LDAP_SYNC_CRONTAB
|
||||
if crontab:
|
||||
# 优先使用 crontab
|
||||
interval = None
|
||||
tasks = {
|
||||
task_name: {
|
||||
'task': import_ldap_user.name,
|
||||
'interval': interval,
|
||||
'crontab': crontab,
|
||||
'enabled': True,
|
||||
}
|
||||
}
|
||||
create_or_update_celery_periodic_tasks(tasks)
|
||||
|
|
|
@ -108,11 +108,6 @@ class AppletHostDeployAppletSerializer(AppletHostDeploymentSerializer):
|
|||
class Meta(AppletHostDeploymentSerializer.Meta):
|
||||
fields = AppletHostDeploymentSerializer.Meta.fields + ['applet_id']
|
||||
|
||||
def create(self, validated_data):
|
||||
applet_id = validated_data.pop('applet_id', None)
|
||||
deployment = super().create(validated_data)
|
||||
return deployment
|
||||
|
||||
|
||||
class AppletHostAccountSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
|
|
|
@ -2,22 +2,19 @@
|
|||
#
|
||||
|
||||
from celery import shared_task
|
||||
from django.conf import settings
|
||||
from django.utils import timezone
|
||||
|
||||
from users.notifications import PasswordExpirationReminderMsg
|
||||
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
|
||||
from orgs.models import Organization
|
||||
from .models import User
|
||||
from users.notifications import UserExpirationReminderMsg
|
||||
from settings.utils import LDAPServerUtil, LDAPImportUtil
|
||||
from common.const.crontab import CRONTAB_AT_AM_TEN, CRONTAB_AT_PM_TWO
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from common.const.crontab import CRONTAB_AT_AM_TEN, CRONTAB_AT_PM_TWO
|
||||
from common.utils import get_logger
|
||||
from ops.celery.decorator import after_app_ready_start
|
||||
from ops.celery.utils import (
|
||||
create_or_update_celery_periodic_tasks
|
||||
)
|
||||
from users.notifications import PasswordExpirationReminderMsg
|
||||
from users.notifications import UserExpirationReminderMsg
|
||||
from .models import User
|
||||
|
||||
logger = get_logger(__file__)
|
||||
|
||||
|
||||
|
@ -78,54 +75,3 @@ def check_user_expired_periodic():
|
|||
}
|
||||
}
|
||||
create_or_update_celery_periodic_tasks(tasks)
|
||||
|
||||
|
||||
@shared_task(verbose_name=_('Import ldap user'))
|
||||
def import_ldap_user():
|
||||
logger.info("Start import ldap user task")
|
||||
util_server = LDAPServerUtil()
|
||||
util_import = LDAPImportUtil()
|
||||
users = util_server.search()
|
||||
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)
|
||||
errors = util_import.perform_import(users, org)
|
||||
if errors:
|
||||
logger.error("Imported LDAP users errors: {}".format(errors))
|
||||
else:
|
||||
logger.info('Imported {} users successfully'.format(len(users)))
|
||||
|
||||
|
||||
@shared_task(verbose_name=_('Periodic import ldap user'))
|
||||
@after_app_ready_start
|
||||
def import_ldap_user_periodic():
|
||||
if not settings.AUTH_LDAP:
|
||||
return
|
||||
task_name = 'import_ldap_user_periodic'
|
||||
if not settings.AUTH_LDAP_SYNC_IS_PERIODIC:
|
||||
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
|
||||
if crontab:
|
||||
# 优先使用 crontab
|
||||
interval = None
|
||||
tasks = {
|
||||
task_name: {
|
||||
'task': import_ldap_user.name,
|
||||
'interval': interval,
|
||||
'crontab': crontab,
|
||||
'enabled': True,
|
||||
}
|
||||
}
|
||||
create_or_update_celery_periodic_tasks(tasks)
|
||||
|
|
Loading…
Reference in New Issue