Use joins instead of multiple lookups in groups sql

Currently to determine a user's groups (or vice versa) we do
- 1 lookup for the user.
- 1 lookup for the association table.
- n lookups for the n groups the user has.

The association and groups lookup are replaced with a join query.

Fixes: bug 1218675
Change-Id: I560659eda1a30635399829e1c86f06734c90f9e2
(cherry picked from commit 6f9b75e2c3bbbd7dfc5e2d414d7271bb4f8bcf71)
Merge "Use joins instead of multiple lookups in groups sql" into stable/grizzly