Welcome Guest, Not a member yet? Register   Sign In
Avoid using other libraries in own library
#1

(This post was last modified: 08-03-2016, 11:58 AM by Call-Me-Captain.)

Heya guys!

I have another (probably stupid - I apologize in advance) question.

After getting a lot of advice in one of my previous threads, I figured that I'd like to move most of my controllers' functionality into libraries.
Also, in order to keep those libraries as independent as possible, I am trying to avoid using other libraries in my own.

To be even more specific: Right now, I'm implementing a basic user system as a library. For the reason above and also because I would like to keep my login (And other functionality) functional even if no HTML form is provided, I do not want to use libraries such as CI's form validator in my library's code.
Basically, the code in the library should only give basic functionality that runs depending on a certain input. The input shouldn't be read-out by itself, but rather by the code in the controllers, or some other place.

I hope I'm making sense, but if I'm not, here's the code I currently have - Followed by a small description of my actual problem.
(I hope that I created this thread in the right sub-forum, but it looks like a "Best practice" problem to me, since I could easily solve it by removing the barriers I put up myself - Obviously, I do not want to do that, though.)

In my library:
PHP Code:
To clarify"cUser" is a CI Model used to perform queries to the database.
CI is an instance of CodeIgniter assigned to a class variable by $this->CI = &get_instance();
/**
 * Log-in an user with his given credentials. Logged-in user's ID will be stored in his session.
 * $username - Username of the user.
 * $password - Not yet hashed password of the user.
 * Return - array ( "result" => true/false, "message" => "Specify the result" )
**/
function login($username$password){
    
//Check if the user is already logged in. If he is, return success message.
    
if( isset($this->CI->session->auth_uid) ) return array("result" => true"message" => "Already logged in.");
    
    
//Check if the username and password match in the database. Returns -1 on failure or userID on success.
    
$qr $this->CI->cUser->matchUserPass($usernameMD5($password));
    
    
//Failure? Return the information.
    
if($qr == -1) return array("result" => false"message" => "Username or Password invalid.");
    
    
//Success? Set session userID. Then, return success.
    
else $this->CI->session->auth_uid $qr;
    return array(
"result" => true"message" => "Successfully logged in.");


In my controller (Old function, not adapted to the use of my library. My following question will be about how to best adapt it.):
PHP Code:
function login($lastURL "/user/"){ //set default to view user (todo)
    
if( isset($this->session->auth_uid) ){
        
$this->session->set_flashdata('auth_msg''Already logged in.'); //todo languages
        
redirect($lastURL);
    }
    
$this->load->library('form_validation');
    
$this->form_validation->set_rules("username""Username""required");//todo add more rules
    
$this->form_validation->set_rules("password""Password""required");
    if(
$this->form_validation->run() == FALSE){
        
$this->load->view("Login");
    }else{ 
//todo dont send pass in plain text (hash on client side or https)
        
$qr $this->user->matchUserPass($this->input->post('username'), MD5($this->input->post('password')));
        if(
$qr == -1){
            
$this->session->auth_msg "Username or Password invalid.";
            
$this->session->mark_as_flash('auth_msg');
            
$this->load->view("Login");
        }else{
            
$this->session->set_flashdata('auth_msg''Successfully logged in.'); //todo languages
            
$this->session->auth_uid $qr;
            
redirect($lastURL);
        }
    }

Last code before moving on to my question. Here's approximatively how I'd like my new login function to look:
PHP Code:
//My library is loaded in the contructor or something.

function login($lastURL "/user/"){ //set defaul to view user //todo:

    
$this->load->library('form_validation');
    
$this->form_validation->set_rules("username""Username""required");
    
$this->form_validation->set_rules("password""Password""required");
    if(
$this->form_validation->run() == FALSE){
        
$this->load->view("Login");
    }else{ 
//todo dont send pass in plain text
        
$result $this->usersystem->login($this->input->post("username"), $this->input->post("password");
 
               $this->session->set_flashdata('auth_msg'$result['message']);
        if(
$result['result']) redirect($lastURL);
 
               else $this->load->view("Login");
    }

There are multiple problems with that code, so I obviously can't leave it like that.
1) I can't check if the user is already logged in. Because if he is, the login page will get displayed anyway. Running my library's function before the form validation would also be bullshit though, since I don't have anything I can pass as argument.
2) Even though I wanted to be library independent, I'm still using the session library in my own library. This is a minor problem, but if someone has an idea on how to beautifully avoid this, I'd be glad to hear it!
3) Nah, actually the main problem is the first. I do not want to call my library's function twice, but I don't see another solution that wouldn't completely butcher the code.
Does anyone have an idea Big Grin?
Thanks!
Reply


Messages In This Thread
Avoid using other libraries in own library - by Call-Me-Captain - 08-03-2016, 10:57 AM



Theme © iAndrew 2016 - Forum software by © MyBB