2

How can I create/see the list of user according their roles?

I'm interested in any answers to do this programmatically, or via any other way/technique I could use for this.

3

Create a View Page of users with exposed filter as role.

| improve this answer | |
1

Install module admin_views and visit /admin/people page.

| improve this answer | |
  • why is necessary install the admin_views to see the admin/people? – Adrian Cid Almaguer Apr 11 '16 at 13:51
  • the page view will be modified .. – Himanshu Pathak Apr 12 '16 at 12:13
  • But I think that with the same info that you have without the module – Adrian Cid Almaguer Apr 12 '16 at 12:36
  • The view is having exposed filter for further adding filters. So I prefer admin views to be installed for such tasks. – Himanshu Pathak Apr 13 '16 at 12:16
1

Option 1: Using the Views module

Install the the Views module and use the Views UI to create an appropriate views page of entity type "Users". Then add the User Role as filter, which you also expose. And probably also limit the access to this view to users who have some specific permission.

Here is a sample of such view, in Views export format, which you should be able to import in your own site also (using the Views UI):

$view = new view();
$view->name = 'users_and_roles';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'users';
$view->human_name = 'Users and roles';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Users and roles';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['access']['perm'] = 'administer users';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'table';
$handler->display->display_options['style_options']['columns'] = array(
  'name' => 'name',
  'rid' => 'rid',
);
$handler->display->display_options['style_options']['default'] = '-1';
$handler->display->display_options['style_options']['info'] = array(
  'name' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'rid' => array(
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
);
/* Field: User: Name */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'users';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['label'] = '';
$handler->display->display_options['fields']['name']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['name']['alter']['ellipsis'] = FALSE;
/* Field: User: Roles */
$handler->display->display_options['fields']['rid']['id'] = 'rid';
$handler->display->display_options['fields']['rid']['table'] = 'users_roles';
$handler->display->display_options['fields']['rid']['field'] = 'rid';
/* Field: User: Active */
$handler->display->display_options['fields']['status']['id'] = 'status';
$handler->display->display_options['fields']['status']['table'] = 'users';
$handler->display->display_options['fields']['status']['field'] = 'status';
$handler->display->display_options['fields']['status']['not'] = 0;
/* Field: User: Created date */
$handler->display->display_options['fields']['created']['id'] = 'created';
$handler->display->display_options['fields']['created']['table'] = 'users';
$handler->display->display_options['fields']['created']['field'] = 'created';
$handler->display->display_options['fields']['created']['date_format'] = 'long';
$handler->display->display_options['fields']['created']['second_date_format'] = 'long';
/* Field: User: Last login */
$handler->display->display_options['fields']['login']['id'] = 'login';
$handler->display->display_options['fields']['login']['table'] = 'users';
$handler->display->display_options['fields']['login']['field'] = 'login';
$handler->display->display_options['fields']['login']['date_format'] = 'long';
$handler->display->display_options['fields']['login']['second_date_format'] = 'long';
/* Field: User: Uid */
$handler->display->display_options['fields']['uid']['id'] = 'uid';
$handler->display->display_options['fields']['uid']['table'] = 'users';
$handler->display->display_options['fields']['uid']['field'] = 'uid';
/* Sort criterion: User: Created date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'users';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Filter criterion: User: Roles */
$handler->display->display_options['filters']['rid']['id'] = 'rid';
$handler->display->display_options['filters']['rid']['table'] = 'users_roles';
$handler->display->display_options['filters']['rid']['field'] = 'rid';
$handler->display->display_options['filters']['rid']['exposed'] = TRUE;
$handler->display->display_options['filters']['rid']['expose']['operator_id'] = 'rid_op';
$handler->display->display_options['filters']['rid']['expose']['label'] = 'Roles';
$handler->display->display_options['filters']['rid']['expose']['operator'] = 'rid_op';
$handler->display->display_options['filters']['rid']['expose']['identifier'] = 'rid';
$handler->display->display_options['filters']['rid']['expose']['remember_roles'] = array(
  2 => '2',
  1 => 0,
  3 => 0,
  4 => 0,
  5 => 0,
  6 => 0,
);

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'users-and-roles';

Using this (extremely basic) view, you'll get a result as in this example:

            Roles                  Active  Created date                       Last login                        Uid 
John.Smith  editor                 Yes     Friday, March 11, 2016 - 21:05     Friday, April 1, 2016 - 18:19     19
Demo.User   member                 Yes     Monday, November 23, 2015 - 10:11  Sunday, March 27, 2016 - 18:39    8
Super.User  administrator, member  Yes     Tuesday, October 27, 2015 - 10:55  Thursday, April 7, 2016 - 12:47   1
Anonymous                          No           0

Note: out of the Views-box you get nothing, you really need to create such (minimalistic) view as shown above. If you're familiar with Views, this is really a getting started with Views sample. If you're not, it may be a little challenging at first ...

Option 2: Using the Forena module

Install the Forena module and use these Drupal Administration reports that are delivered with it:

- Active Users

+-----------+-----------------------+-------------------------+---------+
| User      | Email                 | Last Logged in          | Actions |
+-----------+-----------------------+-------------------------+---------+
| Webmaster | webmaster@example.com | Sun, 03/27/2016 - 18:39 | logs    |
| Admin     | info@example.com      | Thu, 04/07/2016 - 12:47 | logs    |
| Pierre    | pierre@example.com    | Fri, 04/01/2016 - 18:19 | logs    |
+-----------+-----------------------+-------------------------+---------+

Using the hyperlink "logs" (in the column titled "Actions"), you get a new report (= Logs for user) showing the most recent log entries for the selected user with "type", "message", "timestamp" and with a subsequent hyperlink ("details") that will show additional details for the selected log record (such as "severity", "hostname", etc).

- Role Details

After selecting any of the available roles (= a report parameter), you get a report that looks like in this example:

+-----------+---------+------------------------------+
| User Name | User Id | Actions                      |
+-----------+---------+------------------------------+
| Admin     | 1       | View Profile    Edit Profile |
| Webmaster | 8       | View Profile    Edit Profile |
+-----------+---------+------------------------------+

The hyperlinks View Profile / Edit Profile can be used to actually view/edit the profile of the selected user.

Tuning/cloning the delivered reports

Optionally, if you want to customize these reports, use the query writing tool that comes with Forena, which is called the Forena Query Builder. This is a separated module that comes with Forena. It can be enabled as per standard Drupal instructions to enable an additional module. For a video tutorial about this query writing tool, checkout Define data blocks with optional filters.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.