Пример #1
0
 /**
  * Remove permission
  *
  * @param \BOS\UserBundle\Entity\Permission $permission
  */
 public function removePermission(\BOS\UserBundle\Entity\Permission $permission)
 {
     $this->permissions->removeElement($permission);
 }
Пример #2
0
 public function __construct(ContainerInterface $container)
 {
     $this->container = $container;
     try {
         $this->em = $this->container->get('doctrine')->getManager('bos');
     } catch (\Exception $e) {
     }
     if (!$this->em) {
         $this->em = $this->container->get('doctrine')->getEntityManager();
     }
     $custom = null;
     if (!$this->container->getParameter('bos_login_name')) {
         die("BOSUser needs the 'bos_login_name' parameter defined in config.yml. Please refer to the documentation.");
     }
     if (!$this->container->getParameter('bos_default_behaviour')) {
         die("BOSUser needs the 'bos_default_behaviour' parameter defined in config.yml. Please refer to the documentation.");
     }
     $custom = $this->container->getParameter('bos_user_entity');
     //chequeo el sistema en el que estoy y sus permisos
     //sistema
     //if(!$containerSystem){
     //   die("BOSUser needs the 'bos_login_name' parameter defined in config.yml. Please refer to the documentation.");
     // }
     //permisos
     //die($containerSystem."");
     if ($custom) {
         $t = explode(":", $custom);
         $fBundleName = $t[0];
         $fEntityName = $t[1];
         $foundName = "";
         $foundDir = "";
         $bundles = $this->container->get('kernel')->getBundles();
         $bundleName = '';
         foreach ($bundles as $type => $bundle) {
             $cBundle = new \ReflectionClass($bundle);
             $cName = $cBundle->getName();
             $temp = explode("\\", $cName);
             $bundleName = trim($temp[count($temp) - 1]);
             $bundleDir = "";
             for ($i = 0; $i < count($temp) - 1; $i++) {
                 if ($temp[$i] != "\\") {
                     $bundleDir = $bundleDir . $temp[$i] . "\\";
                 }
             }
             if ($bundleName == $fBundleName) {
                 $foundName = $bundleName;
                 $foundDir = $bundleDir;
             }
         }
         if ($foundName == "") {
             die("BOSUser: Couldn't find the bundle '" . $fBundleName . "'. Check your parameters and try again.");
         }
         $this->entityClass = $foundDir . "Entity\\" . $fEntityName;
         try {
             $this->bos = $this->em->getRepository($custom);
         } catch (\Exception $e) {
             die("BOSUser: "******"";
         $this->bos = $this->em->getRepository("BOSUserBundle:BOSUser");
     }
     if ($this->isLoggedIn()) {
         //Keep the user data updated
         $session = $this->getSession();
         $username = $this->getUser()->getUsername();
         $user = $this->bos->findOneBy(array("username" => $username));
         $session->set('bos_user', $user);
     }
     $containerSystem = $this->container->getParameter('bos_system');
     $containerRoles = $this->container->getParameter('roles');
     $mSystem = $this->container->getParameter('bos_system');
     if (!isset($mSystem) || !$mSystem || $mSystem == "") {
         die("You must set a system name to use BOSUser.");
     }
     $actualSystem = $this->em->getRepository('BOSUserBundle:System')->findOneBy(array("name" => $mSystem));
     $this->system = $actualSystem;
     /* If the system doesnt exist, we create it */
     if (!$actualSystem) {
         $actualSystem = new \BOS\UserBundle\Entity\System();
         $actualSystem->setName($containerSystem);
         $this->em->persist($actualSystem);
         $this->em->flush();
     }
     //CREACION DE PARAMETROS DE SISTEMAS
     $rol_temp = NULL;
     /* Revisamos para borrar los que quede de mas en la base */
     $manager = $this->em;
     $roles = $manager->getRepository("BOSUserBundle:Role")->findBy(array("system" => $actualSystem));
     foreach ($roles as $role) {
         $cRole = $this->getRoleByName($role->getName(), $containerRoles);
         /* cRole tiene el rol que esta en el config.yml con sus permisos */
         if ($cRole != null) {
             $rolePermissions = $role->getRolePermissions();
             /* Encontró el Rol de la base en el YML, ahora reviso sus permisos */
             foreach ($rolePermissions as $rolePermission) {
                 $permission = $rolePermission->getPermission();
                 /* Para cada permiso del rol en la base de datos */
                 //print_r($cRole["permissions"]);
                 if (!in_array($permission->getName(), $cRole["permissions"])) {
                     $manager->remove($rolePermission);
                 }
             }
         } else {
             $rp = $manager->getRepository("BOSUserBundle:RolePermission")->findBy(array("role" => $role));
             foreach ($rp as $rperm) {
                 $manager->remove($rperm);
                 $manager->flush();
             }
             $manager->remove($role);
             $manager->flush();
         }
     }
     $manager->flush();
     $permissions = $manager->getRepository("BOSUserBundle:Permission")->findBy(array("system" => $actualSystem));
     foreach ($permissions as $permission) {
         $q = $manager->createQuery("SELECT rp FROM BOSUserBundle:RolePermission rp " . "JOIN BOSUserBundle:Role r WHERE r = rp.role " . "WHERE r.system = :system AND rp.permission = :permission");
         $q->setParameter("system", $actualSystem);
         $q->setParameter("permission", $permission);
         $result = $q->getResult();
         $amountUsed = sizeof($result);
         if ($amountUsed == 0) {
             /* No tiene ningún RolePermission */
             $manager->remove($permission);
             $manager->flush();
         }
     }
     //guardo los roles que figuran en el yml si hay roles y sistema seteados
     if ($containerRoles && $actualSystem) {
         //recorro los roles y los guardo
         $mKeys = array_keys($containerRoles);
         $i = -1;
         foreach ($containerRoles as $actualRole) {
             $i++;
             $mRoleName = $mKeys[$i];
             $role = $this->em->getRepository('BOSUserBundle:Role')->findOneBy(array("name" => $mRoleName, "system" => $actualSystem));
             //si el rol no existe lo creo
             if (!$role) {
                 $role = new Role();
                 $role->setName($mRoleName);
                 $role->setSystem($actualSystem);
                 $this->em->persist($role);
                 $this->em->flush();
             }
             foreach ($actualRole["permissions"] as $actualPermission) {
                 $permission = $this->em->getRepository("BOSUserBundle:Permission")->findOneBy(array("system" => $actualSystem, "name" => $actualPermission));
                 //si el permiso no, existe lo creo
                 if (!$permission) {
                     $permission = new Permission();
                     $permission->setName($actualPermission);
                     $permission->setSystem($actualSystem);
                     $this->em->persist($permission);
                     $this->em->flush();
                 }
                 $rPermission = $this->em->getRepository('BOSUserBundle:RolePermission')->findOneBy(array("role" => $role, "permission" => $permission));
                 if (!$rPermission) {
                     $rPermission = new RolePermission();
                     $rPermission->setRole($role);
                     $rPermission->setPermission($permission);
                     $this->em->persist($rPermission);
                     $this->em->flush();
                 }
             }
         }
     }
     try {
     } catch (\Exception $e) {
         die("Error: " . $e->getMessage());
     }
 }