Intro to User Profiles

User profile tables allow you to have unique data that is tied to your users, while acknowledging that not all users are the same. For instance, you may need to store different types of data for a customer versus an admin, or one type of employee versus another. By default Community Auth no longer integrates profile tables, but setting them up is simple, as you can see by the rather short length of this blog post.

The goal of this blog post is to show you how to retrieve profile data and add it to the other authentication variables that Community Auth makes available.

Please note: Before 11/9/2015 Community Auth had some class visibility settings that may need to be adjusted if you’re trying to follow along, and if you’re version of Community Auth is older. See the following link for details:

https://bitbucket.org/skunkbad/community-auth-for-codeigniter-3/commits/9a95f0531275d6b0965c2df2420213a355f44cd7?at=master

User Profiles Need a Table

For the purposes of showing you how to retreive some profile data, I’m creating a customer profile table. In the default authentication configuration that comes with Community Auth, “customer” is a user role, and is associated with auth level #1. Because this is just an example, I’ve only added two profile fields, first_name and last_name. Get started by creating the new customer_profiles table:

CREATE TABLE IF NOT EXISTS `customer_profiles` (
  `customer_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL,
  `first_name` varchar(28) DEFAULT NULL,
  `last_name` varchar(28) DEFAULT NULL,
  PRIMARY KEY (`customer_id`),
  FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`) 
  ON DELETE CASCADE
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

Make sure to add the table to Community Auth’s DB tables config file. This is necessary if you plan to use my code below.

$config['customer_profiles'] = 'customer_profiles';

MY_Controller

Community Auth comes with a MY_Controller, and you need to add some stuff to it. Notice that when you are done, you’ll be able to access the selected customer profile data in your controllers, views, and anywhere that CodeIgniter config is available.

public $profile_data = NULL;

protected function _set_user_variables()
{
  parent::_set_user_variables();

  // For controllers
  $this->profile_data = array(
    'first_name' => $this->auth_data->first_name,
    'last_name' => $this->auth_data->last_name,
  );

  // For CI config
  $this->config->set_item( 'profile_data', $this->profile_data );

  // For views
  $this->load->vars( $this->profile_data );
}

New File: My_auth_model.php

We need to enhance what happens during login and when checking if somebody is logged in. What I’m doing here is just adding in the customer profile data if the logged in user is a customer.

<?php

class My_auth_model extends Auth_model {
  
  public function __construct()
  {
    parent::__construct();
  }

  // --------------------------------------------------------------

  public function get_auth_data( $user_string )
  {
    if( $auth_data = parent::get_auth_data( $user_string ) )
    {
      return $this->add_profile_data( $auth_data );
    }

    return FALSE;
  }

  // --------------------------------------------------------------

  public function check_login_status( $user_id, $user_login_time )
  {
    if( $auth_data = parent::check_login_status( $user_id, $user_login_time ) )
    {
      return $this->add_profile_data( $auth_data );
    }

    return FALSE;
  }
  
  // -----------------------------------------------------------------------

  public function add_profile_data( $auth_data )
  {
    if( $auth_data->auth_level == '1' )
    {
      // Selected profile data
      $selected_columns = array(
        'first_name',
        'last_name'
      );

      $query = $this->db->select( $selected_columns )
        ->from( config_item('customer_profiles') )
        ->where( 'user_id', $auth_data->user_id )
        ->limit(1)
        ->get();

      if( $query->num_rows() == 1 )
      {
        foreach( $query->row_array() as $k => $v )
        {
          $auth_data->$k = $v;
        }
      } 
    }

    return $auth_data;
  }
  
  // -----------------------------------------------------------------------
}

/* End of file My_auth_model.php */
/* Location: /application/models/My_auth_model */

Make sure to tell Community Auth to use My_auth_model by changing Community Auth’s model in config/authentication.php:

$config['declared_auth_model'] = 'my_auth_model';

You should also autoload My_auth_model in config/autoload.php:

// Make sure my_auth_model is AFTER auth_model
$autoload['model'] = array(
  'auth_model', 'my_auth_model'
);

Conclusion

The code in this blog post should have allowed you to implement user profiles into your installation of Community Auth. This is by no means the only way to do this, but kept simple for the sake of showing that it is simple to do. Other things, like creating the profile records, updating the profiles records, and other application specific details have been left out on purpose.