mouf / security.rightsservice
一组用于管理Web应用程序中用户权限的类。如果您想限制某些用户对Web应用程序某些部分(例如,如果您想创建一个只有管理员才能访问的“管理员”部分)的访问,则应使用此包。
Requires
- php: ^8
- mouf/mouf-validators-interface: ~2.0
- mouf/security.userservice: ^3.0
- mouf/utils.constants.secret: ^1.0
Suggests
- twig/twig: This package proposes a Twig 'is_allowed' function to check rights.
README
Mouf框架只是一个IOC框架。因此,它不提供任何管理用户访问权限的方法。希望Mouf团队提供了这个“rightsservice”包,可以帮助您进行用户管理。此包依赖于“userservice”包来提供用户管理,并为其添加访问权限管理。
"rightsservice"包
此包位于“security”目录中。此包提供了您可以使用以实现用户授权的组件。这不是一个“即用即装”的包。为了使用此包,您必须在您的侧开发一些组件。此包将提供实用函数,以确定用户是否有权执行某些操作……此包不提供任何添加或删除用户权限的方法。您将不得不在您的开发中处理这个问题。该包包含以下类和接口
- MoufRightService类:这是主类。它可以用来判断当前登录用户(或特定用户)是否有某些权限,等等...
- RightsServiceInterface接口:大多数依赖于“userservice”的库将依赖于此接口。如果默认的MoufRightService类不能满足您的需求,您可以开发自己的“rightsservice”实例,该实例将实现RightsServiceInterface接口。
- MoufRightService类将需要一个数据访问对象(DAO)来访问您的数据库。DAO不属于此包,因此您必须提供它。您的DAO需要扩展RightsDaoInterface接口。
- 最后,由您的RightsDao类返回的对象将实现RightInterface接口。在MoufRight类中提供了一个示例实现。
在使用“rightsservice”包时,您必须记住这一点:您为“rightsservice”包提供了一个DAO,这将帮助它了解用户的权限,而userservice将帮助您管理权限并通过会话缓存来高效地访问这些权限。
注意:如果用户未登录,我们将没有任何权限。此包不允许未登录人员拥有权限。
在Mouf中,权限可以是显示按钮到访问Web页面等任何东西。权限可以可选地具有作用域。如果它们具有作用域,则用户只能在那个作用域中拥有权限。例如,在一个项目管理应用程序中,可能有多个项目,并且可能用户只能管理一个项目。因此,“AdminProject”权限可能仅限于该特定项目。
MoufRightService组件
此组件有3个必需的属性,必须使用Mouf用户界面进行连接
- userService:这是用于检索当前用户ID的userService组件的指针。请注意,当将rightsService连接到userService时,连接必须双向进行!rightsService通过“userService”属性引用userService,在userService中,必须在“authenticationListeners”属性中引用rightsService。
- rightsDao:这是用于查询数据库以了解与用户关联的权限的Data Access Object。
- log:用于记录消息的记录器。
- errorPageUrl:这是包含错误消息的页面,如果用户没有请求的权限(403错误页面)。
MoufRightService包含以下方法
interface RightsServiceInterface { /** * Returns true if the current user has the right passed in parameter. * A scope can be optionnally passed. * A scope can be anything from a string to an object. If it is an object, * it must be serializable (because it will be stored in the session). * * @param string $right * @param mixed $scope */ public function isAllowed($right, $scope = null); /** * Returns true if the user whose id is $user_id has the $right. * A scope can be optionnally passed. * A scope can be anything from a string to an object. If it is an object, * it must be serializable (because it will be stored in the session). * * @param string $user_id * @param string $right * @param mixed $scope */ public function isUserAllowed($user_id, $right, $scope = null); /** * Rights are cached in session, this function will purge the rights in session. * This can be useful if you know the rights previously fetched for * the current user will change. * */ public function flushRightsCache(); /** * If the user has not the requested right, this function will * redirect the user to an error page (or a login page...) * * @param string $right * @param mixed $scope */ public function redirectNotAuthorized($right, $scope = null); }
您将使用isAllowed方法来了解当前用户是否有权限。如果您想了解不是当前用户的用户的权限,可以使用isUserAllowed变体。flushRightsCache方法将允许您清除当前用户的权限缓存(为了节省数据库调用,用户的权限列表缓存在会话中)。最后,redirectNotAuthorized函数将检查用户是否具有正确的授权,如果用户没有权限查看页面,则将用户重定向到403页面($errorPageUrl)。
为了让RightsService工作,您需要提供一个实现此接口的RightsDao
/** * Daos implementing this interface can be used to query the database for the list of rights * a user has. * The Dao will return objects implementing the RightInterface. * */ interface RightsDaoInterface { /** * Returns a list of all the rights for the user passed in parameter. * * @param string $user_id * @return array<RightInterface> */ public function getRightsForUser($user_id); /** * Returns the RightInterface object associated to the user (or null if the * user has no such right). * * @param string $user_id * @param string $right * @return RightInterface */ public function getRightForUser($user_id, $right); }
RightsDao提供表示权限的返回对象。权限是一个具有名称(权限名称)和应用于权限的数组范围的对象。范围可以是任何东西,从字符串到对象,但它必须是可序列化的(以便存储在会话中)。这些“Right”对象需要实现RightInterface接口
/** * Objects implementing this interface represent a basic right a user has. * For instance: right to display a button, right to access a webpage, right to perform some action... * A right can have a scope associated. * A scope can be anything from a string to an object, but it must be serializable (because * some services can cache it, for instance in the Session). * */ interface RightInterface { /** * Returns the name for that right. * * @return string */ public function getName(); /** * Returns an array of scopes this right has. * If null, the right has a global scope on all the application. * * @return array<mixed> */ public function getScopes(); /** * Returns true if the right applies to the scope passed in parameter, false otherwise. * * @return boolean */ public function hasScope($scope); }
“rightsservice”包提供了默认实现RightInterface的MoufRight类。您可以通过确保您将要编写的RightsDao返回MoufRight类的对象来使用它。