Groups

Create a group

Creating new Groups is very easy and in this section you will learn how to create one.

Param Required Default Type Description
$attributes true null array The group attributes.

Below we’ll list all the valid keys that you can pass through the $attributes.

Key Required Type Description
name true string The name of the group.
permissions false array The group permissions, pass a key/value pair.

Example

  1. try
  2. {
  3. // Create the group
  4. $group = Sentry::createGroup(array(
  5. 'name' => 'Moderator',
  6. 'permissions' => array(
  7. 'admin' => 1,
  8. 'users' => 1,
  9. ),
  10. ));
  11. }
  12. catch (Cartalyst\Sentry\Groups\NameRequiredException $e)
  13. {
  14. echo 'Name field is required';
  15. }
  16. catch (Cartalyst\Sentry\Groups\GroupExistsException $e)
  17. {
  18. echo 'Group already exists';
  19. }

Exceptions

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

Exception Description
Cartalyst\Sentry\Groups\NameRequiredException If you don’t provide the group name, this exception will be thrown.
Cartalyst\Sentry\Groups\GroupExistsException This exception will be thrown when the group you are trying to create already exists on your database.

Update a group

Example

  1. try
  2. {
  3. // Find the group using the group id
  4. $group = Sentry::findGroupById(1);
  5. // Update the group details
  6. $group->name = 'Users';
  7. $group->permissions = array(
  8. 'admin' => 1,
  9. 'users' => 1,
  10. );
  11. // Update the group
  12. if ($group->save())
  13. {
  14. // Group information was updated
  15. }
  16. else
  17. {
  18. // Group information was not updated
  19. }
  20. }
  21. catch (Cartalyst\Sentry\Groups\NameRequiredException $e)
  22. {
  23. echo 'Name field is required';
  24. }
  25. catch (Cartalyst\Sentry\Groups\GroupExistsException $e)
  26. {
  27. echo 'Group already exists.';
  28. }
  29. catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e)
  30. {
  31. echo 'Group was not found.';
  32. }

Exceptions

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

Exception Description
Cartalyst\Sentry\Groups\NameRequiredException If you don’t provide the group name, this exception will be thrown.
Cartalyst\Sentry\Groups\GroupExistsException This exception will be thrown when the group you are trying to create already exists on your database.
Cartalyst\Sentry\Groups\GroupNotFoundException If the provided group was not found, this exception will be thrown.

Delete a Group

Example

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

Exceptions

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

Exception Description
Cartalyst\Sentry\Groups\GroupNotFoundException If the provided group was not found, this exception will be thrown.

Finding Groups

Sentry provides simple methods to find you your groups.

Find all the Groups

This will return all the groups.

  1. $groups = Sentry::findAllGroups();

Find a group by its ID

Find a group by it’s ID.

  1. try
  2. {
  3. $group = Sentry::findGroupById(1);
  4. }
  5. catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e)
  6. {
  7. echo 'Group was not found.';
  8. }

Find a Group by it’s Name

Find a group by it’s name.

  1. try
  2. {
  3. $group = Sentry::findGroupByName('admin');
  4. }
  5. catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e)
  6. {
  7. echo 'Group was not found.';
  8. }

Exceptions

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

Exception Description
Cartalyst\Sentry\Groups\GroupNotFoundException If the provided group was not found, this exception will be thrown.

Helpers

getPermissions()

Returns the permissions of a group.

  1. try
  2. {
  3. // Find the group using the group id
  4. $group = Sentry::findGroupById(1);
  5. // Get the group permissions
  6. $groupPermissions = $group->getPermissions();
  7. }
  8. catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e)
  9. {
  10. echo 'Group does not exist.';
  11. }