Welcome Guest, Not a member yet? Register   Sign In
Shield: access of a page for one user
#1

Hi,

how can i do the following scenario?

I have page A and page B. User A has only access to page A and user B has only access to page B. Do i have to check a user id in the controller or is there another way?
Reply
#2

Using permissions, you can set a permission per group or per user, so user A has access to Permission A and user B has access to Permission B
Reply
#3

(12-27-2022, 10:02 AM)superior Wrote: Using permissions, you can set a permission per group or per user, so user A has access to Permission A and user B has access to Permission B

OK, can i name the permissions as i want? Then i have to set for every single user a permission.
Reply
Reply
#5

Ok and does it mean for 100 users and 100 pages i have to set 100 permissions in the permissions array? Is it better to store the user id in a database table which contains the page and the user id?
Reply
#6

No you can create Groups and give the Groups Permission access rules.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

(12-30-2022, 12:17 AM)InsiteFX Wrote: No you can create Groups and give the Groups Permission access rules.

Yes, but a "group" only consists of one user, because only one user has access to this page. It is like a profile page. The contents of the page for every user are different. I only have to connect the contents (stored in a view or database) with a user id.

User A has access to page A
User B has access to page B ...
Reply
#8

(This post was last modified: 02-22-2023, 05:35 PM by grimpirate.)

If what you're referring to is specifically a profile page, what you would do is exclude public pages from your 'session' filter as explained here. That way the profile pages will only be accessible by a user who is logged in. The difference is the data that goes to the page, which would be lifted from your user's profile and updates the view accordingly.
Reply
#9

(This post was last modified: 02-22-2023, 11:38 PM by luckmoshy.)

According to your asking these are what you can do in your specific controller
PHP Code:
if ($user->can('users.create')) {
    //do your redirect to
}

if (! 
$user->inGroup('superadmin''admin')) {
    //
}

if (! 
$user->hasPermission('users.create')) {
    //

Codeigniter First, Codeigniter Then You!!
yekrinaDigitals

Reply
#10

I see this has been out here for a bit... I just did this in my app and like the flexibility of Shield.

All profile pages have the same sections with some specific to the individual user (IE: Payment Details, Site Activity, Commissions , Timecards, Etc...) Splitting these sections into different permissions was the best for my app. I used a different method for permissions where I chose the "Controller.Method" permissions instead of the standard Shield "Group.Permissions" method. I also only provide one group to users (I have 741 users on this app).

This allows me to assign each permission to a user regardless of what group they are in. Some users need "update" access to inventory within the same group that defaults to "read" - I only have to manage users and not groups.

NOTE: This works for my app and may not for yours, the below is my implementation to be used as guidance, the full code is not shown, only the specific parts that apply to this question.

Since all of the information is specific to the user it should be part of the data received by the view. for instance, if the user is "Salary" he will not have a timecard, check the group: 
Pass the permissions and group to the view:

PHP Code:
$user                  auth()->user(); // Check who is logged in
$permission         $user->getPermissions(); // get the permissions for the logged in user
 
  $data = [
      'user'            => $user,
      'group'          => (!empty($user->groups) ? $user->groups[0] : 'Unassigned')
  ];

  return view('users/yourViewName'$data); 

Now in your view you can show data based on group:

PHP Code:
<?php if( $group == 'managers' ): ?>
Enter Manager Data Here..
<?php endif; ?>

and Permissions:

PHP Code:
<?php if( $permission == 'users.commissions' ): ?>
Enter Commission Data Here..
<?php endif; ?>

Hope this helps someone else looking for answers Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB