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
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: