validateRule() public method

Validates a rule on the form and adds its result to the errors collection.
See also: Gdn_Validation::ValidateRule()
public validateRule ( string $FieldName, string | array $Rule, string $CustomError = '' ) : boolean
$FieldName string The name of the field to validate.
$Rule string | array The rule to validate against.
$CustomError string A custom error string.
return boolean Whether or not the rule succeeded.
Exemplo n.º 1
0
 /**
  * Request password reset.
  *
  * @access public
  * @since 2.0.0
  */
 public function passwordRequest()
 {
     Gdn::locale()->setTranslation('Email', t(UserModel::signinLabelCode()));
     if ($this->Form->isPostBack() === true) {
         $this->Form->validateRule('Email', 'ValidateRequired');
         if ($this->Form->errorCount() == 0) {
             try {
                 $Email = $this->Form->getFormValue('Email');
                 if (!$this->UserModel->passwordRequest($Email)) {
                     $this->Form->setValidationResults($this->UserModel->validationResults());
                     Logger::event('password_reset_failure', Logger::INFO, 'Can\'t find account associated with email/username {Input}.', array('Input' => $Email));
                 }
             } catch (Exception $ex) {
                 $this->Form->addError($ex->getMessage());
             }
             if ($this->Form->errorCount() == 0) {
                 $this->Form->addError('Success!');
                 $this->View = 'passwordrequestsent';
                 Logger::event('password_reset_request', Logger::INFO, '{Input} has been sent a password reset email.', array('Input' => $Email));
             }
         } else {
             if ($this->Form->errorCount() == 0) {
                 $this->Form->addError("Couldn't find an account associated with that email/username.");
                 Logger::event('password_reset_failure', Logger::INFO, 'Can\'t find account associated with email/username {Input}.', array('Input' => $this->Form->getValue('Email')));
             }
         }
     }
     $this->render();
 }
Exemplo n.º 2
0
 /**
  * Change the owner of an addon.
  *
  * @param int $AddonID Addon to manage.
  * @throws Exception Addon not found.
  */
 public function changeOwner($AddonID)
 {
     $this->permission('Garden.Settings.Manage');
     $Addon = $this->AddonModel->getSlug($AddonID);
     if (!$Addon) {
         throw notFoundException('Addon');
     }
     if ($this->Form->authenticatedPostBack()) {
         $this->Form->validateRule('User', 'ValidateRequired');
         if ($this->Form->errorCount() == 0) {
             $NewUser = $this->Form->getFormValue('User');
             if (is_numeric($NewUser)) {
                 $User = Gdn::userModel()->getID($NewUser, DATASET_TYPE_ARRAY);
             } else {
                 $User = Gdn::userModel()->getByUsername($NewUser);
             }
             if (!$User) {
                 $this->Form->addError('@' . self::notFoundString('User', $NewUser));
             }
         }
         if ($this->Form->errorCount() == 0) {
             $this->AddonModel->setField($Addon['AddonID'], 'InsertUserID', val('UserID', $User));
         }
     } else {
         $this->Form->addError('You must POST to this page.');
     }
     $this->render();
 }
 /**
  *
  *
  * @param bool $UserID
  * @throws Exception
  * @throws Gdn_UserException
  */
 public function sso($UserID = false)
 {
     $this->permission('Garden.Users.Edit');
     $ProviderModel = new Gdn_AuthenticationProviderModel();
     $Form = new Gdn_Form();
     if ($this->Request->isAuthenticatedPostBack()) {
         // Make sure everything has been posted.
         $Form->validateRule('ClientID', 'ValidateRequired');
         $Form->validateRule('UniqueID', 'ValidateRequired');
         if (!validateRequired($Form->getFormValue('Username')) && !validateRequired($Form->getFormValue('Email'))) {
             $Form->addError('Username or Email is required.');
         }
         $Provider = $ProviderModel->getProviderByKey($Form->getFormValue('ClientID'));
         if (!$Provider) {
             $Form->addError(sprintf('%1$s "%2$s" not found.', t('Provider'), $Form->getFormValue('ClientID')));
         }
         if ($Form->errorCount() > 0) {
             throw new Gdn_UserException($Form->errorString());
         }
         // Grab the user.
         $User = false;
         if ($Email = $Form->getFormValue('Email')) {
             $User = Gdn::userModel()->GetByEmail($Email);
         }
         if (!$User && ($Username = $Form->getFormValue('Username'))) {
             $User = Gdn::userModel()->GetByUsername($Username);
         }
         if (!$User) {
             throw new Gdn_UserException(sprintf(t('User not found.'), strtolower(t(UserModel::SigninLabelCode()))), 404);
         }
         // Validate the user's password.
         $PasswordHash = new Gdn_PasswordHash();
         $Password = $this->Form->getFormValue('Password', null);
         if ($Password !== null && !$PasswordHash->CheckPassword($Password, val('Password', $User), val('HashMethod', $User))) {
             throw new Gdn_UserException(t('Invalid password.'), 401);
         }
         // Okay. We've gotten this far. Let's save the authentication.
         $User = (array) $User;
         Gdn::userModel()->saveAuthentication(array('UserID' => $User['UserID'], 'Provider' => $Form->getFormValue('ClientID'), 'UniqueID' => $Form->getFormValue('UniqueID')));
         $Row = Gdn::userModel()->getAuthentication($Form->getFormValue('UniqueID'), $Form->getFormValue('ClientID'));
         if ($Row) {
             $this->setData('Result', $Row);
         } else {
             throw new Gdn_UserException(t('There was an error saving the data.'));
         }
     } else {
         $User = Gdn::userModel()->getID($UserID);
         if (!$User) {
             throw notFoundException('User');
         }
         $Result = Gdn::sql()->select('ua.ProviderKey', '', 'ClientID')->select('ua.ForeignUserKey', '', 'UniqueID')->select('ua.UserID')->select('p.Name')->select('p.AuthenticationSchemeAlias', '', 'Type')->from('UserAuthentication ua')->join('UserAuthenticationProvider p', 'ua.ProviderKey = p.AuthenticationKey')->where('UserID', $UserID)->get()->resultArray();
         $this->setData('Result', $Result);
     }
     $this->render('Blank', 'Utility', 'Dashboard');
 }