Skip to content

Commit 0a59f96

Browse files
committed
Added functionality for Active Directory groups to be able to have virtual folders created for them along with associated configuration options.
1 parent f6323e8 commit 0a59f96

File tree

2 files changed

+120
-3
lines changed

2 files changed

+120
-3
lines changed

configuration.example.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,42 @@
172172
'public_keys' => [],
173173
];
174174

175+
// If automatic groups mode is disabled, then you have to manually add the allowed groups into $allowed_groups down below:
176+
// If enabled, then any groups you are a memberof will automatically be added in using the template below.
177+
$auto_groups_mode = false;
178+
179+
$auto_groups_mode_virtual_folder_template = [
180+
[
181+
//"id" => 0,
182+
"name" => "groups-#GROUP#",
183+
"mapped_path" => 'C:\groups\#GROUP#',
184+
//"used_quota_size" => 0,
185+
//"used_quota_files" => 0,
186+
//"last_quota_update" => 0,
187+
"virtual_path" => "/groups/#GROUP#",
188+
"quota_size" => -1,
189+
"quota_files" => -1
190+
]
191+
];
192+
193+
// List of groups where a virtual folder will be created and associated with any group members:
194+
$allowed_groups = [];
195+
196+
// Note: that for each group you need to provide a nested array (this allows for more than one virtual folder per group to be defined):
197+
$allowed_groups['example'] = [
198+
[
199+
//"id" => 0,
200+
"name" => "groups-#GROUP#",
201+
"mapped_path" => 'C:\groups\#GROUP#',
202+
//"used_quota_size" => 0,
203+
//"used_quota_files" => 0,
204+
//"last_quota_update" => 0,
205+
"virtual_path" => "/groups/#GROUP#",
206+
"quota_size" => -1,
207+
"quota_files" => -1
208+
]
209+
];
210+
175211
// Add a minimum length for usernames (set to 0 to ignore length):
176212
$username_minimum_length = 4;
177213

functions.php

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ function authenticateUser() {
9292

9393
logMessage('Before authentication attempt for: ' . $data['username']);
9494
if ($connection->auth()->attempt($userDistinguishedName, $data['password'])) {
95+
$groups = getUserGroups($user);
96+
9597
// User has been successfully authenticated.
9698
logMessage('After authentication attempt for: ' . $data['username'] . ' (success!)');
97-
$output = createResponseObject($connectionName, $data['username']);
99+
$output = createResponseObject($connectionName, $data['username'], $groups);
98100

99101
logMessage('Disconnecting from ' . $connectionName);
100102
$connection->disconnect();
@@ -128,8 +130,15 @@ function authenticateUser() {
128130
denyRequest();
129131
}
130132

131-
function createResponseObject($connectionName, $username) {
132-
global $home_directories, $virtual_folders, $default_output_object, $connection_output_objects, $user_output_objects;
133+
function createResponseObject($connectionName, $username, $groups = []) {
134+
global $home_directories,
135+
$virtual_folders,
136+
$default_output_object,
137+
$connection_output_objects,
138+
$user_output_objects,
139+
$allowed_groups,
140+
$auto_groups_mode,
141+
$auto_groups_mode_virtual_folder_template;
133142

134143
$userHomeDirectory = str_replace('#USERNAME#', $username, $home_directories[$connectionName]);
135144

@@ -159,6 +168,45 @@ function createResponseObject($connectionName, $username) {
159168
}
160169
}
161170

171+
// Support for automatically creating virtual folders for allowed groups the user may be a member of:
172+
if (!empty($groups)) {
173+
if ($auto_groups_mode) {
174+
foreach($groups as $group) {
175+
if (isset($auto_groups_mode_virtual_folder_template)) {
176+
foreach($auto_groups_mode_virtual_folder_template as $virtual_group_folder) {
177+
178+
$virtual_group_folder['name'] = str_replace('#GROUP#', $group, $virtual_group_folder['name']);
179+
$virtual_group_folder['mapped_path'] = str_replace('#GROUP#', $group, $virtual_group_folder['mapped_path']);
180+
$virtual_group_folder['virtual_path'] = str_replace('#GROUP#', $group, $virtual_group_folder['virtual_path']);
181+
182+
$output['virtual_folders'][] = $virtual_group_folder;
183+
184+
// Defaulting to open permissions on the virtual group folder:
185+
$output['permissions'][$virtual_group_folder['virtual_path']] = ["*"];
186+
}
187+
}
188+
}
189+
} else {
190+
if (!empty($allowed_groups)) {
191+
foreach($groups as $group) {
192+
if (isset($allowed_groups[$group])) {
193+
foreach($allowed_groups[$group] as $virtual_group_folder) {
194+
195+
$virtual_group_folder['name'] = str_replace('#GROUP#', $group, $virtual_group_folder['name']);
196+
$virtual_group_folder['mapped_path'] = str_replace('#GROUP#', $group, $virtual_group_folder['mapped_path']);
197+
$virtual_group_folder['virtual_path'] = str_replace('#GROUP#', $group, $virtual_group_folder['virtual_path']);
198+
199+
$output['virtual_folders'][] = $virtual_group_folder;
200+
201+
// Defaulting to open permissions on the virtual group folder:
202+
$output['permissions'][$virtual_group_folder['virtual_path']] = ["*"];
203+
}
204+
}
205+
}
206+
}
207+
}
208+
}
209+
162210
return $output;
163211
}
164212

@@ -273,6 +321,39 @@ function homeDirectoryEntriesExist() {
273321
}
274322
}
275323

324+
function getUserGroups($user) {
325+
$groups = array();
326+
327+
if (isset($user['memberof'])) {
328+
if (isset($user['memberof']['count'])) {
329+
unset($user['memberof']['count']);
330+
}
331+
332+
foreach($user['memberof'] as $group) {
333+
$group = str_replace('CN=', '', $group);
334+
335+
$endGroupName = strpos($group, ',OU');
336+
337+
$group = substr($group, 0, $endGroupName);
338+
339+
// Perform uniformity transformations:
340+
$group = strtolower($group);
341+
$group = str_replace('#', '', $group);
342+
$group = str_replace('(', '', $group);
343+
$group = str_replace(')', '', $group);
344+
$group = str_replace(' ', '-', $group);
345+
$group = str_replace('&', 'and', $group);
346+
$group = preg_replace('/[^a-zA-Z0-9\-\._]/','', $group);
347+
348+
if (!empty($group)) {
349+
$groups[] = $group;
350+
}
351+
}
352+
}
353+
354+
return $groups;
355+
}
356+
276357
function logMessage($message, $extra = []) {
277358
if (defined('_SFTPGO_LOG') && _SFTPGO_LOG === true) {
278359
global $log;

0 commit comments

Comments
 (0)