10-31-2016, 12:11 PM
What makes this a little tricky is that user levels are not "levels" as in one is higher than another. Personally, if I were doing this, I would create the permission levels and then go into the database into the permissions level and give some space (like 5 or 10 between them) in case you ever have to add more levels. Do this BEFORE you start assigning all these permissions to pages. I would do
10 – CSR
20 – System Administrator (full access)
30 – Log Access
40 – User Management
50 – Manager
Then, the trick is going to be to go into the figuring out the HIGHEST level of permissions that a user has access to by their own user id (which is expressed as $user->data()->id).
I had a few minutes so I typed out the code (including var_dumps so you can see what each query is bringing back) and a drop down box at the bottom showing you the permission names (but still recording the ids). If you are writing your own thing to create new users I STRONGLY recommend you check out the code that does this on the backend in admin_users.php to see the other things that have to happen when you create a new user.
Here is the code on hastebin (which is probably easier to read)
http://hastebin.com/ricacecoja.xml
//get a list of the highest permission level your logged in user has...
//now you need to figure out what permissions they can use for their dropdown box or whatever.
10 – CSR
20 – System Administrator (full access)
30 – Log Access
40 – User Management
50 – Manager
Then, the trick is going to be to go into the figuring out the HIGHEST level of permissions that a user has access to by their own user id (which is expressed as $user->data()->id).
I had a few minutes so I typed out the code (including var_dumps so you can see what each query is bringing back) and a drop down box at the bottom showing you the permission names (but still recording the ids). If you are writing your own thing to create new users I STRONGLY recommend you check out the code that does this on the backend in admin_users.php to see the other things that have to happen when you create a new user.
Here is the code on hastebin (which is probably easier to read)
http://hastebin.com/ricacecoja.xml
Code:
<?php
Code:
$highestPermQ = $db->query("SELECT * FROM user_permission_matches WHERE user_id = ? ORDER BY permission_id DESC",array($user->data()->id));
Code:
$highestPerm = $highestPermQ->first();
Code:
dump($highestPerm);
//now you need to figure out what permissions they can use for their dropdown box or whatever.
Code:
$highest = $highestPerm->permission_id;
Code:
dump($highest); //This SHOULD give you the highest permission level they have
Code:
$availableQ = $db->query("SELECT * FROM permissions WHERE id !=2 AND id < ?",array($highest));
Code:
$available = $availableQ->results();
Code:
dump($available); //should show all ids below the one listed above but NOT admin (2)
Code:
?>
Code:
<div class="form-group">
Code:
<label for="gen_loc">Available user levels</label>
Code:
<select class="form-control" name="available" id="available" value=""required>
Code:
<?php foreach($available as $a){ ?>
Code:
<option value="<?=$a->id?>"><?=$a->name?></option>
Code:
<?php } ?></select>
Code:
</div>