package com.uas.eis.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Component;

import com.uas.eis.core.support.TokenHandler;

public class RoleDao extends JdbcDaoSupport{
	
	@Autowired
	private BaseDao baseDao;
	
	public String login(String username, String password) {
		return TokenHandler.createToken(username, password);
	}
	
	public boolean getEnable(String username) {
		int enable = baseDao.queryForObject("select er_enable from EIS_ROLE where er_name = '" + username + "'", Integer.class);
		return enable == -1;
	}
	
	public boolean checkActionAccess(String username, String action) {
		String role = baseDao.queryForObject("select eu_role from EIS_USER; where eu_name = '" + username + "'", String.class);
		String roles = baseDao.queryForObject("select ei_roles from EIS_INTERFACE where ei_path = '" + action + "'", String.class);
		String[] allRoles = roles.split(",");
		boolean flag = false;
		
		for(String r: allRoles){
	        if(r.equals(role)) {
	        	flag = true;
	        	break;
	        }
	    }
		return flag;
	}
}
