<BODY>
        <h1>
            Administration Page For Groups
        </h1>  
        <a href="CreateGroup.php"><b>Create And Add A New Group</b></a>
        <br/><br/>
        <table border="1">
            <tr>
            <th>Display Name</th>
            <th>Description</th>
            <th>Mail Enabled</th>
            <th>Edit Link</th>
            <th>Delete Link</th>
            </tr>  
            <?php 
$groups = GraphServiceAccessHelper::getFeed('groups');
foreach ($groups as $group) {
    $editLinkValue = "EditGroup.php?id=" . $group->objectId;
    $deleteLinkValue = "DeleteGroup.php?id=" . $group->objectId;
    $groupmembersLinkValue = "DisplayMembersOfGroup.php?id=" . $group->objectId . '&name=' . urlencode($group->{'displayName'});
    if ($group->{'mailEnabled'} == 1) {
        $mailEnabled = 'True';
    } else {
        $mailEnabled = 'False';
    }
    echo '<tr><td><a href=' . $groupmembersLinkValue . '>' . $group->{'displayName'} . '</a></td><td>' . $group->{'description'} . '</td><td>' . $mailEnabled . '</td><td>' . '<a href=\'' . $editLinkValue . '\'>' . 'Edit Group' . '</a></td><td>' . '<a href=\'' . $deleteLinkValue . '\'>' . 'Delete Group' . '</a></td></tr>';
}
?>
            </table>            
    </BODY>
</HTML>
            Administration Page For Users
        </h1>  
        <a href="CreateUser.php"><b>Create And Add A New User</b></a>    
        <br/><br/>
        <table border="1">
            <tr>
            <th>Display Name</th>
            <th>User Principal Name</th>
            <th>Object ID</th>
            <th>Immutable ID</th>
            <th>Account Enabled</th>        
            <th>Edit Link</th>
            <th>Delete Link</th>
            </tr>  
            <?php 
