BanModel::checkUser PHP Method

checkUser() public static method

Add ban data to all Get requests.
Since: 2.0.18
public static checkUser ( $User, Gdn_Validation $Validation = null, boolean $UpdateBlocks = false, &$BansFound = null ) : boolean
$Validation Gdn_Validation
$UpdateBlocks boolean
return boolean Whether user is banned.
    public static function checkUser($User, $Validation = null, $UpdateBlocks = false, &$BansFound = null)
    {
        $Bans = self::AllBans();
        $Fields = array('Name' => 'Name', 'Email' => 'Email', 'IPAddress' => 'LastIPAddress');
        $Banned = array();
        if (!$BansFound) {
            $BansFound = array();
        }
        foreach ($Bans as $Ban) {
            // Convert ban to regex.
            $Parts = explode('*', str_replace('%', '*', $Ban['BanValue']));
            $Parts = array_map('preg_quote', $Parts);
            $Regex = '`^' . implode('.*', $Parts) . '$`i';
            $value = val($Fields[$Ban['BanType']], $User);
            if ($Ban['BanType'] === 'IPAddress') {
                $value = ipDecode($value);
            }
            if (preg_match($Regex, $value)) {
                $Banned[$Ban['BanType']] = true;
                $BansFound[] = $Ban;
                if ($UpdateBlocks) {
                    Gdn::sql()->update('Ban')->set('CountBlockedRegistrations', 'CountBlockedRegistrations + 1', false, false)->where('BanID', $Ban['BanID'])->put();
                }
            }
        }
        // Add the validation results.
        if ($Validation) {
            foreach ($Banned as $BanType => $Value) {
                $Validation->addValidationResult(Gdn_Form::LabelCode($BanType), 'ValidateBanned');
            }
        }
        return count($Banned) == 0;
    }

Usage Example

Beispiel #1
0
 /**
  * Synchronizes the user based on a given UserKey.
  *
  * @param string $UserKey A string that uniquely identifies this user.
  * @param array $Data Information to put in the user table.
  * @return int The ID of the user.
  */
 public function synchronize($UserKey, $Data)
 {
     $UserID = 0;
     $Attributes = val('Attributes', $Data);
     if (is_string($Attributes)) {
         $Attributes = dbdecode($Attributes);
     }
     if (!is_array($Attributes)) {
         $Attributes = [];
     }
     // If the user didnt log in, they won't have a UserID yet. That means they want a new
     // account. So create one for them.
     if (!isset($Data['UserID']) || $Data['UserID'] <= 0) {
         // Prepare the user data.
         $UserData = [];
         $UserData['Name'] = $Data['Name'];
         $UserData['Password'] = randomString(16);
         $UserData['Email'] = val('Email', $Data, '*****@*****.**');
         $UserData['Gender'] = strtolower(substr(val('Gender', $Data, 'u'), 0, 1));
         $UserData['HourOffset'] = val('HourOffset', $Data, 0);
         $UserData['DateOfBirth'] = val('DateOfBirth', $Data, '');
         $UserData['CountNotifications'] = 0;
         $UserData['Attributes'] = $Attributes;
         $UserData['InsertIPAddress'] = ipEncode(Gdn::request()->ipAddress());
         if ($UserData['DateOfBirth'] == '') {
             $UserData['DateOfBirth'] = '1975-09-16';
         }
         // Make sure there isn't another user with this username.
         if ($this->validateUniqueFields($UserData['Name'], $UserData['Email'])) {
             if (!BanModel::checkUser($UserData, $this->Validation, true)) {
                 throw permissionException('Banned');
             }
             // Insert the new user.
             $this->addInsertFields($UserData);
             $UserID = $this->insertInternal($UserData);
         }
         if ($UserID > 0) {
             $NewUserRoleIDs = $this->newUserRoleIDs();
             // Save the roles.
             $Roles = val('Roles', $Data, false);
             if (empty($Roles)) {
                 $Roles = $NewUserRoleIDs;
             }
             $this->saveRoles($UserID, $Roles, false);
         }
     } else {
         $UserID = $Data['UserID'];
     }
     // Synchronize the transientkey from the external user data source if it is present (eg. WordPress' wpnonce).
     if (array_key_exists('TransientKey', $Attributes) && $Attributes['TransientKey'] != '' && $UserID > 0) {
         $this->setTransientKey($UserID, $Attributes['TransientKey']);
     }
     return $UserID;
 }
All Usage Examples Of BanModel::checkUser