2016-07-15 15:43:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace manager\repo;
|
|
|
|
use \manager\Database;
|
|
|
|
use \manager\Repo;
|
|
|
|
use \manager\ManagerError;
|
|
|
|
use \manager\repo\cluster as clusterRepo;
|
|
|
|
|
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
|
|
|
*
|
|
|
|
* @return error<ManagerError> Retourne l'erreur 'ManagerError' associée
|
|
|
|
*
|
|
|
|
*/
|
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 )
|
|
|
|
return ManagerError::NoMatchFound;
|
|
|
|
|
|
|
|
|
|
|
|
/* [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 )
|
|
|
|
return ManagerError::NoMatchFound;
|
|
|
|
|
|
|
|
|
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-07-15 15:43:47 +00:00
|
|
|
return ManagerError::NoMatchFound;
|
|
|
|
|
|
|
|
|
|
|
|
/* [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-07-15 15:43:47 +00:00
|
|
|
return ManagerError::Success;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-18 08:42:18 +00:00
|
|
|
$insert = Database::getPDO()->prepare("INSERT INTO action_merge(id_action_merge, id_target, id_source, id_action)
|
|
|
|
VALUES(DEFAULT, :id_target, :id_source, :id_action)");
|
2016-07-15 15:43:47 +00:00
|
|
|
$inserted = $insert->execute([
|
2016-07-19 16:01:16 +00:00
|
|
|
':id_target' => $id_target,
|
|
|
|
':id_source' => $id_source,
|
2016-07-18 08:42:18 +00:00
|
|
|
':id_action' => $id_action
|
2016-07-15 15:43:47 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
/* (2) Si erreur SQL, on retourne une erreur */
|
|
|
|
if( !$inserted )
|
|
|
|
return ManagerError::RepoError;
|
|
|
|
|
|
|
|
|
|
|
|
return ManagerError::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2016-07-18 08:42:18 +00:00
|
|
|
* @id_action<int> UID d'une PERMISSION
|
2016-07-15 15:53:21 +00:00
|
|
|
*
|
|
|
|
* @return error<ManagerError> Retourne l'erreur 'ManagerError' associée
|
|
|
|
*
|
|
|
|
*/
|
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) )
|
|
|
|
return ManagerError::NoMatchFound;
|
|
|
|
|
|
|
|
|
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-18 08:42:18 +00:00
|
|
|
$delete = Database::getPDO()->prepare("DELETE FROM action_merge
|
|
|
|
WHERE id_action_merge = :id_action_merge
|
|
|
|
AND id_target = :id_target
|
|
|
|
AND id_source = :id_source
|
|
|
|
AND id_action = :id_action");
|
2016-07-15 15:53:21 +00:00
|
|
|
$deleted = $delete->execute([
|
2016-07-18 08:42:18 +00:00
|
|
|
':id_action_merge' => $existingId,
|
|
|
|
':id_target' => $id_target,
|
|
|
|
':id_source' => $id_source,
|
|
|
|
':id_action' => $id_action
|
2016-07-15 15:53:21 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
/* (2) Si erreur SQL, on retourne une erreur */
|
|
|
|
if( !$deleted || self::getById($existingId) !== false )
|
|
|
|
return ManagerError::RepoError;
|
|
|
|
|
|
|
|
|
|
|
|
return ManagerError::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-19 16:01:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RETOURNE LA LISTE D'ACCES POUR UNE MACHINE D'ID DONNE
|
|
|
|
*
|
|
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
|
|
* @id_machine<int> UID de la machine
|
|
|
|
*
|
|
|
|
* @return permissions<Array> Liste des accès des utilisateurs à cette machine
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getPermissions($id_warehouse, $id_machine){
|
|
|
|
|
|
|
|
$getPermissions = Database::getPDO()->prepare("SELECT u.code, GROUP_CONCAT(DISTINCT a.id_action ORDER BY a.id_action ASC) as actions
|
|
|
|
FROM
|
|
|
|
user as u,
|
|
|
|
user_cluster as uc,
|
|
|
|
user_cluster_merge as ucm,
|
|
|
|
machine as m,
|
|
|
|
machine_cluster as mc,
|
|
|
|
machine_cluster_merge as mcm,
|
|
|
|
action as a,
|
|
|
|
action_merge as am
|
|
|
|
|
|
|
|
WHERE u.id_user = ucm.id_user
|
|
|
|
AND uc.id_user_cluster = ucm.id_user_cluster
|
|
|
|
AND am.id_source = uc.id_user_cluster
|
|
|
|
AND m.id_warehouse = u.id_warehouse
|
|
|
|
AND m.id_warehouse = :id_warehouse
|
|
|
|
AND m.id_machine = :id_machine
|
|
|
|
AND m.id_machine = mcm.id_machine
|
|
|
|
AND mc.id_machine_cluster = mcm.id_machine_cluster
|
|
|
|
AND am.id_target = mc.id_machine_cluster
|
|
|
|
AND a.id_action = am.id_action
|
|
|
|
|
|
|
|
GROUP BY u.code");
|
|
|
|
|
|
|
|
$getPermissions->execute([
|
|
|
|
':id_warehouse' => $id_warehouse,
|
|
|
|
':id_machine' => $id_machine
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
return Database::delNumeric( $getPermissions->fetchAll() );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-15 15:43:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|