Commit 7ee10e95 authored by Stephane Bouvry's avatar Stephane Bouvry
Browse files

MAJ purge des rôles

parent 6cd9ff73
Loading
Loading
Loading
Loading
Loading
+0 −106
Original line number Diff line number Diff line
<?php
/**
 * Created by PhpStorm.
 * User: bouvry
 * Date: 04/10/19
 * Time: 11:49
 */

namespace Oscar\Command;


use Moment\Moment;
use Oscar\Connector\ConnectorRepport;
use Oscar\Entity\Authentification;
use Oscar\Entity\LogActivity;
use Oscar\Entity\Person;
use Oscar\Entity\Role;
use Oscar\Service\ConnectorService;
use Oscar\Service\OscarConfigurationService;
use Oscar\Service\OscarUserContext;
use Oscar\Service\PersonService;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class OscarPersonsSyncCommand extends OscarCommandAbstract
{
    protected static $defaultName = 'persons:sync';

    protected function configure()
    {
        $this
            ->setDescription("Execute la synchronisation des personnes")
            ->addArgument("connectorname", InputArgument::REQUIRED, "Connector (rest)")
            ->addOption('no-rebuild','b', InputOption::VALUE_NONE, 'Ignore la reconstruction de l\'index de recherche après la mise à jour')
            ->addOption('purge','p', InputOption::VALUE_NONE, 'Supprime les personnes d\'Oscar si elles ne sont plus proposées dans la source distante (et qu\'elles ne sont pas utilisées dans Oscar)')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->addOutputStyle($output);

        /** @var OscarUserContext $oscaruserContext */
        $oscaruserContext = $this->getServicemanager()->get(OscarUserContext::class);

        $io = new SymfonyStyle($input, $output);

        $io->title("Synchronisation des personnes");

        $connectorName = $input->getArgument('connectorname');


        /** @var OscarConfigurationService $oscarConfig */
        $oscarConfig = $this->getServicemanager()->get(OscarConfigurationService::class);

        $io->section("Connector infos : ");
        $io->writeln("Connecteur : <bold>$connectorName</bold>");

        $purge = $input->getOption('purge');

        /** @var ConnectorService $connectorService */
        $connectorService = $this->getServicemanager()->get(ConnectorService::class);

        $connector = $connectorService->getConnector("person.".$connectorName);

        try {
            $connector->setOptionPurge($input->getOption('purge'));

            /** @var ConnectorRepport $repport */
            $repport = $connector->execute();
            foreach ($repport->getRepportStates() as $type => $out) {
                $short = substr($type, 0, 3);
                $io->section( "Opération " . strtoupper($type));
                foreach ($out as $line) {
                    $io->writeln("$short\t " . date('Y-m-d H:i:s', $line['time']) . " " . $line['message'] );
                }
            }
        } catch (\Exception $e) {
            $io->error($e->getMessage());
            exit(0);
        }

        $io->section("Reconstruction de l'index de recherche : ");
        if( !$input->getOption('no-rebuild') ){
            /** @var PersonService $personService */
            $personService = $this->getServicemanager()->get(PersonService::class);

            try {
                $persons = $personService->getPersons();
                $personService->getSearchEngineStrategy()->rebuildIndex($persons);
                $io->success(sprintf('Index de recherche mis à jour avec %s personnes indexées', count($persons)));
            } catch ( \Exception $e ){
                $io->error($e->getMessage());
            }
        } else {
            $io->warning("Pas de reconstruction d'index");
        }


    }
}
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ class ConnectorAccessCurlHttp implements IConnectorAccess
        curl_setopt($curl, CURLOPT_FAILONERROR, true);

        // Fix CURL error curl: (18) transfer closed with 141 bytes remaining to read
        curl_setopt($curl, CURLOPT_IGNORE_CONTENT_LENGTH, true);
        //curl_setopt($curl, CURLOPT_IGNORE_CONTENT_LENGTH, true);

        $return = curl_exec($curl);

