# GetUserRoleAccess: TPP_GetRoleAccess replaced by inline MySQL (2026-11-27)

## What changed

Only `MantriAPIController::GetUserRoleAccess` was modified. It no longer executes any stored procedure.

The SQL of `TPP_GetRoleAccess (@LoginId, @CLIId, @ERIId, @ModuleFlihId)` is reproduced by a local function, `$TPP_GetRoleAccess`, inside the method. It uses prepared statements on `PDV_FLIH` and `MSA_URAR`. The two places that called the procedure (the module list, then the menus of each module) now call this function with the same four arguments.

Everything else in the method is unchanged:

- credential check
- icon URL prefixing of `moduleicon` and `menuicon`
- the `menudata` JSON string
- the exception handling, which returns `granted` / `Success` with the error text, as in C#

| Procedure logic | PHP |
|---|---|
| `IF @ERIId <> -2`: `AND URAR.URAR_EriId = @ERIId AND URAR.URAR_CliId = @CLIId` | Same condition, values bound with `?` |
| `IF @ModuleFlihId = 0`: `SELECT DISTINCT` modules from the `PDV_FLIH FLIH` ⋈ `PDV_FLIH MFLIH` self-join, `LEFT JOIN MSA_URAR`, `FLIH_App = 1 AND MFLIH.FLIH_Id > 0` | Same query; aliases `moduleid`, `modulecode`, `modulename`, `moduleicon` |
| `ELSE` (any non-zero id, as in the procedure): `AND FLIH.FLIH_ModuleFlihId = @ModuleFlihId … ORDER BY FLIH.FLIH_SerialNo` | Same |
| `ELSE`, with `@ERIId = -2`: every permission is the literal `1`; no URAR join | Same; the permissions are returned as the integer `1` |
| `ELSE`, other roles: `ISNULL(URAR.URAR_x, 1)` with `LEFT JOIN MSA_URAR` | `COALESCE(URAR.URAR_x, 1)` with `LEFT JOIN MSA_URAR` |
| `@LoginId` | Accepted. It is not used by the procedure's SQL, and is not used here either. |
| `EXEC(@SqlQuery)`, dynamic SQL built from concatenated values | Prepared statements; no dynamic values in the SQL text |

## Behaviour notes

**Permission types.** SQL Server `ISNULL(bit, 1)` returns a `bit`, so the C# API returned `true`/`false` for the five permissions of non-`-2` roles. MariaDB returns `COALESCE(bit, 1)` as a DECIMAL. The method converts those five columns back to booleans, so the JSON stays as it was:

- roles other than `-2`: `"viewmode":true`
- role `-2`: `"viewmode":1`

**The COALESCE default cannot take effect.** `MSA_URAR` permission columns are `NOT NULL DEFAULT b'0'`. Also, for roles other than `-2`, the `URAR_EriId`/`URAR_CliId` condition removes menus that have no URAR row. So a missing permission row never reaches `COALESCE`, in the original procedure or here. Kept exactly as specified.

**`PDV_FLIH`** is a view over `msa_flih` that returns only rows with `FLIH_Deleted = 1`. That is the existing view definition and is used unchanged.

## Test results

**GetUserRoleAccess tests: 19 / 19 pass.**

The tests ran through the real controller, inside one transaction that was **rolled back**; `msa_flih` and `msa_urar` were unchanged afterwards. Seed data:

- two modules
- five menus: two app menus with different serial numbers, a menu with `FLIH_App = 0`, a menu hidden by the view, and an app menu with an empty icon
- `MSA_URAR` rows for role 5 at locations 1000 and 1001

| Case | Result |
|---|---|
| Role `-2` | Both modules returned (DISTINCT, no URAR filter). Menus ordered by `FLIH_SerialNo`. The `FLIH_App = 0` menu and the view-hidden menu are excluded. Every permission is the integer `1`. Icons are prefixed; a NULL or empty icon gives `""`. Response keys and aliases are exactly as before. |
| Role 5, location 1000 | Only the module and menu that have rights for that role and location. Permissions come from `MSA_URAR` as `true`/`false`. |
| Role 5, location 1001 | Only the other module and its menu |
| Role without any `MSA_URAR` rows | `granted` with `[]` |
| `@LoginId` 1 vs 999 | Identical result |
| Invalid apikey | `notgranted` / `Credentials not matching` (unchanged) |

**Through Apache with real data:** `granted` with `[]`, because the local tables hold no matching rows. No errors were logged.

**Regression: all suites pass.**

| Suite | Result |
|---|---|
| Unit | 45 / 45 |
| Migration HTTP | 29 / 29 |
| Route sweep | 70 / 70 |
| Master data | 66 / 66 |
| Employee APIs | 30 / 30 HTTP and 40 / 40 transactional |
| GetEmployees | 23 / 23 |
