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_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, ' .
        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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.