You are here

function user_permissions_profile_permissions_form_submit in User Permissions 5

Same name and namespace in other branches
  1. 6 user_permissions.module \user_permissions_profile_permissions_form_submit()
  2. 7 user_permissions.module \user_permissions_profile_permissions_form_submit()

File

./user_permissions.module, line 79

Code

function user_permissions_profile_permissions_form_submit($form_id, $form_values) {

  // if the user has a role created by this module, then update the permissions for this role
  // otherwise create the new role with the new permissions if any permissions were given
  $rid = $form_values["rid"];
  $uid = (int) substr_replace($form_values["role_name"], "", 0, 11);
  $perms = array_filter($form_values[$rid]);
  if ($rid == DRUPAL_ANONYMOUS_RID) {
    if (!empty($perms)) {

      // create new role with the name $role_name
      $role_name = $form_values["role_name"];
      db_query("INSERT INTO {role} (name) VALUES ('%s')", $role_name);
      $rid = db_result(db_query("SELECT LAST_INSERT_ID()"));
      db_query("INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)", $uid, $rid);
      db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $rid, implode(', ', array_keys($perms)));
    }
  }
  else {
    db_query("DELETE FROM {permission} WHERE rid = %d", $rid);
    if (!empty($perms)) {
      db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $rid, implode(', ', array_keys($perms)));
    }
    else {

      // if $perms is empty, delete all permission/role information
      // related to this role to reduce database clutter
      db_query("DELETE FROM {users_roles} WHERE uid = %d and rid = %d", $uid, $rid);
      db_query("DELETE FROM {role} WHERE rid = %d", $rid);
    }
  }
  drupal_set_message(t('The changes have been saved.'));

  // Clear the cached pages and menus:
  menu_rebuild();
}