getDefaultOption() public method

Override this method to define a default option.
See also: __construct()
public getDefaultOption ( ) : string
return string
 public function validate($value, Constraint $constraint)
 {
     $usernameErrors = array();
     $roleNameErrors = array();
     $workspace = $constraint->getDefaultOption();
     $wsRoleNames = array();
     $workspaceRoles = $this->roleManager->getRolesByWorkspace($workspace);
     foreach ($workspaceRoles as $workspaceRole) {
         $wsRoleNames[] = $workspaceRole->getTranslationKey();
     }
     $data = $this->ut->formatCsvOutput(file_get_contents($value));
     $lines = str_getcsv($data, PHP_EOL);
     foreach ($lines as $line) {
         $linesTab = explode(';', $line);
         $nbElements = count($linesTab);
         if (trim($line) !== '' && $nbElements !== 2) {
             $this->context->addViolation($constraint->message);
             return;
         }
     }
     foreach ($lines as $i => $line) {
         if (trim($line) !== '') {
             $datas = explode(';', $line);
             $username = $datas[0];
             $roleName = $datas[1];
             $user = $this->userManager->getOneUserByUsername($username);
             if (is_null($user)) {
                 $msg = $this->translator->trans('workspace_user_invalid', array('%username%' => $username, '%line%' => $i + 1), 'platform') . ' ';
                 $usernameErrors[] = $msg;
             }
             if (!in_array($roleName, $wsRoleNames)) {
                 $msg = $this->translator->trans('line_number', array('%line%' => $i + 1), 'platform') . ' ';
                 $msg .= $this->translator->trans('unavailable_role', array('%translationKey%' => $roleName), 'platform');
                 $roleNameErrors[] = $msg;
             }
         }
     }
     foreach ($usernameErrors as $error) {
         $this->context->addViolation($error);
     }
     foreach ($roleNameErrors as $error) {
         $this->context->addViolation($error);
     }
 }
Example #2
0
 public function validate($value, Constraint $constraint)
 {
     $mode = $constraint->getDefaultOption();
     $lines = str_getcsv(file_get_contents($value), PHP_EOL);
     $authDrivers = $this->authenticationManager->getDrivers();
     foreach ($lines as $line) {
         $linesTab = explode(';', $line);
         $nbElements = count($linesTab);
         if (trim($line) != '') {
             if ($nbElements < 5) {
                 $this->context->addViolation($constraint->message);
                 return;
             }
         }
     }
     $usernames = array();
     $mails = array();
     if ($mode === 1) {
         $currentDate = new \DateTime();
         $timestamp = $currentDate->getTimestamp();
         $fakeUsername = '******' . $timestamp . '@@@';
         $fakeMail = 'fake_email_' . $timestamp . '@fake-' . $timestamp . '-claroline-connect.com';
     }
     foreach ($lines as $i => $line) {
         if (trim($line) != '') {
             $user = explode(';', $line);
             $firstName = $user[0];
             $lastName = $user[1];
             $username = $user[2];
             $pwd = $user[3];
             $email = $user[4];
             if (isset($user[5])) {
                 $code = trim($user[5]) === '' ? null : $user[5];
             } else {
                 $code = null;
             }
             if (isset($user[6])) {
                 $phone = trim($user[6]) === '' ? null : $user[6];
             } else {
                 $phone = null;
             }
             if (isset($user[7])) {
                 $authentication = trim($user[7]) === '' ? null : $user[7];
             } else {
                 $authentication = null;
             }
             if (isset($user[8])) {
                 $modelName = trim($user[7]) === '' ? null : $user[7];
             } else {
                 $modelName = null;
             }
             !array_key_exists($email, $mails) ? $mails[$email] = array($i + 1) : ($mails[$email][] = $i + 1);
             !array_key_exists($username, $usernames) ? $usernames[$username] = array($i + 1) : ($usernames[$username][] = $i + 1);
             $existingUser = null;
             if ($mode === 1) {
                 try {
                     $existingUser = $this->userManager->getUserByUsernameOrMail($username, $email);
                 } catch (NonUniqueResultException $e) {
                     $msg = $this->translator->trans('line_number', array('%line%' => $i + 1), 'platform');
                     $msg .= ' ' . $this->translator->trans('username_and_email_from_two_different_users', array('%username%' => $username, '%email%' => $email), 'platform');
                     $this->context->addViolation($msg);
                     continue;
                 }
             }
             if (!is_null($existingUser)) {
                 // For an update, we will validate user with a fake username and email
                 $upperExistingUsername = strtoupper(trim($existingUser->getUsername()));
                 $upperExistingMail = strtoupper(trim($existingUser->getMail()));
                 $upperUsername = strtoupper(trim($username));
                 $upperMail = strtoupper(trim($email));
                 if ($upperExistingUsername === $upperUsername && $upperExistingMail === $upperMail) {
                     $existingUser->setUsername($fakeUsername);
                     $existingUser->setMail($fakeMail);
                 } elseif ($upperExistingUsername === $upperUsername) {
                     $existingUser->setUsername($fakeUsername);
                     $existingUser->setMail($email);
                 } else {
                     $existingUser->setUsername($username);
                     $existingUser->setMail($fakeMail);
                 }
                 $existingUser->setFirstName($firstName);
                 $existingUser->setLastName($lastName);
                 $existingUser->setPlainPassword($pwd);
                 $existingUser->setAdministrativeCode($code);
                 $existingUser->setPhone($phone);
                 $errors = $this->validator->validate($existingUser, array('registration', 'Default'));
                 $existingUser->setUsername($username);
                 $existingUser->setMail($email);
             } else {
                 $newUser = new User();
                 $newUser->setFirstName($firstName);
                 $newUser->setLastName($lastName);
                 $newUser->setUsername($username);
                 $newUser->setPlainPassword($pwd);
                 $newUser->setMail($email);
                 $newUser->setAdministrativeCode($code);
                 $newUser->setPhone($phone);
                 $errors = $this->validator->validate($newUser, array('registration', 'Default'));
             }
             if ($authentication) {
                 if (!in_array($authentication, $authDrivers)) {
                     $msg = $this->translator->trans('authentication_invalid', array('%authentication%' => $authentication, '%line%' => $i + 1), 'platform') . ' ';
                     $this->context->addViolation($msg);
                 }
             }
             foreach ($errors as $error) {
                 $this->context->addViolation($this->translator->trans('line_number', array('%line%' => $i + 1), 'platform') . ' ' . $error->getInvalidValue() . ' : ' . $error->getMessage());
             }
         }
     }
     if ($modelName) {
         $model = $this->om->getRepository('ClarolineCoreBundle:Model\\WorkspaceModel')->findOneByName($modelName);
         if (!$model) {
             $msg = $this->translator->trans('model_invalid', array('%model%' => $modelName, '%line%' => $i + 1), 'platform') . ' ';
             $this->context->addViolation($msg);
         }
     }
     foreach ($usernames as $username => $lines) {
         if (count($lines) > 1) {
             $msg = $this->translator->trans('username_found_at', array('%username%' => $username, '%lines%' => $this->getLines($lines)), 'platform') . ' ';
             $this->context->addViolation($msg);
         }
     }
     foreach ($mails as $mail => $lines) {
         if (count($lines) > 1) {
             $msg = $this->translator->trans('email_found_at', array('%email%' => $mail, '%lines%' => $this->getLines($lines)), 'platform') . ' ';
             $this->context->addViolation($msg);
         }
     }
 }