2016-07-15 15:43:47 +00:00
|
|
|
<?php
|
|
|
|
|
2016-10-18 14:03:03 +00:00
|
|
|
namespace database\repo;
|
2016-11-05 13:57:35 +00:00
|
|
|
use \database\core\DatabaseDriver;
|
2016-10-18 14:03:03 +00:00
|
|
|
use \database\core\Repo;
|
2016-10-18 17:09:47 +00:00
|
|
|
use \error\core\Error;
|
2016-10-18 14:03:03 +00:00
|
|
|
use \orm\core\Table;
|
|
|
|
use \orm\core\Rows;
|
2016-07-15 15:43:47 +00:00
|
|
|
|
2016-07-25 09:19:05 +00:00
|
|
|
|
2016-07-18 08:42:18 +00:00
|
|
|
class action_merge extends parentRepo{
|
2016-07-15 15:43:47 +00:00
|
|
|
|
2016-07-18 08:42:18 +00:00
|
|
|
protected static function table_name(){ static $table_name = 'action_merge'; return $table_name; }
|
2016-07-15 15:43:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* AJOUT D'UNE PERMISSION ENTRE 2 GROUPES (UTIL -> MACHINE)
|
|
|
|
*
|
|
|
|
* @id_target<int> UID d'un groupe MACHINE
|
|
|
|
* @id_source<int> UID d'un groupe UTILISATEUR
|
2016-07-18 08:42:18 +00:00
|
|
|
* @id_action<int> UID d'une ACTION
|
2016-07-15 15:43:47 +00:00
|
|
|
*
|
2017-01-15 16:27:02 +00:00
|
|
|
* @return error<Error> Retourne l'erreur 'Error' associée
|
2016-07-15 15:43:47 +00:00
|
|
|
*
|
|
|
|
*/
|
2016-07-18 08:42:18 +00:00
|
|
|
public static function addPermission($id_target, $id_source, $id_action){
|
2016-07-15 15:43:47 +00:00
|
|
|
|
|
|
|
/* [1] On vérifie l'existance du groupe 'target'
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) On rédige la requête */
|
2016-07-19 16:01:16 +00:00
|
|
|
$targetCheckR = new Repo('machine_cluster/getById', [
|
2016-07-15 15:43:47 +00:00
|
|
|
$_SESSION['WAREHOUSE']['id'],
|
|
|
|
$id_target
|
|
|
|
]);
|
|
|
|
$targetCheck = $targetCheckR->answer();
|
|
|
|
|
|
|
|
/* (2) Si aucun résultat, on retourne une erreur */
|
|
|
|
if( $targetCheck === false )
|
2016-10-18 17:09:47 +00:00
|
|
|
return Error::NoMatchFound;
|
2016-07-15 15:43:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [2] On vérifie l'existance du groupe 'source'
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) On rédige la requête */
|
2016-07-19 16:01:16 +00:00
|
|
|
$sourceCheckR = new Repo('user_cluster/getById', [
|
2016-07-15 15:43:47 +00:00
|
|
|
$_SESSION['WAREHOUSE']['id'],
|
|
|
|
$id_source
|
|
|
|
]);
|
|
|
|
$sourceCheck = $sourceCheckR->answer();
|
|
|
|
|
|
|
|
/* (2) Si aucun résultat, on retourne une erreur */
|
|
|
|
if( $sourceCheck === false )
|
2016-10-18 17:09:47 +00:00
|
|
|
return Error::NoMatchFound;
|
2016-07-15 15:43:47 +00:00
|
|
|
|
|
|
|
|
2016-07-18 08:42:18 +00:00
|
|
|
/* [3] On vérifie l'existance de l'action
|
2016-07-15 15:43:47 +00:00
|
|
|
=========================================================*/
|
|
|
|
/* (1) On rédige la requête */
|
2016-07-18 08:42:18 +00:00
|
|
|
$actionCheckR = new Repo('action/getById', [ $id_action ]);
|
|
|
|
$actionCheck = $actionCheckR->answer();
|
2016-07-15 15:43:47 +00:00
|
|
|
|
|
|
|
/* (2) Si aucun résultat, on retourne une erreur */
|
2016-07-18 08:42:18 +00:00
|
|
|
if( $actionCheck === false )
|
2016-10-18 17:09:47 +00:00
|
|
|
return Error::NoMatchFound;
|
2016-07-15 15:43:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [4] On vérifie que l'entrée n'existe pas déja
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) On effectue la requête */
|
|
|
|
$checkAlready = self::getByIdTarget($id_target);
|
|
|
|
|
|
|
|
/* (2) Si on a un résultat, on continue la vérification */
|
|
|
|
if( is_array($checkAlready) && count($checkAlready) > 0 ){
|
|
|
|
|
|
|
|
/* (3) Si les données sont identiques pour au moins une entrée, on retourne un succès */
|
|
|
|
foreach($checkAlready as $s=>$sameTarget)
|
2016-07-18 08:42:18 +00:00
|
|
|
if( $sameTarget['id_source'] == $id_source && $sameTarget['id_action'] == $id_action )
|
2016-10-18 17:09:47 +00:00
|
|
|
return Error::Success;
|
2016-07-15 15:43:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-18 08:42:18 +00:00
|
|
|
/* [5] On ajoute la action
|
2016-07-15 15:43:47 +00:00
|
|
|
=========================================================*/
|
|
|
|
/* (1) On effectue l'insertion */
|
2016-07-24 23:14:35 +00:00
|
|
|
$inserted = Table::get('action_merge')
|
|
|
|
->insert([
|
|
|
|
'id_action_merge' => Rows::INSERT_DEFAULT,
|
|
|
|
'id_target' => $id_target,
|
|
|
|
'id_source' => $id_source,
|
|
|
|
'id_action' => $id_action
|
|
|
|
]);
|
2016-07-15 15:43:47 +00:00
|
|
|
|
|
|
|
/* (2) Si erreur SQL, on retourne une erreur */
|
|
|
|
if( !$inserted )
|
2016-10-18 17:09:47 +00:00
|
|
|
return Error::RepoError;
|
2016-07-15 15:43:47 +00:00
|
|
|
|
|
|
|
|
2016-10-18 17:09:47 +00:00
|
|
|
return Error::Success;
|
2016-07-15 15:43:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-15 15:53:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* SUPPRESSION D'UNE PERMISSION ENTRE 2 GROUPES (UTIL -> MACHINE)
|
|
|
|
*
|
|
|
|
* @id_target<int> UID d'un groupe MACHINE
|
|
|
|
* @id_source<int> UID d'un groupe UTILISATEUR
|
2017-01-15 16:27:02 +00:00
|
|
|
* @id_action<int> UID d'une PERMISSION
|
2016-07-15 15:53:21 +00:00
|
|
|
*
|
2016-10-18 17:09:47 +00:00
|
|
|
* @return error<Error> Retourne l'erreur 'Error' associée
|
2016-07-15 15:53:21 +00:00
|
|
|
*
|
|
|
|
*/
|
2016-07-18 08:42:18 +00:00
|
|
|
public static function removePermission($id_target, $id_source, $id_action){
|
2016-07-15 15:53:21 +00:00
|
|
|
|
|
|
|
/* [1] On vérifie que l'entrée existe
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) On effectue la requête */
|
|
|
|
$checkExists = self::getByIdTarget($id_target);
|
|
|
|
|
|
|
|
/* (2) Si on a un résultat correspondant aux données, c'est bon */
|
|
|
|
$existingId = null;
|
|
|
|
if( is_array($checkExists) && count($checkExists) > 0 ){
|
|
|
|
|
|
|
|
/* (3) Si les données sont identiques pour au moins une entrée, on peut supprimer */
|
|
|
|
foreach($checkExists as $s=>$sameTarget)
|
2016-07-18 08:42:18 +00:00
|
|
|
if( $sameTarget['id_source'] == $id_source && $sameTarget['id_action'] == $id_action )
|
|
|
|
$existingId = $sameTarget['id_action_merge'];
|
2016-07-15 15:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Si on a pas trouvé, on retourne une erreur */
|
|
|
|
if( is_null($existingId) )
|
2016-10-18 17:09:47 +00:00
|
|
|
return Error::NoMatchFound;
|
2016-07-15 15:53:21 +00:00
|
|
|
|
|
|
|
|
2016-07-18 08:42:18 +00:00
|
|
|
/* [2] On supprime la action
|
2016-07-15 15:53:21 +00:00
|
|
|
=========================================================*/
|
|
|
|
/* (1) On effectue la suppression */
|
2016-07-24 23:14:35 +00:00
|
|
|
$deleted = Table::get('action_merge')
|
|
|
|
->whereId($existingId)
|
|
|
|
->whereIdTarget($id_target)
|
|
|
|
->whereIdSource($id_source)
|
|
|
|
->whereIdAction($id_action)
|
|
|
|
->unique()
|
|
|
|
->delete();
|
2016-07-15 15:53:21 +00:00
|
|
|
|
|
|
|
/* (2) Si erreur SQL, on retourne une erreur */
|
|
|
|
if( !$deleted || self::getById($existingId) !== false )
|
2016-10-18 17:09:47 +00:00
|
|
|
return Error::RepoError;
|
2016-07-15 15:53:21 +00:00
|
|
|
|
|
|
|
|
2016-10-18 17:09:47 +00:00
|
|
|
return Error::Success;
|
2016-07-15 15:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-19 16:01:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-15 16:27:02 +00:00
|
|
|
/* RETOURNE LA LISTE DES UTILISATEURS AYANT LES PERMISSIONS POUR UNE MACHINE D'ID DONNE
|
2016-07-19 16:01:16 +00:00
|
|
|
*
|
|
|
|
* @id_warehouse<int> UID de l'entrepot
|
2017-01-15 16:27:02 +00:00
|
|
|
* @id_machine<int> UID de la machine
|
2016-07-19 16:01:16 +00:00
|
|
|
*
|
2017-01-15 16:27:02 +00:00
|
|
|
* @return permissions<Array> Liste des accès des utilisateurs à cette machine
|
2016-07-19 16:01:16 +00:00
|
|
|
*
|
|
|
|
*/
|
2017-01-15 16:27:02 +00:00
|
|
|
public static function getAccess($id_warehouse, $id_machine){
|
2016-07-19 16:01:16 +00:00
|
|
|
|
2016-07-25 09:19:05 +00:00
|
|
|
//TODO: GROUP_CONCAT(DISTINCT a.id_action ORDER BY a.id_action ASC)
|
|
|
|
$u = Table::get('user')
|
|
|
|
->select('id_user')
|
|
|
|
->select('code');
|
|
|
|
|
|
|
|
$ucm = Table::get('user_cluster_merge')
|
|
|
|
->join('id_user', $u);
|
|
|
|
|
|
|
|
$m = Table::get('machine')
|
|
|
|
->whereId(5);
|
|
|
|
|
|
|
|
$mcm = Table::get('machine_cluster_merge')
|
|
|
|
->join('id_machine', $m);
|
2016-07-19 16:01:16 +00:00
|
|
|
|
2016-07-25 09:19:05 +00:00
|
|
|
$am= Table::get('action_merge')
|
|
|
|
->join('id_source', $ucm)
|
|
|
|
->join('id_target', $mcm)
|
|
|
|
->select('id_action', Rows::SEL_CONCAT, Rows::SEL_DISTINCT);
|
2016-07-19 16:01:16 +00:00
|
|
|
|
2016-07-25 09:19:05 +00:00
|
|
|
return $am->fetch();
|
2016-07-19 16:01:16 +00:00
|
|
|
}
|
|
|
|
|
2017-01-15 16:27:02 +00:00
|
|
|
|
|
|
|
|
2016-07-19 16:01:16 +00:00
|
|
|
|
|
|
|
|
2017-01-15 16:27:02 +00:00
|
|
|
/* RETOURNE LA LISTE DES PERMISSIONS EXISTANTES
|
|
|
|
*
|
|
|
|
* @return permissions<Array> Liste des permissions
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getPermissions(){
|
|
|
|
|
|
|
|
/* (1) Liste des permissions existantes */
|
|
|
|
$availablePermissions = Table::get('action')
|
|
|
|
->select('id_action', null, null, 'id_permission')
|
|
|
|
->select('name');
|
|
|
|
|
|
|
|
return $availablePermissions->fetch();
|
|
|
|
}
|
2016-07-19 16:01:16 +00:00
|
|
|
|
|
|
|
|
2017-01-15 16:27:02 +00:00
|
|
|
/* RETOURNE LES GROUPES D'UTILISATEURS AYANT UNE PERMISSION SUR UN GROUPE DE MACHINE
|
|
|
|
*
|
|
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
|
|
* @id_target<int> UID du groupe de machine
|
|
|
|
* @id_action<int> UID de l'action
|
|
|
|
*
|
|
|
|
* @return clusters<Array> Liste des user_cluster ayant la permission sur le groupe target
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getAuthenticatedClusters($id_warehouse, $id_target, $id_action){
|
|
|
|
|
|
|
|
/* [1] On rédige la requête
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) On récupère les groupes (on construit le selecteur) */
|
|
|
|
$clusters = Table::get('user_cluster')
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
->select('id_user_cluster')
|
|
|
|
->select('name');
|
|
|
|
|
|
|
|
/* (2) On rédige le "fetcher" */
|
|
|
|
$fetcher = Table::get('action_merge')
|
|
|
|
->whereIdTarget($id_target)
|
|
|
|
->whereIdAction($id_action)
|
|
|
|
->join('id_source', $clusters);
|
|
|
|
|
|
|
|
return $fetcher->fetch();
|
|
|
|
}
|
2016-07-15 15:43:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|