Returns: `Promise<Array>`: An array of policy objects with `sub`, `obj`, and `act` properties.
`enforce(sub, obj, act)`
Checks if a given request should be allowed based on the policies.
`removePolicy(sub, obj, act)`
Removes a policy rule.
`addGroupingPolicy(user, role)`
Adds a grouping policy (assigns a role to a user).
`removeGroupingPolicy(user, role)`
Removes a grouping policy.
`getRolesForUser(user)`
Retrieves roles assigned to a user.
`getUsersForRole(role)`
Retrieves users assigned to a role.
`getPermissionsForUser(user)`
Retrieves permissions assigned to a user.
-
Parameters:
- `user` (String): The user.
-
Returns: `Promise<Array<Array>>`: An array of permissions, each represented as an array `[sub, obj, act]`.
`addPermission(user, obj, act)`
Alias for `addPolicy(sub, obj, act)`.
-
Parameters:
- `user` (String): The user.
- `obj` (String): The object.
- `act` (String): The action.
-
Returns: `Promise`
`removePermission(user, obj, act)`
Alias for `removePolicy(sub, obj, act)`.
-
Parameters:
- `user` (String): The user.
- `obj` (String): The object.
- `act` (String): The action.
-
Returns: `Promise`
`hasPermission(user, obj, act)`
Checks if a specific permission exists.
`getAllSubjects()`
Retrieves all subjects (users) in the policies.
- Returns: `Promise<Array>`: An array of subjects.
`getAllObjects()`
Retrieves all objects in the policies.
- Returns: `Promise<Array>`: An array of objects.
`getAllActions()`
Retrieves all actions in the policies.
- Returns: `Promise<Array>`: An array of actions.
`getAllRoles()`
Retrieves all roles in the policies.
- Returns: `Promise<Array>`: An array of roles.
`savePolicy()`
Saves the current policy to the adapter.
`loadPolicy()`
Loads the policy from the adapter.
`hasPolicy(sub, obj, act)`
Checks if a specific policy exists.
`hasRoleForUser(user, role)`
Checks if a user has a specific role.
`addRoleForUser(user, role)`
Assigns a role to a user.
`deleteRoleForUser(user, role)`
Removes a specific role from a user.
`deleteRolesForUser(user)`
Removes all roles from a user.
`deleteUser(user)`
Deletes a user from the policies.
`deleteRole(role)`
Deletes a role from the policies.
-
Parameters:
- `role` (String): The role.
-
Returns: `Promise`
`deletePermission(...permissions)`
Deletes one or more permissions.
`addPermissionForUser(user, ...permissions)`
Adds one or more permissions for a user.
`deletePermissionForUser(user, ...permissions)`
Deletes one or more permissions from a user.
`deletePermissionsForUser(user)`
Deletes all permissions for a user.
`hasPermissionForUser(user, ...permissions)`
Checks if a user has specific permissions.
Configuration
Environment Variables
To configure the gRPC client, you can set the following environment variables:
- `ACCESS_CONTROL_GRPC_HOST`: The hostname of the Casbin gRPC server. Defaults to `'localhost'` if not set.
- `ACCESS_CONTROL_GRPC_PORT`: The port number of the Casbin gRPC server. Defaults to `'50051'` if not set.
Casbin Model
Provide the Casbin model as a string when initializing the enforcer. This allows flexibility to define different authorization models as needed.
Contributing
Contributions are welcome! Please follow these steps to contribute:
-
Fork the Repository
Click the "Fork" button at the top right of the repository page to create your own fork.
-
Clone Your Fork
```bash
git clone https://github.com/yourusername/casbin-grpc-client.git
cd casbin-grpc-client
```
-
Create a Feature Branch
```bash
git checkout -b feature/YourFeatureName
```
-
Make Changes
Implement your feature or bug fix.
-
Commit Your Changes
```bash
git commit -m "Add your message here"
```
-
Push to Your Fork
```bash
git push origin feature/YourFeatureName
```
-
Open a Pull Request
Navigate to the original repository and open a pull request from your feature branch.
License
This project is licensed under the MIT License.
Additional Information
Including `casbin.proto`
Ensure that the `casbin.proto` file is included in your npm package. This is necessary for the gRPC client to function correctly. The provided `client.js` expects the `casbin.proto` file to be in the same directory.
Serverless Function Considerations
When using `casbin-grpc-client` in serverless environments (e.g., AWS Lambda, Azure Functions):
-
Cold Starts: Initialize the `CasbinClient` outside the function handler to reuse connections across invocations and minimize cold start latency.
```javascript
// handler.js
const CasbinClient = require('casbin-grpc-client');
const casbinClient = new CasbinClient({
grpcHost: process.env.ACCESS_CONTROL_GRPC_HOST || 'localhost',
grpcPort: process.env.ACCESS_CONTROL_GRPC_PORT || '50051',
});
// Initialize outside the handler for connection reuse
casbinClient.initializeAdapterAndEnforcer('your-connection-string', modelText)
.then(() => console.log('Casbin initialized'))
.catch(console.error);
exports.handler = async (event, context) => {
// Your function logic using casbinClient
};
```
-
Connection Limits: Be mindful of the number of concurrent connections your gRPC server can handle. Serverless functions can scale rapidly, potentially leading to connection saturation.
Error Handling
All methods return Promises and should be handled using `async/await` or `.then/.catch` to manage errors gracefully.
```javascript
try {
const allowed = await casbinClient.enforce('alice', 'data1', 'read');
if (allowed) {
// Proceed with the action
} else {
// Deny the action
}
} catch (error) {
console.error('Error enforcing policy:', error);
}
```
Testing
Before deploying your package, thoroughly test it in a local environment to ensure all methods function as expected.
-
Install Locally
In a test project, install the package from the local path:
```bash
npm install /path/to/casbin-grpc-client
```
-
Use `npm link`
```bash
cd /path/to/casbin-grpc-client
npm link
cd /path/to/your/test/project
npm link casbin-grpc-client
```
Support
If you encounter any issues or have questions, feel free to open an issue on the repository.