Users

In this section we’ll cover how you can create or register users, assign groups and permissions to users.

Sentry::createUser()

To create a new user you need to pass an array() of user fields into the create() method, please note, that the login field and the password are required, all the other fields are optional.

Param Required Default Type Description
$credentials true null array The user credentials and attributes.

Examples

Create a User and assign this new user an existing group.

  1. try
  2. {
  3. // Create the user
  4. $user = Sentry::createUser(array(
  5. 'email' => 'john.doe@example.com',
  6. 'password' => 'test',
  7. 'activated' => true,
  8. ));
  9. // Find the group using the group id
  10. $adminGroup = Sentry::findGroupById(1);
  11. // Assign the group to the user
  12. $user->addGroup($adminGroup);
  13. }
  14. catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
  15. {
  16. echo 'Login field is required.';
  17. }
  18. catch (Cartalyst\Sentry\Users\PasswordRequiredException $e)
  19. {
  20. echo 'Password field is required.';
  21. }
  22. catch (Cartalyst\Sentry\Users\UserExistsException $e)
  23. {
  24. echo 'User with this login already exists.';
  25. }
  26. catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e)
  27. {
  28. echo 'Group was not found.';
  29. }

Create a new user and set permissions on this user.

This example does pretty much the same as the previous one with the exception that we are not assigning him any group, but we are granting this user some permissions.

  1. try
  2. {
  3. // Create the user
  4. $user = Sentry::createUser(array(
  5. 'email' => 'john.doe@example.com',
  6. 'password' => 'test',
  7. 'activated' => true,
  8. 'permissions' => array(
  9. 'user.create' => -1,
  10. 'user.delete' => -1,
  11. 'user.view' => 1,
  12. 'user.update' => 1,
  13. ),
  14. ));
  15. }
  16. catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
  17. {
  18. echo 'Login field is required.';
  19. }
  20. catch (Cartalyst\Sentry\Users\PasswordRequiredException $e)
  21. {
  22. echo 'Password field is required.'
  23. }
  24. catch (Cartalyst\Sentry\Users\UserExistsException $e)
  25. {
  26. echo 'User with this login already exists.';
  27. }

Exceptions

Below is a list of exceptions that this method can throw.

Exception Description
Cartalyst\Sentry\Users\LoginRequiredException When you don’t provide the required login field, this exception will be thrown.
Cartalyst\Sentry\Users\PasswordRequiredException When you don’t provide the password field, this exception will be thrown.
Cartalyst\Sentry\Users\UserExistsException This exception will be thrown when the user you are trying to create already exists on your database.
Cartalyst\Sentry\Groups\GroupNotFoundException This exception will be thrown when the group that’s being assigned to the user doesn’t exist.

Sentry::register()

Registering a user will require the user to be manually activated but you can bypass this passing a boolean of true as a second parameter.

If the user already exists but is not activated, it will create a new activation code.

Param Required Default Type Description
$credentials true null array The user credentials and attributes.
$activate false false boolean Flag to wether activate the user or not.

Example

  1. try
  2. {
  3. // Let's register a user.
  4. $user = Sentry::register(array(
  5. 'email' => 'john.doe@example.com',
  6. 'password' => 'test',
  7. ));
  8. // Let's get the activation code
  9. $activationCode = $user->getActivationCode();
  10. // Send activation code to the user so he can activate the account
  11. }
  12. catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
  13. {
  14. echo 'Login field is required.';
  15. }
  16. catch (Cartalyst\Sentry\Users\PasswordRequiredException $e)
  17. {
  18. echo 'Password field is required.';
  19. }
  20. catch (Cartalyst\Sentry\Users\UserExistsException $e)
  21. {
  22. echo 'User with this login already exists.';
  23. }

Exceptions

Below is a list of exceptions that this method can throw.

Exception Description
Cartalyst\Sentry\Users\LoginRequiredException When you don’t provide the required login field, this exception will be thrown.
Cartalyst\Sentry\Users\PasswordRequiredException When you don’t provide the required password field, this exception will be thrown.
Cartalyst\Sentry\Users\UserExistsException This exception will be thrown when the user you are trying to create already exists on your database.

Update a User

Updating users information is very easy with Sentry, you just need to find the user you want to update and update their information. You can add or remove groups from users as well.