+26 −1
Original line number Diff line number Diff line
@@ -77,6 +77,12 @@ class ConnectorPersonREST extends AbstractConnector
        return $this->personHydrator;
    }

    protected function log( string $text ) :void {
        if( true ){
            echo "$text";
        }
    }

    /**
     * @param PersonRepository $personRepository
     * @param bool $force
@@ -85,15 +91,16 @@ class ConnectorPersonREST extends AbstractConnector
     */
    function syncPersons(PersonRepository $personRepository, $force)
    {
        $exist = [];
        $exist = $personRepository->getUidsConnector($this->getName());
        $repport = new ConnectorRepport();
        $this->getPersonHydrator()->setPurge($this->getOptionPurge());
        $repport->addnotice(sprintf("Il y'a déjà %s personne(s) synchronisée(s) pour le connector '%s'", count($exist), $this->getName()));
        $access = $this->getAccessStrategy();
        $this->log("Pending access : " . count($exist));

        try {
            $json = $access->getDataAll();
            $this->log("data gain : " . count($json));
            $personsDatas = null;

            if( is_object($json) && property_exists($json, 'persons') ){
@@ -176,16 +183,31 @@ class ConnectorPersonREST extends AbstractConnector
                        if( count($personOscarToDelete->getActivities()) > 0 ){
                            $activeIn[] = "activité";
                        }

                        if( count($personOscarToDelete->getProjectAffectations()) > 0 ){
                            $activeIn[] = "projet";
                        }

                        // Récupération des affectations issues de la synchro
                        $synchronizedAffectationsOrganizations = $personOscarToDelete->getOrganizationsSync();
                        if( count($synchronizedAffectationsOrganizations) > 0 ){
                            $personRepository->removeOrganizationPersons($synchronizedAffectationsOrganizations);
                            $personRepository->flush($synchronizedAffectationsOrganizations);
                        }

                        if( count($personOscarToDelete->getOrganizations()) > 0 ){
                            $activeIn[] = "organisation";
                        }


                        if( count($activeIn) == 0 ){
                            $idsToDelete[] = $personOscarToDelete->getId();
                        } else {

                            // Tentative de suppression des rôles synchronisés



                            $repport->addwarning("$personOscarToDelete n'a pas été supprimé car il est actif dans : " . implode(', ', $activeIn));
                        }

@@ -204,10 +226,13 @@ class ConnectorPersonREST extends AbstractConnector
                }
            }
        } catch (\Exception $e ){
            $this->log("ERROR : " . $e->getMessage());
            throw new \Exception("Impossible de synchroniser les personnes : " . $e->getMessage());
        }

        $this->log("FLUSH...");
        $personRepository->flush(null);
        $this->log("FLUSH DONE...");

        return $repport;
    }
+5 −0
Original line number Diff line number Diff line
@@ -63,6 +63,11 @@ class OrganizationPerson implements ILoggable
        return $this;
    }

    public function isSync()
    {
        return !($this->getOrigin() == null || $this->getOrigin() == '');
    }

    /**
     * @return Role
     */
