We're updating the issue view to help you get more done. 

Improve rights\permissions management code

Description

Currently permissions are set via event listeners, which may be fired multiple times for one actual save button press by the user (or one REST API call)

It is possible to just set them the moment they are changed, right on the backend (becaus we KNOW we need to save them and how to save them, no need to fire an event and wait for a listener to do the job - as discussed with ), and instead of listening to a "patientChangingEvent" compare current and old and fire some kind of "permissionsChangedEvent" if they have changed 9so that interested parties can listen to that change specifically)

This becomes especially problematic with family studies, where a family may have multiple patients. On save, each patient doc is modified, event is fired, permissions are updated in regular event listener, family permission listener also starts, reads all member patient docs, updates family permissions, next patient is updated, event is fired, regular permission listener updates permissions, family permission listener updates permissions (for which it reads all member patient documents again, since it needs aggregate owner/collaborator information)), and so on. Adding some debug output here is what happens when a pedigree with just two patients is saved (with no actual permission changes taking place!):

```2016-09-09 23:47:04,339 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .p.i.RightsUpdateEventListener - RIGHTUPDATE
2016-09-09 23:47:05,002 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .f.l.PermissionsChangeListener - FAMILY PERMISSIONS UPDATE
2016-09-09 23:47:05,005 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .p.i.RightsUpdateEventListener - RIGHTUPDATE
2016-09-09 23:47:05,519 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .f.l.PermissionsChangeListener - FAMILY PERMISSIONS UPDATE
2016-09-09 23:47:05,521 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .p.i.RightsUpdateEventListener - RIGHTUPDATE
2016-09-09 23:47:05,984 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .f.l.PermissionsChangeListener - FAMILY PERMISSIONS UPDATE
2016-09-09 23:47:05,987 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .p.i.RightsUpdateEventListener - RIGHTUPDATE
2016-09-09 23:47:06,425 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .f.l.PermissionsChangeListener - FAMILY PERMISSIONS UPDATE
2016-09-09 23:47:06,428 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .p.i.RightsUpdateEventListener - RIGHTUPDATE
2016-09-09 23:47:06,851 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .f.l.PermissionsChangeListener - FAMILY PERMISSIONS UPDATE
2016-09-09 23:47:06,853 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .p.i.RightsUpdateEventListener - RIGHTUPDATE
2016-09-09 23:47:07,291 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .f.l.PermissionsChangeListener - FAMILY PERMISSIONS UPDATE
2016-09-09 23:47:07,294 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .p.i.RightsUpdateEventListener - RIGHTUPDATE
2016-09-09 23:47:07,712 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .f.l.PermissionsChangeListener - FAMILY PERMISSIONS UPDATE
2016-09-09 23:47:07,714 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .p.i.RightsUpdateEventListener - RIGHTUPDATE
2016-09-09 23:47:08,142 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .f.l.PermissionsChangeListener - FAMILY PERMISSIONS UPDATE
2016-09-09 23:47:08,145 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .p.i.RightsUpdateEventListener - RIGHTUPDATE
2016-09-09 23:47:08,568 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .f.l.PermissionsChangeListener - FAMILY PERMISSIONS UPDATE
2016-09-09 23:47:08,570 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .p.i.RightsUpdateEventListener - RIGHTUPDATE
2016-09-09 23:47:09,052 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .f.l.PermissionsChangeListener - FAMILY PERMISSIONS UPDATE
2016-09-09 23:47:09,055 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .p.i.RightsUpdateEventListener - RIGHTUPDATE
2016-09-09 23:47:09,502 http://localhost:7777/get/PhenoTips/FamilyPedigreeInterface?rand=0.5187819702968555&action=save ERROR .f.l.PermissionsChangeListener - FAMILY PERMISSIONS UPDATE```

..also note that this took 5 seconds on a core i7

The issue is probably amplified by PT-2782, but it is a problem without it as well

Environment

None

Status

Assignee

Marta Girdea

Reporter

Andrew Misyura

Labels

None

External issue ID

None

External issue ID

None

Components

Priority

Medium