Examples

In this example we are just updating the user information.

Note: If you provide another email address, and that email address is already registered in your system, the following Exception Cartalyst\Sentry\Users\UserExistsException will be thrown.

  1. try
  2. {
  3. // Find the user using the user id
  4. $user = Sentry::findUserById(1);
  5. // Update the user details
  6. $user->email = 'john.doe@example.com';
  7. $user->first_name = 'John';
  8. // Update the user
  9. if ($user->save())
  10. {
  11. // User information was updated
  12. }
  13. else
  14. {
  15. // User information was not updated
  16. }
  17. }
  18. catch (Cartalyst\Sentry\Users\UserExistsException $e)
  19. {
  20. echo 'User with this login already exists.';
  21. }
  22. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  23. {
  24. echo 'User was not found.';
  25. }

Assign a new Group to a User

In this example we are assigning the provided Group to the provided User.

Note: If the provided Group is not found an Exception Cartalyst\Sentry\Groups\GroupNotFoundException will be thrown.

  1. try
  2. {
  3. // Find the user using the user id
  4. $user = Sentry::findUserById(1);
  5. // Find the group using the group id
  6. $adminGroup = Sentry::findGroupById(1);
  7. // Assign the group to the user
  8. if ($user->addGroup($adminGroup))
  9. {
  10. // Group assigned successfully
  11. }
  12. else
  13. {
  14. // Group was not assigned
  15. }
  16. }
  17. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  18. {
  19. echo 'User was not found.';
  20. }
  21. catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e)
  22. {
  23. echo 'Group was not found.';
  24. }

Remove a Group from the User

In this example we are removing the provided Group from the provided User.

Note: If the provided Group is not found an Exception Cartalyst\Sentry\Groups\GroupNotFoundException will be thrown.

  1. try
  2. {
  3. // Find the user using the user id
  4. $user = Sentry::findUserById(1);
  5. // Find the group using the group id
  6. $adminGroup = Sentry::findGroupById(1);
  7. // Assign the group to the user
  8. if ($user->removeGroup($adminGroup))
  9. {
  10. // Group removed successfully
  11. }
  12. else
  13. {
  14. // Group was not removed
  15. }
  16. }
  17. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  18. {
  19. echo 'User was not found.';
  20. }
  21. catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e)
  22. {
  23. echo 'Group was not found.';
  24. }

Update the User details and assign a new Group

This is a combination of the previous examples, where we are updating the user information and assigning a new Group the provided User.

Note: If the provided Group is not found an Exception Cartalyst\Sentry\Groups\GroupNotFoundException will be thrown.

  1. try
  2. {
  3. // Find the user using the user id
  4. $user = Sentry::findUserById(1);
  5. // Find the group using the group id
  6. $adminGroup = Sentry::findGroupById(1);
  7. // Assign the group to the user
  8. if ($user->addGroup($adminGroup))
  9. {
  10. // Group assigned successfully
  11. }
  12. else
  13. {
  14. // Group was not assigned
  15. }
  16. // Update the user details
  17. $user->email = 'john.doe@example.com';
  18. $user->first_name = 'John';
  19. // Update the user
  20. if ($user->save())
  21. {
  22. // User information was updated
  23. }
  24. else
  25. {
  26. // User information was not updated
  27. }
  28. }
  29. catch (Cartalyst\Sentry\Users\UserExistsException $e)
  30. {
  31. echo 'User with this login already exists.';
  32. }
  33. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  34. {
  35. echo 'User was not found.';
  36. }
  37. catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e)
  38. {
  39. echo 'Group was not found.';
  40. }

Exceptions

Below is a list of exceptions that the methods can throw.

Exception Description
Cartalyst\Sentry\Users\LoginRequiredException When you don’t provide the required login field, this exception will be thrown.
Cartalyst\Sentry\Users\UserExistsException This exception will be thrown when the user you are trying to create already exists in your database.
Cartalyst\Sentry\Users\UserNotFoundException If the provided user was not found, this exception will be thrown.
Cartalyst\Sentry\Groups\GroupNotFoundException This exception will be thrown when the group that’s being assigned to the user doesn’t exist.

Delete a user

Deleting users is very simple and easy.

Example

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

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.

Activating a User

