public function listaPermissoes($usrId)
 {
     $sql = "\n\t\tSELECT ma.acaoId, ma.moduloId, ma.acaoDescricao, ma.acaoPagina,\tma.acaoIndex, ma.acaoMenu\n\t\tFROM modulos_permissao AS mp\n\t\tINNER JOIN modulos_acao AS ma ON mp.acaoId = ma.acaoId\n\t\tAND mp.usrId =" . $usrId . "\n\t\tORDER BY ma.moduloId, ma.acaoId;\n\t\t";
     $sqlFuncoes = new sqlFuncoes();
     $sqlFuncoes->setSql($sql);
     return $sqlFuncoes->listaRegistros();
 }
// SETA AS PERMISSOES DO USUARIO #############################################################
// gera lista de permissoes atuais
$sql = "\nSELECT acaoId\nFROM modulos_permissao\nWHERE usrId =" . $usrId . "\nORDER BY moduloId, acaoId;\n";
$sqlFuncoes = new sqlFuncoes();
$sqlFuncoes->setSql($sql);
$permissoes = $sqlFuncoes->listaRegistros();
unset($sqlFuncoes);
$permissoesAtuais = array();
for ($p = 0; $p < count($permissoes); $p++) {
    $permissoesAtuais[] = $permissoes[$p]['acaoId'];
}
// gera lista de todas as permissoes possiveis para o usuario
$sql = "\nSELECT acaoId, moduloId, acaoPagina\nFROM modulos_acao\nWHERE usrRoot = 'n'\nAND acaoRestrita = 's'\nORDER BY moduloId, acaoId ASC;\n";
$sqlFuncoes = new sqlFuncoes();
$sqlFuncoes->setSql($sql);
$permissoesPossiveis = $sqlFuncoes->listaRegistros();
unset($sqlFuncoes);
// ###########################################################################################
// ALATERA AS PERMISSOES NO BANCO DE DADOS ###################################################
foreach ($permissoesPossiveis as $chave => $permissao) {
    if (in_array($permissao['acaoId'], $permissoesAtuais) and !array_key_exists($permissao['acaoPagina'], $_POST)) {
        $sql = "\n\t\tDELETE\n\t\tFROM modulos_permissao\n\t\tWHERE acaoId = " . $permissao['acaoId'] . "\n\t\tAND usrId = " . $usrId . "\n\t\tLIMIT 1;\n\t\t";
        $sqlFuncoes = new sqlFuncoes();
        $sqlFuncoes->setSql($sql);
        $sqlFuncoes->excluiRegistro();
        unset($sqlFuncoes);
    } elseif (!in_array($permissao['acaoId'], $permissoesAtuais) and array_key_exists($permissao['acaoPagina'], $_POST)) {
        $sql = "\n\t\tINSERT INTO modulos_permissao\n\t\t\t(acaoId, moduloId, usrId)\n\t\tVALUES\n\t\t\t(" . $permissao['acaoId'] . ", " . $permissao['moduloId'] . ", " . $usrId . ");\n\t\t";
        $sqlFuncoes = new sqlFuncoes();
        $sqlFuncoes->setSql($sql);
        $sqlFuncoes->incluiRegisto();
            $usrId = $sessaoId;
        } else {
            // retorna mensagem de erro
            setcookie('msgErro[privilegio]', 'Acesso restrito a usuários com permissão.', time() + 10, '/administracao/usuarios');
            header('Location: /administracao/usuarios');
            exit;
        }
    }
}
// ###########################################################################################
// VERIFICA A EXISTENCIA DO USUARIO ##########################################################
$sqlUsuario = "\nSELECT usrId, usrEmail, usrNome, usrLogin, usrApelido, usrFoto, usrDescricao, usrMetadescricao, usrPalavraschave, usrAtivo, usrRoot\nFROM usuarios\nWHERE usrId = " . $usrId . "\n";
require_once '../classes/sql-funcoes.php';
$sqlFuncoes = new sqlFuncoes();
$sqlFuncoes->setSql($sqlUsuario);
$usrDados = $sqlFuncoes->listaRegistros(true);
if (empty($usrDados)) {
    setcookie('msgErro[usrId]', 'Registro do usuário não encontrado.', time() + 10, '/administracao/usuarios');
    header('Location: /administracao/usuarios');
    exit;
}
unset($sqlFuncoes);
// ###########################################################################################
// CARREGA O ARQUIVO cabecalho.php ###########################################################
include '../include/geral/php/cabecalho.php';
// ###########################################################################################
// DEFINICAO DO FUZO E DA DATA ###############################################################
date_default_timezone_set('America/Sao_Paulo');
setlocale(LC_ALL, "pt_BR");
$dataCompleta = strftime("%A, %d de %B de %Y");
// ###########################################################################################
$fotoNome = explode('.', $usrFoto["name"]);
$fotoNome = strtolower($usrApelidourl . "-" . time() . "." . $fotoNome[1]);
$fotoDiretorio = $_SERVER['DOCUMENT_ROOT'] . "cms-base/imagens/usuarios/" . $fotoNome;
$fotoEndereco = "/imagens/usuarios/" . $fotoNome;
// ###########################################################################################
// CADASTRAR USUARIO NA BASE DE DADOS ########################################################
$sqlUsuario = "\nINSERT INTO usuarios\n\t(usrEmail, usrNome, usrLogin, usrApelido, usrApelidourl, usrFoto, usrDescricao, usrMetadescricao, usrPalavraschave, usrSenha)\nVALUES\n\t('{$usrEmail}', '{$usrNome}', '{$usrLogin}', '{$usrApelido}', '{$usrApelidourl}', '{$fotoEndereco}', '{$usrDescricao}', '{$usrMetadescricao}', '{$usrPalavraschave}', '{$usrSenha}');\n";
$sqlFuncoes = new sqlFuncoes();
$sqlFuncoes->setSql($sqlUsuario);
$usrId = $sqlFuncoes->incluiRegisto(true);
unset($sqlFuncoes);
// inclui permissoes para o usuario
$sql = "\nSELECT acaoId, moduloId\nFROM modulos_acao\nWHERE acaoRestrita = 'n'\nORDER BY moduloId, acaoId ASC;\n";
$sqlFuncoes = new sqlFuncoes();
$sqlFuncoes->setSql($sql);
$acoes = $sqlFuncoes->listaRegistros();
unset($sqlFuncoes);
foreach ($acoes as $chave => $valor) {
    $sql = "\n\tINSERT INTO modulos_permissao (acaoId, moduloId, usrId)\n\tVALUES ('{$valor['acaoId']}', '{$valor['moduloId']}', '{$usrId}');\n\t";
    $sqlFuncoes = new sqlFuncoes();
    $sqlFuncoes->setSql($sql);
    $sqlFuncoes->incluiRegisto();
    unset($sqlFuncoes);
}
// ###########################################################################################
// UPLOAD DA FOTO ############################################################################
move_uploaded_file($usrFoto["tmp_name"], $fotoDiretorio);
// ###########################################################################################
// REDIRECIONA PARA A PAGINA DE PERMISSOES DO USUARIO ########################################
setcookie('msgOk', 'Usu&aacute;rio cadastrado com sucesso.', time() + 10, '/administracao/usuario-permissao/');
header("Location: /administracao/usuario-permissao/" . $usrId);
// SETA AS PERMISSOES DO USUARIO #############################################################
// gera lista de permissoes atuais
$sql = "\nSELECT acaoId\nFROM modulos_permissao\nWHERE usrId =" . $usrId . "\nORDER BY moduloId, acaoId;\n";
$sqlFuncoes = new sqlFuncoes();
$sqlFuncoes->setSql($sql);
$permissoes = $sqlFuncoes->listaRegistros();
unset($sqlFuncoes);
$permissoesAtuais = array();
for ($p = 0; $p < count($permissoes); $p++) {
    $permissoesAtuais[] = $permissoes[$p]['acaoId'];
}
// gera lista de todas as permissoes possiveis para o usuario
$sql = "\nSELECT ma.moduloId, m.moduloDescricao, ma.acaoId, ma.acaoDescricao, ma.acaoPagina, ma.acaoIndex\nFROM modulos_acao AS ma\nINNER JOIN modulos AS m ON ma.moduloId = m.moduloId\nAND ma.acaoRestrita = 's'\nAND\tma.usrRoot = 'n'\nORDER BY ma.moduloId, ma.acaoId ASC;\n";
$sqlFuncoes = new sqlFuncoes();
$sqlFuncoes->setSql($sql);
$permissoes = $sqlFuncoes->listaRegistros();
unset($sqlFuncoes);
$permissoesPossiveis = array();
for ($p = 0; $p < count($permissoes); $p++) {
    if ($permissoes[$p]['acaoIndex'] == "s") {
        $permissoesPossiveis[$permissoes[$p]['moduloId']]['moduloId'] = $permissoes[$p]['moduloId'];
        $permissoesPossiveis[$permissoes[$p]['moduloId']]['acaoId'] = $permissoes[$p]['acaoId'];
        $permissoesPossiveis[$permissoes[$p]['moduloId']]['acaoDescricao'] = $permissoes[$p]['acaoDescricao'];
        $permissoesPossiveis[$permissoes[$p]['moduloId']]['acaoPagina'] = $permissoes[$p]['acaoPagina'];
    } elseif ($permissoes[$p]['acaoIndex'] == "n") {
        $permissoesPossiveis[$permissoes[$p]['moduloId']]['moduloAcoes'][$permissoes[$p]['acaoId']]['moduloId'] = $permissoes[$p]['moduloId'];
        $permissoesPossiveis[$permissoes[$p]['moduloId']]['moduloAcoes'][$permissoes[$p]['acaoId']]['acaoId'] = $permissoes[$p]['acaoId'];
        $permissoesPossiveis[$permissoes[$p]['moduloId']]['moduloAcoes'][$permissoes[$p]['acaoId']]['acaoDescricao'] = $permissoes[$p]['acaoDescricao'];
        $permissoesPossiveis[$permissoes[$p]['moduloId']]['moduloAcoes'][$permissoes[$p]['acaoId']]['acaoPagina'] = $permissoes[$p]['acaoPagina'];
    }
}