Jump to content

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)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.