User activation is very easy with Sentry, you need to first find the user you want to activate, then use the attemptActivation() method and provide the activation code, if the activation passes it will return true otherwise, it will return false .

Note: If the user you are trying to activate, is already activated, the following Exception Cartalyst\Sentry\Users\UserAlreadyActivatedException will be thrown.

Example

  1. try
  2. {
  3. // Find the user using the user id
  4. $user = Sentry::findUserById(1);
  5. // Attempt to activate the user
  6. if ($user->attemptActivation('8f1Z7wA4uVt7VemBpGSfaoI9mcjdEwtK8elCnQOb'))
  7. {
  8. // User activation passed
  9. }
  10. else
  11. {
  12. // User activation failed
  13. }
  14. }
  15. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  16. {
  17. echo 'User was not found.';
  18. }
  19. catch (Cartalyst\Sentry\Users\UserAlreadyActivatedException $e)
  20. {
  21. echo 'User is already activated.';
  22. }

Exceptions

Below is a list of exceptions that the methods can throw.

Exception Description
Cartalyst\Sentry\Users\UserAlreadyActivatedException If the provided user is already activated, this exception will be thrown.
Cartalyst\Sentry\Users\UserNotFoundException If the provided user was not found, this exception will be thrown.

Reset a User Password

In this section you will learn how easy it is to reset a user password with Sentry 2.

Step 1

The first step is to get a password reset code, to do this we use the
getResetPasswordCode() method.

Example
  1. try
  2. {
  3. // Find the user using the user email address
  4. $user = Sentry::findUserByLogin('john.doe@example.com');
  5. // Get the password reset code
  6. $resetCode = $user->getResetPasswordCode();
  7. // Now you can send this code to your user via email for example.
  8. }
  9. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  10. {
  11. echo 'User was not found.';
  12. }

Step 2

After your user received the password reset code you need to provide a way for them to validate that code, and reset their password.

All the logic part on how you pass the reset password code is all up to you.

Example

  1. try
  2. {
  3. // Find the user using the user id
  4. $user = Sentry::findUserById(1);
  5. // Check if the reset password code is valid
  6. if ($user->checkResetPasswordCode('8f1Z7wA4uVt7VemBpGSfaoI9mcjdEwtK8elCnQOb'))
  7. {
  8. // Attempt to reset the user password
  9. if ($user->attemptResetPassword('8f1Z7wA4uVt7VemBpGSfaoI9mcjdEwtK8elCnQOb', 'new_password'))
  10. {
  11. // Password reset passed
  12. }
  13. else
  14. {
  15. // Password reset failed
  16. }
  17. }
  18. else
  19. {
  20. // The provided password reset code is Invalid
  21. }
  22. }
  23. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  24. {
  25. echo 'User was not found.';
  26. }

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.

Finding Users

Finding users can sometimes be difficult and harsh, well, Sentry provides you simple methods to find your users.

Get the Current Logged in User

Returns the user that’s set with Sentry, does not check if a user is logged in or not. To do that, use check() instead.

  1. try
  2. {
  3. // Get the current active/logged in user
  4. $user = Sentry::getUser();
  5. }
  6. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  7. {
  8. // User wasn't found, should only happen if the user was deleted
  9. // when they were already logged in or had a "remember me" cookie set
  10. // and they were deleted.
  11. }

Find all the Users

This will return all the users.

  1. $users = Sentry::findAllUsers();

Find all the Users with access to a permissions(s)

Finds all users with access to a permission(s).

  1. // Feel free to pass a string for just one permission instead
  2. $users = Sentry::findAllUsersWithAccess(array('admin', 'other'));

Find all the Users in a Group

Finds all users assigned to a group.

  1. $group = Sentry::findGroupByName('admin');
  2. $users = Sentry::findAllUsersInGroup($group);

Find a User by their Credentials

Find a user by an array of credentials, which must include the login column. Hashed fields will be hashed and checked against their value in the database.

  1. try
  2. {
  3. $user = Sentry::findUserByCredentials(array(
  4. 'email' => 'john.doe@example.com',
  5. 'password' => 'test',
  6. 'first_name' => 'John',
  7. ));
  8. }
  9. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  10. {
  11. echo 'User was not found.';
  12. }

Find a User by their Id

Find a user by their ID.

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

