Check if the user is admin of a given organic group

Public

Takes user id and group id as input
Return 1 if yes else 0

</> CopyGet raw version
php
  1. function _mymodulename_check_user_is_admin_of_group($uid, $gid) {
  2. $query = db_select('og_users_roles', 'og');
  3. $query->condition('gid', $gid, '=');
  4. $query->condition('uid', $uid, '=');
  5. $query->distinct();
  6. $query->fields('og', array('rid'));
  7. $result = $query->execute();
  8. $role_id = $result->fetchAssoc();
  9. if ($role_id['rid'] == 3) {
  10. return TRUE;
  11. } else
  12. return FALSE;
  13. }

Comments

Hans Rossel's picture

Better do if ($role_id['rid'] >= 3) {

because OG can create additional admin rid numbers when you allow to set permissions per group

pillaerds's picture

The function is OK to retrieve if someone is administrator member of a group. But the administrator (= author of the group node) is not in the og_users_roles table.
So I created another function to determine if someone is author of the group node.

  1. if (_check_user_is_adminmember_of_group($user->uid, $form['gid']['#value']) || _check_user_is_admin_of_group($user->uid, $form['gid']['#value']) ) {
  2. $form['field_expiration_date']['#access'] = TRUE;
  3. } else {
  4. $form['field_expiration_date']['#access'] = FALSE;
  5. }
  6.  
  7. and
  8.  
  9. function _check_user_is_adminmember_of_group($uid, $gid) {
  10. $query = db_select('og_users_roles', 'og');
  11. $query->condition('gid', $gid, '=');
  12. $query->condition('uid', $uid, '=');
  13. $query->distinct();
  14. $query->fields('og', array('rid'));
  15. $result = $query->execute();
  16. $role_id = $result->fetchAssoc();
  17. if ($role_id['rid'] >= 3) {
  18. return TRUE;
  19. }
  20. else {
  21. return FALSE;
  22. }
  23. }
  24.  
  25. function _check_user_is_admin_of_group($uid, $gid) {
  26. $query = db_select('node', 'n');
  27. $query->condition('nid', $gid, '=');
  28. $query->distinct();
  29. $query->fields('n', array('uid'));
  30. $result = $query->execute();
  31. $node_uid = $result->fetchAssoc();
  32. if ($node_uid['uid'] == $uid) {
  33. return TRUE;
  34. }
  35. else {
  36. return FALSE;
  37. }
  38. }
Rimu Atkinson's picture

The organic groups module has a function og_get_user_roles which returns an array of roles a given user has in the group. Look in the file called og.module to see more information about it.