From d8291ca2303ea50dd8b99791696f26abe08c5ccb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ry=C5=A1ka?= Date: Mon, 23 Jun 2014 19:32:53 +0200 Subject: [PATCH 01/12] use path relative to plugin directory resolve coubeatczech/wordpress-crowd-plugin#1 --- Crowd-Login-Admin.php | 2 +- Crowd-Login.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Crowd-Login-Admin.php b/Crowd-Login-Admin.php index ddec6d6..5873b7a 100644 --- a/Crowd-Login-Admin.php +++ b/Crowd-Login-Admin.php @@ -203,4 +203,4 @@ } ?> - \ No newline at end of file + diff --git a/Crowd-Login.php b/Crowd-Login.php index ada0c4a..47bd868 100644 --- a/Crowd-Login.php +++ b/Crowd-Login.php @@ -8,8 +8,8 @@ Author URI: */ -require_once( WP_PLUGIN_DIR."/crowd-login/Crowd.php"); -require_once( ABSPATH . WPINC . '/registration.php'); +require_once(__DIR__ . "/Crowd.php"); +require_once(ABSPATH . WPINC . '/registration.php'); //Admin function crowd_menu() { From 79ef120e75e4614c39611b89d763bc71e85f2459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ry=C5=A1ka?= Date: Mon, 23 Jun 2014 20:26:25 +0200 Subject: [PATCH 02/12] do some basic reformattings & add indentation coubeatczech/wordpress-crowd-plugin#2 --- Crowd-Login-Admin.php | 305 +++++++++++++++++++++--------------------- 1 file changed, 152 insertions(+), 153 deletions(-) diff --git a/Crowd-Login-Admin.php b/Crowd-Login-Admin.php index 5873b7a..f52be06 100644 --- a/Crowd-Login-Admin.php +++ b/Crowd-Login-Admin.php @@ -1,68 +1,65 @@ - - - -
- -
-
-

Settings

-

These are rather important.

-

Application Name:
-
-*The application name given to you by your Crowd administrator. Example: crowd_app -

-

Application Password:
-
-*The application password given to you by your Crowd administrator. -

-

Crowd URL:
-
-*Example: https://crowd.example.local:8443/crowd -

- - -
-
-

Advanced

-

For the intrepid and daring among you.

-

Login mode:
- >
- >
->

-

For latter two options, create account as:
- -

-

-Security mode:
- >
- >
-

-
-
-
-

Test Settings

-

Use this form as a limited test for those settings you saved.* This will test user creation and group membership. If settings don't work, use another browser to try actually logging in. (unless you'd rather get locked out)

-

*You did save them, right?

-
-

Username:
- -

Password:
- -

- - -
-

-

Test Results:

- -

-
- -

Oddly, your version of PHP doesn't allow file_get_contents to use URLs. But even more oddly, your browser doesn't allow frames! I think it's time for you to consider leaving 1998 in the past.

"; ?> -
+ +
+
+

Settings

+

These are rather important.

+
+

+ Application Name:
+
+ *The application name given to you by your Crowd administrator. Example: crowd_app +

+
+
+

+ Application Password:
+
+ *The application password given to you by your Crowd administrator. +

+
+

+ Crowd URL:
+
+ *Example: https://crowd.example.local:8443/crowd +

+ + +
+
+

Advanced

+

For the intrepid and daring among you.

+

Login mode:
+ >
+ >
+ > +

+

For latter two options, create account as:
+ +

+

+ Security mode:
+ >
+ >
+

+
+
+
+

Test Settings

+

Use this form as a limited test for those settings you saved.* This will test user creation and group membership. If settings don't work, use another browser to try actually logging in. (unless you'd rather get locked out)

+

*You did save them, right?

+
+

+ Username:
+ +

+

Password:
+ +

+ + +
+

+

Test Results:

+ +

+
+ + Debug Info:
"; + echo "crowd_directory_type: ".get_option("crowd_directory_type")."/".$_POST['LDAP']."
"; + echo "crowd_login_mode: ".get_option("crowd_login_mode")."/".$_POST['mode']."
"; + echo "crowd_group: ".get_option("crowd_group")."/".$_POST['group_name']."
"; + echo "crowd_account_type: ".get_option("crowd_account_type")."/".$_POST['create_type']."

