summaryrefslogtreecommitdiff
path: root/Simple-UM-Login.php
blob: bd737113a53090cafa8465b706eaaf2c65fd9a75 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/* Plugin Name: Simple UM Login
 * Plugin URI: http://mckenzierobotics.org
 * Description:  Authenticates Wordpress usernames against UM.
 * Version: 1.4.0.5.1
 * Author: Luke Shumaker, based on work by Clifton H. Griffin II
 * Author URI: http://lukeshu.ath.cx
 */
require_once(dirname(__FILE__).'/umClient.php');
require_once( ABSPATH . WPINC . '/registration.php');

// Admin
function simpleum_menu() {
	include 'Simple-UM-Login-Admin.php';
}

function simpleum_admin_actions() {
    add_options_page("Simple UM Login", "Simple UM Login", 10, "simple-um-login", "simpleum_menu");
}

function simpleum_activation_hook() {
	// Store settings
	add_option("simpleum_url", "http://um.mydomain.local");

	// Version 1.3
	add_option("simpleum_login_mode", "mode_normal");
	add_option("simpleum_group", "");
	add_option("simpleum_account_type", "Contributor");

	// Version 1.3.0.2
	add_option("simpleum_security_mode", "security_low");
}

// Add the menu
add_action('admin_menu', 'simpleum_admin_actions');

// Add filter
add_filter('authenticate', 'sul_authenticate', 1, 3);

// Authenticate function
function sul_authenticate($user, $username, $password) {
	if ( is_a($user, 'WP_User') ) { return $user; }

	// Failed, should we let it continue to lower priority authenticate methods?
	if(get_option("simpleum_security_mode") == "security_high") {
		remove_filter('authenticate', 'wp_authenticate_username_password', 20, 3);
	}

	if ( empty($username) || empty($password) ) {
		$error = new WP_Error();

		if ( empty($username) )
			$error->add('empty_username', __('<strong>ERROR</strong>: The username field is empty.'));

		if ( empty($password) )
			$error->add('empty_password', __('<strong>ERROR</strong>: The password field is empty.'));

		return $error;
	}

	$uminfo = sul_get_user($username, $password);
	if (is_array($uminfo)) {
		$user = get_userdatabylogin($username);
		if ( !$user || (strtolower($user->user_login) != strtolower($username)) ) {
			// No existing WP user, can we create?
			switch(get_option('simpleum_login_mode')) {
			case "mode_create_all":
				$new_user_id = sul_create_wp_user($uminfo);
				if (!is_a($new_user_id, 'WP_Error')) {
					//It worked
					return new WP_User($new_user_id);
				} else {
					do_action( 'wp_login_failed', $username );
					return new WP_Error('invalid_username', __('<strong>Simple UM Login Error</strong>: UM credentials are correct and user creation is allowed but an error occurred creating the user in Wordpress. Actual WordPress error: '.$new_user_id->get_error_message()));
				}
				break;
			case "mode_create_group":
				if (sul_is_in_group($uminfo)) {
					$new_user_id = sul_create_wp_user($uminfo);
					if(!is_a($new_user_id, 'WP_Error')) {
						//It worked
						return new WP_User($new_user_id);
					} else {
						do_action( 'wp_login_failed', $username );
						return new WP_Error('invalid_username', __('<strong>Simple UM Login Error</strong>: UM credentials are correct and user creation is allowed and you are in the correct group but an error occurred creating the user in Wordpress. Actual WordPress error: '.$new_user_id->get_error_message()));
					}
				} else {
					do_action( 'wp_login_failed', $username );
					return new WP_Error('invalid_username', __('<strong>Simple UM Login Error</strong>: UM Login credentials are correct and user creation is allowed but UM user was not in correct UM group.'));
				}
				break;
			default:
				do_action( 'wp_login_failed', $username );
				return new WP_Error('invalid_username', __('<strong>Simple UM Login Error</strong>: Simple UM Login mode does not permit account creation.'));
			}
		} else {
			// Wordpress user exists, should we check group membership?
			if (get_option('simpleum_login_mode') == "mode_create_group") {
				if (sul_is_in_group($uminfo)) {
					return new WP_User($user->ID);
				} else {
					do_action( 'wp_login_failed', $username );
					return new WP_Error('invalid_username', __('<strong>Simple UM Login Error</strong>: UM credentials were correct but user is not in the correct group.'));
				}
			} else {
				// Otherwise, we're ready to return the user
				return new WP_User($user->ID);
			}
		}
	} else {
		return new WP_Error('invalid_username', __('<strong>Simple UM Login Error</strong>: Simple UM Login could not authenticate your credentials. The security settings do not permit trying the Wordpress user database as a fallback.'));
	}
}

function sul_get_user($username, $password) {
	$cookiejar = tempnam(dirname(__FILE__).'/tmp', 'jar');
	$umclient = new umClient(get_option('simpleum_url'), $cookiejar);
	$result = $umclient->get_userinfo($username, $password);
	unlink($cookiejar);
	return $result;
}

function sul_is_in_group($userinfo) {
	// TODO
	return true;
}

function sul_create_wp_user($userinfo) {
	$result = 0;

	$userData = array(
	                  'user_pass'     => microtime(),
	                  'user_login'    => $userinfo['username'],
	                  'user_nicename' => sanitize_title($userinfo['firstname'].' '.$userinfo['lastname']),
	                  'user_email'    => $userinfo['email'],
	                  'display_name'  => $userinfo['firstname'].' '.$userinfo['lastname'],
	                  'first_name'    => $userinfo['firstname'],
	                  'last_name'     => $userinfo['lastname'],
	                  'role'          => strtolower(get_option('simpleum_account_type'))
	                  );

	$result = wp_insert_user($userData);
	return $result;
}

//Temporary fix for e-mail exists bug
if ( !function_exists('get_user_by_email') ) :
/**
 * Retrieve user info by email.
 *
 * @since 2.5
 *
 * @param string $email User's email address
 * @return bool|object False on failure, User DB row object
 */
function get_user_by_email($email) {
	if(strlen($email) == 0 || empty($email) || $email == "" || strpos($email, "@") == false) {
		return false;
	} else {
		return get_user_by('email', $email);
	}
}
endif;

register_activation_hook( __FILE__, 'simpleum_activation_hook' );
?>