2.4. Updating the User Class

To support the permissions requirements, the User class needs to be updated. First, the various permissions are identified as constants whose values increment in powers of 2. As the constructor initializes the object's internal fields property to a new empty user, it must be modified to include the permissions field.

const CREATE_FORUM = 2;
const MOVE_MESSAGE = 4;
const DELETE_MESSAGE = 8;
const DELETE_FORUM = 16;

public function __construct()
{
    $this->uid = null;
    $this->fields = array('username' => '',
                             'password' => '',
                             'emailAddr' => '',
                             'isActive' => false,
                             'permission' => 0);
}

The three methods getById(), getByUsername() and save() all work with the underlying WROX_USER database table. Since the table's definition has changed, they too must be modified to take into account the new permissions field.

public static function getById($userId)
{
    $u = new User();
    $query = sprintf('SELECT USERNAME, PASSWORD, EMAIL_ADDR, ' .
        'IS_ACTIVE, PERMISSION FROM %sUSER WHERE USER_ID = %d',
        DB_TBL_PREFIX, $userId);
    $result = mysql_query($query, $GLOBALS['DB']);
    if (mysql_num_rows($result))
    {
        $row = mysql_fetch_assoc($result);
        $u->username = $row['USERNAME'];
        $u->password = $row['PASSWORD'];
        $u->emailAddr = $row['EMAIL_ADDR'];
        $u->isActive = $row['IS_ACTIVE'];
$u->permission = $row['PERMISSION']; $u->uid = $userId; } mysql_free_result($result); return $u; } public static function getByUsername($username) { $u = new User(); $query = sprintf('SELECT USER_ID, PASSWORD, ...

Get PHP and MySQL®: Create-Modify-Reuse now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.