Welcome Guest, Not a member yet? Register   Sign In
Best way to handle user permissions in a view
#1

[eluser]jprateragg[/eluser]
I'm in the process of migrating an application to CI. In my original app, I used a function to see if a user was allowed to perform a certain function. Depending on the result of the function (true/false), a hyperlink would be constructed, or just text without a hyperlink. Since I probably can't do this in my view, what's the best way to handle this? I don't want to clutter up my controller with functions to check this and output a string, but if it's the best way I guess I'll have to. I'm just curious as to how other people do this. Thanks!
#2

[eluser]JoostV[/eluser]
Say you have a logged_in() method in an auth library that returns TRUE if logged in, you can do this in your view:
Code:
echo $this->auth->logged_in() ? anchor($href, $title) : $title;
#3

[eluser]jprateragg[/eluser]
I had thought about that, but I have to check for different levels of user permissions. I'm using the role/permission method for maintaining user permissions. I would need the library to be able to access my models and session data.
#4

[eluser]JoostV[/eluser]
So, do
Code:
echo $this->auth->can_access($role) ? anchor($href, $title) : $title;
Smile
#5

[eluser]jprateragg[/eluser]
I think I have this figured out now. Thanks for the advice. I'm just going to create an Auth library, load the CI instance, and just use that data to traverse through the user permissions and other security permissions. Thanks!
#6

[eluser]PhilTem[/eluser]
The best way to do what you want to do is create an auth-helper that provides the appropriate function(s) you need so that you can call it in your view (without breaking the MVC-rule).

Say, your auth-helper has a method has_access, then you would just do
Code:
echo has_access('resource to access') ? anchor($url, $title) : $title;
#7

[eluser]JoostV[/eluser]
Check out Ion auth. It's excellent.
#8

[eluser]jprateragg[/eluser]
[quote author="PhilTem" date="1352754135"]The best way to do what you want to do is create an auth-helper that provides the appropriate function(s) you need so that you can call it in your view (without breaking the MVC-rule).

Say, your auth-helper has a method has_access, then you would just do
Code:
echo has_access('resource to access') ? anchor($url, $title) : $title;
[/quote]
The only problem is I have to use some application logic on which parts of the application can be modified/updated based on a centralized workflow. My current method fetches data from the database, analyzes, it combines it with the current logged in user's permissions, and determines if the item should be turned into a link. This keeps me from having to pass 5-6 parameters to the function. I would hate to have to break the MVC rule, but I don't know of another way to do this. Should I just fetch all that data in my $data[] array in my controller, than pass that data to a helper function?
#9

[eluser]JoostV[/eluser]
Basically you have two options:
1. Make it so that the non-accessible data are not in the array you send to the view.
2. Make it so that the view does not display the non-accessible data.

I would go for option 1.




Theme © iAndrew 2016 - Forum software by © MyBB