Throttle // 限制

Disable the Throttling Feature // 禁止限制功能

Disables the throttling feature.
禁止限制功能。

Can be done on the throttle provider (global) level or on a throttle instance itself.
可以通过 throttle provider (全局命名空间) 或 throttle 实例本身来完成。

Example

  1. // Get the Throttle Provider
  2. $throttleProvider = Sentry::getThrottleProvider();
  3. // Disable the Throttling Feature
  4. $throttleProvider->disable();

Enable the Throttling Feature // 启用限制功能

Enables the throttling feature.
启用限制功能。

Can be done on the throttle provider (global) level or on a throttle instance itself.
可以通过 throttle provider (全局命名空间) 或 throttle 实例本身来完成。

Example

  1. // Get the Throttle Provider
  2. $throttleProvider = Sentry::getThrottleProvider();
  3. // Enable the Throttling Feature
  4. $throttleProvider->enable();

Check the Throttling Feature Status // 检查限制功能是否开启

Checks to see if the throttling feature is enabled or disabled.
检查限制功能是否开启。

Example

  1. // Get the Throttle Provider
  2. $provider = Sentry::getThrottleProvider();
  3. // Check if the Throttling feature is enabled or disabled
  4. if($provider->isEnabled())
  5. {
  6. // The Throttling Feature is Enabled
  7. }
  8. else
  9. {
  10. // The Throttling Feature is Disabled
  11. }

User Throttling // 针对用户的限制

Ban user(s) // 禁用用户

Bans the user associated with the throttle.
禁用与 throttle 相关联的用户。

  1. try
  2. {
  3. // Find the user using the user id
  4. $throttle = Sentry::findThrottlerByUserId(1);
  5. // Ban the user
  6. $throttle->ban();
  7. }
  8. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  9. {
  10. echo 'User was not found.';
  11. }

Unban user(s) // 解禁用户

Unbans the user associated with the throttle.
解除禁用与 throttle 相关联的用户。

  1. try
  2. {
  3. // Find the user using the user id
  4. $throttle = Sentry::findThrottlerByUserId(1);
  5. // Unban the user
  6. $throttle->unBan();
  7. }
  8. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  9. {
  10. echo 'User was not found.';
  11. }

Check if a User is Banned // 检查用户是否已被禁用

Checks to see if the user is banned.
检查用户是否已被禁用。

  1. try
  2. {
  3. $throttle = Sentry::findThrottlerByUserId(1);
  4. if($banned = $throttle->isBanned())
  5. {
  6. // User is Banned
  7. }
  8. else
  9. {
  10. // User is not Banned
  11. }
  12. }
  13. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  14. {
  15. echo 'User was not found.';
  16. }

Suspend user(s) // 暂时挂起用户

Suspends a user temporarily. Length of the suspension is set by the driver or setSuspensionTime($minutes).
暂时挂起用户,该挂起时长取决于其驱动设置或 setSuspensionTime($minutes) 方法。

  1. try
  2. {
  3. // Find the user using the user id
  4. $throttle = Sentry::findThrottlerByUserId(1);
  5. // Suspend the user
  6. $throttle->suspend();
  7. }
  8. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  9. {
  10. echo 'User was not found.';
  11. }

Unsuspend user(s) // 取消用户挂起状态

Unsuspends a login. This also clears all previous attempts by the specified login if they were suspended.
取消挂起状态。这也将清除所有之前的尝试记录,如果他曾经被挂起。

  1. try
  2. {
  3. // Find the user using the user id
  4. $throttle = Sentry::findThrottlerByUserId(1);
  5. // Unsuspend the user
  6. $throttle->unsuspend();
  7. }
  8. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  9. {
  10. echo 'User was not found.';
  11. }

Check if a User is Suspended // 检查用户是否已被挂起

Checks to see if the user is suspended.
检查用户是否已被挂起。

  1. try
  2. {
  3. $throttle = Sentry::findThrottlerByUserId(1);
  4. if($suspended = $throttle->isSuspended())
  5. {
  6. // User is Suspended
  7. }
  8. else
  9. {
  10. // User is not Suspended
  11. }
  12. }
  13. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  14. {
  15. echo 'User was not found.';
  16. }

Set the User Suspension Time // 设置用户挂起时长

Sets the length of the suspension.
设置用户挂起时长。

  1. try
  2. {
  3. $throttle = Sentry::findThrottlerByUserId(1);
  4. $throttle->setSuspensionTime(10);
  5. }
  6. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  7. {
  8. echo 'User was not found.';
  9. }

Get the User Suspension Time // 获取用户挂起时长

Retrieves the length of the suspension time set by the throttling driver.
获取用户挂起时长。

  1. try
  2. {
  3. $throttle = Sentry::findThrottlerByUserId(1);
  4. $suspensionTime = $throttle->getSuspensionTime();
  5. }
  6. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  7. {
  8. echo 'User was not found.';
  9. }