Find a User by their Login Id

Find a user by their login ID.

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

Find a User by their Activation Code

Find a user by their registration activation code.

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

Find a User by their Reset Password Code

Find a user by their reset password code.

  1. try
  2. {
  3. $user = Sentry::findUserByResetPasswordCode('8f1Z7wA4uVt7VemBpGSfaoI9mcjdEwtK8elCnQOb');
  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.

Helpers

checkPassword()

Checks if the provided password matches the user’s current password.

  1. try
  2. {
  3. // Find the user using the user id
  4. $user = Sentry::findUserById(1);
  5. if($user->checkPassword('mypassword'))
  6. {
  7. echo 'Password matches.';
  8. }
  9. else
  10. {
  11. echo 'Password does not match.';
  12. }
  13. }
  14. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  15. {
  16. echo 'User was not found.';
  17. }

getGroups()

Returns the user groups.

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

getPermissions()

Returns the user permissions.

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

getMergedPermissions()

Returns an array of merged permissions from groups and the user permissions.

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

hasAccess($permission)

Checks to see if a user been granted a certain permission. This includes any
permissions given to them by groups they may be apart of as well. Users may
also have permissions with a value of ‘-1’. This value is used to deny users of
permissions that may have been assigned to them from a group.

Any user with superuser permissions automatically has access to everything,
regardless of the user permissions and group permissions.

  1. try
  2. {
  3. // Find the user using the user id
  4. $user = Sentry::findUserByID(1);
  5. // Check if the user has the 'admin' permission. Also,
  6. // multiple permissions may be used by passing an array
  7. if ($user->hasAccess('admin'))
  8. {
  9. // User has access to the given permission
  10. }
  11. else
  12. {
  13. // User does not have access to the given permission
  14. }
  15. }
  16. catch (Cartalyst\Sentry\UserNotFoundException $e)
  17. {
  18. echo 'User was not found.';
  19. }

hasAnyAccess($permissions)

This method calls the hasAccess() method, and it is used to check if an user
has access to any of the provided permissions.

If one of the provided permissions is found it will return true even though the
user may not have access to the other provided permissions.

  1. try
  2. {
  3. // Find the user using the user id
  4. $user = Sentry::getUserProvider()->findById(1);
  5. // Check if the user has the 'admin' and 'foo' permission.
  6. if ($user->hasAnyAccess(array('admin', 'foo')))
  7. {
  8. // User has access to one of the given permissions
  9. }
  10. else
  11. {
  12. // User does not have access to any of the given permissions
  13. }
  14. }
  15. catch (Cartalyst\Sentry\UserNotFoundException $e)
  16. {
  17. echo 'User was not found.';
  18. }

isActivated()

Checks if a user is activated.

  1. try
  2. {
  3. // Find the user
  4. $user = Sentry::findUserByLogin('jonh.doe@example.com');
  5. // Check if the user is activated or not
  6. if ($user->isActivated())
  7. {
  8. // User is Activated
  9. }
  10. else
  11. {
  12. // User is Not Activated
  13. }
  14. }
  15. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  16. {
  17. echo 'User was not found.';
  18. }

isSuperUser()

Returns if the user is a super user, it means, that has access to everything regardless of permissions.

  1. try
  2. {
  3. // Find the user
  4. $user = Sentry::findUserByLogin('jonh.doe@example.com');
  5. // Check if this user is a super user
  6. if ($user->isSuperUser())
  7. {
  8. // User is a super user
  9. }
  10. else
  11. {
  12. // User is not a super user
  13. }
  14. }
  15. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  16. {
  17. echo 'User was not found.';
  18. }

inGroup($group)

Checks if a user is in a certain group.

  1. try
  2. {
  3. // Find the user using the user id
  4. $user = Sentry::findUserByID(1);
  5. // Find the Administrator group
  6. $admin = Sentry::findGroupByName('Administrator');
  7. // Check if the user is in the administrator group
  8. if ($user->inGroup($admin))
  9. {
  10. // User is in Administrator group
  11. }
  12. else
  13. {
  14. // User is not in Administrator group
  15. }
  16. }
  17. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  18. {
  19. echo 'User was not found.';
  20. }
  21. catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e)
  22. {
  23. echo 'Group was not found.';
  24. }