PHPackages                             jsalam/ugrpm - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. jsalam/ugrpm

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

jsalam/ugrpm
============

Manage users and groups roles and permessions

v1.0.0(3y ago)120MITPHPPHP &gt;=8.0

Since Jan 10Pushed 1y ago1 watchersCompare

[ Source](https://github.com/salamj/ugrpm)[ Packagist](https://packagist.org/packages/jsalam/ugrpm)[ Docs](https://aljehni.com/libraries/ugrpm)[ RSS](/packages/jsalam-ugrpm/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

UGRPM
=====

[](#ugrpm)

A simple and easy to use PHP object oriented library to manage (Users,Groups,Roles).

This library use MySql database and PDO Object to connect.

It's work with user as his **id** and don't care what the table you had made or its columns.

You **must** have a database connection and pass it as PDO connection object to the UGRPM constructor.

Built by Salam Aljehni *()*

Library home: *()*

Github link *()*

Installation
------------

[](#installation)

- *Import tables from sql directory to your database*
- Add UGRPM to composer.json then update.

```
composer require jsalam/ugrpm
```

- *Add namespace where you want to use UGRPM, Group or Role classes **only** what you need.*

```
use Jsalam\UGRPM\UGRPM;
use Jsalam\UGRPM\Role;
use Jsalam\UGRPM\Group;
```

OR

```
use Jsalam\UGRPM\{UGRPM,Group,Role};
```

If you would use *try ... catch* you must use exceptions classes.

##### Exceptions using will be discuss below.

[](#exceptions-using-will-be-discuss-below)

Usage
-----

[](#usage)

- First, you ***must*** create a new instance of UGRPM class by passing your PDO connection object.

Suppose you create PDO object like this:

```
$connect = new  PDO('mysql:host=localhost;dbname=DB_NAME','DB_USER','DB_PASSWORD');
```

Then, create UGRPM object:

```
$ugrpm = new  UGRPM($connect);
```

#### Roles

[](#roles)

##### Create Role

[](#create-role)

Role class constructor accept two parameters, `id` and `role` , to create a new Role instance use:

```
use Jsalam\UGRPM\Role;
$role = new  Role(id:3,role:"Apps\Library@create");
```

Default value to `id` is `0` and `""` to role.

If Role not exists in the database and you want to create one **do not pass** `id`.

After that you can work with `$role` object by it's methods:

```
$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");
```

##### Work With Roles

[](#work-with-roles)

Insert role to database, retrieve role(s) by (id,role,class,method,class and method) ,get all roles and remove role.

```
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
```

### Groups

[](#groups)

UGRPM enable you to create groups with (id,name,description) properties, then, you may work with them like add role to groups , add users to group, update groups ...

#### Create Group

[](#create-group)

Group class constructor accept three parameters, `id` and `groupName`, `description` , to create a new Group instance use:

```
use Jsalam\UGRPM\Group;

$group = new  Group(id:11,groupName:"Editors",description:"Editors Group");
```

Default value to `id` is `0` and `""` to `groupName` and `description`. If group not exists in the database and you want to create one **do not pass** `id`. After that you can work with `$group` object by it's methods:

```
$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");
```

##### Work With Groups

[](#work-with-groups)

Insert group to database, retrieve group(s) by (`id`,`groupName`) ,get all groups and remove group.

```
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);
```

### Group Roles

[](#group-roles)

In this section you will learn how to work with (Role-Group) methods. We will make example contains groups and roles and make relations between them.

```
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]);
```

### User Roles

[](#user-roles)

Like (Group-Roles) we can add ,retrieve and remove roles to users or users to roles. Without many examples, these are the methods you can use:

```
$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);
```

### User Group

[](#user-group)

Since users may belongs to groups, we can add user(s) to group(s) and retrieve group's users or user's group , and removing in the way.

These method are available to use.

```
// 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);
```

There are Role several exceptions to use, namespaces for them are:

### Exceptions

[](#exceptions)

- Group Exceptions
- Role Exceptions
- Group Role Exceptions
- User Group Exceptions
- User Role Exceptions

#### Group Exceptions

[](#group-exceptions)

- `Jsalam\UGRPM\Exceptions\GroupExceptions\DuplicatedGroupException`

Catched when trying to create a group with a name that already existing.

```
$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\Exceptions\GroupExceptions\GroupNotFoundException`

Catched when trying to get group that's not found in database using `getGroupById` or `getGroupByGroupName`.

- `Jsalam\UGRPM\Exceptions\GroupExceptions\GroupTypeException`

Catched when passing array of groups that contain item with invalid `Group Object` or Integer value (group id).

Like *createRoleGroups($role,\[`ARRAY_OF_GROUPS`\])*.

#### Role Exceptions

[](#role-exceptions)

- `Jsalam\UGRPM\Exceptions\RoleExceptions\DuplicatedRoleException`

Catched when create a role with `role` property that already exists in database using `createRole()` method.

- `Jsalam\UGRPM\Exceptions\RoleExceptions\InvalidRoleClassNameException`

Catched when create *new Role(role:`classNamespace`@method)* with invalid role class, that class must be a valid namespace like `Apps\Content` or `\Apps\Content\Article` ...

- `Jsalam\UGRPM\Exceptions\RoleExceptions\RoleNameException`

Catched when create a role with `role` invalid property, `role` **must** be `classNamespace` then `@` then `method name`.

- `Jsalam\UGRPM\Exceptions\RoleExceptions\RoleNotFoundException`

Catched when trying to get role that's not found in database using `getRoleById` or `getRolesByClass`...

- `Jsalam\UGRPM\Exceptions\RoleExceptions\RoleTypeException`'

Catched when passing array of roles that contain item with invalid `Role Object` or Integer value (role id).

Like createGroupRoles($group,\[`ARRAY_OF_ROLES`\])

#### Group Role Exceptions

[](#group-role-exceptions)

- `Jsalam\UGRPM\Exceptions\GroupRoleExceptions\GroupAlreadyHasRoleException`

Catched when trying to add role(s) to group(s) that already have that role(s).

#### User Group Exceptions

[](#user-group-exceptions)

- `Jsalam\UGRPM\Exceptions\UserGroupExceptions\UserAlreadyInGroupException`

Catched when trying to add user(s) to group(s) where already in that group role(s).

### User Role Exceptions

[](#user-role-exceptions)

- `Jsalam\UGRPM\Exceptions\UserRoleExceptions\UserAlreadyHasRoleException`

Catched when trying to add role(s) to user(s) where already have that role(s).

Credit
------

[](#credit)

Salam Aljehni, salam\[at\]gmail.com, link: (aljehni.github.io)\[\]

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance28

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1224d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/63398b7c24e9f20e87767dd0bf595ea7d26f57905b1e4eea68d54940c9a9e82a?d=identicon)[salamj](/maintainers/salamj)

---

Tags

phprolesgroupsUserspermessions

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jsalam-ugrpm/health.svg)

```
[![Health](https://phpackages.com/badges/jsalam-ugrpm/health.svg)](https://phpackages.com/packages/jsalam-ugrpm)
```

###  Alternatives

[santigarcor/laratrust

This package provides a flexible way to add Role-based Permissions to Laravel

2.3k5.4M43](/packages/santigarcor-laratrust)[shanmuga/laravel-entrust

This package provides a flexible solution to add ACL to Laravel

68312.9k2](/packages/shanmuga-laravel-entrust)[tomatophp/filament-users

Manage your users with a highly customizable user resource for FilamentPHP with integration of filament-shield and filament-impersonate

90102.0k7](/packages/tomatophp-filament-users)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
