You are here

function subuser_user in Subuser 6

Same name and namespace in other branches
  1. 5 subuser.module \subuser_user()

Implementation of hook_user().

File

./subuser.module, line 262
Allows users of a particular role to create sub user account in another role.

Code

function subuser_user($op, &$edit, &$account, $category = NULL) {
  global $user;
  switch ($op) {
    case 'form':

      // Allow users with sufficient permission to limit the number of subusers on a per-user basis.
      if (user_access('administer subuser settings')) {
        $user_limit = db_result(db_query("SELECT subuser_limit FROM {subuser_limit} WHERE uid=%d", $account->uid));
        $form['subuser_limit'] = array(
          '#type' => 'textfield',
          '#title' => t('Subuser limit'),
          '#description' => t('Enter the maximum number of subusers this user can create'),
          '#default_value' => $user_limit ? $user_limit : variable_get('subuser_limit', NULL),
          '#size' => 5,
          '#maxlength' => 4,
        );
        return $form;
      }
      break;
    case 'insert':
      if (isset($edit['origin']) && $edit['origin'] == 'subuser') {
        db_query('INSERT INTO {subuser_relationship} (parent_id, uid)
                  VALUES (%d, %d)', $edit['parent_user'], $account->uid);
      }
      break;
    case 'update':

      // Check and see if we even need to bother ourselves with subusers at all.
      $subusers = subuser_get_subusers($account->uid);
      if (count($subusers) > 0) {

        // If we're blocking a user, get the subusers so we can block them too.
        $block_subusers = variable_get('subuser_children_block', NULL);
        if ($account->status == 1 && $block_subusers == 1 && isset($edit['status']) && $edit['status'] == 0) {
          subuser_block_subusers($subusers);
        }

        // If we're unblocking a user, get the subusers so we can unblock them too.
        $unblock_subusers = variable_get('subuser_children_unblock', NULL);
        if ($account->status == 0 && $unblock_subusers == 1 && isset($edit['status']) && $edit['status'] == 1) {
          subuser_unblock_subusers($subusers);
        }
      }

      // Allow role changes to cascade down to subusers.
      $role_change_subusers = variable_get('subuser_children_roles', NULL);
      if ($role_change_subusers == 1) {
        $new_roles = $edit['roles'];
        $old_roles = $account->roles;

        // Compare the count of old and new roles, > 1 means roles added, < 0 means they've been removed.
        $role_change = count($new_roles) - count($old_roles);
        if ($role_change !== 0) {
          if (is_array($new_roles) && is_array($old_roles)) {

            // Are we exempting a role from cascading? If so pull it out of new & old roles.
            $rid = variable_get('subuser_cascade_exempt_rid', NULL);
            if (isset($rid)) {
              unset($old_roles[$rid]);
              unset($new_roles[$rid]);
            }
            $old_keys = array_keys($old_roles);
            $new_keys = array_keys($new_roles);

            // We need to do the diff backwards depending on if we have a role addition or subtraction.
            $diff = $role_change > 0 ? array_diff($new_keys, $old_keys) : array_diff($old_keys, $new_keys);
            $op = $role_change > 0 ? 'add_role' : 'remove_role';
            subuser_tweak_roles_of_subusers($subusers, $op, $diff);
          }
        }
      }

      // Check to see if this user has a subuser limit set (and it's not the default value).
      if (isset($edit['subuser_limit']) && $edit['subuser_limit'] !== variable_get('subuser_limit', NULL)) {
        db_query("INSERT INTO {subuser_limit} (uid, subuser_limit) VALUES (%d, %d) ON DUPLICATE KEY UPDATE subuser_limit = %d", $account->uid, $edit['subuser_limit'], $edit['subuser_limit']);
        unset($edit['subuser_limit']);
      }
      break;
    case 'delete':
      $subusers = subuser_get_subusers($account->uid);

      // If there are subusers for this account, figure out what to do with them.
      if (count($subusers) > 0) {
        $subuser_orphaned_children = variable_get('subuser_orphaned_children', 0);
        if ($subuser_orphaned_children == 1) {

          // Block the subuser accounts too.
          subuser_block_subusers($subusers);
        }
        if ($subuser_orphaned_children == 2) {

          // Delete the subuser accounts too.
          foreach ($subusers as $uid) {
            user_delete(NULL, $uid);
          }
        }
      }
      db_query('DELETE FROM {subuser_relationship} WHERE uid = %d', $account->uid);
      break;
    case 'view':
      $parent = db_fetch_object(db_query('SELECT parent_id
                                          FROM {subuser_relationship}
                                          WHERE uid = %d', $account->uid));
      if ($parent) {

        // Display link to parent user if available.
        $parent = user_load($parent->parent_id);
        $account->content['subuser_parent'] = array(
          '#type' => 'user_profile_item',
          '#title' => t(SUBUSER_PARENT),
          '#value' => theme('username', $parent),
          '#weight' => 10,
        );
      }

      // The parent user should either have access to create subusers, or have
      // existing subusers.
      $create = subuser_user_create_access($account);

      // Check for a per-user limit on the number of subusers that this user can create.
      $limit = subuser_get_subuser_limit($account->uid);
      $current_subuser_count = count(subuser_get_subusers($account->uid));

      // The user is over the limit if they do have a limit and they have used it up.  They are not over the limit if the limit = 0 (unlimited).
      $over_limit = $limit - $current_subuser_count > 0 || $limit == 0 ? FALSE : TRUE;
      $administer = user_access('administer users') || user_access('administer subusers') && $account->uid == $user->uid;
      $show_administer_link = variable_get('subuser_show_admin', 1);
      $view = views_get_view('subusers');
      if ($create || isset($view->results) && $view->results) {
        $view = views_embed_view('subusers');
        if ($create && (!$over_limit || user_access('bypass subuser limit'))) {
          $links[] = l(t(SUBUSER_CREATE), 'user/' . $account->uid . '/subuser/create');
        }
        if ($administer && $current_subuser_count >= 1 && $show_administer_link) {
          $links[] = l(t(SUBUSER_ADMINISTER), 'admin/user/user');
        }
        if (isset($links)) {
          $output = implode(' | ', $links);
        }
        $output .= '<br />' . $view;
        $account->content['subuser'] = array(
          '#type' => 'user_profile_category',
          '#title' => t(SUBUSER_LIST),
          '#weight' => 11,
        );
        $account->content['subuser']['list'] = array(
          '#type' => 'user_profile_item',
          '#value' => $output,
          '#weight' => 11,
        );
      }
      break;
  }
}