2016-04-12, 20:14:09
BTW: Simple user permissions (logged in, not-logged in) are just fine, but however, you may have even more fun and flexibility by adding a new user "roles" field to your Users category in ItemManager:
This way you can show relevant content depending on the user role, for example:
Your complete code in template.php file world might then look like this:
Note: If users already exist, these users may fail to be synced to new "roles" field, you should re-save/update users in admin manually or by ItemManager API call:
This way you can show relevant content depending on the user role, for example:
PHP Code:
if($role->value == 'user') {
echo 'Content for users only';
} elseif($role->value == 'admin') {
echo 'Content for admins only';
} elseif($role->value == 'superuser') {
echo 'Superusers content';
} else {
echo 'User has no role selected';
}
Your complete code in template.php file world might then look like this:
PHP Code:
if(!empty($_SESSION['loggedin'])) {
$manager = new IManager();
$catClass = $manager->getCategoryClass();
$category = $catClass->getCategory('name=Users');
$itemClass = $manager->getItemClass();
$itemClass->limitedInit($category->get('id'), (int)$_SESSION['userid']);
$user = $itemClass->items[(int)$_SESSION['userid']];
if(!$user) {die('User data not found');}
$role = $user->fields->roles;
if($role->value == 'user') {
echo 'Content for users only';
} elseif($role->value == 'admin') {
echo 'Content for admins only';
} elseif($role->value == 'superuser') {
echo 'Superusers content';
} else {
echo 'User has no role selected';
}
}
Note: If users already exist, these users may fail to be synced to new "roles" field, you should re-save/update users in admin manually or by ItemManager API call:
PHP Code:
<?php
$manager = new IManager();
$catClass = $manager->getCategoryClass();
$category = $catClass->getCategory('name=Users');
$itemClass = $manager->getItemClass();
$itemClass->init($category->get('id'));
foreach($itemClass->items as $user) {
if($user->save()) {
echo '<p>User name: <strong>'.htmlspecialchars($user->name).'</strong> was successfully updated</p>';
}
}