<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class User_model extends CI_Model { | |
private $db1; | |
public function __construct() { | |
parent::__construct(); | |
$this->db1 = $this->load->database( 'leadgen', true ); | |
} | |
public function setup_user( $user_info, $token_obj ) { | |
$db_data = array( | |
'google_id' => $user_info['id'], | |
'email' => $user_info['email'], | |
'verified_email' => $user_info['verified_email'], | |
'name' => $user_info['name'], | |
'given_name' => $user_info['given_name'], | |
'family_name' => $user_info['family_name'], | |
'link' => $user_info['link'], | |
'picture' => $user_info['picture'], | |
'gender' => $user_info['gender'], | |
'locale' => $user_info['locale'], | |
'hd' => $user_info['hd'], | |
'modified' => time(), | |
); | |
$token_keys = array( 'access_token', 'token_type', 'expires_in', | |
'id_token', 'refresh_token' ); | |
foreach ( $token_keys as $key ) { | |
if ( property_exists( $token_obj, $key ) ) { | |
$db_data[$key] = $token_obj->$key; | |
} | |
} | |
if ( property_exists( $token_obj, 'created' ) ) { | |
$db_data['token_created'] = $token_obj->created; | |
} | |
$user_id = NULL; | |
$this->db1->where( 'google_id', $user_info['id'] ); | |
$query = $this->db1->get( 'admin_user' ); | |
if ( $query->num_rows() == 1 ) { | |
$user = $query->row_array(); | |
$user_id = $user['id']; | |
$this->db1->where( 'id', $user_id ); | |
$this->db1->update( 'admin_user', $db_data ); | |
} | |
else { | |
$db_data['created'] = time(); | |
$this->db1->insert( 'admin_user', $db_data ); | |
$user_id = $this->db1->insert_id(); | |
} | |
return $user_id; | |
} | |
public function user_load( $id = NULL ) { | |
$user = NULL; | |
if ( $id && is_numeric( $id ) ) { | |
$this->db1->where( 'id', $id ); | |
$query = $this->db1->get( 'admin_user' ); | |
if ( $query->num_rows() == 1 ) { | |
$user = $query->row_array(); | |
} | |
} | |
return $user; | |
} | |
} | |
/* | |
represents the user table: | |
CREATE TABLE `admin_user` ( | |
`id` mediumint(8) unsigned NOT NULL auto_increment, | |
`access_token` tinytext, | |
`token_type` tinytext, | |
`expires_in` mediumint(8) unsigned default NULL, | |
`id_token` text, | |
`refresh_token` text, | |
`token_created` bigint(20) unsigned default NULL, | |
`google_id` tinytext, | |
`email` tinytext, | |
`verified_email` tinyint(3) unsigned default NULL, | |
`name` tinytext, | |
`given_name` tinytext, | |
`family_name` tinytext, | |
`link` tinytext, | |
`picture` tinytext, | |
`gender` tinytext, | |
`locale` tinytext, | |
`hd` tinytext, | |
`created` bigint(20) unsigned default NULL, | |
`modified` bigint(20) unsigned default NULL, | |
`is_admin` int(1) unsigned NOT NULL default '0', | |
PRIMARY KEY (`id`), | |
KEY `by_gid` (`google_id`(50)) | |
) | |
*/ | |
/* End of the User_model.php file */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment