Exploring this module and trying to determine if it can do what I need. Using the latest from Git.
Is there a way for:
1) Site admin (not group owner) to define default roles and permissions for all new groups created by users.
a) Tried using default roles, and created scientist, lead scientist, and coordinator roles in Drupal. When set to use default roles for groups, OG does not show these roles as being available in the group (only shows non-member, member, and administrator-member).
b) A user should not have to create roles and permissions every time they create a group.

2) Using default roles, I see the roles and permissions are read only. That's fine... but an option to not show roles and permission links, while still enabling them to manage people, would be a bonus.

3) Not sure if this is a bug... but when trying to work with roles created within a group itself (ie scientist) and no permissions assigned, the user assigned to this role was allowed to access the group permissions and roles as if an admin. Was this because it was placed AFTER administrator-member? I did not see a way to move this role below.

4) If I, as the Admin, decide the content type Group is a group type, why can a user then change that when creating or editing a group they create? I tried hiding the Group Type, Group Audience, and Group Roles and Permissions from users so they cannot change them... thus, accepting the defaults I set... but they still appear no matter what I do.

I fairly new to this, so I understand I am probably missing some things here. Help?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

amitaibu’s picture

1) The system roles and OG roles are not the same. OG roles are defined via admin/config/group/permissions

2) Yes, this is a good suggestion, to show users with the right permissions a link to the global roles/ permissions -- care to write a patch? :)

3) Are you sure this user didn't have "administer group" defined via the system permissions? If so, please describe step by step how to reproduce.

4) For now you can set the group field to be required, or use hook_form_alter() to hide the options. since it will be probably a common task, maybe we should add those options in the group field widget settings

rbruhn’s picture

Thank you for your reply.

1) Don't you mean the OG role permissions are defined here? I don't see a way for a site admin to define more default roles for groups. There are the three you have defined, but no method to create more. Or am I missing something?
For example, the site I'm moving to Drupal has groups with 3 defined roles that would need to exist between "member" and "admin-member." The users should not have to define those roles and assign permissions when they create a group. As site admin, I would like to define roles and assign permissions globally that apply to all groups.

2) Perhaps. See below :-)

3) I will try to repeat this process.

4) No comprehension. See below... lol

I am a PHP programmer professionally but this is my first foray into Drupal. We have decided Drupal to be the best CMS for our project, and I'm just getting into what it can do and what is available. Groups is a big part of our system when it comes to who can access, edit, submit, etc.

So, when I'm up to snuff on the API I will be more than happy to patch, invent, and/or otherwise twist and help out where I can. :-) Some of the things I mention above are definitely needed by our system, so it will require me doing something.

Thanks again for the reply :-)

rbruhn’s picture

FileSize
4.32 KB

Related to what I discussed above....
Attached is my attempt to add the functionality of adding/editing/removing default roles. As mentioned above, I'm just learning Drupal, so there will probably be mistakes or things I should have done differently. Feel free to criticize and make suggestions. I used existing OG functions where possible... or at least where I found them. Not sure if it is right to do this or whether functions to retrieve/insert data should be encapsulated within the new module itself.

I added this module within the OG module directory, hoping it might become part of the project as a whole. I named it og_roles because og_default_roles is used in some current functions. Could be named something else if desired. Following the OG module example, I took much of the code from the User module and altered it according to the need.

If this seems like something you might want to add to the project, might I make a few proposals?

1) Would it be possible to add "weight" to the og_role table? When adding roles via this module or the user group interface, the roles appear out of order in the list and permissions. Of course, this would mean changing select statements to order by weight, as well as changing some forms so weight can be set. Changing the form in the og_roles to allow for weights is a simple matter.

2) Use constants to define the default "non-member", "member", and "administrator member" ids instead of defining constants to their string equivalents.

I noticed in the Drupal user module, ids for non-member and member are used, and the administrator role string can be renamed. Since I could not find code in the Drupal core comparing or setting access according to the string, I assume the only reason for Drupal core not allowing the change of "non-member" and "member" is so people do not get confused. Unless I missed something?

Being able to change these strings in OG can be done by altering code so their Ids are used instead of the strings themselves. I know this is not an easy thing to do, as the constants are used throughout the OG module, but I'm willing to help convert code. Or, perhaps provide a way to change the constants?

Anyway... I admit I'm new to this... so feel free to let her rip :-)

rorymadden’s picture

subscribe

amitaibu’s picture

Status: Active » Needs work

> I don't see a way for a site admin to define more default roles for groups.

Indeed this is done via code see og.api.php -- hook_og_default_roles(). So your code souhldn't deal with all the UI of adding a role -- we already have it on OG UI, it just need to allow adding it via UI.

Anyway, f this is your first Drupal module, then boy -- you are on the right way! ;)

rbruhn’s picture

>Anyway, f this is your first Drupal module, then boy -- you are on the right way! ;)

Thank you, but the credit is not deserved. Most of the code is simply copied and pasted from the Drupal User module, then tweaked and tinkered with where necessary.

> Indeed this is done via code see og.api.php -- hook_og_default_roles().
I will take a look at what you described here when I have some time. Thank you for pointing it out.

YK85’s picture

subscribing

Would you additions allow support for #1034992: Allow choice for groups to have owners or be public (anyone can edit) when creating the group?
If not, would it be something you may be interested in helping with adding to the great OG module?

Thank you!

HippoOnDiet’s picture

I was going to ask Amitaibu how to create a new default group, it would be a lot of work if I have to create a new group individually for 10 groups.

I have tested rbruhn module as posted on thread #3 and it works great!
Good job rbruhn :) this is what I need...

Just 2 things to update in og_roles.admin.inc to follow changes from https://github.com/amitaibu/og/blob/b70be0ecc21b81a554b4afb6d4f93d11b7a2...

Line 145-156 from the og_roles.admin.inc

function og_roles_user_admin_role_submit($form, &$form_state) {
  $role = (object) $form_state['values'];
  
  if ($form_state['values']['op'] == t('Save role')) {
-    og_user_role_save($role);
+    og_role_save($role);
    drupal_set_message(t('The role has been renamed.'));
  }
  elseif ($form_state['values']['op'] == t('Add role')) {
    $role->gid = 0;
-    og_user_role_save($role);
+    og_role_save($role);
    drupal_set_message(t('The role has been added.'));
  }
amitaibu’s picture

The ability to add/ remove global roles is already in OG itself (download latest from github or wait for the git migration to grab it from drupal.org)

HippoOnDiet’s picture

Awesome Amitaibu... :)

You are very fast updating your module! Thank you again and appreciate your work!

rbruhn’s picture

I've come back to this after working on some other things. Thanks for adding the user role stuff to your module.
In order to make it "prettier" I took some time rewriting some of the dev branch so "weight" can be used with the roles. The code changes will allow a site administrator to order the roles, as well as allow a group admin to reorder them. This also gives a nicer looking table in the permissions so instead of this:

non-member | member | administrator member | scientist | lead scientist

you will get this:

non-member | member | scientist | lead scientist | administrator member

when adding new roles.

Edit: This is an after thought after already writing this post. When a group is allowed to override the default roles, so a group admin can manage a group's roles and permissions, the default roles are duplicated for that group id. Where is this code located? Probably best to use the same weights as the defaults.

The code changes I made are below, listed by file and function.

Need to add the weight column to the og_role table and update it so:
non-member defaults to 0
member weight = 1
administrator member weight = 2

'weight' => array(
  'type' => 'int',
  'size' => 'normal',
  'default' => 0,
  'description' => 'The weight of this role in listings and the user interface.',
),

og.module - Changed line 2064 and 2067 sql statements to order by weight.
I didn't notice anywhere else in the code where the order would matter. Something to look into?

if (!empty($permission)) {
    $roles = db_query("SELECT r.rid, r.name FROM {og_role} r INNER JOIN {og_role_permission} p ON r.rid = p.rid WHERE p.permission = :permission AND r.gid = :gid ORDER BY r.weight", array(':permission' => $permission, ':gid' => $gid))->fetchAllKeyed();
  }
  else {
    $roles = db_query("SELECT rid, name FROM {og_role} WHERE gid = :gid ORDER BY weight", array(':gid' => $gid))->fetchAllKeyed();
  }

og.module - Changed function og_role_save().
Added if (!isset($role->weight)) like it's used in the user module.

