jsalam / ugrpm
管理用户和组的角色和权限
Requires
- php: >=8.0
Requires (Dev)
- phpunit/phpunit: ^9
README
这是一个简单易用的PHP面向对象库,用于管理(用户、组、角色)。
此库使用MySQL数据库和PDO对象进行连接。
它以用户ID为其工作,而不关心您创建的表或其列。
您必须有一个数据库连接,并将它作为PDO连接对象传递给UGRPM构造函数。
由Salam Aljehni构建 (https://aljehni.github.io)
库主页: (https://aljehni.github.io/ugrpm/)
Github链接 (https://github.com/salamj/ugrpm)
安装
- 从sql目录导入表到您的数据库
- 将UGRPM添加到composer.json,然后更新。
composer require jsalam/ugrpm
- 添加您想要使用UGRPM、Group或Role类的命名空间,仅添加您需要的。
use Jsalam\UGRPM\UGRPM; use Jsalam\UGRPM\Role; use Jsalam\UGRPM\Group;
或者
use Jsalam\UGRPM\{UGRPM,Group,Role};
如果您将使用异常类。
下面的内容将讨论异常的使用。
用法
- 首先,您必须通过传递您的PDO连接对象来创建UGRPM类的一个新实例。
假设您创建PDO对象如下
$connect = new PDO('mysql:host=localhost;dbname=DB_NAME','DB_USER','DB_PASSWORD');
然后,创建UGRPM对象
$ugrpm = new UGRPM($connect);
角色
创建角色
Role类的构造函数接受两个参数,id和role,使用以下方式创建新的Role实例
use Jsalam\UGRPM\Role; $role = new Role(id:3,role:"Apps\Library@create");
id的默认值为0,role为""。
如果Role在数据库中不存在且您想要创建一个,则不传递id。
之后,您可以通过其方法与$role对象一起工作。
$id = $role->getId(); // 3 $roleRole = $role->getRole(); //"App\Content@create" $class = $role->getRoleClass(); // "App\Content" $method = $role->getRoleMethod();// "create" // Change role properties $role->setId(44); $role->setRoleClass("Apps\Content"); $role->setRoleMethod("add"); // or both in one $role->setRole("Apps\Content@add");
与角色一起工作
将角色插入数据库,通过(id,role,class,method,class和method)检索角色(s),获取所有角色并删除角色。
use Jsalam\URRPM\UGRPM; use Jsalam\UGRPM\Role; $role = $ugrpm->createRole(new Role(role:"App\Content@create")); $getRole = $ugrpm->getRoleById(#ID); // int ID $getRoles1 =$ugrpm->getRolesByClass("Class\Namespace"); // array of Roles $getRoles2 = $ugrpm->getRolesByMethod("create");// array of roles $getRoles3 = $ugrpm->getRoleByClassMethod("App\Content@create");// One role or empty $allRoles = $ugrpm->getAllRoles(); // array of roles // Remove Role $ugrpm->removeRole($role);//true
组
UGRPM使您能够创建具有(id,name,description)属性的组,然后,您可以像添加角色到组,添加用户到组,更新组等一样使用它们。
创建组
Group类的构造函数接受三个参数,id和groupName,description,使用以下方式创建新的Group实例
use Jsalam\UGRPM\Group; $group = new Group(id:11,groupName:"Editors",description:"Editors Group");
id的默认值为0,groupName和description为""。如果组在数据库中不存在且您想要创建一个,则不传递id。之后,您可以通过其方法与$group对象一起工作。
$id = $group->getId(); // 11 $name = $grouo->getGroupName(); //"Editors" $desc = $group->getDescription(); // "Editors Group" // Change group properties $group>setId(4); $group->setGroupName("Articles Editors"); $group->setDescription("Articles Editors Group");
与组一起工作
将组插入数据库,通过(id,groupName)检索组(s),获取所有组和删除组。
use Jsalam\UGRPM\Group; use Jsalam\UGRPM\UGRPM; $group = $ugrpm->createGroup(new Group(groupName:"Editors",description:"Editors Group")); $group->setDescription("Group of Editors"); $ugrpm->updateGroup($group); $group1 = $ugrpm->getGroupById(33); $group2 = $ugrpm->getGroupByGroupName("Gallary Managers"); $allGroups = $ugrpm->getAllGroups();// array of groups. $ugrpm->removeGroup($group);
组角色
在本节中,您将学习如何使用(角色-组)方法。我们将制作包含组和角色以及它们之间关系的示例。
use Jsalam\UGRPM\Group; use Jsalam\UGRPM\Role; use Jsalam\UGRPM\UGRPM; // ... $ugrpm initialized before, see installation above //Roles $roleCreateArticle = $ugrpm->createRole(new Role(role:"App\Article\create")); $roleEditArticle = $ugrpm->createRole(new Role(role:"App\Article\edit")); $roleCreateContent = $ugrpm->createRole(new Role(role:"App\Content\create")); $roleEditContent = $ugrpm->createRole(new Role(role:"App\Content\edit")); // Groups $createGroup = $ugrpm->createGroup(new Group(groupName:"Creators",description:"Creators Group")); $editGroup = $ugrpm->createGroup(new Group(groupName:"Editors",description:"Editors Group")); $manageGroup = $ugrpm->createGroup(new Group(groupName:"Managers",description:"Managers Group")); $ugrpm->createGroupRole($createGroup,$roleCreateArticle); $ugrpm->createGroupRole($createGroup,$roleCreateContent); // We can do in one: $ugrpm->createGroupRoles($editGroup,[$roleEditArticle,$roleEditContent]); // getting group's roles $ugrpm->createGroupRoles($manageGroup,array_merge($ugrpm->getGroupRoles($createGroup),$ugrpm->getGroupRoles($editGroup))); // get Groups have edit artices role. $groupsEditing = $ugrpm->getRoleGroups($roleEditArticle); // [$createGroup , $manageGroup] // remove group's editing roles $ugrpm->removeGroupRoles($manageGroup,[$roleEditArticle,$roleEditContent]);
用户角色
与(组-角色)类似,我们可以向用户或用户到角色添加、检索和删除角色。不提供太多示例,以下是可以使用的方法
$user1Id = 10; $user2Id = 32 $role1 = // ... $role2 = // ... $roles = [$role10,$role20,$role30//,...]; // Add $role1 to the user1Id $ugrpm->createUserRole($user1Id,$role1); //Add $roles to the $user2Id $ugrpm->createUserRoles($user2Id,$roles); // Add The $role2 to array of users IDs $ugrpm->createRoleUsers($role2,[19,$user2Id,9]); // Remove $role2 from $user1Id $ugrpm->removeUserRole($user1Id,$role2); // Remove all $roles from the user whos id is 19 $ugrpm->removeUserRoles(19,$roles); // Remove the users in the array from the $role2 $ugrpm->removeRoleUsers($role2,[22,$use1Id,199]); // Check if $user1Id have the role $role2 $ugrpm->userHaveRole($user1Id,$role2); // Get roles belongs to the user $user1Id, His groups'role not included $ugrpm->getUserRoles($user1Id); // Get All roles belongs to the user $user1Id and his groups'role $ugrpm->getAllUserRoles($user2Id); // Retrieve users who have the role $role1 $ugrpm->getRoleUsers($role1);
用户组
由于用户可能属于组,我们可以向组添加用户(s)和检索组的用户或用户组,并按这种方式删除。
以下方法可用于使用。
// Add the user with $userId to $group $ugrpm->addUserToGroup($userId,$group); // AddMany users to $group $ugrpm->addUsersToGroup([$userId1,$userId2,...],$group); // Add user to many groups $ugrpm->addUserToGroups($userId,[$group1,$group2,...]); // Add many users to many groups $ugrpm->addUsersToGroups([$uid1,$uid2,$uid3],[$group1,$group2,$group3]); // Remove user from Group $ugrpm->removeUserFromGroup($uid,$group); // Remove user from many groups $ugrpm->removeUserFromGroups($uid,[$group1,$group2,...]); // Remove many users from group $ugrpm->removeUsersFromGroup([$uid1,$uid2,...],$group); // Retrieve user's groups $ugrpm->getUserGroups($userId); // Retrieve group's users $ugrpm->getGroupUsers($group); // Check if user in group $ugrpm->userInGroup($uid,$group);
有几个角色异常可以使用,它们的命名空间如下
异常
- 组异常
- 角色异常
- 组角色异常
- 用户组异常
- 用户角色异常
组异常
Jsalam\UGRPM\异常\分组异常\重复分组异常
当尝试创建一个已存在的名称的分组时捕获。
$group = new Group(id:10;groupName:"Editors",description:"Users with editing ability"); $ugrpm->createGroup($group); // will throw DuplicatedGroupException if group with "Editors" existed before. try{ $ugrpm->createGroup($group); }catch(DuplicatedGroupException $e){ echo $e->getMessage(); // or what you want }
Jsalam\UGRPM\异常\分组异常\分组未找到异常
当使用 getGroupById
或 getGroupByGroupName
在数据库中找不到分组时捕获。
Jsalam\UGRPM\异常\分组异常\分组类型异常
当传递包含无效的 Group 对象
或整数值(分组 ID)的组数组时捕获。
例如 createRoleGroups($role,[GROUPS 数组
])。
角色异常
Jsalam\UGRPM\异常\角色异常\重复角色异常
当使用 createRole()
方法创建一个数据库中已存在的 role
属性的角色时捕获。
Jsalam\UGRPM\异常\角色异常\无效角色类名异常
当创建 new Role(role:类命名空间
@方法) 时捕获,无效的角色类必须是一个有效的命名空间,如 Apps\Content
或 \Apps\Content\Article
...
Jsalam\UGRPM\异常\角色异常\角色名称异常
当创建一个具有无效 role
属性的角色时捕获,role
必须 是 类命名空间
然后是 @
然后是 方法名称
。
Jsalam\UGRPM\异常\角色异常\角色未找到异常
当尝试使用 getRoleById
或 getRolesByClass
... 在数据库中找不到角色时捕获。
Jsalam\UGRPM\异常\角色异常\角色类型异常
'
当传递包含无效的 Role 对象
或整数值(角色 ID)的组数组时捕获。
例如创建组角色 createGroupRoles($group,[
ROLES 数组
])
组角色异常
Jsalam\UGRPM\异常\分组角色异常\分组已具有角色异常
当尝试向已具有该角色的分组添加角色(们)时捕获。
用户组异常
Jsalam\UGRPM\异常\用户分组异常\用户已存在于分组中异常
当尝试向已在该组中担任角色的分组添加用户(们)时捕获。
用户角色异常
Jsalam\UGRPM\异常\用户角色异常\用户已具有角色异常
当尝试向已具有该角色的用户添加角色(们)时捕获。
版权信息
萨勒姆·阿尔杰尼,salam[at]gmail.com,链接:[aljehni.github.io](https://aljehni.github.io)