You are here

function userprotect_up_add_validate in User protect 7

Same name and namespace in other branches
  1. 5 userprotect.module \userprotect_up_add_validate()
  2. 6 userprotect.module \userprotect_up_add_validate()

Custom validation function for adding a user for protection.

1 string reference to 'userprotect_up_add_validate'
userprotect_protections_bypass in ./userprotect.admin.inc
Helper function. Builds tables for protected users and admin bypass.

File

./userprotect.admin.inc, line 188
Administration functions for userprotect module.

Code

function userprotect_up_add_validate($form, &$form_state) {

  // If a user has been submitted.
  if ($username = $form['#value']) {
    $type = $form['#userprotect_type'];

    // If the user is valid, and they are not already being protected...
    if ($uid = db_query("SELECT uid FROM {users} WHERE name = :name", array(
      ':name' => $username,
    ))
      ->fetchField()) {
      if (!db_query("SELECT uid FROM {userprotect} WHERE uid = :uid AND up_type = :up_type", array(
        ':uid' => $uid,
        ':up_type' => $type,
      ))
        ->fetchField()) {
        if ($uid != 1 && $type == 'admin' && !db_query("SELECT ur.uid FROM {users_roles} ur INNER JOIN {role_permission} rp ON ur.rid = rp.rid WHERE rp.permission = :permission AND ur.uid = :uid", array(
          ':permission' => 'administer users',
          ':uid' => $uid,
        ))
          ->fetchField()) {
          form_set_error('up_add', t('%user does not have user administration privileges.', array(
            '%user' => $username,
          )));
        }
        else {

          // Transform the username into a uid.
          form_set_value($form, $uid, $form_state);
        }
      }
      else {
        form_set_error('up_add', t('%user is already on this list.', array(
          '%user' => $username,
        )));
      }
    }
    else {
      form_set_error('up_add', t('The username is invalid.'));
    }
  }
}