function og_role_save($role) {
  if ($role->name) {
    // Prevent leading and trailing spaces in role names.
    $role->name = trim($role->name);
  }
  if (!isset($role->weight)) {
    // Set a role weight to make this new role last.
    $query = db_select('og_role');
    $query->addExpression('MAX(weight)');
    $role->weight = $query->execute()->fetchField() + 1;
  }
  if (!empty($role->rid) && $role->name) {
    $status = drupal_write_record('og_role', $role, 'rid');
    module_invoke_all('og_role_update', $role);
  }
  else {
    $status = drupal_write_record('og_role', $role);
    module_invoke_all('og_role_insert', $role);
  }

  og_invalidate_cache();

  return $status;
}

og_ui.admin.inc - changed function og_ui_user_admin_roles()
Added code for the weight field and button to save weights.

function og_ui_user_admin_roles($form, $form_state, $entity_type = '', $etid = 0, $rid = 0) {
  $gid = 0;
  if ($entity_type && $group = og_get_group($entity_type, $etid)) {
    $gid = $group->gid;
  }

  $form['gid'] = array('#type' => 'value', '#value' => $gid);

  if ($rid) {
    if ($gid) {
      og_set_breadcrumb($entity_type, $etid, array(l(t('Group'), "$entity_type/$etid/group")), l(t('Roles'), "group/$entity_type/$etid/admin/people/roles"));
    }

    $group_roles = og_roles($gid);
    if (!in_array($rid, array_keys($group_roles))) {
      drupal_goto($gid ? "group/$entity_type, $etid/admin/people/roles" : 'admin/config/group/roles');
    }
    // Display the edit role form.
    $role = og_role_load($rid);
    $form['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Role name'),
      '#default_value' => $role->name,
      '#size' => 30,
      '#required' => TRUE,
      '#maxlength' => 64,
      '#description' => t('The name for this role. Example: "moderator", "editorial board", "site architect".'),
    );
    $form['rid'] = array(
      '#type' => 'value',
      '#value' => $rid,
    );
    $form['weight'] = array(
      '#type' => 'value',
      '#value' => $role->weight,
    );
    $form['actions'] = array('#type' => 'actions');
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save role'),
    );
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete role'),
    );
  }
  else {
    // No role ID.
    if ($gid) {
      og_set_breadcrumb($entity_type, $etid, array(l(t('Group'), "$entity_type/$etid/group")));
    }
    if (!$gid || !og_is_group_default_access($group->gid)) {
      $form['name'] = array(
        '#type' => 'textfield',
        '#title' => t('Name'),
        '#title_display' => 'invisible',
        '#size' => 32,
        '#maxlength' => 64,
      );
      $form['roles'] = array(
        '#tree' => TRUE,
      );
      $order = 0;
      foreach (og_roles($gid) as $rid => $name) {
        $form['roles'][$rid]['#role'] = (object) array(
          'rid' => $rid,
          'name' => $name,
          'weight' => $order,
        );
        $form['roles'][$rid]['#weight'] = $order;
        $form['roles'][$rid]['weight'] = array(
          '#type' => 'textfield',
          '#title' => t('Weight for @title', array('@title' => $name)),
          '#title_display' => 'invisible',
          '#size' => 4,
          '#default_value' => $order,
          '#attributes' => array('class' => array('role-weight')),
        );
        $order++;
      }
      $form['add'] = array(
        '#type' => 'submit',
        '#value' => t('Add role'),
        '#validate' => array('og_ui_user_admin_roles_validate'),
        '#submit' => array('og_ui_user_admin_roles_submit'),
      );

      // Attach the CSS to the form.
      $form['#attached'] = array(
        'css' => array (
          'type' => 'file',
          'data' => drupal_get_path('module', 'og_ui') . '/css/og_ui.css',
        ),
      );

      $form['actions'] = array('#type' => 'actions');
      $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save order'),
        '#submit' => array('og_ui_user_admin_roles_order_submit'),
      );
    }
  }

  return $form;
}

og_ui.admin.inc - changed function og_ui_user_admin_roles_submit()
Added if (!empty($form_state['values']['weight']))

