Mat
Mat

Reputation: 1688

Codeigniter A3M reduce database calls for authorisation

I'm using A3M in my Codeigniter app. One of the things it does is determine what items appear in the main navigation menu, depending on the logged in user's permissions.

This works fine, but I'm a little concerned about the number of database calls required to generate the menu -- especially as it happens on each-and-every page.

Does anyone have any ideas about how to avoid this? Some type of caching perhaps.

Thanks

Upvotes: 0

Views: 74

Answers (1)

Mat
Mat

Reputation: 1688

Turned out to be pretty simple, so I'm posting the solution here in case anyone else finds it useful.

Without going into massive detail (unless somebody asks), the best approach seems to be to create a database query to extract an array of all the current user's permissions. This is pretty simple by joining the a3m_account, a3m_rel_account_role, a3m_rel_role_permission and a3m_acl_permission tables.

Once you have the result, 'flatten' it by adding each key_name to a single-dimensional array called, for example, $permissions. You can then check permissions by doing an in_array('permission_to_check',$permissions)

This allows you to perform loads of permission checks with a single database call. This has dramatically sped up up my application. You could even add the $permissions array to the session data at login, so permission checks through out the session don't require a database call (but obviously, changes won't take place immediately).

Upvotes: 0

Related Questions