public function testAddDeleteShadowUser()
 {
     //    Dal::register_query_callback("explain_query");
     global $network_info;
     echo "getting a user\n";
     $user = Test::get_test_user();
     $testusername = $user->first_name . " " . $user->last_name;
     echo "test user = {$testusername}\n";
     $namespace = 'php_unit';
     // testuser data
     $testdata = array('user_id' => "pa_" . $user->user_id, 'login_name' => 'testuser', 'email' => $namespace . $user->email, 'first_name' => $user->first_name, 'last_name' => $user->last_name);
     echo "TEST DATA:\n";
     print_r($testdata);
     $shadow_user = new ShadowUser($namespace);
     echo "Test load this shadow user, this should fail\n";
     $sh = $shadow_user->load($testdata['user_id']);
     $this->assertNull($sh);
     echo "Create a shadow user\n";
     $shadow_user = ShadowUser::create($namespace, $testdata, $network_info);
     echo "SHADOW USER DATA:\n";
     print_r($shadow_user);
     $this->assertNotNull($shadow_user);
     echo "Test updating the data\n";
     $testdata2 = $testdata;
     $testdata2['email'] = $namespace . "add" . $user->email;
     $testdata2['login_name'] = "newlogin";
     $testdata2['first_name'] = "newName";
     print_r($testdata2);
     $su2 = new ShadowUser($namespace);
     // load this with new data
     $su2->load($testdata2);
     unset($su2);
     Cache::reset();
     // now load it only via the original remote uid
     $su3 = new ShadowUser($namespace);
     $su3->load($testdata['user_id']);
     echo "UPDATED SHADOW USER DATA:\n";
     print_r($su3);
     echo "Delete it\n";
     ShadowUser::delete($shadow_user->user_id);
     // there should not be a shadow user of this id anymore
     $this->assertNull($shadow_user->load($testdata['user_id']));
 }
