juspay / juspay/hyperswitch

[TASK] Enable org-level custom roles in backend

Open
#11,645 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
43.7k
Forks
5.1k
Avg merge
2d 20h
Merged PRs (30d)
205

Description

## Summary
Enable custom roles for organizations by removing the blocking code that currently prevents creation of organization-level custom roles.

## Goal
Organization admins should be able to create custom roles with specific permissions at the organization level. Currently, custom roles only work for merchants and profiles. This feature extends that capability to organizations while maintaining proper ACL enforcement.

## Technical Approach

### Files to Modify

**File: `crates/router/src/core/user_role/role.rs`**

Remove the blocking checks in two functions:

1. **In `create_role()` function (lines 96-99)**:
```rust
// DELETE THIS BLOCK:
if matches!(role_entity_type, EntityType::Organization) {
return Err(report!(UserErrors::InvalidRoleOperation))
.attach_printable("User trying to create org level custom role");
}
```

2. **In `create_role_v2()` function (lines 195-198)**:
```rust
// DELETE THIS BLOCK:
if matches!(role_entity_type, EntityType::Organization) {
return Err(report!(UserErrors::InvalidRoleOperation))
.attach_printable("User trying to create org level custom role");
}
```

### Existing Security Mechanisms (No Changes Needed)

The following security checks already exist and will continue to enforce proper permissions:

1. **Entity Hierarchy Validation** (lines 200-214): Ensures user's entity type >= max(role_scope, entity_type)
2. **Scope Validation** (lines 200-206): Ensures role scope >= entity type
3. **Permission Validation**: User can only assign permissions they have access to
4. **Duplicate Name Check**: Prevents duplicate role names within entity scope

### API Endpoints

No new API endpoints needed. Existing endpoints already support org-level roles:
- `POST /user/role/v2` - Create custom role
- `GET /user/role/list?entity_type=organization` - List org-level roles
- `GET /user/parent/list?entity_type=organization` - Get available permissions

### Testing

Add tests for:
- Org admin creating org-level custom role with various permission combinations
- Merchant admin attempting to create org-level role (should fail with hierarchy error)
- Profile user attempting to create org-level role (should fail)
- Verify scope >= entity_type validation still works correctly

## Subtasks

- [ ] **Remove blocking code from create_role()**
- **What:** Delete lines 96-99 that block Organization-level role creation
- **Files:** `crates/router/src/core/user_role/role.rs`
- **Done when:** create_role() accepts EntityType::Organization without error
- **PR scope:** Single file change, removes guard clause

- [ ] **Remove blocking code from create_role_v2()**
- **What:** Delete lines 195-198 that block Organization-level role creation
- **Files:** `crates/router/src/core/user_role/role.rs`
- **Done when:** create_role_v2() accepts EntityType::Organization without error
- **PR scope:** Single file change, removes guard clause

- [ ] **Add tests for org-level role creation**
- **What:** Add integration tests for org-level custom role CRUD operations
- **Files:** Test files in `crates/router/tests/` or relevant test module
- **Done when:** Tests verify org admins can create org roles and non-org users cannot
- **PR scope:** New test cases covering security scenarios

## Acceptance Criteria

- [ ] Org admins can successfully create custom roles with `entity_type: "organization"`
- [ ] Merchant admins receive `InvalidRoleOperation` error when attempting to create org-level roles
- [ ] Profile users cannot create custom roles at all (button disabled in frontend)
- [ ] Existing merchant and profile custom role functionality unchanged
- [ ] All existing tests pass
- [ ] New tests cover org-level role creation scenarios

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.