# Case study: groups and users in LimeSurvey

Tested version: LimeSurvey 7.12.1 (DB version 722), PHP 8.4.14, PostgreSQL 17.12, local lab (PHP built-in server, URL format "path").
All tests were run over HTTP, logged in as each account for real (the code was not simulated): 1 director (superadmin), 4 managers, 10 users, 6 user groups, 7 survey groups, 22 automated checks (`tests/suite.py`).

## 1. Short answer

The organization can be built, but not with stock LimeSurvey 7.12.1.

| Requirement | Stock LimeSurvey | With the provided patch |
|---|---|---|
| 1. A manager only sees the users he created | No. He sees all users, including the director and the other managers. | Yes |
| 2. He can only add his own users to his groups | No. The full user list is offered, and adding a user of another manager (or the director) is accepted. | Yes |
| 3. A theme (blue background, etc.) set for a group and applied by default | Yes, through the survey group (not through the user group, see 4.3). | Yes |
| 4. The manager sees all surveys of his users, and only those | Partly. Only surveys stored in his own survey groups. | Yes |

Two points are solved by permission settings alone, with no code change (section 4.1).

## 2. What was built (as in the statement)

Director = admin. Managers: Gest_user_info, Gest_user_reseau, Gest_user_compta_gene, Gest_user_compta_ana, created by the director. Each manager created his user groups (Serveur, Messagerie, Cablage, Logistique, General, Analytique), his survey groups (Secteur_Messagerie, Secteur_Serveur, Batiment, Depots, Compta_Gene, Secteur_Analytique), his users (user_info_1 to 4, user_reseau_1 to 4, user_compta_1 and 2) and assigned them to his groups. Users created surveys.
Permissions: managers = users, user groups, survey groups with read/create/update/delete, plus survey creation. Users = survey creation.

## 3. Errors and gaps found (stock LimeSurvey 7.12.1)

Numbers: 12 of 22 checks fail with the recommended permissions, 14 of 22 with the permissions of the statement.

1. **Unfiltered user list.** `User::search()` (application/models/User.php) has no filter on `parent_id`. The global permission "Users: read" is described by LimeSurvey itself as "view all users", and it is required to open the page. Result: Gest_user_info sees the 14 other accounts. In the same way `getUserList()` (helpers/common_helper.php) returns all users as soon as the read permission exists, so the pick lists for survey owner, survey sharing and survey group permissions are complete lists.
2. **No check on the user being added to a group.** `UserGroupController::actionAddUserToGroup` checks that the group belongs to the manager, but never that the added user is his. The drop-down in `actionViewGroup` offers every account. Tested: user_reseau_1, Gest_user_compta_ana and the admin account (superadmin) were all added to the group Serveur. `actionBatchAddGroup` (bulk action) has the same flaw. A user group does not grant rights by itself, but this breaks requirement 2, and these groups are later used to give rights on survey groups.
3. **Global permissions on survey groups = access to all survey groups.** With "Survey groups: read/update/delete" at global level (the case in the statement), Gest_user_info renamed and then deleted survey groups of Gest_user_reseau. This is the behavior LimeSurvey documents ("view, edit, delete the survey groups of other users"), not a bug, but it does not fit the intended organization. It also affects themes: the global survey group permission opens the theme options of other managers' groups.
4. **Users' surveys.** A manager only sees surveys he owns, or surveys stored in a survey group he owns (option `ownerManageAllSurveysInGroup`, true by default). A user who creates a survey in the "Default" survey group (always offered) makes it invisible to his manager. Tested: the survey of user_info_2 created in the default group does not show for Gest_user_info.

Checks that pass in every case: a manager cannot edit, delete or change the permissions of a user he did not create; he cannot edit or delete the user group of another manager; a user only sees his own surveys; an unrelated manager sees nothing; a user outside the linked group cannot store a survey in the survey group of another sector.

## 4. Solutions

### 4.1 Without changing the code (needed in all cases)

* Manager permissions: Users (read, create, update, delete), User groups (read, create, update, delete), **Survey groups: create only**, Surveys: create. Do not give Export on users (the export sends all accounts), nor Super admin, nor Themes.
  With "Survey groups: create only", the manager owns his groups (all rights on them) and can no longer touch those of others (checks R5a and R5b pass). The theme of his own group stays editable by him.