"; + } + ?> -*/ -//Echo settings -if($debug == "true") -{ -echo "

Debug Info:
"; -echo "crowd_directory_type: ".get_option("crowd_directory_type")."/".$_POST['LDAP']."
"; -echo "crowd_login_mode: ".get_option("crowd_login_mode")."/".$_POST['mode']."
"; -echo "crowd_group: ".get_option("crowd_group")."/".$_POST['group_name']."
"; -echo "crowd_account_type: ".get_option("crowd_account_type")."/".$_POST['create_type']."

"; -} -?> - - From 675a7d7084ac4979f156fa00f94a7ed648685df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ry=C5=A1ka?= Date: Mon, 23 Jun 2014 21:40:52 +0200 Subject: [PATCH 03/12] hide input elements in advanced > login mode Hide input elements in advanced > login mode, that are not used when the according radio button is select. coubeatczech/wordpress-crowd-plugin#2 --- Crowd-Login-Admin.php | 36 ++++++++++++++++++++----------- Crowd-Login.php | 9 ++++++++ crowd-login-admin.js | 50 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 crowd-login-admin.js diff --git a/Crowd-Login-Admin.php b/Crowd-Login-Admin.php index f52be06..653fc87 100644 --- a/Crowd-Login-Admin.php +++ b/Crowd-Login-Admin.php @@ -109,6 +109,10 @@ $crowd_account_type = get_option('crowd_account_type'); ?> +
@@ -140,19 +144,25 @@

Advanced

For the intrepid and daring among you.

-

Login mode:
- >
- >
- > -

-

For latter two options, create account as:
- +

Login mode: +

+
+ /> + +
+
+ /> + +
+
+ /> + +
+
+ /> + +
+

