Skip to content

Commit b3e418c

Browse files
authored
Account creation (ltb-project#225)
* First implementaton of creation page * Doc for entry creation * Delete entry * Doc for delete * Message for create/delete results * change error codes for delete * Modal to confirm entry deletion * DN escaping and encoding * Small typo in HTML code * Improve messages for audit log * Use macro system also for update
1 parent 73dd98e commit b3e418c

25 files changed

+561
-21
lines changed

conf/config.inc.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,20 @@
180180

181181
$use_update = true;
182182
$update_items = array('firstname', 'lastname', 'title', 'businesscategory', 'employeenumber', 'employeetype', 'mail', 'mailquota', 'phone', 'mobile', 'fax', 'postaladdress', 'street', 'postalcode', 'l', 'state', 'organizationalunit', 'organization', 'manager', 'secretary');
183+
$update_items_macros = array('fullname' => '%firstname% %lastname%');
183184

184185
$use_rename = true;
185186
$rename_items = array('identifier');
186187

188+
$use_create = true;
189+
$create_items = array('identifier', 'firstname', 'lastname', 'mail');
190+
$create_objectclass = array('top', 'person', 'organizationalPerson', 'inetOrgPerson');
191+
$create_dn_items = array('identifier');
192+
$create_base = $ldap_user_base;
193+
$create_items_macros = array('fullname' => '%firstname% %lastname%');
194+
195+
$use_delete = true;
196+
187197
# Local password policy
188198
# This is applied before directory password policy
189199
# Minimal length
@@ -346,6 +356,12 @@
346356
#$display_prehook_updatevalidity_error = true;
347357
#$ignore_prehook_updatevalidity_error = true;
348358

359+
## Delete
360+
361+
#$prehook_delete = "/usr/share/service-desk/prehook_delete.sh";
362+
#$display_prehook_delete_error = true;
363+
#$ignore_prehook_delete_error = true;
364+
349365
### Posthooks
350366

351367
# The posthook is only launched if the action was successful
@@ -388,6 +404,11 @@
388404
#$posthook_updatevalidity = "/usr/share/service-desk/posthook_updatevalidity.sh";
389405
#$display_posthook_updatevalidity_error = true;
390406

407+
## Delete
408+
409+
#$posthook_delete = "/usr/share/service-desk/posthook_delete.sh";
410+
#$display_posthook_delete_error = true;
411+
391412
# The name of an HTTP Header that may hold a reference to an extra config file to include.
392413
#$header_name_extra_config="SSP-Extra-Config";
393414

docs/createentry.rst

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Create entry
2+
============
3+
4+
This allows to create a new account in the directory.
5+
6+
Disable or enable feature
7+
-------------------------
8+
9+
If feature is enabled, a new button is shown in the menu.
10+
To disable it:
11+
12+
.. code-block:: php
13+
14+
$use_create = false;
15+
16+
Items
17+
-----
18+
19+
You can choose which items will be asked for the entry creation:
20+
21+
.. code-block:: php
22+
23+
$create_items = array('firstname', 'lastname', 'title', 'businesscategory', 'mail');
24+
25+
DN
26+
--
27+
28+
Choose which items will be used to compute the DN (RDN):
29+
30+
.. code-block:: php
31+
32+
$create_dn_items = array('identifier');
33+
34+
Set the branch where entries are created (by default this is the user search base):
35+
36+
.. code-block:: php
37+
38+
$create_base = "ou=service,ou=users,dc=example,dc=com";
39+
40+
41+
Object classes
42+
--------------
43+
44+
Set which object classes are used to create the entry:
45+
46+
.. code-block:: php
47+
48+
$create_objectclass = array('top', 'person', 'organizationalPerson', 'inetOrgPerson');
49+
50+
Macros
51+
------
52+
53+
You may need to create additional attributes based on submitted items.
54+
This is possible by defining a macro for the corresponding item:
55+
56+
.. code-block:: php
57+
58+
$create_items_macros = array('fullname' => '%firstname% %lastname%');

docs/deleteentry.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Deleteentry
2+
============
3+
4+
Disable or enable feature
5+
-------------------------
6+
7+
If feature is enabled, a delete button is shown on entry display page.
8+
To disable it:
9+
10+
.. code-block:: php
11+
12+
$use_delete = false;

docs/hook.rst

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ Account disable
141141

142142
The script is called with one parameter: login.
143143

144-
Define prehook or posthook script (and disable the feature):
144+
Define prehook or posthook script (and enable the feature):
145145

146146
.. code-block:: php
147147
@@ -166,7 +166,7 @@ Update validity dates
166166

167167
The script is called with one parameter: login.
168168

169-
Define prehook or posthook script (and updatevalidity the feature):
169+
Define prehook or posthook script (and enable the feature):
170170

171171
.. code-block:: php
172172
@@ -185,3 +185,28 @@ To ignore prehook error:
185185
.. code-block:: php
186186
187187
$ignore_prehook_updatevalidity_error = true;
188+
189+
Delete
190+
------
191+
192+
The script is called with one parameter: login.
193+
194+
Define prehook or posthook script (and enable the feature):
195+
196+
.. code-block:: php
197+
198+
$prehook_delete = "/usr/share/service-desk/prehook_delete.sh";
199+
$posthook_delete = "/usr/share/service-desk/posthook_delete.sh";
200+
201+
To display hook error:
202+
203+
.. code-block:: php
204+
205+
$display_prehook_delete_error = true;
206+
$display_posthook_delete_error = true;
207+
208+
To ignore prehook error:
209+
210+
.. code-block:: php
211+
212+
$ignore_prehook_delete_error = true;

docs/index.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@ LDAP Tool Box Service Desk documentation
3232
dashboards.rst
3333
configuration-mail.rst
3434
audit.rst
35-
updateentry.rst
35+
createentry.rst
3636
renameentry.rst
37+
updateentry.rst
38+
deleteentry.rst

docs/updateentry.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,13 @@ You can choose which items will be available for the update:
2121
$update_items = array('firstname', 'lastname', 'title', 'businesscategory', 'employeenumber', 'employeetype', 'mail', 'mailquota', 'phone', 'mobile', 'fax', 'postaladdress', 'street', 'postalcode', 'l', 'state', 'organizationalunit', 'organization', 'manager', 'secretary');
2222
2323
.. tip:: Other items will be read-only if they are listed in display items
24+
25+
Macros
26+
------
27+
28+
You may need to update additional attributes based on submitted items.
29+
This is possible by defining a macro for the corresponding item:
30+
31+
.. code-block:: php
32+
33+
$update_items_macros = array('fullname' => '%firstname% %lastname%');

htdocs/create.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
/*
3+
* Create an entry
4+
*/
5+
6+
$result = "";
7+
$dn = "";
8+
$entry = "";
9+
$action = "displayform";
10+
$result = "";
11+
12+
if (isset($_POST["action"]) and $_POST["action"]) {
13+
$action = $_POST["action"];
14+
}
15+
16+
if ($result === "") {
17+
18+
require_once("../conf/config.inc.php");
19+
require __DIR__ . '/../vendor/autoload.php';
20+
require_once("../lib/date.inc.php");
21+
22+
# Connect to LDAP
23+
$ldap_connection = $ldapInstance->connect();
24+
25+
$ldap = $ldap_connection[0];
26+
$result = $ldap_connection[1];
27+
28+
if ($ldap) {
29+
30+
# Create entry
31+
if ($action == "createentry") {
32+
33+
# Get all data
34+
$create_attributes = array();
35+
foreach ($create_items as $item) {
36+
$values = array();
37+
$item_keys = preg_grep("/^$item(\d+)$/", array_keys($_POST));
38+
foreach ($item_keys as $item_key) {
39+
if (isset($_POST[$item_key]) and !empty($_POST[$item_key])) {
40+
$value = $_POST[$item_key];
41+
if ( $attributes_map[$item]['type'] == "date" || $attributes_map[$item]['type'] == "ad_date" ) {
42+
$value = $directory->getLdapDate(new DateTime($_POST[$item_key]));
43+
}
44+
$values[] = $value;
45+
}
46+
}
47+
48+
$create_attributes[ $attributes_map[$item]['attribute'] ] = $values;
49+
}
50+
51+
$create_attributes['objectclass'] = $create_objectclass;
52+
53+
$dn = "";
54+
55+
foreach ($create_dn_items as $dn_item) {
56+
$attribute = $attributes_map[$dn_item]['attribute'];
57+
if ($dn) { $dn .= "+"; }
58+
$dn .= $attribute . "=" . ldap_escape($create_attributes[$attribute][0], "", LDAP_ESCAPE_DN);
59+
}
60+
61+
$dn .= "," . $create_base;
62+
63+
# Use macros
64+
foreach ($create_items_macros as $item => $macro) {
65+
$value = preg_replace_callback('/%(\w+)%/',
66+
function ($matches) use ($item, $create_attributes, $attributes_map) {
67+
return $create_attributes[ $attributes_map[$matches[1]]['attribute'] ][0];
68+
},
69+
$macro);
70+
error_log( "Use macro $macro for item $item: $value" );
71+
$create_attributes[ $attributes_map[$item]['attribute'] ] = $value;
72+
}
73+
74+
# Create entry
75+
if (!ldap_add($ldap, $dn, $create_attributes)) {
76+
error_log("LDAP - modify failed for $dn");
77+
$result = "createfailed";
78+
$action = "displayform";
79+
} else {
80+
$errno = ldap_errno($ldap);
81+
if ( $errno ) {
82+
error_log("LDAP - create error $errno (".ldap_error($ldap).") for $dn");
83+
$result = "createfailed";
84+
$action = "displayform";
85+
} else {
86+
$result = "createok";
87+
$action = "displayentry";
88+
}
89+
}
90+
91+
if ($audit_log_file) {
92+
auditlog($audit_log_file, $dn, $audit_admin, "createentry", $result, $comment);
93+
}
94+
95+
}
96+
97+
# Display form
98+
if ($action == "displayform") {
99+
100+
# Compute lists
101+
$item_list = array();
102+
103+
foreach ($create_items as $item) {
104+
if ( $attributes_map[$item]["type"] === "static_list") {
105+
$item_list[$item] = isset($attributes_static_list[$item]) ? $attributes_static_list[$item] : array();
106+
}
107+
if ( $attributes_map[$item]["type"] === "list") {
108+
$item_list[$item] = $ldapInstance->get_list( $attributes_list[$item]["base"], $attributes_list[$item]["filter"], $attributes_list[$item]["key"], $attributes_list[$item]["value"] );
109+
}
110+
}
111+
112+
}
113+
}
114+
}
115+
116+
if ( $action == "displayentry" ) {
117+
$location = 'index.php?page=display&dn='.urlencode($dn).'&createresult='.$result;
118+
header('Location: '.$location);
119+
}
120+
121+
$smarty->assign("entry", $entry);
122+
$smarty->assign("action", $action);
123+
124+
$smarty->assign("item_list", $item_list);
125+
126+
$smarty->assign("create_items", $create_items);
127+
$smarty->assign("show_undef", $display_show_undefined);
128+
129+
?>

0 commit comments

Comments
 (0)