function og_ui_user_admin_roles_submit($form, &$form_state) {
  $gid = $form_state['values']['gid'];
  if ($gid) {
    $group = og_load($gid);
  }
  $role = new stdClass();
  $role->gid = $gid;
  $role->name = $form_state['values']['name'];
  if (!empty($form_state['values']['rid'])) {
    $role->rid = $form_state['values']['rid'];
  }
  if (!empty($form_state['values']['weight'])) {
    $role->weight = $form_state['values']['weight'];
  }
  // Indicates if we need to save role.
  $save = FALSE;
  if ($form_state['values']['op'] == t('Save role')) {
    og_role_save($role);
    drupal_set_message(t('The role has been renamed.'));
  }
  elseif ($form_state['values']['op'] == t('Add role')) {
    og_role_save($role);
    drupal_set_message(t('The role has been added.'));
  }
  elseif ($form_state['values']['op'] == t('Delete role')) {
    og_role_delete($form_state['values']['rid']);
    drupal_set_message(t('The role has been deleted.'));
  }

  $form_state['redirect'] = $gid ? 'group/' . $group->entity_type . '/' . $group->etid . '/admin/people/roles' : 'admin/config/group/roles';
  return;
}

og_ui.admin.inc - added function og_ui_user_admin_roles_order_submit()
Based on user module.

function og_ui_user_admin_roles_order_submit($form, &$form_state) {
  foreach ($form_state['values']['roles'] as $rid => $role_values) {
    $role = $form['roles'][$rid]['#role'];
    $role->weight = $role_values['weight'];
    og_role_save($role);
  }
  drupal_set_message(t('The role settings have been updated.'));
}

og_ui.admin.inc - changed function theme_og_ui_user_admin_new_role()
Added various lines to handle the weight field and making the table rows draggable.

function theme_og_ui_user_admin_new_role($variables) {
  $form = $variables['form'];
  $header = array(t('Name'), t('Weight'), array('data' => t('Operations'), 'colspan' => 2));
  // The group entity.
  if ($gid = $form['gid']['#value']) {
    $group = og_get_group('group', $gid);
  }

  $default_access = $gid && og_is_group_default_access($gid);

  $order = 0;
  foreach (og_roles($gid) as $rid => $name) {
    $text = !$default_access ? t('edit permissions') : t('view permissions');
    $path = $gid ? 'group/' . $group->entity_type .'/' . $group->etid . '/admin/people' : 'admin/config/group';
    $permissions = l($text, $path . '/permissions/' . $rid);
    $weight = !$default_access ? drupal_render($form['roles'][$rid]['weight']) : $order;

    if (!$default_access && !in_array($name, array(OG_ANONYMOUS_ROLE, OG_AUTHENTICATED_ROLE))) {
      $rows[] = array('data' => array($name, $weight, l(t('edit role'), $path . '/roles/edit/' . $rid), $permissions), 'class' => array('draggable'));
    }
    else {
      $rows[] = array('data' => array($name, $weight, t('locked'), $permissions), 'class' => array('draggable'));
    }
    $order++;
  }
  
  $rows[] = array(array('data' => drupal_render($form['name']) . drupal_render($form['add']), 'colspan' => 4, 'class' => 'edit-name'));

  if (!$default_access) drupal_add_tabledrag('og-user-roles', 'order', 'sibling', 'role-weight');

  $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'og-user-roles')));
  $output .= drupal_render_children($form);
  
  return $output;
}
amitaibu’s picture

What am I supposed to do with all this code?

rbruhn’s picture

I was simply showing you the changes I made in order to allow ordering the roles by weight. Either the default roles in the admin area, or the roles in a group admin page. I did not know how else to show you accept by putting it here.

How else should I do it?

amitaibu’s picture

Oh, you mean a patch? Then create a patch ;)

rbruhn’s picture

didn't know how to do that. are there instructions some where?

Edit: Nevermind... reading about it now

rbruhn’s picture

Hope this is correct :-)

Edit: Forgot, but patch would require changing the db. Not sure how to represent that.
Need to add the weight column to the og_role table and update it so:
non-member defaults to 0
member weight = 1
administrator member weight = 2

amitaibu’s picture

Creating patches (you should create a single patch) -- http://drupal.org/patch/create

You should see how user.module of core is dealing with weights and imitate that.

rbruhn’s picture

Trying this again. Let me know if something is wrong.

rv0’s picture

this is very interesting

subscribe

rv0’s picture

Title: Roles and permissions, types and audience fields - functionality » Sortable roles by weight

ok patch in #18 still applies clean to latest dev

but as noted in #16, the patch doesnt include adding the required columns to the database, so it still needs work.

Changing title of the issue to be more descriptive to what this patch is for.

pillarsdotnet’s picture

Added og_update_7101() function which calls db_add_field() to add the weight field. Tested and working so far.

pillarsdotnet’s picture

Status: Needs work » Needs review
jlapp’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
FileSize
8.09 KB

