-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic-add-users.php
executable file
·177 lines (165 loc) · 4.88 KB
/
dynamic-add-users.php
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
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* Dynamic Add Users replaces the 'Add User' screen with a dynamic directory search.
*
* This plugin also allows searching for groups in a directory and bulk-adding
* group members to a site as well as synchronizing site-roles based on group-membership.
* It provides several services that other plugins can use to look up users and
* groups and apply roles to users.
*
* @link https://github.com/middlebury/dynamic-add-users
* @since 1.0.0
* @package Dynamic_Add_Users
*
* @wordpress-plugin
* Plugin Name: Dynamic Add Users
* Plugin URI: https://github.com/middlebury/dynamic-add-users
* Description: Replaces the 'Add User' screen with a dynamic search for users and groups.
* Version: 1.2.4
* Author: Middlebury College
* Author URI: https://github.com/middlebury/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: dynamic-add-users
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Currently plugin version.
* Start at version 1.0.0 and use SemVer - https://semver.org
* Rename this for your plugin and update it as you release new versions.
*/
define( 'DYNAMIC_ADD_USERS_VERSION', '1.2.4' );
require_once( __DIR__ . '/vendor/autoload.php' );
use DynamicAddUsers\DynamicAddUsersPlugin;
/**
* Answer the plugin instance.
*
* @return \DynamicAddUsers\DynamicAddUsersPluginInterface
*/
function dynamic_add_users() {
static $plugin;
if (!isset($plugin)) {
$plugin = new DynamicAddUsersPlugin();
}
return $plugin;
}
// Initialize the plugin instance.
dynamic_add_users();
/*******************************************************
* Actions and Filters
*******************************************************/
/**
* Action: Make changes to a newly created user account.
*
* This plugin will call
* doAction('dynamic_add_users__update_user_on_create', $user)
* when a user account is first created. Below is an example implementation.
*
* @param WP_User $user
*/
function dynamic_add_users__update_user_on_create(WP_User $user) {
/*
// Example: Set the authentication type for new accounts.
if (defined('CAMPUSPRESS_AUTH_SHIBBOLETH')) {
update_usermeta($user->ID, 'shibboleth_account', true);
}
*/
}
/**
* Action: Set/unset roles and capabilities for the user based on groups.
*
* This plugin will call
* doAction('dynamic_add_users__update_user_on_login', $user, $groups)
* when a user logs in. Below is an example implementation.
*
* @param WP_User $user
* @param array $groups
* An array of group-id => group-display-name.
*/
function dynamic_add_users__update_user_on_login(WP_User $user, array $groups) {
/*
// Example: Let institution users do something.
if (in_array('CN=institution,OU=General,OU=Groups,DC=middlebury,DC=edu', array_keys($groups))) {
$user->add_cap('middlebury_custom_capability');
}
// For all other users disallow this capability.
else {
$user->remove_cap('middlebury_custom_capability');
}
*/
}
/**
* Filter: Filter directory results when searching for users.
*
* Each match is an array like:
*
* [
* 'user_login' => '',
* 'user_email' => '',
* 'user_nicename' => '',
* 'display_name' => '',
* ]
*
* This plugin will call
* apply_filters('dynamic_add_users__filter_user_matches', $matches)
* when searches against the directory are run. Below is an example
* implementation.
*
* @param array $matches
* @return array The filtered matches.
*/
function dynamic_add_users__filter_user_matches($matches) {
/*
// Example: Filter out accounts prefixed with 'guest_'.
$results = [];
foreach ($matches as $match) {
if (!preg_match('/^guest_[a-z0-9]{31,34}$/i', $match['user_login'])) {
$results[] = $match;
}
}
return $results;
*/
}
/**
* Filter: Filter directory results when searching for groups.
*
* Each matches is an array of group ID => display name. Keys and values depend
* on the directory implementation and should not have their format assumed.
* Examples:
*
* [
* '100' => 'All Students',
* '5' => 'Faculty',
* ]
*
* or
*
* [
* 'cn=All Students,OU=Groups,DC=middlebury,DC=edu' => 'All Students',
* 'cn=Faculty,OU=Groups,DC=middlebury,DC=edu' => 'Faculty',
* ]
*
* This plugin will call
* apply_filters('dynamic_add_users__filter_group_matches', $matches)
* when searches against the directory are run. Below is an example
* implementation.
*
* @param array $matches
* @return array The filtered matches.
*/
function dynamic_add_users__filter_group_matches($matches) {
/*
// Example: Filter out groups prefixed with 'xx' or 'zz'.
$results = [];
foreach ($matches as $id => $displayName) {
if (!preg_match('/^(xx|zz).+$/i', $displayName)) {
$results[$id] = $displayName;
}
}
return $results;
*/
}