isBanned() public static méthode

Check whether or not a banned value is banned for a given reason.
public static isBanned ( integer $banned, integer $reason ) : boolean
$banned integer The banned value.
$reason integer The reason for the banning or an empty string to check if banned for any reason. This should be one of the `BanModel::BAN_*` constants.
Résultat boolean Returns true if the value is banned or false otherwise.
 public function toString()
 {
     $Session = Gdn::session();
     $Controller = Gdn::controller();
     $UserID = $Controller->User->UserID;
     $MemberOptions = array();
     $ProfileOptions = array();
     $Controller->EventArguments['UserID'] = $UserID;
     $Controller->EventArguments['ProfileOptions'] =& $ProfileOptions;
     $Controller->EventArguments['MemberOptions'] =& $MemberOptions;
     if ($Controller->EditMode) {
         return '<div class="ProfileOptions">' . anchor(t('Back to Profile'), userUrl($Controller->User), array('class' => 'ProfileButtons')) . '</div>';
         //         $ProfileOptions[] = array('Text' => t('Back to Profile'), 'Url' => userUrl($Controller->User), 'CssClass' => 'BackToProfile');
     } else {
         // Profile Editing
         if (hasEditProfile($Controller->User->UserID)) {
             $ProfileOptions[] = array('Text' => sprite('SpEditProfile') . ' ' . t('Edit Profile'), 'Url' => userUrl($Controller->User, '', 'edit'));
         } elseif ($Session->isValid() && $UserID == $Session->UserID) {
             $ProfileOptions[] = array('Text' => sprite('SpEditProfile') . ' ' . t('Preferences'), 'Url' => userUrl($Controller->User, '', 'preferences'));
         }
         // Ban/Unban
         $MayBan = checkPermission('Garden.Moderation.Manage') || checkPermission('Garden.Users.Edit') || checkPermission('Moderation.Users.Ban');
         if ($MayBan && $UserID != $Session->UserID) {
             if (BanModel::isBanned($Controller->User->Banned, BanModel::BAN_AUTOMATIC | BanModel::BAN_MANUAL)) {
                 $ProfileOptions[] = array('Text' => sprite('SpBan') . ' ' . t('Unban'), 'Url' => "/user/ban?userid={$UserID}&unban=1", 'CssClass' => 'Popup');
             } elseif (!$Controller->User->Admin) {
                 $ProfileOptions[] = array('Text' => sprite('SpBan') . ' ' . t('Ban'), 'Url' => "/user/ban?userid={$UserID}", 'CssClass' => 'Popup');
             }
         }
         // Delete content.
         if (checkPermission('Garden.Moderation.Manage')) {
             $ProfileOptions[] = array('Text' => sprite('SpDelete') . ' ' . t('Delete Content'), 'Url' => "/user/deletecontent?userid={$UserID}", 'CssClass' => 'Popup');
         }
     }
     return parent::ToString();
 }
 /**
  * Ban a user and optionally delete their content.
  *
  * @since 2.1
  * @param type $UserID
  */
 public function ban($UserID, $Unban = false)
 {
     $this->permission(array('Garden.Moderation.Manage', 'Garden.Users.Edit', 'Moderation.Users.Ban'), false);
     $User = Gdn::userModel()->getID($UserID, DATASET_TYPE_ARRAY);
     if (!$User) {
         throw notFoundException($User);
     }
     $UserModel = Gdn::userModel();
     // Block banning the super admin or system accounts.
     $User = $UserModel->getID($UserID);
     if (val('Admin', $User) == 2) {
         throw forbiddenException("@You may not ban a system user.");
     } elseif (val('Admin', $User)) {
         throw forbiddenException("@You may not ban a super admin.");
     }
     // Is the user banned for other reasons?
     $this->setData('OtherReasons', BanModel::isBanned(val('Banned', $User, 0), ~BanModel::BAN_AUTOMATIC));
     if ($this->Form->authenticatedPostBack()) {
         if ($Unban) {
             $UserModel->unban($UserID, array('RestoreContent' => $this->Form->getFormValue('RestoreContent')));
         } else {
             if (!ValidateRequired($this->Form->getFormValue('Reason'))) {
                 $this->Form->addError('ValidateRequired', 'Reason');
             }
             if ($this->Form->getFormValue('Reason') == 'Other' && !ValidateRequired($this->Form->getFormValue('ReasonText'))) {
                 $this->Form->addError('ValidateRequired', 'Reason Text');
             }
             if ($this->Form->errorCount() == 0) {
                 if ($this->Form->getFormValue('Reason') == 'Other') {
                     $Reason = $this->Form->getFormValue('ReasonText');
                 } else {
                     $Reason = $this->Form->getFormValue('Reason');
                 }
                 // Just because we're banning doesn't mean we can nuke their content
                 $DeleteContent = checkPermission('Garden.Moderation.Manage') ? $this->Form->getFormValue('DeleteContent') : false;
                 $UserModel->ban($UserID, array('Reason' => $Reason, 'DeleteContent' => $DeleteContent));
             }
         }
         if ($this->Form->errorCount() == 0) {
             // Redirect after a successful save.
             if ($this->Request->get('Target')) {
                 $this->RedirectUrl = $this->Request->get('Target');
             } elseif ($this->deliveryType() == DELIVERY_TYPE_ALL) {
                 $this->RedirectUrl = url(userUrl($User));
             } else {
                 $this->jsonTarget('', '', 'Refresh');
             }
         }
     }
     // Permission flag for view
     $this->setData('_MayDeleteContent', checkPermission('Garden.Moderation.Manage'));
     $this->setData('User', $User);
     $this->addSideMenu();
     $this->title($Unban ? t('Unban User') : t('Ban User'));
     if ($Unban) {
         $this->View = 'Unban';
     }
     $this->render();
 }
