Permalink
Browse files

Adding and deleting now work via AJAX.

  • Loading branch information...
1 parent 2a7154e commit 00b6ffddf69c9557fda7c365bc653e39aa0940e1 @adamfranco adamfranco committed Aug 31, 2009
Showing with 234 additions and 53 deletions.
  1. +13 −6 actions/create_group.php
  2. +35 −0 actions/delete_group.php
  3. +8 −41 actions/list.php
  4. +23 −0 lib/LdapConnector.class.php
  5. +54 −0 lib/functions.php
  6. +4 −0 public/group_manager.css
  7. +97 −6 public/group_manager.js
View
@@ -33,13 +33,20 @@
} catch (LdapException $e) {
}
-$entry['cn'] = $newGroupName;
-$entry['objectClass'][0] = 'top';
-$entry['objectClass'][1] = 'group';
-$entry['groupType']="2";
-$entry['managedBy'] = $_SESSION['user'];
+$entry['cn'][0] = $newGroupName;
+$entry['objectclass'][0] = 'top';
+$entry['objectclass'][1] = 'group';
+$entry['grouptype']="2";
+$entry['managedby'][0] = $_SESSION['user'];
// $entry["sAMAccountName"] = $newGroupName;
+// create the new group
$ldap->add($groupId, $entry);
-forward('list');
+// Print out its HTML for insertion into the document
+while(ob_get_level())
+ ob_end_clean();
+header('Content-Type: text/html');
+$entry['dn'] = $groupId;
+printGroupHtml($ldap, $entry);
+exit;
View
@@ -0,0 +1,35 @@
+<?php
+/**
+ * Add a new user to a group.
+ *
+ * @since 8/28/09
+ * @package
+ *
+ * @copyright Copyright &copy; 2009, Middlebury College
+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
+ */
+
+if (!isset($_POST['group_id']) || !$_POST['group_id'])
+ throw new InvalidArgumentException("No group_id passed");
+
+$groupId = base64_decode($_POST['group_id'], true);
+if (!$groupId)
+ throw new InvalidArgumentException("Invalid group_id passed");
+
+
+// Verify that the current user really can manage the group.
+$groups = $ldap->read('(objectclass=group)', $groupId, array('managedby', 'member'));
+if (count($groups) != 1)
+ throw new Exception("Could not find the group specified");
+$group = $groups[0];
+if ($group['managedby'][0] != $_SESSION['user'])
+ throw new PermissionDeniedException("You are not authorized to manage this group.");
+
+// Delete the group
+$ldap->delete($groupId);
+
+while(ob_get_level())
+ ob_end_clean();
+header('Content-Type: text/plain');
+print "Success";
+exit;
View
@@ -4,6 +4,8 @@
<li><a href="<?php echo getUrl('list_all'); ?>">All Groups</a></li>
</ul>
+<div id='groups'>
+
<?php
$groups = array();
foreach ($ldapConfig['WritableGroupContainers'] as $baseDN) {
@@ -19,51 +21,16 @@
$groups = array_values($groups);
foreach ($groups as $group) {
- $levels = dnToLevels($group['dn']);
-
- print "\n<div class='group'>";
-// print "\n\t<h2>".$group['cn'][0]."</h2>";
-// print "\n\t<h2>".implode('/', $levels)."</h2>";
-
-// print "\n\t<fieldset class='location'>\n\t\t<legend>Location</legend>";
-// foreach ($levels as $level)
-// print "\n\t<ul>\n\t<li>".$level." <br/>";
-// foreach ($levels as $level)
-// print "\n\t</li>\n\t</ul>";
-// print "\n\t</fieldset>";
-
- print "\n\t<fieldset class='members'>\n\t\t<legend>".implode(' / ', $levels)."</legend>";
- print "\n\t\t<ul>";
-
- if (isset($group['member']) && is_array($group['member'])) {
- sort ($group['member']);
- foreach ($group['member'] as $memberDN) {
- $members = $ldap->read('(objectclass=*)', $memberDN, array('givenName', 'sn', 'mail'));
- $member = $members[0];
-
- print "\n\t\t<li>".$member['givenname'][0]." ".$member['sn'][0]." (".$member['mail'][0].") ";
- print "\n\t\t\t<input type='hidden' class='group_id' value='".base64_encode($group['dn'])."'/>";
- print "\n\t\t\t<input type='hidden' class='member_id' value='".base64_encode($memberDN)."'/>";
- print "<button class='remove_button'>Remove</button>";
- print "</li>";
- }
- }
- print "\n\t\t</ul>";
- print "\n\t\t<input type='text' class='new_member' size='50'/>";
- print "\n\t\t\t<input type='hidden' class='group_id' value='".base64_encode($group['dn'])."'/>";
- print "\n\t\t<button class='add_button'>Add</button>";
-
- print "\n\t</fieldset>";
-
-
- print "\n</div>";
+ printGroupHtml($ldap, $group);
}
?>
-<form action="<?php echo getUrl('create_group'); ?>" method="post" class="create_group">
+</div>
+
+<form action="<?php echo getUrl('create_group'); ?>" method="post" id="create_group_form">
<p>Create a new group in
- <select name="container_dn">
+ <select name="container_dn" id="new_group_container_dn">
<?php
foreach ($ldapConfig['WritableGroupContainers'] as $dn) {
print "\n\t\t\t<option value=\"".base64_encode($dn)."\">".implode(" / ", dnToLevels($dn))."</option>";
@@ -72,7 +39,7 @@
</select>
named
- <input type="text" name="new_group_name"/>
+ <input type="text" name="new_group_name" id='new_group_name'/>
<input type="submit" value="Create"/>
</p>
</form>
@@ -323,6 +323,29 @@ public function add ($dn, array $entry) {
return true;
}
+ /**
+ * Delete an entry frp, the LDAP directory
+ *
+ * @param string $dn The DN of the entry to delete
+ * @return boolean True on success. Exceptions will be thrown on error
+ * @access public
+ * @since 8/28/09
+ */
+ public function delete ($dn) {
+ if (!$this->_connection)
+ throw new LDAPException ("Not connected to LDAP host <b>".$this->_config['LDAPHost']."</b>.");
+
+ if (!$this->_bind)
+ $this->bindAsAdmin();
+
+ $success = ldap_delete($this->_connection, $dn);
+
+ if (ldap_errno($this->_connection) || !$success)
+ throw new LDAPException("Delete failed for dn '$dn' with message: ".ldap_error($this->_connection).' Code: '.ldap_errno($this->_connection));
+
+ return true;
+ }
+
/**
* Add an attribute value to an LDAP entry
*
View
@@ -44,4 +44,58 @@ function dnToLevels ($dn) {
array_pop($levels);
$levels = array_reverse($levels);
return $levels;
+}
+
+/**
+ * Print an HTML block for a group, respecting permissions.
+ *
+ * @param LdapConnector $ldap
+ * @param array $group The group result from an LDAP search.
+ * @return void
+ * @since 8/31/09
+ */
+function printGroupHtml (LdapConnector $ldap, array $group) {
+ $showControls = ($group['managedby'][0] == $_SESSION['user']);
+
+ $levels = dnToLevels($group['dn']);
+
+ print "\n<div class='group'>";
+
+ print "\n\t<fieldset class='members'>\n\t\t<legend>".implode(' / ', $levels)."</legend>";
+ print "\n\t\t<ul>";
+
+ if (isset($group['member']) && is_array($group['member'])) {
+ sort ($group['member']);
+ foreach ($group['member'] as $memberDN) {
+ $members = $ldap->read('(objectclass=*)', $memberDN, array('givenName', 'sn', 'mail'));
+ $member = $members[0];
+
+ print "\n\t\t<li>".$member['givenname'][0]." ".$member['sn'][0]." (".$member['mail'][0].") ";
+ if ($showControls) {
+ print "\n\t\t\t<input type='hidden' class='group_id' value='".base64_encode($group['dn'])."'/>";
+ print "\n\t\t\t<input type='hidden' class='member_id' value='".base64_encode($memberDN)."'/>";
+ print "<button class='remove_button'>Remove</button>";
+ }
+ print "</li>";
+ }
+ }
+ print "\n\t\t</ul>";
+
+ if ($showControls) {
+ print "\n\t\t<div class='add_member_controls'>";
+ print "\n\t\t<input type='text' class='new_member' size='50'/>";
+ print "\n\t\t\t<input type='hidden' class='group_id' value='".base64_encode($group['dn'])."'/>";
+ print "\n\t\t<button class='add_button'>Add</button>";
+ print "\n\t\t</div>";
+
+ print "\n\t\t<div class='delete_controls'>";
+ print "\n\t\t\t<input type='hidden' class='group_id' value='".base64_encode($group['dn'])."'/>";
+ print "\n\t\t<button class='delete_button'>Delete Group</button>";
+ print "\n\t\t</div>";
+ }
+
+ print "\n\t</fieldset>";
+
+
+ print "\n</div>";
}
View
@@ -42,4 +42,8 @@
.group fieldset.members {
width: 450px;
+}
+
+.group div.delete_controls {
+ margin-top: 10px;
}
View
@@ -54,6 +54,63 @@ function addUser (groupId, userId, userName, list) {
return true;
}
+/**
+ * Create a group
+ *
+ * @param string groupName
+ * @param string containerDN
+ * @param jQuery containerElement A container to add the new group to.
+ * @return boolean TRUE if removal will continue, FALSE if canceled
+ * @access public
+ * @since 8/28/09
+ */
+function createGroup (groupName, containerDN, containerElement) {
+ $.ajax({
+ type: "POST",
+ url: "index.php",
+ data: {action: 'create_group', container_dn: containerDN, new_group_name: groupName},
+ error: function (request, textStatus, errorThrown) {
+ alert('An error has occurred, could not create the group.');
+ },
+ success: function (data, textStatus) {
+ containerElement.append(data);
+
+ setDeleteActions();
+ setRemoveActions();
+ setAddActions();
+ }
+ });
+
+ return true;
+}
+
+/**
+ * Delete a group.
+ *
+ * @param string groupId
+ * @param jQuery element The element to hide when removing.
+ * @return boolean TRUE if removal will continue, FALSE if canceled
+ * @access public
+ * @since 8/28/09
+ */
+function deleteGroup (groupId, element) {
+ if (!confirm("Are you sure that you wish to permenantly delete this group?"))
+ return false;
+
+ $.ajax({
+ type: "POST",
+ url: "index.php",
+ data: {action: 'delete_group', group_id: groupId},
+ error: function (request, textStatus, errorThrown) {
+ alert('An error has occurred, could not remove user.');
+ element.show("slow");
+ }
+ });
+
+ element.hide("slow");
+ return true;
+}
+
/*********************************************************
* Add our button actions via jQuery
*********************************************************/
@@ -67,11 +124,7 @@ function setRemoveActions() {
});
}
-$(document).ready(function() {
-
- // OnClick actions for the remove buttons
- setRemoveActions();
-
+function setAddActions() {
// OnClick actions for the add buttons
$(".group .members button.add_button").click(function() {
addUser(
@@ -91,10 +144,48 @@ $(document).ready(function() {
$(this).siblings("input.group_id:first").attr('value'),
data[1],
data[0],
- $(this).parent().children("ul").eq(0)
+ $(this).parent().parent().children("ul").eq(0)
);
$(this).attr('value', '');
}
});
+}
+
+function setDeleteActions() {
+ // Set the delete-group actions
+ $(".group button.delete_button").click(function() {
+ deleteGroup(
+ $(this).siblings("input.group_id:first").attr('value'),
+ $(this).parents('.group')
+ );
+ });
+}
+
+$(document).ready(function() {
+
+ // OnClick actions for the remove buttons
+ setRemoveActions();
+
+ // OnClick actions for the add buttons
+ setAddActions();
+
+ // Set the delete-group actions
+ setDeleteActions();
+
+ $("#create_group_form").submit(function() {
+ var name = $(this).find('#new_group_name');
+ if (name.attr('value').length) {
+ createGroup(
+ name.attr('value'),
+ $(this).find("#new_group_container_dn").attr('value'),
+ $('#groups').eq(0)
+ );
+ name.attr('value', '');
+ } else {
+ alert("You must enter a group name.");
+ }
+
+ return false;
+ });
});

0 comments on commit 00b6ffd

Please sign in to comment.