Example #2
0
 static function create($namespace, $userinfo, $network_info)
 {
     Logger::log("ShadowUser::create " . serialize($userinfo), LOGGER_ACTION);
     // setup the needed info
     if (empty($userinfo['login_name'])) {
         $userinfo['display_login_name'] = $userinfo['first_name'] . '.' . $userinfo['last_name'];
     } else {
         $userinfo['display_login_name'] = $userinfo['login_name'];
     }
     // this is the real internal PA login_name
     // which should NOT be displayed
     // instead use the display_login_name
     $userinfo['login_name'] = $namespace . "." . $userinfo['user_id'];
     $userinfo['confirm_password'] = $userinfo['password'] = substr(md5($userinfo['email'] . rand()), 0, 12);
     // Test to see if the email was used before
     $res = Dal::query("SELECT user_id FROM users WHERE email=?", array($userinfo['email']));
     if ($res->numrows() > 0) {
         // oops email has been used
         // use the prefix.email@address,tld format
         // to ensure we have an unique string for email
         $userinfo['email'] = $userinfo['user_id'] . "+" . $userinfo['email'];
     }
     $reg_user = new User_Registration();
     if ($reg_user->register($userinfo, $network_info)) {
         // Success!
         $reg_user->newuser->set_last_login();
         // also save the external user_id
         $reg_user->newuser->set_profile_field($namespace, 'user_id', $userinfo['user_id'], 0);
         $reg_user->newuser->set_profile_field($namespace, 'display_login_name', $userinfo['display_login_name'], 0);
         $reg_user->newuser->is_active = ACTIVE;
         $reg_user->newuser->save();
         // load it as a shadow user
         Cache::reset();
         $su = new ShadowUser($namespace);
         $su->load($userinfo['user_id']);
         // and make sure all info is actually stored
         $su->sync($userinfo);
         Logger::log("ShadowUser::create done " . serialize($userinfo), LOGGER_ACTION);
         return $su;
     } else {
         throw new PAException(BAD_PARAMETER, $reg_user->msg);
         return NULL;
     }
 }
 static function create($namespace, $userinfo, $network_info)
 {
     // setup the needed info
     if (empty($userinfo['login_name'])) {
         $userinfo['display_login_name'] = $userinfo['first_name'] . '.' . $userinfo['last_name'];
     } else {
         $userinfo['display_login_name'] = $userinfo['login_name'];
     }
     // this is the real internal PA login_name
     // which should NOT be displayed
     // instead use the display_login_name
     $userinfo['login_name'] = $namespace . "." . $userinfo['user_id'];
     $userinfo['confirm_password'] = $userinfo['password'] = substr(md5($userinfo['email'] . rand()), 0, 12);
     $reg_user = new User_Registration();
     if ($reg_user->register($userinfo, $network_info)) {
         // Success!
         $reg_user->newuser->set_last_login();
         // also save the external user_id
         $reg_user->newuser->set_profile_field($namespace, 'user_id', $userinfo['user_id'], 0);
         $reg_user->newuser->set_profile_field($namespace, 'display_login_name', $userinfo['display_login_name'], 0);
         // load it as a shadow user
         Cache::reset();
         $su = new ShadowUser($namespace);
         $su->load($userinfo['user_id']);
         return $su;
     } else {
         throw new PAException(BAD_PARAMETER, $reg_user->msg);
         return NULL;
     }
 }
 function render_for_ajax()
 {
     $op = $this->params["op"];
     $this->gid = @$this->params['blog_id'];
     if ($op != 'paging' && empty(PA::$login_user)) {
         return __("Login required");
     }
     switch ($op) {
         case "save_post":
             // $this->note = "Save piost goes here.";
             // validation
             // return "<pre>".print_r($this->params,1)."</pre>";
             $post = $this->params;
             $this->cid = @$post['cid'];
             $errmsg = '';
             $err = FALSE;
             if (empty($post['title'])) {
                 $errmsg .= __("Please add a title.");
                 $err = TRUE;
             } else {
                 $post['title'] = $this->html($post['title']);
             }
             if (empty($post['body'])) {
                 $errmsg .= __("Please add some text.");
                 $err = TRUE;
             } else {
                 $post['body'] = $this->html($post['body']);
             }
             if ($err) {
                 $this->err = $errmsg;
                 foreach ($post as $k => $v) {
                     $this->content->{$k} = $v;
                 }
                 $this->inner_template = 'newpost.tpl';
             } else {
                 $tags = array();
                 if (!empty($post['tags'])) {
                     foreach (explode(',', $post['tags']) as $term) {
                         $tr = trim($term);
                         if ($tr) {
                             $tags[] = $tr;
                         }
                     }
                 }
                 $post_saved = BlogPost::save_blogpost($this->cid, PA::$login_user->user_id, $post["title"], $post["body"], NULL, $tags, $this->gid);
                 if (empty($post_saved['cid'])) {
                     $this->note = "<pre>" . print_r($this, 1) . "</pre>";
                     $this->err = "<pre>" . print_r($post_saved['errors'], 1) . "</pre>";
                     foreach ($post as $k => $v) {
                         $this->content->{$k} = $v;
                     }
                     $this->inner_template = 'newpost.tpl';
                 }
             }
             break;
         case "new_post":
             $this->inner_template = 'newpost.tpl';
             break;
         case "edit_post":
             $this->inner_template = 'newpost.tpl';
             $this->cid = @$this->params['cid'];
             $this->content = NULL;
             if ($this->cid) {
                 $this->content = Content::load_content((int) $this->cid, (int) PA::$login_uid);
             }
             break;
         case "delete_post":
             $post = $this->params;
             // owner check would go here
             try {
                 Content::delete_by_id($post['cid']);
                 $this->note = __("Post was deleted successfully.");
                 unset($this->params['cid']);
                 // or we'd have a permalink to a post we no longer have
             } catch (PAException $e) {
                 $this->err = __("There was an error deleting this post: ") . $e->getMessage();
             }
             break;
         case "remove_author":
             // the group is not loaded at this point soo we do it here
             $g = ContentCollection::load_collection($this->gid, PA::$login_user->user_id);
             // unjoin user to group
             if ($g->leave($this->params['pa_id'])) {
                 $this->note = "Successfully removed author.";
             } else {
                 $this->err = "Couldn't remove author.";
             }
             break;
         case "add_author":
             // the group is not loaded at this point soo we do it here
             $g = ContentCollection::load_collection($this->gid, PA::$login_user->user_id);
             // find real PA user_id
             $su = new ShadowUser($this->skin);
             try {
                 $su->load($this->params['remote_id']);
                 if ($su->user_id) {
                     // join user to group
                     if ($g->join($su->user_id)) {
                         $this->note = "Successfully removed author.";
                         unset($this->params['remote_id']);
                     } else {
                         $this->err = "Couldn't add author.";
                     }
                 } else {
                     $this->err = "Couldn't add author with UserID " . $this->params['remote_id'] . " no such user.";
                 }
             } catch (PAException $e) {
                 $this->err = "There was an error adding author: " . $e->getMessage();
             }
             break;
         default:
             break;
     }
     return $this->render();
 }
Example #5
0
function peopleaggregator_deleteShadowUser($args)
{
    global $_PA;
    $remote_user = @$args['remoteUser'];
    if (!$_PA->enable_widgetization_server) {
        throw new PAException(OPERATION_NOT_PERMITTED, "Widgetization disabled; you cannot use remote user functions");
    }
    require_once PA::$path . "/api/User/ShadowUser.php";
    $remote_id = explode(":", $remote_user);
    if (count($remote_id) == 1) {
        throw new PAException(INVALID_ID, "Remote user IDs must be of the form 'namespace:id'");
    }
    $u = new ShadowUser($remote_id[0]);
    $u->load($remote_id[1]);
    if (!$u->user_id) {
        throw new PAException(USER_NOT_FOUND, "Failed to locate user '" . $remote_id[1] . "' in namespace '" . $remote_id[0] . "'");
    }
    $u->remove();
    return array("success" => TRUE, "msg" => "User {$remote_user} was deleted.");
}