Security mode:
diff --git a/Crowd-Login.php b/Crowd-Login.php index 47bd868..97c57ee 100644 --- a/Crowd-Login.php +++ b/Crowd-Login.php @@ -8,6 +8,8 @@ Author URI: */ +define ("PLUGIN_ROOT", plugin_dir_url(__FILE__)); + require_once(__DIR__ . "/Crowd.php"); require_once(ABSPATH . WPINC . '/registration.php'); @@ -16,6 +18,13 @@ function crowd_menu() { include 'Crowd-Login-Admin.php'; } +function load_crowd_login_admin_js($hook) { + if ("settings_page_crowd-login" === $hook) { + wp_enqueue_script("crowd-login-admin-js", PLUGIN_ROOT . "/crowd-login-admin.js", array("jquery", "underscore")); + } +} +add_action("admin_enqueue_scripts", "load_crowd_login_admin_js"); + function crowd_admin_actions() { add_options_page("Crowd Login", "Crowd Login", 10, "crowd-login", "crowd_menu"); } diff --git a/crowd-login-admin.js b/crowd-login-admin.js new file mode 100644 index 0000000..00eba45 --- /dev/null +++ b/crowd-login-admin.js @@ -0,0 +1,50 @@ +function selectRole(checkedRole, selectName) { + function checked(role) { + return (role === checkedRole) ? 'checked="checked"' : "" ; + } + var selectNameHtml = (null == selectName) ? "crowd_account_type" : selectName; + var html = ' '; + return html; +} +function groupInput(group) { + return '

'; +} +function mapGroup() { + var positions = ["Administrator", "Editor", "Author", "Contributor", "Subscriber"]; + function oneLineHtml(position) { + var html = '
' + position + "
"; + return html; + } + var html = "
" + _.chain(positions).map(oneLineHtml).reduce(function(acc,b) { return acc + b}, "").value() + "
"; + return html; +} + +(function($){ + $(document).ready(function(){ + + $(".cl-mode").click(function(){ + $(".additional-input").remove(); + var id = $(this).attr("id"); + if (id === "cl-mode-create-all") { + var html = selectRole(crowdAccountType); + $("#cl-mode-create-all").parent().append(html); + } else if (id === "cl-mode-create-group") { + var html = selectRole(crowdAccountType); + var parent = $("#cl-mode-create-group").parent() + parent.append(html); + parent.append(groupInput(crowdGroup)); + } else if (id === "cl-mode-map-group") { + $("#cl-mode-map-group").parent().append(mapGroup()); + } + }); + + $(".cl-mode[checked='checked']").click(); + + }); +})(jQuery); From d6bb893e58b9f29b4002bdbd93a00a17855ca436 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ry=C5=A1ka?= Date: Tue, 24 Jun 2014 00:43:23 +0200 Subject: [PATCH 04/12] add mapping from crowd groups to wordpress roles coubeatczech/wordpress-crowd-plugin#2 --- Crowd-Login-Admin.php | 27 ++++++++++++++++++++++++++- crowd-login-admin.js | 4 ++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Crowd-Login-Admin.php b/Crowd-Login-Admin.php index 653fc87..de160ff 100644 --- a/Crowd-Login-Admin.php +++ b/Crowd-Login-Admin.php @@ -72,6 +72,14 @@ //If admin options updated (uses hidden field) if ($_POST['stage'] == 'process') { + + $roles = array("Administrator", "Editor", "Author", "Contributor", "Subscriber"); + $roles_and_values = array(); + foreach ($roles as $role) { + $roleValue = $_POST["cl-mapping-crowd-group-$role"]; + $roles_and_values[$role] = $roleValue; + } + update_option("crowd_wordpress_role_mappings", $roles_and_values); update_option('crowd_url', $_POST['crowd_url']); update_option('crowd_app_name', $_POST['crowd_app_name']); update_option('crowd_app_password', $_POST['crowd_app_password']); @@ -107,11 +115,28 @@ $crowd_login_mode = get_option('crowd_login_mode'); $crowd_group = get_option('crowd_group'); $crowd_account_type = get_option('crowd_account_type'); +$crowd_wordpress_role_mappings = get_option("crowd_wordpress_role_mappings"); ?>
@@ -159,7 +184,7 @@
- /> + />
diff --git a/crowd-login-admin.js b/crowd-login-admin.js index 00eba45..f36505b 100644 --- a/crowd-login-admin.js +++ b/crowd-login-admin.js @@ -16,9 +16,9 @@ function groupInput(group) { return '
'; } function mapGroup() { - var positions = ["Administrator", "Editor", "Author", "Contributor", "Subscriber"]; + var positions = _.keys(crowdWordpressRoleMappings); function oneLineHtml(position) { - var html = '
' + position + "
"; + var html = '
' + position + "
"; return html; } var html = "
" + _.chain(positions).map(oneLineHtml).reduce(function(acc,b) { return acc + b}, "").value() + "
"; From 610a3d7a21523f3dc9412e0e6a1a706305e90c02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ry=C5=A1ka?= Date: Tue, 24 Jun 2014 11:33:23 +0200 Subject: [PATCH 05/12] use the group mapping to roles during login coubeatczech/wordpress-crowd-plugin#2 --- Crowd-Login.php | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/Crowd-Login.php b/Crowd-Login.php index 97c57ee..325b1f1 100644 --- a/Crowd-Login.php +++ b/Crowd-Login.php @@ -38,6 +38,7 @@ function crowd_activation_hook() { add_option('crowd_security_mode', 'security_low'); add_option('crowd_login_mode', 'mode_normal'); add_option('crowd_account_type', 'Contributor'); + add_option("crowd_wordpress_role_mappings", array()); } // Reset Crowd instance and principal token @@ -100,14 +101,34 @@ function crowd_authenticate($user, $username, $password) { } $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_map_group": + $mappings = get_option("crowd_wordpress_role_mappings"); + $crowd_groups = get_crowd_groups($username)->string; + $role = NULL; + foreach ($mappings as $mapping_key => $mapping_value) { + if ($mapping_value === $crowd_groups) { + $role = $mapping_key; + } + } + if ($role != NULL) { + $new_user_id = crowd_create_wp_user($username, $role); + return new WP_User($new_user_id); + } else { + do_action("wp_login_failed", $username); + return new WP_Error('group not mapped', __("Crowd Login Error: Crowd group is not mapped.")); + } + break; + case 'mode_create_all': - $new_user_id = crowd_create_wp_user($username); + $new_user_id = crowd_create_wp_user($username, get_option('crowd_account_type')); if(!is_a($new_user_id, 'WP_Error')) { //It worked return new WP_User($new_user_id); @@ -119,7 +140,7 @@ function crowd_authenticate($user, $username, $password) { case 'mode_create_group': if(crowd_is_in_group($username)) { - $new_user_id = crowd_create_wp_user($username); + $new_user_id = crowd_create_wp_user($username, get_option('crowd_account_type')); if(!is_a($new_user_id, 'WP_Error')) { //It worked return new WP_User($new_user_id); @@ -177,6 +198,15 @@ function crowd_can_authenticate($username, $password) { return $princ_token; } +function get_crowd_groups($username) { + global $crowd; + if ($crowd == NULL) { + return NULL; + } + $groups = $crowd->findGroupMemberships($username); + return $groups; +} + function crowd_is_in_group($username) { global $crowd; $result = false; @@ -198,7 +228,7 @@ function crowd_is_in_group($username) { return $result; } -function crowd_create_wp_user($username) { +function crowd_create_wp_user($username, $role) { global $crowd, $princ_token; $result = 0; @@ -222,7 +252,7 @@ function crowd_create_wp_user($username) { 'display_name' => $person['givenName'] .' '. $person['sn'], 'first_name' => $person['givenName'], 'last_name' => $person['sn'], - 'role' => strtolower(get_option('crowd_account_type')) + 'role' => strtolower($role) // get_option('crowd_account_type')) ); $result = wp_insert_user($userData); From b55640320b7bcb456268af40c82e925fd099b512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ry=C5=A1ka?= Date: Tue, 24 Jun 2014 11:43:50 +0200 Subject: [PATCH 06/12] reuse the function `get_crowd_groups` coubeatczech/wordpress-crowd-plugin#2 --- Crowd-Login.php | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/Crowd-Login.php b/Crowd-Login.php index 325b1f1..15a1d66 100644 --- a/Crowd-Login.php +++ b/Crowd-Login.php @@ -208,23 +208,15 @@ function get_crowd_groups($username) { } function crowd_is_in_group($username) { - global $crowd; $result = false; + $crowd_group = get_option('crowd_group'); - // If we can't get a Crowd instance, fail - if ($crowd == NULL) { - return $result; - } - - $crowd_group = $get_option('crowd_group'); - - $groups = $crowd->findGroupMemberships($username); + $groups = get_crowd_groups($username); if ($groups == NULL) { return $result; } - $result = in_array($crowd_group, $groups); - + $result = $crowd_group === $groups->string; return $result; } From 05366bdb5bdf5bae162d7d395e8cdf8f2be87619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ry=C5=A1ka?= Date: Tue, 24 Jun 2014 11:48:03 +0200 Subject: [PATCH 07/12] hide group mapping inputs on focus out coubeatczech/wordpress-crowd-plugin#2 --- crowd-login-admin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crowd-login-admin.js b/crowd-login-admin.js index f36505b..d880a32 100644 --- a/crowd-login-admin.js +++ b/crowd-login-admin.js @@ -21,7 +21,7 @@ function mapGroup() { var html = '
' + position + "
"; return html; } - var html = "
" + _.chain(positions).map(oneLineHtml).reduce(function(acc,b) { return acc + b}, "").value() + "
"; + var html = '
' + _.chain(positions).map(oneLineHtml).reduce(function(acc,b) { return acc + b}, "").value() + "
"; return html; } From e6328f40373f36bf9c12227ce7970f0a72ea4a45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ry=C5=A1ka?= Date: Tue, 24 Jun 2014 11:53:03 +0200 Subject: [PATCH 08/12] change checked in option to selected coubeatczech/wordpress-crowd-plugin#2 --- crowd-login-admin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crowd-login-admin.js b/crowd-login-admin.js index d880a32..e6c9bea 100644 --- a/crowd-login-admin.js +++ b/crowd-login-admin.js @@ -1,6 +1,6 @@ function selectRole(checkedRole, selectName) { function checked(role) { - return (role === checkedRole) ? 'checked="checked"' : "" ; + return (role === checkedRole) ? 'selected="selected"' : "" ; } var selectNameHtml = (null == selectName) ? "crowd_account_type" : selectName; var html = '