1

Good day, I need to create a view, which only shows data related to the user logged in user. When I have a user called "john smith", I create a contents as I like, BUT I don't want other users to see them.

A content can have multiple input from different users, so ONLY admin can see all content. while, each user, can only see his OWN content which he posted him self.

Think of it like a database system, where each student can add projects for his own records, but each student can see ONLY his own.

I tried looking around first, and I came across this question https://stackoverflow.com/questions/20840490/drupal7-filter-view-based-on-current-users-roles which seemed bit related, but I got lost in the too much explanation because they not entirely related to what I want to achieve.

In user roles permission, I perfectly found (add content, edit own content) but its missing one more thing which I want.."view own content"!!

I think it has something to do with filed permission, or something like when adding a content, set its permission to be view my that user only.

In coding the view to show ONLY table rows related to the that user, maybe something like this:

global $user;
if (in_array (student_field_name  == $user))
//view the row related to that user;

if (in_array('administrator', $user->uid))
//show ALL data rows available

To summarize, I want the user to be able to SEE his own data only. while admin will be able to see ALL content view of that page.

2
  • So add a filter for the author field of a Node is equal to the logged in user. The author created the content -- nobody else.
    – tenken
    Commented Jul 1, 2015 at 15:02
  • hmm, i see..maks sense..im newbie in drupal actually, so im not quite sure how its done..can u guide me on how its done? do i go to contextual filter? from there i only find "tracker - user" then , choosing tracker -user ID..which seems to be the one..after that do i choose default value and insert php code to suite the condition? (im not sure wht to type there to match the values together as condition, like if $user == field name then excute or something like that) thanks Commented Jul 1, 2015 at 16:25

2 Answers 2

3

To create a view that is limited to content created by the current user (logged in) and Admin:

  • create the view as you normally would

  • Under Advanced, Relationship, create a relationship to Content:Author

  • Create a Filter, User:Current, Logged in Yes, using the author relationship

  • Create a filter, Author ID = admin

  • Create a Filter Group (click on arrow next to Add on Filter Criteria)

  • 'Create a new filter group' and make it look something like this:

enter image description here

This will limit the view to content only created by the currently logged in user and/or Admin, but does not limit access to the nodes themselves, for that you will need a module such as Node Access:

Nodeaccess is a Drupal access control module which provides view, edit and delete access to nodes. Users with the 'grant node permissions' permission will have a grant tab on node pages which allows them to grant access to that node by user or role. Administrators can set default access controls per content type, and also define which roles are available to grant permissions to on the node grants tab.

7
  • God bless u man..it worked, this is what i was looking for. but i have 1 problem..the administrator, which is supposed to see everything now cannot see the contents created by other users!! how can i add this to the current filter to allow admin to see everything? can the code access module do that for me? the module is new to me actually, so i'll test to check how it works. Commented Jul 2, 2015 at 6:53
  • for NodeAccess you should be able to allow the admin to see everything (actually an Admin should have bypass access control permission set which would do that as well). For the View it gets more complicated - you will need to create a new View without the Logged-in user restriction and limit it's Access to Admin only under Page Settings or Block Settings (center top of the Views UI)
    – Geoff
    Commented Jul 2, 2015 at 9:37
  • with your suggested filter and node access.. i stil couldnt get it done! i've spent long hours trying to figure out why is it not viewing everything for admin while limitting the non admin..its like it either all of them get self access only, or all get to see all nodes.. worst case is creating 2 views 1 for admin and one for the nons..but its trouble some cuz i have 10s of contents to edit their views and make them two..is there any alternative way to get the job done? i may have missed something..do u know specific methods to make admin see all content while non admin see their owns? thanks Commented Jul 2, 2015 at 14:02
  • @AbdulazizThedragon see my edit
    – Geoff
    Commented Jul 2, 2015 at 15:14
  • thank u for ur effort but no success bro. just like the previous method, it doesn't show ALL nodes of that content!! it seems that once i add the filter content author or the current, it restricts the nodes to ones created by that user, even for admin :( "knowing that admin is supposed to have full access whether given perm or not" i even checked my permissions (i had previously checked everything), but noticed that i mistakenly chose the ones with "own"content i removed everything related to "own", but i still have same result..no change, still cant get admin to see all existing nodes. Commented Jul 3, 2015 at 9:27
1

In the View, add a contextual filter on Author: Uid, and use the following settings:

WHEN THE FILTER VALUE IS NOT AVAILABLE:

Provide default value Type: PHP Code

Use the following code:

global $user;

if (
    in_array('administrator', $user->roles) || 
    in_array('editor', $user->roles)
)
{return -1;}
else
{return $user->uid;}

WHEN THE FILTER VALUE IS AVAILABLE OR A DEFAULT IS PROVIDED

Specify validation criteria Validator: PHP Code

Use the following code:

if ($argument == -1) 
{return FALSE;}
else
{return TRUE;}

Action to take if filter value does not validate: Display all results for the specified field (this is what will give admins access to all results) And that's it!

Notes:

Compared to my initial settings, the relationship (Content: Author) isn't needed anymore; neither is the "Author" filter (which was brought in by the relationship anyway).

Apparently, for Drupal 6 the condition in the first PHP snippet should rather be if (in_array('super user', array_values($user->roles))).

1
  • i saw this answer..u merely copied it.. but my concern is the user ID rather than the role..because the data fields i want to view should be based on user name. thats why i said i feel lost..Please refer to my question, i have added some extra info im looking for which are related to row, or content. thanks Commented Jul 1, 2015 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.