ForbesLindesay/connect-roles

Grouping routes

Closed this issue · 4 comments

Hi.

Any way to enable route access grouping?
So I don't add {}.can('access private page') for every route?

I don't quite understand what you mean. Can you give a little example of code you would like to be able to write, and how you would expect it to behave?

sorry, @ForbesLindesay for long reply.

I can add an example from PHP (Laravel) of what I want to achieve (should be quite clear):

Route::group(array('before' => 'basicAuth', 'prefix' => 'admin/dashboard'), function()
{
    Route::get('', array(
            'as' => 'indexDashboard',
            'uses' => 'DashboardController@getIndex')
    );

    Route::get('logout', array(
            'as' => 'logout',
            'uses' => 'DashboardController@getLogout')
    );

    Route::get('access-denied', array(
            'as' => 'accessDenied',
            'uses' => 'DashboardController@getAccessDenied')
    );

});

Here the router check if the user has passed basic authorization.

Sounds like you're after creating a cascade:

roles.use(function (req, action) {
  // false stops the cascade and denies access
  if (!req.isAuthenticated() && /^\/admin\/dashboard/i.test(req.url)) return false;
  // undefined allows the cascade to continue
});

roles.use('access admin page', function (req) {
  return req.user.isAdmin();
});

That way user.can('access admin page') (if the url starts with /admin/dashboard) would first check the user is authenticated, then check that the user can access the admin page.