package cn.com.qmth.stmms.biz.api.auth.service.validator;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.com.qmth.stmms.biz.api.auth.exception.ApiException;
import cn.com.qmth.stmms.biz.api.auth.interfaces.AuthValidator;
import cn.com.qmth.stmms.biz.api.auth.model.AuthInfo;
import cn.com.qmth.stmms.biz.user.model.User;
import cn.com.qmth.stmms.common.enums.Role;

@Component
public class AdminUserValidator implements AuthValidator {

    @Autowired
    private AuthValidateService validateService;

    @PostConstruct
    protected void init() {
        validateService.addValidator(this);
    }

    @Override
    public boolean validate(AuthInfo auth) {
        User user = auth.getLoginUser();
        if (user == null) {
            throw ApiException.USER_UNEXISTS;
        }
        if (user.getRoles().contains(Role.CAMPUS_ADMIN) || user.getRoles().contains(Role.SCHOOL_ADMIN)) {
            return true;
        } else {
            throw ApiException.USER_NOT_ADMIN;
        }
    }

    @Override
    public String getName() {
        return "adminUser";
    }

}