+99 −66
Original line number Diff line number Diff line
@@ -211,7 +211,8 @@ class Person implements ResourceInterface
    protected $customSettings;


    function __construct()    {
    function __construct()
    {
        $this->projectAffectations = new ArrayCollection();
        $this->activities = new ArrayCollection();
        $this->organizations = new ArrayCollection();
@@ -229,9 +230,12 @@ class Person implements ResourceInterface
    /**
     * Return TRUE si l'objet a un connector.
     */
    public function isConnected( $connectors = null){
    public function isConnected($connectors = null)
    {
        foreach ($this->getConnectors() as $connector => $value) {
            if( $connectors != null && !in_array($connector, $connectors) ) continue;
            if ($connectors != null && !in_array($connector, $connectors)) {
                continue;
            }
            if ($value) {
                return true;
            }
@@ -239,7 +243,8 @@ class Person implements ResourceInterface
        return false;
    }

    public function getConnectorsDatasStr(){
    public function getConnectorsDatasStr()
    {
        $out = [];
        foreach ($this->getConnectors() as $connector => $value) {
            $out[] = sprintf("%s=%s", $connector, $value);
@@ -264,11 +269,13 @@ class Person implements ResourceInterface
        return $this;
    }

    public function getCustomSettingsObj(){
    public function getCustomSettingsObj()
    {
        return json_decode($this->getCustomSettings(), JSON_OBJECT_AS_ARRAY);
    }

    public function getCustomSettingsKey( $key ){
    public function getCustomSettingsKey($key)
    {
        $custom = $this->getCustomSettingsObj();
        if (is_array($custom) && array_key_exists($key, $custom)) {
            return $custom[$key];
@@ -276,7 +283,8 @@ class Person implements ResourceInterface
        return null;
    }

    public function setCustomSettingsObj( $datas ){
    public function setCustomSettingsObj($datas)
    {
        $this->setCustomSettings(json_encode($datas));
        return $this;
    }
@@ -336,7 +344,8 @@ class Person implements ResourceInterface
        return $this;
    }

    public function getRolesFromConnector( $connectorName ){
    public function getRolesFromConnector($connectorName)
    {
        return [];
    }

@@ -393,8 +402,8 @@ class Person implements ResourceInterface
    }



    public function getLeadedOrganizations(){
    public function getLeadedOrganizations()
    {
        $organizations = [];

        /** @var OrganizationPerson $organizationPerson */
@@ -406,7 +415,8 @@ class Person implements ResourceInterface
        return $organizations;
    }

    public function isLeader(){
    public function isLeader()
    {
        return count($this->getLeadedOrganizations()) > 0;
    }

@@ -418,6 +428,24 @@ class Person implements ResourceInterface
        return $this->organizations;
    }

    /**
     * Retourne uniquement les affectations (PersonOrganization)
     * issue de la synchronisation.
     *
     * @return OrganizationPerson[]
     */
    public function getOrganizationsSync(): array
    {
        $syncOrganizations = [];
        /** @var OrganizationPerson $organizationPerson */
        foreach ($this->getOrganizations() as $organizationPerson) {
            if( $organizationPerson->isSync() ){
                $syncOrganizations[] = $organizationPerson;
            }
        }
        return $syncOrganizations;
    }

    /**
     * @param ArrayCollection $organizations
     */
@@ -662,7 +690,6 @@ class Person implements ResourceInterface
    }



    /**
     * @return string
     */
@@ -732,7 +759,6 @@ class Person implements ResourceInterface
    }



    /**
     * @return string
     */
@@ -828,7 +854,8 @@ class Person implements ResourceInterface
        return $this;
    }

    public function setConnector( $data ){
    public function setConnector($data)
    {
        $this->connectors = $data;
        return $this;
    }
@@ -836,7 +863,6 @@ class Person implements ResourceInterface
    //////////////////////////////////////////////////////////



    public function getDisplayName()
    {
        return $this->getFirstname() . ' ' . $this->getLastname();
@@ -858,7 +884,8 @@ class Person implements ResourceInterface
    }


    public function hasDeclarationIn( Activity $activity ){
    public function hasDeclarationIn(Activity $activity)
    {
        /** @var TimeSheet $timesheet */
        foreach ($activity->getTimesheets() as $timesheet) {
            if ($timesheet->getPerson() == $this) {
@@ -887,7 +914,8 @@ class Person implements ResourceInterface
        );
    }

    public function toArrayList(){
    public function toArrayList()
    {
        $datas = $this->toArray();
        $organisations = [];
        /** @var OrganizationPerson $o */
@@ -921,8 +949,8 @@ class Person implements ResourceInterface
    }


    public function mergeTo( Person $person ){

    public function mergeTo(Person $person)
    {
        $activititesWithWP = [];

        /** @var ProjectMember $projectMember */
@@ -942,11 +970,11 @@ class Person implements ResourceInterface
        /** @var WorkPackagePerson $organizationPerson */
        foreach ($this->getWorkPackages() as $workPackage) {
            $workPackage->setPerson($person);

        }
    }

    public function toJson( $options = []){
    public function toJson($options = [])
    {
        $json = $this->toArray();
        $json['urlPerson'] = array_key_exists('urlPerson', $options) ? $options['urlPerson'] : false;
        return $json;
@@ -965,21 +993,26 @@ class Person implements ResourceInterface
    private $cGetDateCreated;
    private $cGetDateUpdated;

    public function getDateCreatedStr(){
    public function getDateCreatedStr()
    {
        if ($this->cGetDateCreated == null) {
            $this->cGetDateCreated = $this->getDateCreated() ? $this->getDateCreated()->format('c') : "";
        }
        return $this->cGetDateCreated;
    }

    public function getDateUpdatedStr(){
    public function getDateUpdatedStr()
    {
        if ($this->cGetDateUpdated == null) {
            $this->cGetDateUpdated = $this->getDateUpdated() ? $this->getDateUpdated()->format('c') : $this->getDateCreatedStr();
            $this->cGetDateUpdated = $this->getDateUpdated() ? $this->getDateUpdated()->format(
                'c'
            ) : $this->getDateCreatedStr();
        }
        return $this->cGetDateUpdated;
    }

    public function getDateCachedStr(){
    public function getDateCachedStr()
    {
        return $this->getDateUpdatedStr();
    }
}
Loading