codeigniter useraccess design - php

im trying to use codeigniter to create a simple website that involve user restricted areas,
and was wondering how can i design my log in controller so that on every page it load a controller that check if this user is allowed in this page/allow to execute that function.
Question:
how can i autoload this login function at every page.
what is the proper secure logic to check for if user is allowed to do this function ? noting that i have 3 different user types, 1.admin 2.secretary 3.worker so how can i assign functions to there owner ? "admin should be able to assign access to other 2 types"
i have completed my project but i have 0 experience with this login user access control thing so any advice will be appreciated

You should take a look at the "MY_Controller". Basically, you create a file called MY_Controller.php and place it in application/core (http://ellislab.com/codeigniter/user-guide/general/core_classes.html)
You place a class in this file called MY_Controller that extends CI_Controller
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
//check if your user is logged in here
}
}
The construct above will be called first before any other controller methods are invoked. So you can check to see if the user is logged in, and if not, redirect to your login form.
There are a lot of different auth libraries that will likely help you with your second question. Check those out and they may help you (don't remember any off the top of my head, but I know you have options).

Related

CakePHP: How to use a function with a model in every controller?

I have a project which includes admin and user section. Both section use the same controllers, just different functions and templates (ex: viewAdmin() and viewUser()). In function beforeRender() of every controllers, I set variable $admin as true for admin functions and false for user functions.
For authentication, I use Shibboleth. Shibboleth uses data from LDAP, while user types were saved in SQL-Database, that means while it can check if the login and password are false, it can't check if the user is admin or not. An user can go to ADMIN section as long as they use the right action (ex: go to the link http://example.com/tool/viewAdmin).
To prevent this, I will have to:
Load model Users
Compare the environment variable uid (login name) with the "login" columns in Users table in my SQL-Database
See the "type" column in Users table to know if user is admin or not.
Compare the result with value of $admin and redirect to an error page when necessary.
The problem is: I don't want to repeat those steps for EVERY controllers.
Currently I have 2 ideas:
Write a function in UsersController, and use it in every controllers.
Create a component and load it in every controllers.
Both methods require me changing code in all controllers. I would like to hear a better way with less work, perhaps by changing app.php or bootstrap.php.
Any suggestion is appreciated.
To share methods in CakePHP controllers you can do:
Create component and include in controller
Or create method in AppController and use it in child controllers
Or PHP way create Trait.
But when you authorize users, then all user data is stored in session, incl. is user roles (example admin, regular, member,.. )
Use the official CakePHP authentication plugin and extend the LDAP adapter with the additional code check you need. This is very easy to do and also a very clean way of solving the problem. Disclaimer: I'm one of the authors of the authentication plugin. https://github.com/cakephp/authentication
Or if you want to stay agnostic to any framework, use my library that is based on the authentication plugin and was decoupled from any framework but still works just nice with Cake https://github.com/Phauthentic/authentication.

PHP MVC Authentication before call class and methods

I tried to make PHP login and authentication system base on MVC. I even need some guide about how can I prevent to access all classes on controller/admin folder without proper login.
I have two ideas :
1- make constructor for all classes on admin folder and check for logged in session and then only allow calls to any other methods in that class.
2- add a secret word at the end of all methods name on my admin folder. Then all calls to those methods will redirected to __call function for check session and if that process done successfully then try to call admin's methods by adding that secret word.
I don't know which one is the better way or is there any other solution?

Organize web application with codeIgniter

I have a problem with the organization of views on CodeIgniter.
Project: Create a simple web application that permits to manage a library(bookcase).
I created 3 models : Member, Categories, Books with their respective controllers.
I implemented the Member model with its controller.
In the member_controller we have:
public function login(){
$this->load->helper('form');
$this->load->helper('email');
$this->load->library('form_validation');
$mail = $this->input->post('mail');
$pass = $this->input->post('pass');
$data['mail'] = $mail;
$data['pass'] = $pass;
$this->form_validation->set_rules('mail', 'mail', 'required');
$this->form_validation->set_rules('pass', 'pass', 'required');
if ($this->form_validation->run() === TRUE)
{
$result=$this->membre_model->login($mail,$pass);
if($result==TRUE){
$this->load->view('templates/header.php');
$this->load->view('membre/logged',$data);
$this->load->view('templates/footer.php');
}
Once user is logged, I want to show all existing categories he previously created(so get them from the database).
How can I do that?
Do I have to call a function of the category controller in the login function of the member controller?
Do I have to load the category view from the Login function?
Do I have to build the site from just one controller(the member controller)?
How to build the webapp with differents views of differents controllers?
Finally, the thing I don't get is how the different controller communicate between them.
How can I do that?
You would have to add an exception when loading each page that will redirect user based on their login status. In other words, if user is logged in, bring him to the application. If user does not exist, redirect him to the registration page.
This can be done by verifying if the user is logged in based on stored session values. If this session is stored than you can let the user view the page. Here is a great tutorial explaning a simple Login system for Codeigniter.
http://www.codefactorycr.com/login-with-codeigniter-php.html
In my opinion, I usually use an Authentication library to simplify the Login system for my application. I would use Ion Auth, it has a great documentation explaning all the functions you can use.
Do I have to call a function of the category controller in the login function of the member controller?
You can simply use the category controller as you would normally. You would change the pages information or redirect the user out based on the session information stored in their browser.
Do I have to load the category view from the Login function?
You would load the category view from the category controller. The login controller would redirect the the user to the category controller after the login is performed.
Do I have to build the site from just one controller(the member controller)?
No, you can have as many controllers as you like.
How to build the webapp with differents views of differents controllers?
A controller would represent a section of your site. This controller would load multiple views for different things. Here is a great little tutorial explaning the MVC worklow. This will help you understand the process.
You can call one controller from another using the URI, so for example you might have an entry like this in your routes file:
$route['books/get_books_by_user/(:any)'] = "books/get_books_by_user/$1";
you could then call form your login controller:
redirect('books/get_books_by_user/'.$user_id)
you would then handle this in a get_books_by_user method in your books controller. For example using:
$user_id = $this->uri->segment(3);
$collection = $this->books_model->get_books_by_user($user_id);
The problem you have is that if you dont want anyone other than the user to see their own book list you have to check the user is logged in from you books controller.
This is why most user auth scripts are presented as a library which can be accessed from any controller. If your auth is not laid out like this you could store user details as session data and check it from there, this would mean you wouldnt have to pass the username via the uri. Check out session docs here for details: http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

How to add authentication to RECESS?

I have a RECESS based project. I want to add password protection to the views where I add/edit/delete my new model objects, they should only be accessed after passing through a password entry view and if someone tries to access directly the said views should again get redirects to the login page. Any help with that?
Yes you can.
create a class (authController) to handle the authentication connect to the database to query the user info
include the authController file in the main Application controller ie (MyRecessApp.class.php)
add the conditionals in your master.layout.php to check if the user is logged in and whether to render content or a login form
IMPORTANT: place the session_begin() that the authController needs in the recess-conf.php file.
Recommended: I ususally add the "loginUser" method to the User controller or you could have the User controller extend the authControllers methods

Codeigniter - Authentication Library or Custom Controller?

I'm wondering what is best in my case. I'm building a site using CodeIgniter with two main sections:
the public part avalaible to everyone
the private one only for registered users
In each page of the public area (one controller) I want to put a sign in form and a sign up link and if the users is logged in he has to be redirected to the private area or a link to it may be shown.
Now I have two choices:
A user controller is the first thing I thought of but in each page of the site I need to control if the user is logged and this is impossible or very bad since I'm using another Controller
So I started working on a library but I'm not sure how implement it (for example form validation should be achieved by the controller or by the library itself?, what about database connection since I haven't a model?)
What do you think is the best? Why? and how would you implement it?
(and yes I like reinventing the wheel and not using an existing library mainly because i want to learn how to do it)
Super Controller
=>assign user data,settings,configs etc
|-----private controller extends super controller
=>check user credentials
|-----admin controller extends super controller
=>check user && admin credentials
Your super controller is your public controller as long as you only do assignments, no checking...
Anything you want public just extends super controller
Anything you want private extends private controller
Form validations and query jobs should be carried out with the controller itself. The library act like a tool no need to implement these things in them but as a need you can use queries in them to check some data but it's better to be worked in the controllers.
The idea for having a log flag is to:
When user is signed in, create a session for it to show the access.
Check every time the session for the private parts.

Categories