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.
要创建一个新的用户,你需要传递一个包含用户字段的 array()create() 方法中,请注意,其中的 login 字段和密码字段是必须的,而所有其它的字段都是可选的。

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.
当你没有提供必要的 login 字段时,这个异常将被抛出。
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.
注册的用户将需要用户手动激活,但你可以通过传递一个布尔值为 true 的第二个参数来跳过激活步骤。

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.
当你没有提供必要的 login 字段时,这个异常将被抛出。
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.
使用 Sentry 更新用户信息是非常容易的,你只需要找到你想要更新的用户并更新他的信息。你同样可以增加或移除用户的分组。

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.
如果你提供了另一个 email 地址,而这个 email 地址已经在系统中注册,则 Cartalyst\Sentry\Users\UserExistsException 异常将被抛出。

  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.
如果指定的分组不存在,则 Cartalyst\Sentry\Groups\GroupNotFoundException 异常将被抛出。

  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.
如果指定的分组不存在,则 Cartalyst\Sentry\Groups\GroupNotFoundException 异常将被抛出。

  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.
如果指定的分组不存在,则 Cartalyst\Sentry\Groups\GroupNotFoundException 异常将被抛出。

  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.
当你没有提供必要的 login 字段时,这个异常将被抛出。
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 .
使用 Sentry 机会用户是非常容易的,你首先需要找到你想要激活的用户,然后使用 attemptActivation() 方法并提供激活码,如果激活成功它将返回 true 反之则返回 false

Note: If the user you are trying to activate, is already activated, the following Exception Cartalyst\Sentry\Users\UserAlreadyActivatedException will be thrown.
如果你尝试激活的用户,已经被激活,则 Cartalyst\Sentry\Users\UserAlreadyActivatedException 异常将被抛出。

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.
在本节中你将学习如何使用 Sentry 2 简单的重置一个用户的密码。

Step 1 // 步骤 1

The first step is to get a password reset code, to do this we use the getResetPasswordCode() method.
第一步是获得一个密码重置代码,为此我们使用 getResetPasswordCode() 方法。

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 // 步骤 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. // 检查重置密码的代码是否有效
  7. if ($user->checkResetPasswordCode('8f1Z7wA4uVt7VemBpGSfaoI9mcjdEwtK8elCnQOb'))
  8. {
  9. // Attempt to reset the user password
  10. // 尝试重置用户密码
  11. if ($user->attemptResetPassword('8f1Z7wA4uVt7VemBpGSfaoI9mcjdEwtK8elCnQOb', 'new_password'))
  12. {
  13. // Password reset passed
  14. // 密码重置通过
  15. }
  16. else
  17. {
  18. // Password reset failed
  19. // 密码重置失败
  20. }
  21. }
  22. else
  23. {
  24. // The provided password reset code is Invalid
  25. // 所提供的密码重置代码是无效的
  26. }
  27. }
  28. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  29. {
  30. echo 'User was not found.';
  31. }

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.
查找用户有时是十分困难的,好了,Sentry 为你提供了简单的方法来找到你的用户。

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.
直接返回设置入 Sentry 中的用户,不检查特定的用户是否登录,如果需要,请使用 check() 代替。

  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. // 用户未找到,应该仅发生在,当用户已登录后,或已经获得 "remember me" cookie 的设置后,其账号被删除时。
  12. }

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. // 当仅指定一个权限时,可以直接传递一个字符串作为参数
  3. $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 // 通过 ID 查找一个用户

Find a user by their ID.
通过 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.
检查用户是否被授予了指定的权限。这包括任何从他们的分组赋予的权限,它们可以是分开定义的。用户也可以拥有值为 ‘-1’ 的私有权限。这个值用于拒绝用户拥有特定的权限,即使他们所在的分组给予了其它值。

Any user with superuser permissions automatically has access to everything, regardless of the user permissions and group permissions.
任何具有 superuser 权限的用户都将自动获得所有权限,无论用户定义了怎样的私有权限或分组。

  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. // 要检查多个权限需要使用一个数组来进行参数传递
  8. if ($user->hasAccess('admin'))
  9. {
  10. // User has access to the given permission
  11. }
  12. else
  13. {
  14. // User does not have access to the given permission
  15. }
  16. }
  17. catch (Cartalyst\Sentry\UserNotFoundException $e)
  18. {
  19. echo 'User was not found.';
  20. }

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.
这个方法调用了 hasAccess() 方法,它是用来检测一个用户是否被授予了指定权限中的一种。

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.
如果指定权限之一通过检查则立即返回 true,即使用户可能并不具备其他权限。

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