<?php
// Questão https://pt.stackoverflow.com/questions/228769/%C3%89-poss%C3%ADvel-interceptar-chamadas-a-fun%C3%A7%C3%B5es-de-uma-classe
## EXEMPLO
## TABELA USUARIOS
## +-------------------+
## | ID | NOME |
## +-------------------+
## | 1 | USER01 |
## | 2 | USER02 |
## | 3 | USER03 |
## +-------------------+
##
## TABELA PERMISSOES
## +-----------------------------------------+
## | ID | USER_ID | CREATE | EDIT | DELETE |
## +-----------------------------------------+
## | 1 | 1 | 1 | 0 | 0 |
## | 1 | 2 | 1 | 1 | 1 |
## | 1 | 3 | 0 | 0 | 1 |
## +-----------------------------------------+
##
## USO DE 0, PARA INDICAR QUE NÃO É PERMITIDO,
## USO DE 1, PARA INDICAR QUE É PERMITIDO
$Users = [
1 => [
'id' => 1 ,
'nome' => 'USER01' ,
'permissoes' => [
'create' => 1 ,
'edit' => 0 ,
'delete' => 0
]
] ,
2 => [
'id' => 2 ,
'nome' => 'USER02' ,
'permissoes' => [
'create' => 1 ,
'edit' => 1 ,
'delete' => 0
]
] ,
3 => [
'id' => 3 ,
'nome' => 'USER03' ,
'permissoes' => [
'create' => 1 ,
'edit' => 1 ,
'delete' => 1
]
] ,
4 => [
'id' => 4 ,
'nome' => 'USER04' ,
'permissoes' => [
'create' => 0 ,
'edit' => 0 ,
'delete' => 1
]
]
] ;
Class usuarios {
/**
* Função para verificar se o usuário logado
* tem permissão para acessar uma página / método etc..
*/
function verificarPermissao( $UserID , $FuncName ) {
global $Users ;
if ( $Users [ $UserID ] [ "permissoes" ] [ $FuncName ] === 1 ) {
$this -> $FuncName ( ) ;
} else {
echo "Usuário não tem permissão para executar: {$FuncName} \n " ;
}
}
function create( ) {
echo "Usuário tem permissão para criar\n " ;
}
function edit( ) {
echo "Usuário tem permissão para editar\n " ;
}
function delete( ) {
echo "Usuário tem permissão para deletar\n " ;
}
}
$user1 = new usuarios( ) ;
$user1 -> verificarPermissao ( 1 , 'edit' ) ;
$user1 -> verificarPermissao ( 1 , 'create' ) ;
$user1 -> verificarPermissao ( 1 , 'delete' ) ;
echo "\n \n " ;
$user2 = new usuarios( ) ;
$user2 -> verificarPermissao ( 2 , 'edit' ) ;
$user2 -> verificarPermissao ( 2 , 'create' ) ;
$user2 -> verificarPermissao ( 2 , 'delete' ) ;
echo "\n \n " ;
$user3 = new usuarios( ) ;
$user3 -> verificarPermissao ( 3 , 'edit' ) ;
$user3 -> verificarPermissao ( 3 , 'create' ) ;
$user3 -> verificarPermissao ( 3 , 'delete' ) ;
echo "\n \n " ;
$user4 = new usuarios( ) ;
$user4 -> verificarPermissao ( 4 , 'edit' ) ;
$user4 -> verificarPermissao ( 4 , 'create' ) ;
$user4 -> verificarPermissao ( 4 , 'delete' ) ;
PD9waHAKCi8vIFF1ZXN0w6NvIGh0dHBzOi8vcHQuc3RhY2tvdmVyZmxvdy5jb20vcXVlc3Rpb25zLzIyODc2OS8lQzMlODktcG9zcyVDMyVBRHZlbC1pbnRlcmNlcHRhci1jaGFtYWRhcy1hLWZ1biVDMyVBNyVDMyVCNWVzLWRlLXVtYS1jbGFzc2UKCgojIyBFWEVNUExPCiMjIFRBQkVMQSBVU1VBUklPUwojIyArLS0tLS0tLS0tLS0tLS0tLS0tLSsKIyMgfCBJRCAgIHwgTk9NRSAgICAgICB8CiMjICstLS0tLS0tLS0tLS0tLS0tLS0tKwojIyB8ICAgIDEgfCBVU0VSMDEgICAgIHwKIyMgfCAgICAyIHwgVVNFUjAyICAgICB8CiMjIHwgICAgMyB8IFVTRVIwMyAgICAgfAojIyArLS0tLS0tLS0tLS0tLS0tLS0tLSsKIyMgCiMjIFRBQkVMQSBQRVJNSVNTT0VTCiMjICstLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSsKIyMgfCBJRCAgIHwgVVNFUl9JRCB8IENSRUFURSB8IEVESVQgfCBERUxFVEUgfAojIyArLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0rCiMj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