Add a Login Attempt // 累加一个登录尝试次数

Adds an attempt to the throttle object.
累加一个登录尝试次数。

  1. try
  2. {
  3. $throttle = Sentry::findThrottlerByUserId(1);
  4. $throttle->addLoginAttempt();
  5. }
  6. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  7. {
  8. echo 'User was not found.';
  9. }

Get Login Attempts // 获取登录尝试次数

Retrieves the number of attempts a user currently has tried. Checks suspension time to see if login attempts can be reset. This may happen if the suspension time was (for example) 10 minutes however the last login was 15 minutes ago, attempts will be reset to 0.
获取用户当前已尝试登录的次数。检查挂起时间内若尝试登录则尝试次数可以被重置。这可能会出现一种情况,如果挂起时间为 (例如) 十分钟,然而最后登录在十五分钟前,则尝试次数将被清零。

  1. try
  2. {
  3. $throttle = Sentry::findThrottlerByUserId(1);
  4. $attempts = $throttle->getLoginAttempts();
  5. }
  6. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  7. {
  8. echo 'User was not found.';
  9. }

Clear Login Attempts // 清除登录尝试次数

Clears all login attempts, it also unsuspends them. This does not unban a login.
清除所有的登录尝试次数,它也将解除他们的挂起状态。但这不会解除禁用状态。

  1. try
  2. {
  3. $throttle = Sentry::findThrottlerByUserId(1);
  4. $throttle->clearLoginAttempts();
  5. }
  6. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  7. {
  8. echo 'User was not found.';
  9. }

Check the User Throttle Status // 检查用户的限制状态

Checks the login throttle status and throws a number of Exceptions upon failure.
检查登录限制状态,并抛出一些对应的异常。

  1. try
  2. {
  3. $throttle = Sentry::findThrottlerByUserId(1);
  4. if ($throttle->check())
  5. {
  6. echo 'Good to go.';
  7. }
  8. }
  9. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  10. {
  11. echo 'User was not found.';
  12. }
  13. catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e)
  14. {
  15. $time = $throttle->getSuspensionTime();
  16. echo "User is suspended for [$time] minutes.";
  17. }
  18. catch (Cartalyst\Sentry\Throttling\UserBannedException $e)
  19. {
  20. ehco 'User is banned.';
  21. }

Set Attempt Limit // 设置尝试次数

Sets the number of attempts allowed before suspension.
设置挂起之前所允许的尝试次数。

  1. try
  2. {
  3. $throttle = Sentry::findThrottlerByUserId(1);
  4. $throttle->setAttemptLimit(3);
  5. }
  6. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  7. {
  8. echo 'User was not found.';
  9. }

Get Attempt Limit // 获取尝试次数

Retrieves the number of attempts allowed by the throttle object.
获取 throttle 对象允许的尝试次数。

  1. try
  2. {
  3. $throttle = Sentry::findThrottlerByUserId(1);
  4. $attemptLimit = $throttle->getAttemptLimit();
  5. }
  6. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  7. {
  8. echo 'User was not found.';
  9. }

Exceptions // 异常

Below is a list of exceptions that the methods can throw.
下面是该方法可能抛出的异常的列表。

Exception Description
Cartalyst\Sentry\Throttle\UserNotFoundException If the provided user was not found, this exception will be thrown.
如果提供的用户不存在,这个异常将被抛出。
Cartalyst\Sentry\Throttling\UserSuspendedException When the provided user is suspended, this exception will be thrown.
当提供的用户被挂起时,这个异常将被抛出。
Cartalyst\Sentry\Users\UserBannedException When the provided user is banned, this exception will be thrown.
当提供的用户被禁用时,这个异常将被抛出。

Find User(s) // 查找用户

Find a User by their Id // 通过 ID 查找用户

Retrieves a throttle object based on the user ID provided. Will always retrieve a throttle object.
基于“用户ID”获取一个 throttle 对象。将始终获取一个 throttle 对象。

  1. try
  2. {
  3. $throttle = Sentry::findThrottlerByUserId(1);
  4. }
  5. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  6. {
  7. echo 'User was not found.';
  8. }

Find a User by their Login // 通过“登录字段”查找用户

Retrieves a throttle object based on the user login provided. Will always retrieve a throttle object.
基于“登录字段”获取一个 throttle 对象。将始终获取一个 throttle 对象。

  1. try
  2. {
  3. $throttle = Sentry::findThrottlerByUserLogin('john.doe@example.com');
  4. }
  5. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  6. {
  7. echo 'User was not found.';
  8. }

Exceptions // 异常

Below is a list of exceptions that the methods can throw.
下面是该方法可能抛出的异常的列表。

Exception Description
Cartalyst\Sentry\Users\UserNotFoundException If the provided user was not found, this exception will be thrown.
如果提供的用户不存在,这个异常将被抛出。