Permalink
Browse files

Implement membership connection caps

  • Loading branch information...
1 parent 66fc1d4 commit 2011b6ee13675ab6a1281714cea9663431175896 @ginatrapani ginatrapani committed Jan 5, 2014
View
@@ -53,4 +53,43 @@ public function testConstructor() {
$this->assertFalse($owner->is_admin);
$this->assertTrue($owner->is_activated);
}
+
+ public function testIsMember() {
+ $owner_values = array('id'=>10, "full_name"=>"ThinkUp J. User", "email"=>'tu_user@example.com',
+ 'last_login'=>'1/1/2010', 'is_admin'=>0, 'is_activated'=>1, 'account_status'=>'', 'failed_logins'=>19,
+ 'email_notification_frequency' => 'both', 'membership_level'=>null );
+
+ $owner = new Owner($owner_values);
+ $this->assertFalse($owner->isMemberAtAnyLevel());
+
+ $owner->membership_level = 'Member';
+ $this->assertTrue($owner->isMemberAtAnyLevel());
+ $this->assertTrue($owner->isMemberLevel());
+ $this->assertFalse($owner->isProLevel());
+
+ $owner->membership_level = 'Early Bird';
+ $this->assertTrue($owner->isMemberAtAnyLevel());
+ $this->assertTrue($owner->isMemberLevel());
+ $this->assertFalse($owner->isProLevel());
+
+ $owner->membership_level = 'Late Bird';
+ $this->assertTrue($owner->isMemberAtAnyLevel());
+ $this->assertTrue($owner->isMemberLevel());
+ $this->assertFalse($owner->isProLevel());
+
+ $owner->membership_level = 'Pro';
+ $this->assertTrue($owner->isMemberAtAnyLevel());
+ $this->assertFalse($owner->isMemberLevel());
+ $this->assertTrue($owner->isProLevel());
+
+ $owner->membership_level = 'Exec';
+ $this->assertTrue($owner->isMemberAtAnyLevel());
+ $this->assertFalse($owner->isMemberLevel());
+ $this->assertFalse($owner->isProLevel());
+
+ $owner->membership_level = 'pajamas';
+ $this->assertFalse($owner->isMemberAtAnyLevel());
+ $this->assertFalse($owner->isMemberLevel());
+ $this->assertFalse($owner->isProLevel());
+ }
}
@@ -92,6 +92,16 @@ public function testGetByInstance() {
$this->assertEqual(sizeof($owner_instances), 3);
}
+ public function testGetByOwner() {
+ $dao = new OwnerInstanceMySQLDAO();
+ $builder1 = FixtureBuilder::build(self::TEST_TABLE_OI, array('instance_id' => 20, 'owner_id'=>50) );
+ $builder2 = FixtureBuilder::build(self::TEST_TABLE_OI, array('instance_id' => 20, 'owner_id'=>51) );
+ $builder3 = FixtureBuilder::build(self::TEST_TABLE_OI, array('instance_id' => 20, 'owner_id'=>52) );
+ $owner_instances = $dao->getByOwner(50);
+ $this->assertIsA($owner_instances, 'Array');
+ $this->assertEqual(sizeof($owner_instances), 1);
+ }
+
public function testInsertOwnerInstance() {
$dao = new OwnerInstanceMySQLDAO();
$result = $dao->insert(10, 20, 'aaa', 'bbb');
@@ -56,7 +56,7 @@ protected function buildData() {
'email'=>'ttuser@example.com', 'is_activated'=>0, 'pwd'=>$pwd1,
'pwd_salt'=>OwnerMySQLDAO::$default_salt, 'activation_code'=>'8888',
'account_status'=>'', 'api_key' => 'c9089f3c9adaf0186f6ffb1ee8d6501c',
- 'email_notification_frequency'=>'both', 'timezone'=>'UTC'));
+ 'email_notification_frequency'=>'both', 'timezone'=>'UTC', 'membership_level'=>'Member'));
$builders[] = FixtureBuilder::build('owners', array('full_name'=>'ThinkUp J. User1',
'email'=>'ttuser1@example.com', 'is_activated'=>1, 'pwd'=>$pwd2,
@@ -87,6 +87,8 @@ public function testGetByEmail() {
$this->assertEqual($existing_owner->account_status, '');
$this->assertEqual($existing_owner->api_key, 'c9089f3c9adaf0186f6ffb1ee8d6501c');
$this->assertEqual($existing_owner->email_notification_frequency, 'both');
+ $this->assertEqual($existing_owner->timezone, 'UTC');
+ $this->assertEqual($existing_owner->membership_level, 'Member');
//owner does not exist
$non_existing_owner = $this->DAO->getByEmail('idontexist@example.com');
@@ -141,6 +141,12 @@
*/
var $options_hash = array();
+ /**
+ * Whether or not to show button to add user
+ * @var bool
+ */
+ var $do_show_add_button = true;
+
public function __construct($owner, $folder_name) {
parent::__construct(true);
$this->owner = $owner;
@@ -155,6 +161,17 @@ public function __construct($owner, $folder_name) {
$plugin_dao = DAOFactory::getDAO('PluginDAO');
$this->plugin_id = $plugin_dao->getPluginId($folder_name);
}
+ if (($owner instanceof Owner) && $owner != null && $owner->isProLevel()) {
+ // For Pro users, cap instances at 10
+ $owner_instance_dao = DAOFactory::getDAO('OwnerInstanceDAO');
+ $owner_instances = $owner_instance_dao->getByOwner($this->owner->id);
+ $total_instances = sizeof($owner_instances);
+ if ($total_instances >= 9) {
+ $this->do_show_add_button = false;
+ $this->addInfoMessage("You've connected ".$total_instances." of 10 accounts to ThinkUp.",
+ 'membership_cap');
+ }
+ }
}
/**
@@ -153,6 +153,20 @@ public function getByInstance($instance_id) {
return $owner_instances;
}
+ public function getByOwner($owner_id) {
+ $q = "SELECT
+ id, owner_id, instance_id, oauth_access_token, oauth_access_token_secret, auth_error
+ FROM
+ #prefix#owner_instances
+ WHERE owner_id = :owner_id";
+
+ $vars = array(':owner_id' => $owner_id);
+ if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
+ $stmt = $this->execute($q, $vars);
+ $owner_instances = $this->getDataRowsAsObjects($stmt, 'OwnerInstance');
+ return $owner_instances;
+ }
+
public function insert($owner_id, $instance_id, $oauth_token = '', $oauth_token_secret = '') {
$q = "INSERT INTO #prefix#owner_instances
(owner_id, instance_id, oauth_access_token, oauth_access_token_secret)
@@ -48,6 +48,7 @@ public function getByEmail($email) {
failed_logins,
api_key,
email_notification_frequency,
+ membership_level,
timezone
FROM #prefix#owners AS o
WHERE email = :email;
@@ -53,12 +53,18 @@ public function doesOwnerHaveAccessToPost(Owner $owner, Post $post);
*/
public function get($owner_id, $instance_id);
/**
- * Get owner instances by an instance id
+ * Get owner instances by an instance ID
* @param int instance_id
* @return array OwnerInstance objects
*/
public function getByInstance($instance_id);
/**
+ * Get owner instances by an owner ID
+ * @param int owner_id
+ * @return array OwnerInstance objects
+ */
+ public function getByOwner($owner_id);
+ /**
* Inserts an owner instance record
* @param int owner_id
* @param int instance_id
@@ -88,6 +88,12 @@ class Owner {
* @var arr Non-persistent, used for UI, array of instances associated with owner.
*/
var $instances = null;
+ /**
+ * Valid values for membership level.
+ * @var array
+ */
+ static $valid_membership_values = array('Early Bird', 'Member', 'Late Bird', 'Pro', 'Exec');
+
public function __construct($row = false) {
if ($row) {
$this->id = $row['id'];
@@ -142,4 +148,29 @@ public function validateRecoveryToken($token) {
$data = explode('_', $this->password_token);
return ((time() - $data[1] <= 86400) && ($token == $data[0]));
}
+
+ /**
+ * Check if the owner is a ThinkUp.com member of any level.
+ * @return bool Whether or not the owner is a member
+ */
+ public function isMemberAtAnyLevel() {
+ return (in_array($this->membership_level, self::$valid_membership_values));
+ }
+
+ /**
+ * Check if the owner is Member level, i.e., Early Bird, Member, or Late Bird.
+ * @return bool Whether or not the owner is a member at member level
+ */
+ public function isMemberLevel() {
+ return ($this->membership_level == 'Member' || $this->membership_level == 'Early Bird'
+ || $this->membership_level == 'Late Bird');
+ }
+
+ /**
+ * Check if the owner is Pro member level.
+ * @return bool Whether or not the owner is a Pro level
+ */
+ public function isProLevel() {
+ return ($this->membership_level === 'Pro');
+ }
}
@@ -28,20 +28,10 @@
*
*/
class FacebookPluginConfigurationController extends PluginConfigurationController {
- /**
- *
- * @var Owner
- */
- var $owner;
- /**
- * Constructor
- * @param Owner $owner
- * @return FacebookPluginConfigurationController
- */
+
public function __construct($owner) {
parent::__construct($owner, 'facebook');
$this->disableCaching();
- $this->owner = $owner;
}
public function authControl() {
@@ -116,17 +106,31 @@ protected function setUpFacebookInteractions($options) {
$fb_user_profile = null;
}
}
- //Plant unique token for CSRF protection during auth per https://developers.facebook.com/docs/authentication/
+ // Plant unique token for CSRF protection during auth per https://developers.facebook.com/docs/authentication/
if (SessionCache::get('facebook_auth_csrf') == null) {
SessionCache::put('facebook_auth_csrf', md5(uniqid(rand(), true)));
}
- $params = array('scope'=>'read_stream,user_likes,user_location,user_website,'.
- 'read_friendlists,friends_location,manage_pages,read_insights,manage_pages',
- 'state'=>SessionCache::get('facebook_auth_csrf'));
+ if (isset($this->owner) && $this->owner->isMemberAtAnyLevel()) {
+ if ($this->owner->isMemberLevel()) {
+ $instance_dao = DAOFactory::getDAO('InstanceDAO');
+ $owner_instances = $instance_dao->getByOwnerAndNetwork($this->owner, 'facebook');
+ if (sizeof($owner_instances) > 0) {
+ $this->do_show_add_button = false;
+ $this->addInfoMessage("To connect another Facebook account to ThinkUp, upgrade your membership.",
+ 'membership_cap');
+ }
+ }
+ }
+
+ if ($this->do_show_add_button) {
+ $params = array('scope'=>'read_stream,user_likes,user_location,user_website,'.
+ 'read_friendlists,friends_location,manage_pages,read_insights,manage_pages',
+ 'state'=>SessionCache::get('facebook_auth_csrf'));
- $fbconnect_link = $facebook->getLoginUrl($params);
- $this->addToView('fbconnect_link', $fbconnect_link);
+ $fbconnect_link = $facebook->getLoginUrl($params);
+ $this->addToView('fbconnect_link', $fbconnect_link);
+ }
self::processPageActions($options, $facebook);
@@ -136,35 +140,38 @@ protected function setUpFacebookInteractions($options) {
$instance_dao = DAOFactory::getDAO('InstanceDAO');
$instances = $instance_dao->getByOwnerAndNetwork($this->owner, 'facebook');
- $owner_instance_dao = DAOFactory::getDAO('OwnerInstanceDAO');
- foreach ($instances as $instance) {
- // TODO: figure out if the scope has changed since this instance last got its tokens,
- // and we need to get re-request permission with the new scope
- $tokens = $owner_instance_dao->getOAuthTokens($instance->id);
- $access_token = $tokens['oauth_access_token'];
- if ($instance->network == 'facebook') { //not a page
- $pages = FacebookGraphAPIAccessor::apiRequest('/'.$instance->network_user_id.'/likes', $access_token);
- if (@$pages->data) {
- $user_pages[$instance->network_user_id] = $pages->data;
- }
+ if ($this->do_show_add_button) {
+ $owner_instance_dao = DAOFactory::getDAO('OwnerInstanceDAO');
+ foreach ($instances as $instance) {
+ // TODO: figure out if the scope has changed since this instance last got its tokens,
+ // and we need to get re-request permission with the new scope
+ $tokens = $owner_instance_dao->getOAuthTokens($instance->id);
+ $access_token = $tokens['oauth_access_token'];
+ if ($instance->network == 'facebook') { //not a page
+ $pages = FacebookGraphAPIAccessor::apiRequest('/'.$instance->network_user_id.'/likes',
+ $access_token);
+ if (@$pages->data) {
+ $user_pages[$instance->network_user_id] = $pages->data;
+ }
- $sub_accounts = FacebookGraphAPIAccessor::apiRequest('/'.$instance->network_user_id.'/accounts',
- $access_token);
- if (!empty($sub_accounts->data)) {
- $user_admin_pages[$instance->network_user_id] = array();
- foreach ($sub_accounts->data as $act) {
- if (self::isAccountPage($act->id, $access_token)) {
- $user_admin_pages[$instance->network_user_id][] = $act;
+ $sub_accounts = FacebookGraphAPIAccessor::apiRequest('/'.$instance->network_user_id.'/accounts',
+ $access_token);
+ if (!empty($sub_accounts->data)) {
+ $user_admin_pages[$instance->network_user_id] = array();
+ foreach ($sub_accounts->data as $act) {
+ if (self::isAccountPage($act->id, $access_token)) {
+ $user_admin_pages[$instance->network_user_id][] = $act;
+ }
}
}
}
+ if (isset($tokens['auth_error']) && $tokens['auth_error'] != '') {
+ $instance->auth_error = $tokens['auth_error'];
+ }
}
- if (isset($tokens['auth_error']) && $tokens['auth_error'] != '') {
- $instance->auth_error = $tokens['auth_error'];
- }
+ $this->addToView('user_pages', $user_pages);
+ $this->addToView('user_admin_pages', $user_admin_pages);
}
- $this->addToView('user_pages', $user_pages);
- $this->addToView('user_admin_pages', $user_admin_pages);
$owner_instance_pages = $instance_dao->getByOwnerAndNetwork($this->owner, 'facebook page');
if (count($owner_instance_pages) > 0) {
@@ -603,4 +603,57 @@ public function testForDeleteCSRFToken() {
$this->assertPattern('/name="csrf_token" value="'. self::CSRF_TOKEN .
'" \/><!\-\- delete page csrf token \-\->/', $output);
}
+
+ public function testOwnerMemberLevelWithAccountConnected() {
+ // build options data
+ $options_array = $this->buildPluginOptions();
+ //Add a connected Facebook account
+ $builders[] = FixtureBuilder::build('instances', array('id'=>2, 'network_user_id'=>14,
+ 'network_username'=>'zuck', 'is_public'=>1, 'network'=>'facebook'));
+ $builders[] = FixtureBuilder::build('owner_instances', array('owner_id'=>1, 'instance_id'=>2));
+
+ $this->simulateLogin('me@example.com');
+ $owner_dao = DAOFactory::getDAO('OwnerDAO');
+ $owner = $owner_dao->getByEmail(Session::getLoggedInUser());
+ //Set membership_level to Member
+ $owner->membership_level = "Member";
+
+ $controller = new FacebookPluginConfigurationController($owner, 'facebook');
+ $output = $controller->go();
+ $this->debug($output);
+
+ // Assert that the Add User button isn't there
+ $this->assertNoPattern('/Add a Facebook Account/', $output);
+ // Assert that the message about upgradiing is there
+ $this->assertPattern('/To connect another Facebook account to ThinkUp, upgrade your membership/', $output);
+ }
+
+ public function testOwnerProLevelWith9AccountsConnected() {
+ self::buildInstanceData();
+ // build options data
+ $options_array = $this->buildPluginOptions();
+ //Add 9 connected Facebok accounts
+ $i = 9;
+ while ($i > 0) {
+ $builders[] = FixtureBuilder::build('instances', array('id'=>(10+$i), 'network_user_id'=>14,
+ 'network_username'=>'zuck', 'is_public'=>1, 'network'=>'facebook'));
+ $builders[] = FixtureBuilder::build('owner_instances', array('owner_id'=>2, 'instance_id'=>(10+$i)));
+ $i--;
+ }
+
+ $this->simulateLogin('me2@example.com', true);
+ $owner_dao = DAOFactory::getDAO('OwnerDAO');
+ $owner = $owner_dao->getByEmail(Session::getLoggedInUser());
+ //Set membership_level to Pro
+ $owner->membership_level = "Pro";
+ $controller = new FacebookPluginConfigurationController($owner, 'facebook');
+ $output = $controller->go();
+
+ $this->debug($output);
+
+ // Assert that the Add User button isn't there
+ $this->assertNoPattern('/Add a Facebook Account/', $output);
+ // Assert that the message about the membership cap is there
+ $this->assertPattern('/You&#39;ve connected 10 of 10 accounts to ThinkUp./', $output);
+ }
}
Oops, something went wrong.

0 comments on commit 2011b6e

Please sign in to comment.