Home arrow PHP arrow Page 11 - User Authentication With patUser (part 2)

Connecting The Dots - PHP

In this second part, find out how you can use the patUser API toview, add, edit and delete users (and user attributes) from your userdatabase.

TABLE OF CONTENTS:
  1. User Authentication With patUser (part 2)
  2. Meeting The Family
  3. Asking For More
  4. Drilling Deeper
  5. All For One, And One For All
  6. Accounting For Change
  7. California Calling
  8. Making New Friends
  9. A Fast Edit
  10. Here Today, Gone Tomorrow
  11. Connecting The Dots
  12. A Well-Formed Plan
  13. Slice And Dice
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 7
May 01, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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 classesinclude("../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.

 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap

Dev Shed Tutorial Topics: