Skip to content

Default life cycle plug -in#

Features#

Set the user expiration time,The timing task will regularly compare the user's expiration time and the current time,If the current time is greater than the user expiration time,The user is disabled

Configure the life cycle timing task#

ClickedEdit life cycle timing taskButton X7mszV.png

Among them, timing running time grammar referencelinux CrontabCommand grammar,The following figure is an example:Run once every day at 8 o'clock,If it fails, try it again every 30 seconds at intervals X4Jmsf.png

Configure user expiration time#

X7nulV.png

X7nGk9.png

X4ye4P.png

X4yn9f.png

Implementation#

Time tasks compare the user's expiration time and current time,If the user has expired,Then set the user attribute is_Active is false,The user will be prohibited from logging in to ARKID
Abstract methods to cover the base class of plug -in,See ARKID.core.extension.account_life.AccountLifeExtension

Abstract method implementation:#

Code#

extension_root.com_longgui_account_life_arkid.AccountLifeArkIDExtension (AccountLifeExtension) #

Source code in extension_root/com_longgui_account_life_arkid/__init__.py
class AccountLifeArkIDExtension(AccountLifeExtension):
    def load(self):
        super().load()
        self.register_front_pages(select_user_page)
        self.register_account_life_schema(UserExpirationListSchema, "user_expiration")

    def create_tenant_config(self, tenant, config, name, type):
        """
        创建生命周期配置,手工解决expiration_time类型为datetime不能json序列化的问题
        """
        config_created = TenantExtensionConfig()
        config_created.tenant = tenant
        config_created.extension = Extension.active_objects.get(package=self.package)
        config_created.config = json.loads(config.json())
        config_created.name = name
        config_created.type = type
        config_created.save()
        return config_created

    def update_tenant_config(self, id, config, name, type):
        """
        更新生命周期配置,手工解决expiration_time类型为datetime不能json序列化的问题
        """
        tenantextensionconfig = TenantExtensionConfig.valid_objects.filter(
            id=id
        ).first()

        tenantextensionconfig.config = json.loads(config.json())
        tenantextensionconfig.name = name
        tenantextensionconfig.type = type
        tenantextensionconfig.save()
        return tenantextensionconfig

    def periodic_task(self, event, **kwargs):
        """
        遍历所有插件配置,如果配置中用户对应的过期时间小于当前时间,则禁用用户
        Args:
            event (arkid.core.event.Event):  生命周期定时任务事件
        """
        logger.info("Doing account life arkid priodic task...")
        configs = self.get_tenant_configs(event.tenant)
        for cfg in configs:
            for item in cfg.config:
                user_id = item.get('user').get('id')
                user = User.objects.get(id=user_id)
                expiration_time = timezone.datetime.strptime(
                    item.get('expiration_time'), '%Y-%m-%dT%H:%M:%S'
                )
                logger.info(f"expiration_time: {expiration_time}/now: {datetime.now()}")
                if expiration_time <= datetime.now():
                    user.offline()

create_tenant_config(self, tenant, config, name, type) #

创建生命周期配置,手工解决expiration_time类型为datetime不能json序列化的问题

Source code in extension_root/com_longgui_account_life_arkid/__init__.py
def create_tenant_config(self, tenant, config, name, type):
    """
    创建生命周期配置,手工解决expiration_time类型为datetime不能json序列化的问题
    """
    config_created = TenantExtensionConfig()
    config_created.tenant = tenant
    config_created.extension = Extension.active_objects.get(package=self.package)
    config_created.config = json.loads(config.json())
    config_created.name = name
    config_created.type = type
    config_created.save()
    return config_created

load(self) #

抽象方法,插件加载的入口方法

Source code in extension_root/com_longgui_account_life_arkid/__init__.py
def load(self):
    super().load()
    self.register_front_pages(select_user_page)
    self.register_account_life_schema(UserExpirationListSchema, "user_expiration")

periodic_task(self, event, **kwargs) #

遍历所有插件配置,如果配置中用户对应的过期时间小于当前时间,则禁用用户

Parameters:

Name Type Description Default
event arkid.core.event.Event

生命周期定时任务事件

required
Source code in extension_root/com_longgui_account_life_arkid/__init__.py
def periodic_task(self, event, **kwargs):
    """
    遍历所有插件配置,如果配置中用户对应的过期时间小于当前时间,则禁用用户
    Args:
        event (arkid.core.event.Event):  生命周期定时任务事件
    """
    logger.info("Doing account life arkid priodic task...")
    configs = self.get_tenant_configs(event.tenant)
    for cfg in configs:
        for item in cfg.config:
            user_id = item.get('user').get('id')
            user = User.objects.get(id=user_id)
            expiration_time = timezone.datetime.strptime(
                item.get('expiration_time'), '%Y-%m-%dT%H:%M:%S'
            )
            logger.info(f"expiration_time: {expiration_time}/now: {datetime.now()}")
            if expiration_time <= datetime.now():
                user.offline()

update_tenant_config(self, id, config, name, type) #

更新生命周期配置,手工解决expiration_time类型为datetime不能json序列化的问题

Source code in extension_root/com_longgui_account_life_arkid/__init__.py
def update_tenant_config(self, id, config, name, type):
    """
    更新生命周期配置,手工解决expiration_time类型为datetime不能json序列化的问题
    """
    tenantextensionconfig = TenantExtensionConfig.valid_objects.filter(
        id=id
    ).first()

    tenantextensionconfig.config = json.loads(config.json())
    tenantextensionconfig.name = name
    tenantextensionconfig.type = type
    tenantextensionconfig.save()
    return tenantextensionconfig

评论