Skip to content

Commit

Permalink
fixed merge- comply with db contraints
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesvdvreken committed Nov 25, 2012
1 parent cf7f8e5 commit efc39cf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
38 changes: 32 additions & 6 deletions application/models/user_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,46 @@ function get_clients( $user_id ){

/*
* Merge user_id_2 to user_id_1
*
* This is bad for all OAuth2.0 clients who have connected to plugid before.
* They can still use their Authentication tokens, but if they have saved the client's id
* it can be different from what we have, hence we have replaced one with another.
*/
function merge( $user_id_1, $user_id_2 ){
// user2 attributes user1
$data = array( 'user_id' => $user_id_1 );
$where = array( 'user_id' => $user_id_2 );

// updating tables
$this->db->update( 'user_tokens', $data, $where );
$this->db->update( 'clients', $data, $where );
$this->db->update( 'auth_tokens', $data, $where );
$this->db->update( 'auth_codes', $data, $where );
// merging tokens (do this for db key contraints)
$tokens1 = $this->db->get_where('user_tokens', $data )->result();
$tokens2 = $this->db->get_where('user_tokens', $where )->result();

foreach( $tokens2 as $t2 ){
$pre_exists = FALSE ;
foreach( $tokens1 as $t1 ){
if( $t2->service_type == $t1->service_type ){
$pre_exists = TRUE ;
break;
}
}
if( !$pre_exists ){
$where['service_type'] = $t2->service_type ;
$this->db->update( 'user_tokens', $data, $where );
// give it to the pre_existing user
}
}

unset($where['service_type']);
$this->db->delete( 'user_tokens', $where ); // delete all remaining, because the pre_existing user already had them
$this->db->delete( 'auth_tokens', $where );
$this->db->delete( 'auth_codes', $where );

//delete user from users table
$this->db->delete('users', $where);
$this->db->delete( 'users', $where );

// updating tables
$this->db->update( 'clients', $data, $where );

}

/**
Expand Down
2 changes: 1 addition & 1 deletion database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ CREATE TABLE IF NOT EXISTS `users` (

CREATE TABLE IF NOT EXISTS `user_tokens` (
`user_id` int(11) NOT NULL,
`service_type` enum('foursquare','facebook','twitter','google','viking') NOT NULL,
`service_type` varchar(32) NOT NULL,
`ext_user_id` varchar(32) NOT NULL,
`access_token` varchar(128) DEFAULT NULL,
`refresh_token` varchar(128) DEFAULT NULL,
Expand Down

0 comments on commit efc39cf

Please sign in to comment.