package com.sysu.admin.support.shiro;

import com.xiesx.fastboot.SpringHelper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.data.domain.AuditorAware;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class ShiroAuditorAware implements AuditorAware<Long> {

    ShiroService mShiroService = SpringHelper.getBean(ShiroService.class);

    @Override
    public Optional<Long> getCurrentAuditor() {
        Subject subject = SecurityUtils.getSubject();
        if (ObjectUtils.isNotEmpty(subject)) {
            ShiroPrincipal principal = (ShiroPrincipal) subject.getPrincipal();
            if (ObjectUtils.isNotEmpty(principal)) {
                return Optional.of(principal.getId());
            }
        }
        return Optional.empty();
    }
}
