You are here

function password_policy_save_policy in Password Policy 7

Same name and namespace in other branches
  1. 5 password_policy.module \password_policy_save_policy()

Saves a policy.

Parameters

array $policy: A policy array.

2 calls to password_policy_save_policy()
password_policy_features_rebuild in ./password_policy.features.inc
Implements hook_features_rebuild().
_password_policy_admin_form_save_policy in ./password_policy.admin.inc
Saves policy from submitted policy form.

File

./password_policy.module, line 1561
Allows enforcing restrictions on user passwords by defining policies.

Code

function password_policy_save_policy(array $policy) {
  if (isset($policy['pid']) && $policy['pid']) {
    $fields = array(
      'name' => $policy['name'],
      'description' => $policy['description'],
      'constraints' => serialize($policy['constraints']),
      'created' => isset($policy['created']) ? $policy['created'] : 0,
      'expiration' => !empty($policy['expiration']) ? $policy['expiration'] : 0,
      'warning' => str_replace(' ', '', $policy['warning']),
      'weight' => !empty($policy['weight']) ? $policy['weight'] : 0,
    );

    // On policy edit form we have no 'enabled' param, so modify update query.
    if (isset($policy['enabled'])) {
      $fields += array(
        'enabled' => $policy['enabled'],
      );
    }
    db_update('password_policy')
      ->fields($fields)
      ->condition('pid', $policy['pid'])
      ->execute();
    watchdog('password_policy', 'Policy %name updated.', array(
      '%name' => $policy['name'],
    ), WATCHDOG_NOTICE, l(t('edit'), 'admin/config/people/password_policy/' . $policy['pid'] . '/edit'));
    db_delete('password_policy_role')
      ->condition('pid', $policy['pid'])
      ->execute();
    db_delete('password_policy_excluded_authentication_modules')
      ->condition('pid', $policy['pid'])
      ->execute();
    $pid = $policy['pid'];
  }
  else {
    $pid = db_insert('password_policy')
      ->fields(array(
      'name' => $policy['name'],
      'description' => $policy['description'],
      'enabled' => $policy['enabled'],
      'constraints' => serialize($policy['constraints']),
      'created' => isset($policy['created']) ? $policy['created'] : 0,
      'expiration' => !empty($policy['expiration']) ? $policy['expiration'] : 0,
      'warning' => str_replace(' ', '', $policy['warning']),
      'weight' => !empty($policy['weight']) ? $policy['weight'] : 0,
    ))
      ->execute();
    watchdog('password_policy', 'New policy %name created.', array(
      '%name' => $policy['name'],
    ), WATCHDOG_NOTICE, l(t('edit'), 'admin/config/people/password_policy/' . $pid . '/edit'));
  }
  foreach (array_filter($policy['roles']) as $rid => $enabled) {
    db_insert('password_policy_role')
      ->fields(array(
      'pid' => $pid,
      'rid' => $rid,
    ))
      ->execute();
  }
  foreach (array_filter($policy['excluded_authentication_modules']) as $module => $excluded) {
    db_insert('password_policy_excluded_authentication_modules')
      ->fields(array(
      'pid' => $pid,
      'module' => $module,
    ))
      ->execute();
  }
}