/**
  * @Route("/edit/{user}",name="registration_admin_edit")
  */
 public function editAction(Request $request, $user)
 {
     $response = new Response();
     $em = $this->getDoctrine()->getManager();
     $form = $this->createForm(EditType::class);
     $form->handleRequest($request);
     $user = $em->getRepository(RegUser::class)->findOneByUsername($user);
     if ($form->isValid()) {
         $data = $form->getData();
         if ($data['action'] === true) {
             $password = new Password();
             $password->setHash($user->getPasswordHash());
             $password->setId('default');
             $password->setEncoder(CryptEncoder::class);
             $userLdap = $this->get('cloud.ldap.util.usermanipulator')->createUserObject($user->getUsername(), $password);
             $userLdap->setAltEmail($user->getAltEmail());
             $userLdap->setGpgPublicKey($user->getGpgPublicKey());
             $this->get('cloud.ldap.util.usermanipulator')->create($userLdap);
             $em->remove($user);
         } elseif ($data['action'] === false) {
             $em->remove($user);
         } else {
             $response->setStatusCode(400);
             return $response;
         }
         $em->flush();
     } else {
         $response->setContent(json_encode(['successfully' => false, 'error' => $form->getErrors(true)->__toString()]));
         return $response;
     }
     $response->setContent(json_encode(['successfully' => true]));
     return $response;
 }
 /**
  * (non-PHPdoc)
  * @see \Cloud\LdapBundle\Security\PasswordEncoderInterface::parsePassword()
  */
 public function parsePassword($password_hash)
 {
     $password = new Password();
     $password->setHash($password_hash);
     $matches = null;
     preg_match('#^{crypt}\\$\\d\\$(rounds=\\d+\\$)?([0-9a-zA-Z_-]+)?(=|\\+)?[0-9a-zA-Z_-]+\\$[^\\$]*$#', $password_hash, $matches);
     if ($matches != null) {
         $password->setId(substr($matches[2], 0, -1));
         $password->setMasterPassword($matches[3] === '+');
     }
     return $password;
 }
 /**
  * (non-PHPdoc)
  * @see \Cloud\LdapBundle\Security\PasswordEncoderInterface::parsePassword()
  */
 public static function parsePassword(Attribute $password_hash)
 {
     $password = new Password();
     $password->setAttribute($password_hash);
     $password->setHash($password_hash->get());
     $password->setId('default');
     /*if(preg_match('#^[0-9A-F]$#',$password_hash->get())===1) {
           $password->setMasterPassword(true);
       }elseif(preg_match('#^[0-9a-f]$#',$password_hash->get())===1) {
           $password->setMasterPassword(false);
       }*/
     $password->setEncoder(NtEncoder::class);
     return $password;
 }
 /**
  * (non-PHPdoc)
  *
  * @see \Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface::encodePassword()
  */
 public static function encodePassword(Password $password)
 {
     $rounds = 60000;
     // incresed rounds for harder bruteforce
     $salt = "";
     if ($password->getId() != null && $password->getId() != "") {
         $salt = self::getRandomeSalt(16 - strlen($password->getId()));
         $salt = $password->getId() . ($password->isMasterPassword() ? '+' : '=') . $salt;
     } else {
         $salt = 'default=' . self::getRandomeSalt();
     }
     $hash = crypt($password->getPasswordPlain(), '$6$rounds=' . $rounds . '$' . $salt . '$');
     $password->setHash('{crypt}' . $hash);
     $password->setPasswordPlain(null);
     $password->setEncoder(CryptEncoder::class);
 }