Jump to content

Zend Undefined property class


kreut

Recommended Posts

Hello,

 

I'm trying to create a restrict access page that only administrators or instructors can view (it's an educational website).  With the code below I get the error: 

Notice: Undefined property: stdClass::$user_id in /Applications/MAMP/htdocs/restrict_access_instructors.php on line 12

 

Any thoughts?

 

Thank you.

 

<?php
require_once('library.php');
try {
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()){
	$identity = $auth->getIdentity();
	$is_instructor = ($identity->user_type == 'instructor');
    $is_admin = ($identity->user_type == 'administrator');
    
	if ($is_instructor || $is_admin){
		$user_id = $identity->user_id;  /this is the line with the error
		} else {
	$auth->clearIdentity();
	header('../login.php');
	exit;
}
	} else {
	header('../login.php');
	exit;
}
} catch (Exception $e) {
echo $e->getMessage();
}

 

Notice: Undefined property: stdClass::$user_id in /Applications/MAMP/htdocs/restrict_access_instructors.php on line 12

 

Link to comment
https://forums.phpfreaks.com/topic/227539-zend-undefined-property-class/
Share on other sites

The error means: user_id doesn't exist. So you probably forgot to assign it.

 

PS: Zend also has a Zend_ACL component for all your access needs it makes this manual checking for instructor and administrator redundant.

 

if($acl->isAllowed($user->getRole(), 'ResourceYouAreAboutToAccess')) {

Thanks!  I saw where I needed to define the user_id and my code worked.  However, now I think that the Zend_ACL would be a cleaner approach.  Would it be at all possible to flesh out your suggestion a bit?  I fear that this is only my second week using Zend, and I'm not quite sure how to implement this procedure. 

 

Thanks again....

You can find a code sample for Zend_Acl at the Zend framework website (scroll down to example #1).

 

Add your roles (instructor, admin) to the Acl object:

 

$acl->addRole(new Zend_Acl_Role('instructor'));
$acl->addRole(new Zend_Acl_Role('admin'));

 

Then check against the acl if the user has access:

 

$acl->isAllowed($identity->user_type, 'nameOfTheResource')) {

 

The advantage of this approach is that your role names are dynamic (eg you can rename admin to Administrator without your code breaking)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.