kreut Posted February 13, 2011 Share Posted February 13, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/227539-zend-undefined-property-class/ Share on other sites More sharing options...
ignace Posted February 13, 2011 Share Posted February 13, 2011 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')) { Quote Link to comment https://forums.phpfreaks.com/topic/227539-zend-undefined-property-class/#findComment-1173707 Share on other sites More sharing options...
kreut Posted February 13, 2011 Author Share Posted February 13, 2011 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.... Quote Link to comment https://forums.phpfreaks.com/topic/227539-zend-undefined-property-class/#findComment-1173787 Share on other sites More sharing options...
ignace Posted February 14, 2011 Share Posted February 14, 2011 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) Quote Link to comment https://forums.phpfreaks.com/topic/227539-zend-undefined-property-class/#findComment-1174006 Share on other sites More sharing options...
kreut Posted February 14, 2011 Author Share Posted February 14, 2011 Thank you for the additional help. It makes perfect sense! Quote Link to comment https://forums.phpfreaks.com/topic/227539-zend-undefined-property-class/#findComment-1174013 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.