summaryrefslogtreecommitdiff
path: root/drupalauth/pglogin.module
blob: b78fc1da9dafd4dd0265de9ee7796ce0aac8a1a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
// $Id$

/**
 * @file
 * Lets users log in with pg community login system
 */


/**
 * Implementation of hook_info().
 */
function pglogin_info($field = 0) {
  $info['name'] = 'pglogin';

  if ($field) {
    return $info[$field];
  }
  else {
    return $info;
  }
}

/**
 * Implementation of hook_auth().
 */
function pglogin_auth($username, $password, $server = FALSE) {
  // disallow empty password, just in case
  if (empty($password)) {
    return false;
  }

  // always lowercase usernames so we don't get weird duplicates
  $username = strtolower($username);

  // connect to the pg database to validate login
  $db = @pg_pconnect("host=wwwmaster.postgresql.org dbname=186_www user=auth_svc password=veryverysecretreplacethis sslmode=require");
  if (!$db) {
    watchdog('error', 'Failed to connect to community login database!');
    return false;
  }

  $rs = @pg_query_params($db, 'SELECT * FROM community_login($1, $2)', array($username, $password));
  if (!$rs) {
    watchdog('error', 'Failed to query community login system!');
    @pg_close($db);
    return false;
  }

  if (@pg_num_rows($rs) != 1) {
    watchdog('user', t('User %user not present in community login system.', array('%user' => $username)));
    @pg_free_result($rs);
    @pg_close($db);
    return false;
  }
  
  $mail = '';
  if (@pg_fetch_result($rs, 0, 'success') == "1") {
    $mail = @pg_fetch_result($rs, 0, 'email');
  }
  else {
    watchdog('user', t('Community login failed for user %user.', array('%user' => $username)));
    @pg_free_result($rs);
    @pg_close($db);
    return false;
  }

  @pg_close($db);

  // Login successful. See if we need to store a copy of user info in drupal db
  $u = user_load(array('name' => $username));
  if (!isset($u->uid)) {
    // User not present in drupal - save stub. Set password to something very random.
    $u = user_save('', array (
      'name' => $username,
      'pass' => user_password(42),
      'mail' => $mail,
      'init' => $mail, // not sure why, but ldap does it so...
      'status' => 1));
     watchdog('user', t('Added record for community user %user', array('%user' => $username)));
  }
  else {
     // User is present, check if email has changed
     if ($u->mail != $mail) {
       $old = $u->mail;
       user_save($u, array( 'mail' => $mail));
       watchdog('user', t('User %user changed email from %old to %new',
         array('%user' => $username, '%old' => $old, '%new' => $mail)));
     }
  }  
  return true;
}


/*
 * Get rid of the send-me-a-password link.
 */
function pglogin_form_alter($form_id, &$form) {
  if ($form_id == 'user_login_block') {
    unset($form['links']);
  }
}