Permalink
Browse files

fixed merge- comply with db contraints

  • Loading branch information...
1 parent cf7f8e5 commit efc39cf4957596ac66536a5190c0fe1b27fe3531 @hannesvdvreken hannesvdvreken committed Nov 25, 2012
Showing with 33 additions and 7 deletions.
  1. +32 −6 application/models/user_model.php
  2. +1 −1 database.sql
@@ -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 );
+
}
/**
View
@@ -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,

0 comments on commit efc39cf

Please sign in to comment.