You are here

function access_grant_form_submit in Access Control Kit 7

Form submission handler for access_grant_form().

See also

access_grant_form()

access_grant_form_validate()

File

./access_grants.admin.inc, line 625
Access grants administrative UI for the access control kit module.

Code

function access_grant_form_submit($form, &$form_state) {
  $grant = $form_state['grant'];
  entity_form_submit_build_entity('access_grant', $grant, $form, $form_state);
  $account = user_load($grant->uid);
  $role = user_role_load($grant->rid);
  $scheme = access_scheme_machine_name_load($grant->scheme);

  // Add the user to the role, if not already a member.
  if (empty($account->roles[$role->rid])) {
    $roles = $account->roles + array(
      $role->rid => $role->name,
    );
    $account->original = clone $account;
    user_save($account, array(
      'roles' => $roles,
    ));
  }

  // Save the grant.
  $status = access_grant_save($grant);

  // Report the change.
  $t_args = array(
    '%user' => format_username($account),
    '%role' => $role->name,
    '%scheme' => $scheme->name,
  );
  switch ($status) {
    case SAVED_NEW:
      drupal_set_message(t("Added %scheme for %user's access as %role.", $t_args));
      break;
    case SAVED_UPDATED:
      drupal_set_message(t("Updated %scheme for %user's access as %role.", $t_args));
      break;
  }
  watchdog('access', "Changed %scheme for %user as %role.", $t_args, WATCHDOG_NOTICE, l(t('edit'), 'admin/access/grant/' . $grant->gid . '/edit'));
}