I know this issue hasn't been updated in almost two years, but I am looking for this functionality on a site I am building. The most recent patch was against 7.x-1.x, so I've updated it to work with the latest OG 7.x-2.x-dev and also added views integration for the new weight field. It's working for me, but I'd appreciate it if anyone would be willing to review / test the attached patch so that we can get it committed into OG before the next major version passes this issue by :)

amitaibu’s picture

Thanks, I'll try to review myself or let someone from Gizra do it next week.

caschbre’s picture

So far our testing has been positive with this patch. The one thing I just noticed is that the role weight is not exported (to features).

RoySegall’s picture

Status: Needs review » Needs work
FileSize
44.85 KB

When the table will be sortable i'll be glad to review the patch.
Roles.png

caschbre’s picture

@RoySegall.... what version are you testing with? Did you clear the cache?

That table is sortable with patch #23. There's also an option to sort in Views by role weight.

amitaibu’s picture

@RoySegall, can you re-test it please with the global roles, instead. From the screenshots it seems you tried it on a non-overridden group.

RoySegall’s picture

Status: Needs work » Needs review
FileSize
7.88 KB
1.62 KB

No cache clear was needed - when a group is not overridden the roles the sortable table will not work and the user will see the weight of the role and this is not very useful.

I fixed that and now when the group isn't overridden the roles and permission - then the user wouldn't be able to change their order but only through the global permission page.

jlapp’s picture

Thanks for the feedback so far and the additional contributions.
I've updated the patch to export the role weights to features, however in order to reliably export/import the role weights I needed to also include the built-in anonymous/authenticated/admin roles in the export where they had previously not been exported. I'm attaching an updated patch along with an interdiff from RoySegall's changes in #29.

amitaibu’s picture

> I needed to also include the built-in anonymous/authenticated/admin roles in the export where they had previously not been exported

This should properly be tested, and since I'm not using this feature, others should do it. I have my doubts if that's the right approach. How is Features dealing with anon/ auth roles when exporting the site-wide roles?

jlapp’s picture

I agree, this should definitely be thoroughly tested especially since I have not worked with Features too much.

I looked for a way to avoid exporting the anonymous/authenticated/admin roles, but couldn't come up with a way of exporting their weights without exporting the whole role definition. If there is a better way I hope someone will suggest it since I'm certainly open to a different approach and would be happy to try it.

As for the site-wide roles, it looks like Features does not allow exporting those (the same as the current OG behavior). You can modify the weights of the roles but not export that value, which seems like a feature gap to me. Of course that is an issue to be addressed elsewhere, but I don't think it makes sense for OG to specifically omit a feature just because the user module is lacking it.

amitaibu’s picture

Status: Needs review » Needs work

Marking as needs work until there's a test

lsolesen’s picture

Category: Support request » Feature request
firewaller’s picture

Patch attached to add views sorting by Role ID (rid)

MorinLuc0’s picture

Reroll of the patch to apply to the 7.x-2.7 version of OG.

Just needed to change the hook_update number.

Amarjit’s picture

I needed a list of members that belong to a organic group, grouped by 'role'.

To sort by 'roles' that have been grouped by 'roles', you can add the sort criteria in Views.
You can see the weight field in the og.views.inc that allows it to be sortable.

  1. Add a new relationship for 'OG Roles from OG membership'
  2. Add the new sortable field 'OG user roles: Role Weight (asc)'

Thanks,
Amarjit

Amarjit’s picture

Confirmed #36 working with OG (7.x-2.8)

capysara’s picture

Patch #35 worked for me with OG 7.x-2.9.

firewaller’s picture

Status: Needs work » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 36: og-roles_sortable_by_weight-987568-36.patch, failed testing. View results

izmeez’s picture

Patch in #36 need to be re-rolled

Error: Cannot redeclare og_update_7206() (previously declared in og.install:1205) in
og.install, line 1225
izmeez’s picture

Attached is a re-roll of the patch in #36 with hook_update number changed to apply against the latest dev 2019-05-09.
@firewaller The patch in #35 is different. Is it meant to be an additional change or is it even needed in light of comment #37 ?
Is this ready to go or does it still need tests?

izmeez’s picture

Status: Needs work » Needs review

Changing status to "Needs review"

izmeez’s picture

The patch in this issue includes a hunk that is also changed in #2021673: [Features] Do not export non-existing roles. thus they conflict with each other. Once committed the other will need to be re-rolled.