User Authentication With patUser (part 2) - Connecting The Dots (
Page 11 of 13 )
Once you've got your users and
groups created, you can begin organizing users into groups, via the
addUserToGroup() and removeUserFromGroup() methods.
Given the following
users and groups,
mysql> SELECT uid, username FROM users;
+-----+----------+
| uid | username |
+-----+----------+
| 2 | joe |
| 3 | sarah |
| 4 | john |
| 5 | tom |
+-----+----------+
4 rows in set (0.00 sec)
mysql> SELECT gid, name FROM groups;
+-----+-----------------+
| gid | name |
+-----+-----------------+
| 1 | Accounts |
| 2 | Administration |
| 3 | Operations |
| 4 | Human Resources |
+-----+-----------------+
4 rows in set (0.00 sec)
it's fairly easy to, say, add "joe" and "sarah" to the "Accounts" and
"Operations" group,
<?php
// include classes
include("../include/patDbc.php");
include("../include/patUser.php");
// initialize database layer
$db = new patMySqlDbc("localhost", "db211", "us111", "secret");
// initialize patUser
$u = new patUser(true);
// connect patUser to database
$u->setAuthDbc($db);
// set table
$u->setAuthTable("users");
$u->setGroupTable("groups"); $u->setGroupRelTable("usergroups");
// add joe to Accounts
$u->addUserToGroup( array("uid" => 2, "gid" => 1) );
// add joe to Operations
$u->addUserToGroup( array("uid" => 2, "gid" => 3) );
// add sarah to Accounts
$u->addUserToGroup( array("uid" => 3, "gid" => 1) );
// add sarah to Operations
$u->addUserToGroup( array("uid" => 3, "gid" => 3) );
?>
or remove "sarah" from "Operations" and move her into "Human Reources".
<?php
// include classes
include("../include/patDbc.php");
include("../include/patUser.php");
// initialize database layer
$db = new patMySqlDbc("localhost", "db211", "us111", "secret");
// initialize patUser
$u = new patUser(true);
// connect patUser to database
$u->setAuthDbc($db);
// set tables
$u->setAuthTable("users");
$u->setGroupTable("groups"); $u->setGroupRelTable("usergroups");
// remove sarah from Operations
$u->removeUserFromGroup( array("uid" => 3, "gid" => 3) );
// add sarah to Human Resources
$u->addUserToGroup( array("uid" => 3, "gid" => 4) );
?>
Once you've got your users organized the way you want them, patUser also
offers the following three utility functions to help you make sense of all the
relationships:
getJoinedGroups() - returns a list of the groups the named
user belongs to, accepts user ID as input
getUsersInGroup() - returns a
list of all the users in a named group, accepts group ID and list of required
user attributes as input
isMemberOfGroup() - returns a Boolean value
indicating whether or not the named user belongs to the named group, accepts
user ID and group ID as input
The following example illustrates how these
work:
<?php
// include classes
include("../include/patDbc.php");
include("../include/patUser.php");
// initialize database layer
$db = new patMySqlDbc("localhost", "db211", "us111", "secret");
// initialize patUser
$u = new patUser(true);
// connect patUser to database
$u->setAuthDbc($db);
// set tables
$u->setAuthTable("users");
$u->setGroupTable("groups"); $u->setGroupRelTable("usergroups");
// add joe to Accounts
$u->addUserToGroup( array("uid" => 2, "gid" => 1) );
// add joe to Operations
$u->addUserToGroup( array("uid" => 2, "gid" => 3) );
// add sarah to Human Resources
$u->addUserToGroup( array("uid" => 3, "gid" => 4) );
// add john to Operations
$u->addUserToGroup( array("uid" => 4, "gid" => 3) );
// which groups is joe a member of?
$foo = $u->getJoinedGroups( array("uid" => 2) );
print_r($foo);
// who belongs to the Operations group?
$foo = $u->getUsersInGroup( array("uid", "username"), array("gid" => 3) );
print_r($foo);
// does joe belong to Operations?
// returns Boolean true (1)
$foo = $u->isMemberOfGroup(2, 3);
print_r($foo);
?>
Here's the output:
Array
(
[0] => Array
(
[gid] => 1
[name] => Accounts
)
[1] => Array
(
[gid] => 3
[name] => Operations
)
)
Array
(
[0] => Array
(
[uid] => 2
[username] => joe
)
[1] => Array
(
[uid] => 4
[username] => john
)
)
1
Needless to say, these utility functions come in very handy when you need
to find out which users belong to which groups, or if you need to alter the
various user and group configurations. You'll see this in action in the
composite example on the next page.