218 lines
6.1 KiB
PHP
218 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace manager\repo;
|
|
use \manager\Database;
|
|
use \manager\Repo;
|
|
use \manager\ManagerError;
|
|
use \manager\repo\cluster as clusterRepo;
|
|
|
|
class action_merge extends parentRepo{
|
|
|
|
protected static function table_name(){ static $table_name = 'action_merge'; return $table_name; }
|
|
|
|
|
|
/* 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
|
|
* @id_action<int> UID d'une ACTION
|
|
*
|
|
* @return error<ManagerError> Retourne l'erreur 'ManagerError' associée
|
|
*
|
|
*/
|
|
public static function addPermission($id_target, $id_source, $id_action){
|
|
|
|
/* [1] On vérifie l'existance du groupe 'target'
|
|
=========================================================*/
|
|
/* (1) On rédige la requête */
|
|
$targetCheckR = new Repo('machine_cluster/getById', [
|
|
$_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 */
|
|
$sourceCheckR = new Repo('user_cluster/getById', [
|
|
$_SESSION['WAREHOUSE']['id'],
|
|
$id_source
|
|
]);
|
|
$sourceCheck = $sourceCheckR->answer();
|
|
|
|
/* (2) Si aucun résultat, on retourne une erreur */
|
|
if( $sourceCheck === false )
|
|
return ManagerError::NoMatchFound;
|
|
|
|
|
|
/* [3] On vérifie l'existance de l'action
|
|
=========================================================*/
|
|
/* (1) On rédige la requête */
|
|
$actionCheckR = new Repo('action/getById', [ $id_action ]);
|
|
$actionCheck = $actionCheckR->answer();
|
|
|
|
/* (2) Si aucun résultat, on retourne une erreur */
|
|
if( $actionCheck === false )
|
|
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)
|
|
if( $sameTarget['id_source'] == $id_source && $sameTarget['id_action'] == $id_action )
|
|
return ManagerError::Success;
|
|
|
|
}
|
|
|
|
|
|
/* [5] On ajoute la action
|
|
=========================================================*/
|
|
/* (1) On effectue l'insertion */
|
|
$inserted = Table::get('action_merge')
|
|
->insert([
|
|
'id_action_merge' => Rows::INSERT_DEFAULT,
|
|
'id_target' => $id_target,
|
|
'id_source' => $id_source,
|
|
'id_action' => $id_action
|
|
]);
|
|
|
|
/* (2) Si erreur SQL, on retourne une erreur */
|
|
if( !$inserted )
|
|
return ManagerError::RepoError;
|
|
|
|
|
|
return ManagerError::Success;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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
|
|
* @id_action<int> UID d'une PERMISSION
|
|
*
|
|
* @return error<ManagerError> Retourne l'erreur 'ManagerError' associée
|
|
*
|
|
*/
|
|
public static function removePermission($id_target, $id_source, $id_action){
|
|
|
|
/* [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)
|
|
if( $sameTarget['id_source'] == $id_source && $sameTarget['id_action'] == $id_action )
|
|
$existingId = $sameTarget['id_action_merge'];
|
|
}
|
|
|
|
/* (3) Si on a pas trouvé, on retourne une erreur */
|
|
if( is_null($existingId) )
|
|
return ManagerError::NoMatchFound;
|
|
|
|
|
|
/* [2] On supprime la action
|
|
=========================================================*/
|
|
/* (1) On effectue la suppression */
|
|
$deleted = Table::get('action_merge')
|
|
->whereId($existingId)
|
|
->whereIdTarget($id_target)
|
|
->whereIdSource($id_source)
|
|
->whereIdAction($id_action)
|
|
->unique()
|
|
->delete();
|
|
|
|
/* (2) Si erreur SQL, on retourne une erreur */
|
|
if( !$deleted || self::getById($existingId) !== false )
|
|
return ManagerError::RepoError;
|
|
|
|
|
|
return ManagerError::Success;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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.id_user, 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.id_user, u.code");
|
|
|
|
$getPermissions->execute([
|
|
':id_warehouse' => $id_warehouse,
|
|
':id_machine' => $id_machine
|
|
]);
|
|
|
|
|
|
return Database::delNumeric( $getPermissions->fetchAll() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
?>
|