* User permissions: Surveys: create, and the "log in" right through the database authentication plugin (auth_db, read).
* The auth_db read permission must be added when permissions are saved by script, otherwise the account cannot log in (a trap met during the tests).
* Theme per sector: the manager opens his survey group (for example Secteur_Serveur), goes to the settings tab, picks the theme, then the theme options (colors, background). Surveys created in this group are set to "Inherit" and take this theme (checks R3a to R3c pass).
* Link between a user group and a survey group: in the security page of the survey group, "Add user group" (for example Serveur). Each member then gets the right to see and use this survey group. Note that this is a copy of the members at the time of the click. A user added to the user group later must get the right again by repeating the operation. A non-member cannot pick this survey group (R3d passes).

### 4.2 Code patch (diff provided: `managerScopeOwnUsers.diff`)

Option `managerScopeOwnUsers`, **false by default**: without enabling it, LimeSurvey behaves exactly as before (check: the same 12 failures before and after the patch, option off). To enable it, in `application/config/config.php`, in the `config` array: `'managerScopeOwnUsers' => true,`. It changes nothing for superadmins.
When enabled, for a user who is not a superadmin:

1. User list (`User::search`) and `getUserList()`: himself and the users he created (`parent_id`).
2. Adding to a user group: the list and the server-side check are limited to his users (`actionAddUserToGroup`, `actionViewGroup`, `actionBatchAddGroup`).
3. Surveys: he sees and can manage the surveys whose owner is a user he created, whatever the survey group (`Survey::getPermissionCriteria` and `Survey::hasPermission`).

6 files, 76 lines added, none removed. The diff applies cleanly on the original tree (`patch -p1`, verified).

### 4.3 Known limits

* The theme belongs to the survey group, not to the user group. LimeSurvey has no direct link between the two; the link is made in the survey group security page (4.1).
* A user can still store a survey in the "Default" survey group. With the patch, his manager still sees it (requirement 4), but the sector theme does not apply to this survey. Users must be asked to choose the survey group of their sector.
* With the patch, a manager owns the surveys of his users with all rights on them: read, edit, export, delete.
* The patch was not run through the official LimeSurvey unit test suite. Only the 22 checks below were run. Review it before any production use.

## 5. Results of the 22 checks

Files: `tests/result_*.json` (name, passed or not, detail). Columns: A = permissions of the statement (survey groups read/update/delete at global level), B = recommended permissions (create only). Option: patch off or on.

| Check | B, off | B, on | A, off | A, on |
|---|---|---|---|---|
| R1 each manager only sees his users (4 checks) | fail x4 | pass x4 | fail x4 | pass x4 |
| R2a add-to-group list limited to his users | fail | pass | fail | pass |
| R2b adding a foreign user / a manager / the director is refused (3) | fail x3 | pass x3 | fail x3 | pass x3 |
| R2c bulk add of a foreign user refused | fail | pass | fail | pass |
| R2d adding his own user still works | pass | pass | pass | pass |
| R3a theme set on his survey group | pass | pass | pass | pass |
| R3b members of the linked group create in this group | pass | pass | pass | pass |
| R3c the survey in the group inherits the theme | pass | pass | pass | pass |
| R3d non-member cannot use this group | pass | pass | pass | pass |
| R4a the manager sees all surveys of his users | fail | pass | fail | pass |
| R4b the other manager only sees those of his users | fail | pass | fail | pass |
| R4c manager with no link: sees nothing | pass | pass | pass | pass |
| R4d a user only sees his own surveys | pass | pass | pass | pass |
| R4e the manager opens a survey of his user | fail | pass | fail | pass |
| R4f he cannot open one of a user of another manager | pass | pass | pass | pass |
| R5a renaming another manager's survey group is refused | pass | pass | fail | fail |
| R5b deleting another manager's survey group is refused | pass | pass | fail | fail |

Passed totals: B off 10/22, B on 22/22, A off 8/22, A on 20/22. The two remaining failures in A are the ones of section 3, point 3, solved by permissions (section 4.1), not by code.

## 6. Re-running the tests

Folder `tests/`: `env.sh` (lab database and server), `setup.py` and `scen.py` (scenario creation), `suite.py` (the 22 checks), `runall.sh` (the 4 combinations). They are written for my lab (ports, paths): adapt them. The passwords of the test accounts are lab values.
