Bug: feat: add groups for get_role_from_token api




diff --git a/crates/api_models/src/events/user_role.rs b/crates/api_models/src/events/user_role.rs
index f46c5cf312b..aee2b3fb66c 100644
--- a/crates/api_models/src/events/user_role.rs
+++ b/crates/api_models/src/events/user_role.rs
@@ -2,8 +2,8 @@ use common_utils::events::{ApiEventMetric, ApiEventsType};
 
 use crate::user_role::{
     role::{
-        CreateRoleRequest, GetRoleRequest, ListRolesResponse, RoleInfoResponse,
-        RoleInfoWithPermissionsResponse, UpdateRoleRequest,
+        CreateRoleRequest, GetRoleFromTokenResponse, GetRoleRequest, ListRolesResponse,
+        RoleInfoResponse, RoleInfoWithPermissionsResponse, UpdateRoleRequest,
     },
     AcceptInvitationRequest, AuthorizationInfoResponse, DeleteUserRoleRequest,
     TransferOrgOwnershipRequest, UpdateUserRoleRequest,
@@ -20,5 +20,6 @@ common_utils::impl_misc_api_event_type!(
     CreateRoleRequest,
     UpdateRoleRequest,
     ListRolesResponse,
-    RoleInfoResponse
+    RoleInfoResponse,
+    GetRoleFromTokenResponse
 );
diff --git a/crates/api_models/src/user_role/role.rs b/crates/api_models/src/user_role/role.rs
index fafb8fede0b..6d0bb2154d9 100644
--- a/crates/api_models/src/user_role/role.rs
+++ b/crates/api_models/src/user_role/role.rs
@@ -23,6 +23,13 @@ pub struct GetGroupsQueryParam {
     pub groups: Option<bool>,
 }
 
+#[derive(Debug, serde::Serialize)]
+#[serde(untagged)]
+pub enum GetRoleFromTokenResponse {
+    Permissions(Vec<Permission>),
+    Groups(Vec<PermissionGroup>),
+}
+
 #[derive(Debug, serde::Serialize)]
 #[serde(untagged)]
 pub enum RoleInfoResponse {
diff --git a/crates/router/src/core/user_role/role.rs b/crates/router/src/core/user_role/role.rs
index cc31798bd8b..c3d534f8860 100644
--- a/crates/router/src/core/user_role/role.rs
+++ b/crates/router/src/core/user_role/role.rs
@@ -1,7 +1,4 @@
-use api_models::user_role::{
-    role::{self as role_api},
-    Permission,
-};
+use api_models::user_role::role::{self as role_api};
 use common_enums::RoleScope;
 use common_utils::generate_id_with_default_len;
 use diesel_models::role::{RoleNew, RoleUpdate};
@@ -20,10 +17,10 @@ use crate::{
     utils,
 };
 
-pub async fn get_role_from_token(
+pub async fn get_role_from_token_with_permissions(
     state: AppState,
     user_from_token: UserFromToken,
-) -> UserResponse<Vec<Permission>> {
+) -> UserResponse<role_api::GetRoleFromTokenResponse> {
     let role_info = user_from_token
         .get_role_info_from_db(&state)
         .await
@@ -35,7 +32,25 @@ pub async fn get_role_from_token(
         .map(Into::into)
         .collect();
 
-    Ok(ApplicationResponse::Json(permissions))
+    Ok(ApplicationResponse::Json(
+        role_api::GetRoleFromTokenResponse::Permissions(permissions),
+    ))
+}
+
+pub async fn get_role_from_token_with_groups(
+    state: AppState,
+    user_from_token: UserFromToken,
+) -> UserResponse<role_api::GetRoleFromTokenResponse> {
+    let role_info = user_from_token
+        .get_role_info_from_db(&state)
+        .await
+        .attach_printable("Invalid role_id in JWT")?;
+
+    let permissions = role_info.get_permission_groups().to_vec();
+
+    Ok(ApplicationResponse::Json(
+        role_api::GetRoleFromTokenResponse::Groups(permissions),
+    ))
 }
 
 pub async fn create_role(
diff --git a/crates/router/src/routes/user_role.rs b/crates/router/src/routes/user_role.rs
index 75de9e7e6f7..65e2aa4cdb8 100644
--- a/crates/router/src/routes/user_role.rs
+++ b/crates/router/src/routes/user_role.rs
@@ -41,14 +41,27 @@ pub async fn get_authorization_info(
     .await
 }
 
-pub async fn get_role_from_token(state: web::Data<AppState>, req: HttpRequest) -> HttpResponse {
+pub async fn get_role_from_token(
+    state: web::Data<AppState>,
+    req: HttpRequest,
+    query: web::Query<role_api::GetGroupsQueryParam>,
+) -> HttpResponse {
     let flow = Flow::GetRoleFromToken;
+    let respond_with_groups = query.into_inner().groups.unwrap_or(false);
+
     Box::pin(api::server_wrap(
         flow,
         state.clone(),
         &req,
         (),
-        |state, user, _| role_core::get_role_from_token(state, user),
+        |state, user, _| async move {
+            // TODO: Permissions to be deprecated once groups are stable
+            if respond_with_groups {
+                role_core::get_role_from_token_with_groups(state, user).await
+            } else {
+                role_core::get_role_from_token_with_permissions(state, user).await
+            }
+        },
         &auth::DashboardNoPermissionAuth,
         api_locking::LockAction::NotApplicable,
     ))

## Type of Change
<!-- Put an `x` in the boxes that apply -->

- [ ] Bugfix
- [x] New feature
- [ ] Enhancement
- [ ] Refactoring
- [ ] Dependency updates
- [ ] Documentation
- [ ] CI/CD

## Description
<!-- Describe your changes in detail -->
Get role from token will respond with groups if asked.

### Additional Changes

- [x] This PR modifies the API contract
- [ ] This PR modifies the database schema
- [ ] This PR modifies application configuration/environment variables

<!--
Provide links to the files with corresponding changes.

Following are the paths where you can find config files:
1. `config`
2. `crates/router/src/configs`
3. `loadtest/config`
-->


## Motivation and Context
<!--
Why is this change required? What problem does it solve?
If it fixes an open issue, please link to the issue here.

If you don't have an issue, we'd recommend starting with one first so the PR
can focus on the implementation (unless it is an obvious bug or documentation fix
that will have little conversation).
-->
Closes #3871 

## How did you test it?
<!--
Did you write an integration/unit/API test to verify the code changes?
Or did you test this change manually (provide relevant screenshots)?
-->
The following api will take a query param called `groups` and if it is set to `true` it will respond with array of groups.

This api was already present previously but was responding with permissions. The default behaviour without the `groups` query param is still the same (permissions will be returned). Groups will only be returned when `groups` param is set to true.
```
curl --location 'http://localhost:8080/user/role?groups=true' \
--header 'Authorization: Bearer JWT'
```
```
[
    "users_view"
]
```

## Checklist
<!-- Put an `x` in the boxes that apply -->

- [x] I formatted the code `cargo +nightly fmt --all`
- [x] I addressed lints thrown by `cargo clippy`
- [x] I reviewed the submitted code
- [ ] I added unit tests for my changes where possible
- [ ] I added a [CHANGELOG](/CHANGELOG.md) entry if applicable