Exemple #3
0
 /**
  * Unban a user.
  *
  * @param int $UserID The user to unban.
  * @param array $Options Options for the unban.
  * @since 2.1
  */
 public function unBan($UserID, $Options = [])
 {
     $User = $this->getID($UserID, DATASET_TYPE_ARRAY);
     if (!$User) {
         throw notFoundException();
     }
     $Banned = $User['Banned'];
     if (!BanModel::isBanned($Banned, BanModel::BAN_AUTOMATIC | BanModel::BAN_MANUAL)) {
         throw new Gdn_UserException(t("The user isn't banned.", "The user isn't banned or is banned by some other function."));
     }
     // Unban the user.
     $NewBanned = BanModel::setBanned($Banned, false, BanModel::BAN_AUTOMATIC | BanModel::BAN_MANUAL);
     $this->setField($UserID, 'Banned', $NewBanned);
     // Restore the user's content.
     if (val('RestoreContent', $Options)) {
         $BanLogID = $this->getAttribute($UserID, 'BanLogID');
         if ($BanLogID) {
             $LogModel = new LogModel();
             try {
                 $LogModel->restore($BanLogID);
             } catch (Exception $Ex) {
                 if ($Ex->getCode() != 404) {
                     throw $Ex;
                 }
             }
             $this->saveAttribute($UserID, 'BanLogID', null);
         }
     }
     // Add an activity for the unbanning.
     if (val('AddActivity', $Options, true)) {
         $ActivityModel = new ActivityModel();
         $Story = val('Story', $Options, null);
         // Notify the moderators of the unban.
         $Activity = ['ActivityType' => 'Ban', 'NotifyUserID' => ActivityModel::NOTIFY_MODS, 'ActivityUserID' => $UserID, 'RegardingUserID' => Gdn::session()->UserID, 'HeadlineFormat' => t('HeadlineFormat.Unban', '{RegardingUserID,You} unbanned {ActivityUserID,you}.'), 'Story' => $Story, 'Data' => ['Unban' => true]];
         $ActivityModel->queue($Activity);
         // Notify the user of the unban.
         $Activity['NotifyUserID'] = $UserID;
         $Activity['Emailed'] = ActivityModel::SENT_PENDING;
         $Activity['HeadlineFormat'] = t('HeadlineFormat.Unban.Notification', "You've been unbanned.");
         $ActivityModel->queue($Activity, false, ['Force' => true]);
         $ActivityModel->saveQueue();
     }
 }