Throttle

Disable the Throttling Feature

Disables the throttling feature.

Can be done on the throttle provider (global) level or on a throttle instance itself.

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.

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.

  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.

  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).

  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.

  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

Retrieves a throttle object based on the user ID provided. Will always retrieve a throttle object.

  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.

  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.