Exemplo n.º 1
0
function crowd_authenticate_soap($user, $username, $password, $crowd_config)
{
    global $crowd;
    try {
        $crowd = new Crowd($crowd_config);
    } catch (CrowdConnectionException $e) {
        $error = new WP_Error();
        $error->add('crowd_conn_error', $e->getMessage());
        return $error;
    }
    try {
        $app_token = $crowd->authenticateApplication();
    } catch (CrowdLoginException $e) {
        $crowd = NULL;
        echo $e->getMessage();
    }
    $auth_result = crowd_can_authenticate($username, $password);
    if ($auth_result == true && !is_a($auth_result, 'WP_Error')) {
        $user = get_userdatabylogin($username);
        if (!$user || strtolower($user->user_login) != strtolower($username)) {
            //No user, can we create?
            switch (get_option('crowd_login_mode')) {
                case 'mode_create_all':
                    $new_user_id = crowd_create_wp_user($username);
                    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>Crowd Login Error</strong>: Crowd 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 (crowd_is_in_group($username)) {
                        $new_user_id = crowd_create_wp_user($username);
                        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>Crowd Login Error</strong>: Crowd 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>Crowd Login Error</strong>: Crowd Login credentials are correct and user creation is allowed but Crowd user was not in the correct group.'));
                    }
                    break;
                default:
                    do_action('wp_login_failed', $username);
                    return new WP_Error('invalid_username', __('<strong>Crowd Login Error</strong>: Crowd Login mode does not permit account creation.'));
            }
        } else {
            //Wordpress user exists, should we check group membership?
            if (get_option('crowd_login_mode') == 'mode_create_group') {
                if (crowd_is_in_group($username)) {
                    return new WP_User($user->ID);
                } else {
                    do_action('wp_login_failed', $username);
                    return new WP_Error('invalid_username', __('<strong>Crowd Login Error</strong>: Crowd 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 {
        if (is_a($auth_result, 'WP_Error')) {
            return $auth_result;
        } else {
            return new WP_Error('invalid_username', __('<strong>Crowd Login Error</strong>: Crowd Login could not authenticate your credentials. The security settings do not permit trying the Wordpress user database as a fallback.'));
        }
    }
}
function crowd_authenticate($user, $username, $password)
{
    global $crowd;
    $crowd_url = get_option('crowd_url');
    $crowd_app_name = get_option('crowd_app_name');
    $crowd_app_password = get_option('crowd_app_password');
    $crowd_config = array('service_url' => $crowd_url . DIRECTORY_SEPARATOR . 'services' . DIRECTORY_SEPARATOR . 'SecurityServer?wsdl', 'app_name' => $crowd_app_name, 'app_credential' => $crowd_app_password);
    try {
        $crowd = new Crowd($crowd_config);
    } catch (CrowdConnectionException $e) {
        $error = new WP_Error();
        $error->add('crowd_conn_error', $e->getMessage());
        return $error;
    }
    try {
        $app_token = $crowd->authenticateApplication();
    } catch (CrowdLoginException $e) {
        $crowd = NULL;
        echo $e->getMessage();
    }
    if (is_a($user, 'WP_User')) {
        return $user;
    }
    //Failed, should we let it continue to lower priority authenticate methods?
    if (get_option('crowd_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;
    }
    $auth_result = crowd_can_authenticate($username, $password);
    if ($auth_result == true && !is_a($auth_result, 'WP_Error')) {
        $user = get_userdatabylogin($username);
        if (!$user || strtolower($user->user_login) != strtolower($username)) {
            //No user, can we create?
            switch (get_option('crowd_login_mode')) {
                case 'mode_create_all':
                    $new_user_id = crowd_create_wp_user($username);
                    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>Crowd Login Error</strong>: Crowd 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 (crowd_is_in_group($username)) {
                        $new_user_id = crowd_create_wp_user($username);
                        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>Crowd Login Error</strong>: Crowd 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>Crowd Login Error</strong>: Crowd Login credentials are correct and user creation is allowed but Crowd user was not in the correct group.'));
                    }
                    break;
                default:
                    do_action('wp_login_failed', $username);
                    return new WP_Error('invalid_username', __('<strong>Crowd Login Error</strong>: Crowd Login mode does not permit account creation.'));
            }
        } else {
            //Wordpress user exists, should we check group membership?
            if (get_option('crowd_login_mode') == 'mode_create_group') {
                if (crowd_is_in_group($username)) {
                    return new WP_User($user->ID);
                } else {
                    do_action('wp_login_failed', $username);
                    return new WP_Error('invalid_username', __('<strong>Crowd Login Error</strong>: Crowd 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 {
        if (is_a($auth_result, 'WP_Error')) {
            return $auth_result;
        } else {
            return new WP_Error('invalid_username', __('<strong>Crowd Login Error</strong>: Crowd Login could not authenticate your credentials. The security settings do not permit trying the Wordpress user database as a fallback.'));
        }
    }
}