$users = GraphServiceAccessHelper::getFeed('users');
// display any records fetched from the database
// plus an input line for a new category
foreach ($users as $user) {
    if ($user->{'accountEnabled'} == 1) {
        $accountEnabled = 'True';
    } else {
        $accountEnabled = 'False';
    }
    $editLinkValue = "EditUser.php?id=" . $user->objectId;
    $deleteLinkValue = "DeleteUser.php?id=" . $user->objectId;
    echo '<tr><td>' . $user->{'displayName'} . '</td><td>' . $user->{'userPrincipalName'} . '</td>';
    echo '<td>' . $user->{'objectId'} . '</td>';
    echo '<td>' . $user->{'immutableId'} . '</td>';
    echo '<td>' . $accountEnabled . '</td>';
    echo '<td>' . '<a href=\'' . $editLinkValue . '\'>' . 'Edit User' . '</a></td><td>' . '<a href=\'' . $deleteLinkValue . '\'>' . 'Delete User' . '</a></td></tr>';
        <?php 
// If this was not a post back show the delete user form
if (!isset($_POST['submit'])) {
    $user = GraphServiceAccessHelper::getEntry('users', $_GET['id']);
    echo '<H1>Do you really want to delete this user?<H1>';
    echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '?id=' . $_GET['id'] . '">';
    echo '<table>';
    echo '<tr><td><b>Display Name:</b></td><td><input type="text" disabled size="20" maxlength="100" name="dname" value="' . $user->{'displayName'} . '"></td></tr>';
    echo '<tr><td><b>Mail Alias:</b></td><td><input type="text" disabled size="20" maxlength="15" name="alias" value="' . $user->{'mailNickname'} . '"></td></tr>';
    echo '<input name="id" type="hidden" value=' . $_GET['id'] . '>';
    echo '<tr><td><input type="submit" value="Delete" name="submit"></td></tr>';
    echo '</table>';
    echo '</form>';
} else {
    // Delete the user and display a message
    $user = GraphServiceAccessHelper::deleteEntry('users', $_POST['id']);
    //Check to see if we got back an error.
    if (!empty($user->{'odata.error'})) {
        $message = $user->{'odata.error'}->{'message'};
        echo '<p>User deletion failed. Service returned error:<b>' . $message->{'value'} . '</b> Please go back to <a href="DeleteUser.php' . '?id=' . $_POST['id'] . '">Delete User</a></p>';
    } else {
        echo '<p>';
        echo '<b>Deleted the User with the following Key:</b>' . $_POST['id'];
        echo '<br/><br/><br/>';
        echo 'You can go back to <a href="DisplayUsers.php">Manage Users</a> to continue managing User information.';
        echo '</p>';
    }
}
?>
        
</body>
            </table>
        </form>        
 <?php 
} else {
    // Validate that the inputs are non-empty.
    if (empty($_POST["dname"]) or empty($_POST["alias"]) or empty($_POST["accountenabled"]) or empty($_POST["forcePasswordChangeOnNextLogin"]) or empty($_POST["password"])) {
        echo '<p>One of the required fields is empty. Please go back to <a href="CreateUser.php">Create User</a></p>';
    } else {
        //collect the form parameters which will be set in the case this was a post back.
        $displayName = $_POST["dname"];
        $alias = $_POST["alias"];
        $accountEnabled = $_POST["accountenabled"];
        $passwordProfile = array('password' => $_POST["password"], 'forceChangePasswordNextLogin' => $_POST["forcePasswordChangeOnNextLogin"]);
        $userEntryInput = array('displayName' => $displayName, 'userPrincipalName' => $alias . '@' . Settings::$appTenantDomainName, 'mailNickname' => $alias, 'passwordProfile' => $passwordProfile, 'accountEnabled' => $accountEnabled);
        // Create the user and display a message
        $user = GraphServiceAccessHelper::addEntryToFeed('users', $userEntryInput);
        //Check to see if we got back an error.
        if (!empty($user->{'odata.error'})) {
            $message = $user->{'odata.error'}->{'message'};
            echo '<p>User creation failed. Service returned error:<b>' . $message->{'value'} . '</b>  Please go back to <a href="createUser.php">Create User</a></p>';
        } else {
            echo '<p>';
            echo '<b>Created User with the following information:</b>';
            echo '<table><tr><td><b>Display Name:   </b>' . $user->{'displayName'} . '</td><td><b>User Principal Name:  </b>' . $user->{'userPrincipalName'} . '</td></tr></table>';
            echo '<br/> <br/>';
            echo 'You can go back to <a href="CreateUser.php">Create User</a> to create more users.';
            echo '</p>';
        }
    }
}
?>
            </table>  
        </form>
               
 <?php 
} else {
    //collect the form parameters which will be set in the case this was a post back.
    $displayName = $_POST["dname"];
    $description = $_POST["description"];
    $alias = $_POST["alias"];
    // Validate that the inputs are non-empty.
    if (empty($displayName) or empty($description) or empty($alias)) {
        echo '<p>One of the required fields is empty. Please go back to <a href="CreateGroup.php">Create Group</a></p>';
    } else {
        $groupEntryInput = array('displayName' => $displayName, 'description' => $description, 'mailNickname' => $alias, 'mailEnabled' => 'False', 'securityEnabled' => 'True');
        // Create the group and display a message to the user
        $group = GraphServiceAccessHelper::addEntryToFeed('groups', $groupEntryInput);
        //Check to see if we got back an error.
        if (!empty($group->{'odata.error'})) {
            $message = $group->{'odata.error'}->{'message'};
            echo '<p>Group creation failed. Service returned error:<b>' . $message->{'value'} . '</b>  Please go back to <a href="CreateGroup.php">Create Group</a></p>';
        } else {
            echo '<p>';
            echo '<b>Created Group with the following information:</b>';
            echo '<table><tr><td><b>Display Name:   </b>' . $group->{'displayName'} . '</td><td><b>Description:  </b>' . $group->{'description'} . '</td></tr></table>';
            echo '<br/> <br/>';
            echo 'You can go back to <a href="CreateGroup.php">Create Group</a> to create more groups.';
            echo '</p>';
        }
    }
}
?>
    echo '<tr><td><b>False:</b></td><td><input type="radio" value="False" name="accountenabled"' . $checkedFalse . '></td></tr>';
    echo '<tr><td><input type="submit" value="submit" name="submit"></td></tr>';
    echo '</table>';
    echo '</form>';
} else {
    // Validate that the inputs are non-empty.
    if (empty($_POST["dname"]) or empty($_POST["alias"]) or empty($_POST["accountenabled"])) {
        echo '<p>One of the required fields is empty. Please go back to <a href="EditUser.php' . '?id=' . $_POST['id'] . '">Update User</a></p>';
    } else {
        //collect the form parameters which will be set in the case this was a post back.
        $displayName = $_POST["dname"];
        $alias = $_POST["alias"];
        $accountEnabled = $_POST["accountenabled"];
        $userEntryInput = array('displayName' => $displayName, 'userPrincipalName' => $alias . '@' . Settings::$appTenantDomainName, 'mailNickname' => $alias, 'accountEnabled' => $accountEnabled);
        // Create the user and display a message
        $user = GraphServiceAccessHelper::updateEntry('users', $_POST['id'], $userEntryInput);
        //Check to see if we got back an error.
        if (!empty($user->{'odata.error'})) {
            $message = $user->{'odata.error'}->{'message'};
            echo '<p>User update failed. Service returned error:<b>' . $message->{'value'} . '</b> Please go back to <a href="EditUser.php' . '?id=' . $_POST['id'] . '">Update User</a></p>';
        } else {
            echo '<p>';
            echo '<b>Updated the User with the following information:</b>';
            echo '<br/>';
            echo '<b>Display Name:   </b>' . $user->{'displayName'};
            echo '<br/>';
            echo '<b>User Principal Name:  </b>' . $user->{'userPrincipalName'};
            echo '<br/>';
            echo '<b>Account Enabled:  </b>';
            echo $user->{'accountEnabled'} ? 'true' : 'false';
            echo '<br/> <br/>';
        <?php 
// If this was not a post back show the delete group form
if (!isset($_POST['submit'])) {
    $group = GraphServiceAccessHelper::getEntry('groups', $_GET['id']);
    echo '<H1>Do you really want to delete this group?<H1>';
    echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '?id=' . $_GET['id'] . '">';
    echo '<table>';
    echo '<tr><td><b>Display Name:</b></td><td><input type="text" disabled size="20" maxlength="100" name="dname" value="' . $group->{'displayName'} . '"></td></tr>';
    echo '<tr><td><b>Group:</b></td><td><input type="text" disabled size="20" maxlength="15" name="alias" value="' . $group->{'description'} . '"></td></tr>';
    echo '<input name="id" type="hidden" value=' . $_GET['id'] . '>';
    echo '<tr><td><input type="submit" value="Delete" name="submit"></td></tr>';
    echo '</table>';
    echo '</form>';
} else {
    // Delete the group and display a message
    $group = GraphServiceAccessHelper::deleteEntry('groups', $_POST['id']);
    //Check to see if we got back an error.
    if (!empty($group->{'odata.error'})) {
        $message = $group->{'odata.error'}->{'message'};
        echo '<p>Group deletion failed. Service returned error:<b>' . $message->{'value'} . '</b> Please go back to <a href="DeleteGroup.php' . '?id=' . $_POST['id'] . '">Delete Group</a></p>';
    } else {
        echo '<p>';
        echo '<b>Deleted the Group with the following Key:</b>' . $_POST['id'];
        echo '<br/><br/><br/>';
        echo 'You can go back to <a href="DisplayGroups.php">Manage Groups</a> to continue managing Group information.';
        echo '</p>';
    }
}
?>
        
</body>
        </STYLE>

    </head>

    <BODY>
        <h1>
            Get changes from AAD.
        </h1>  
            <?php 
$deltaLink = NULL;
if (!empty($_GET)) {
    $deltaLink = $_GET["deltaLink"];
}
$moreChangesAvailable = FALSE;
do {
    $response = GraphServiceAccessHelper::getDeltaLinkFeed($deltaLink, "directoryObjects");
    $changes = $response->{'value'};
    if (!empty($changes)) {
        foreach ($changes as $change) {
            PrintObjectHelper::PrintObject($change);
        }
    } else {
        echo 'No changes available at this time.<br>';
    }
    $deltaLink = $response->{'aad.nextLink'};
    if (empty($deltaLink) || $deltaLink == NULL) {
        $deltaLink = $response->{'aad.deltaLink'};
        $moreChangesAvailable = FALSE;
    } else {
        // more changes are available.
        $moreChangesAvailable = TRUE;
    echo '<tr><td><b>Description:</b></td><td><input type="text" size="20" maxlength="15" name="description" value="' . $group->{'description'} . '"></td></tr>';
    echo '<input name="id" type="hidden" value=' . $_GET['id'] . '>';
    echo '<tr><td><input type="submit" value="submit" name="submit"></td></tr>';
    echo '</table>';
    echo '</form>';
} else {
    // Validate that the inputs are non-empty.
    if (empty($_POST["dname"]) or empty($_POST["description"])) {
        echo '<p>One of the required fields is empty. Please go back to <a href="EditGroup.php' . '?id=' . $_POST['id'] . '">Update Group</a></p>';
    } else {
        //collect the form parameters which will be set in the case this was a post back.
        $displayName = $_POST["dname"];
        $description = $_POST["description"];
        $groupEntryInput = array('displayName' => $displayName, 'description' => $description);
        // Create the group and display a message
        $group = GraphServiceAccessHelper::updateEntry('groups', $_POST['id'], $groupEntryInput);
        //Check to see if we got back an error.
        if (!empty($group->{'odata.error'})) {
            $message = $group->{'odata.error'}->{'message'};
            echo '<p>Group update failed. Service returned error:<b>' . $message->{'value'} . '</b> Please go back to <a href="EditGroup.php' . '?id=' . $_POST['id'] . '">Update Group</a></p>';
        } else {
            echo '<p>';
            echo '<b>Updated the Group with the following information:</b>';
            echo '<br/>';
            echo '<b>Display Name:   </b>' . $group->{'displayName'};
            echo '<br/>';
            echo '<b>Description:  </b>' . $group->{'description'};
            echo '<br/> <br/>';
            echo 'You can go back to <a href="DisplayGroups.php">Manage Groups</a> to continue managing Group information.';
            echo '</p>';
        }
            echo '<p><b>Please provide a valid user display name for the user to be added. User display name:\'' . $_POST['dname'] . '\' is not valid</b></p>';
        } else {
            // We should ideally handle the case where there are multiple members with the same diplay name but let's
            // make the simple assumption that they are distinct.
            foreach ($filteredusers as $filtereduser) {
                $userid = $filtereduser->{'objectId'};
            }
            $useruri = array('url' => 'https://graph.windows.net/' . Settings::$appTenantDomainName . '/users(\'' . $userid . '\')');
            $groupUrl = "groups(" . '\'' . $_GET['id'] . '\')';
            $addedLink = GraphServiceAccessHelper::addLinkForEntries($groupUrl, $useruri, 'members');
        }
    }
}
// Get the list of members for this group and diplay. A simplistic assumption that all members
// are users for this sample.
$users = GraphServiceAccessHelper::getLinkedFeed('groups', $_GET['id'], 'members');
foreach ($users as $user) {
    echo '<tr><td>' . $user->{'objectId'} . '</td><td>' . $user->{'displayName'} . '</td></tr>';
}
?>
        </table>
        <br/><br/>
        <form method="post" action="<?php 
echo $_SERVER['PHP_SELF'] . '?id=' . $_GET['id'] . '&name=' . $_GET['name'];
?>
">            
            <b>To create another user to this group, Provide the display name and click submit(only supported for users with unique display name):<input type="text" size="20" maxlength="100" name="dname"></b>
            <br/><br/>
            <input type="submit" value="submit" name="submit">            
        </form>
    </BODY>