UserModel::deleteID PHP Method

deleteID() public method

Delete a single user.
public deleteID ( integer $userID, array $options = [] )
$userID integer The user to delete.
$options array See {@link UserModel::deleteContent()}, and {@link UserModel::getDelete()}.
    public function deleteID($userID, $options = [])
    {
        if ($userID == $this->getSystemUserID()) {
            $this->Validation->addValidationResult('', 'You cannot delete the system user.');
            return false;
        }
        $Content = [];
        // Remove shared authentications.
        $this->getDelete('UserAuthentication', ['UserID' => $userID], $Content);
        // Remove role associations.
        $this->getDelete('UserRole', ['UserID' => $userID], $Content);
        $this->deleteContent($userID, $options, $Content);
        // Remove the user's information
        $this->SQL->update('User')->set(['Name' => t('[Deleted User]'), 'Photo' => null, 'Password' => randomString('10'), 'About' => '', 'Email' => 'user_' . $userID . '@deleted.email', 'ShowEmail' => '0', 'Gender' => 'u', 'CountVisits' => 0, 'CountInvitations' => 0, 'CountNotifications' => 0, 'InviteUserID' => null, 'DiscoveryText' => '', 'Preferences' => null, 'Permissions' => null, 'Attributes' => dbencode(['State' => 'Deleted']), 'DateSetInvitations' => null, 'DateOfBirth' => null, 'DateUpdated' => Gdn_Format::toDateTime(), 'HourOffset' => '0', 'Score' => null, 'Admin' => 0, 'Deleted' => 1])->where('UserID', $userID)->put();
        // Remove user's cache rows
        $this->clearCache($userID);
        return true;
    }

Usage Example

示例#1
0
 /**
  * Delete a user account.
  *
  * @since 2.0.0
  * @access public
  * @param int $UserID Unique ID.
  * @param string $Method Type of deletion to do (delete, keep, or wipe).
  */
 public function delete($UserID = '', $Method = '')
 {
     $this->permission('Garden.Users.Delete');
     $Session = Gdn::session();
     if ($Session->User->UserID == $UserID) {
         trigger_error(errorMessage("You cannot delete the user you are logged in as.", $this->ClassName, 'FetchViewLocation'), E_USER_ERROR);
     }
     $this->setHighlightRoute('dashboard/user');
     $this->title(t('Delete User'));
     $RoleModel = new RoleModel();
     $AllRoles = $RoleModel->getArray();
     // By default, people with access here can freely assign all roles
     $this->RoleData = $AllRoles;
     $UserModel = new UserModel();
     $this->User = $UserModel->getID($UserID);
     try {
         $CanDelete = true;
         $this->EventArguments['CanDelete'] =& $CanDelete;
         $this->EventArguments['TargetUser'] =& $this->User;
         // These are all the 'effective' roles for this delete action. This list can
         // be trimmed down from the real list to allow subsets of roles to be
         // edited.
         $this->EventArguments['RoleData'] =& $this->RoleData;
         $UserRoleData = $UserModel->getRoles($UserID)->resultArray();
         $RoleIDs = array_column($UserRoleData, 'RoleID');
         $RoleNames = array_column($UserRoleData, 'Name');
         $this->UserRoleData = ArrayCombine($RoleIDs, $RoleNames);
         $this->EventArguments['UserRoleData'] =& $this->UserRoleData;
         $this->fireEvent("BeforeUserDelete");
         $this->setData('CanDelete', $CanDelete);
         $Method = in_array($Method, array('delete', 'keep', 'wipe')) ? $Method : '';
         $this->Method = $Method;
         if ($Method != '') {
             $this->View = 'deleteconfirm';
             $this->RedirectUrl = url('/dashboard/user');
         }
         if ($this->Form->authenticatedPostBack(true) && $Method != '') {
             $UserModel->deleteID($UserID, array('DeleteMethod' => $Method));
             $this->View = 'deletecomplete';
         }
     } catch (Exception $Ex) {
         $this->Form->addError($Ex);
     }
     $this->render();
 }
UserModel