Permalink
Browse files

Admin user can activate/deactivate ThinkUp user accounts from web int…

…erface


Closes #89, Closes #573
  • Loading branch information...
1 parent ed6e7cb commit 52b3631f2538891ccdffc865778cec3211a4a29a @randi2kewl randi2kewl committed with ginatrapani Jan 26, 2011
@@ -279,4 +279,57 @@ public function testAccountStatus() {
//new status
$this->assertEqual($owner->account_status, 'this is a test account status');
}
+
+ public function testSetOwnerActive() {
+ $builders_array = array();
+ # build our data
+ $builders_array[] = FixtureBuilder::build('owners', array('full_name'=>'ThinkUp J. User',
+ 'email'=>'ttuser2@example.com', 'is_activated'=>0));
+
+ $builders_array[] = FixtureBuilder::build('owners', array('full_name'=>'ThinkUp J. User',
+ 'email'=>'ttuser3@example.com', 'is_activated'=>1));
+ # init our dao
+ $dao = new OwnerMySQLDAO();
+
+ // flip form false to true
+ $test_owners_records = $builders_array[0]->columns;
+ $id = $test_owners_records['last_insert_id'];
+ $this->assertTrue($dao->setOwnerActive($id, 1));
+ $owner = $this->DAO->getByEmail('ttuser2@example.com');
+ //new status
+ $this->assertTrue($owner->is_activated);
+
+ // $sql = "select * from " . $this->prefix . 'owners where id = ' . $test_owners_records['last_insert_id'];
+ // $stmt = OwnerMysqlDAO::$PDO->query($sql);
+ // $data = $stmt->fetch();
+ // $this->assertEqual($data['is_activated'], 1);
+
+ // already true
+ $test_owners_records = $builders_array[1]->columns;
+ $id = $test_owners_records['last_insert_id'];
+ // nothing updated, so false
+ $this->assertFalse($dao->setOwnerActive($id, 1));
+ $owner = $this->DAO->getByEmail('ttuser3@example.com');
+ //new status
+ $this->assertTrue($owner->is_activated);
+
+ // $sql = "select * from " . $this->prefix . 'owners where id = ' . $test_owners_records['last_insert_id'];
+ // $stmt = OwnerMysqlDAO::$PDO->query($sql);
+ // $data = $stmt->fetch();
+ // $this->assertEqual($data['is_activated'], 1);
+
+ // flip to false
+ $test_owners_records = $builders_array[0]->columns;
+ $id = $test_owners_records['last_insert_id'];
+ $this->assertTrue($dao->setOwnerActive($id, 0));
+
+ $owner = $this->DAO->getByEmail('ttuser2@example.com');
+ //new status
+ $this->assertFalse($owner->is_activated);
+
+ // $sql = "select * from " . $this->prefix . 'owners where id = ' . $test_owners_records['last_insert_id'];
+ // $stmt = OwnerMysqlDAO::$PDO->query($sql);
+ // $data = $stmt->fetch();
+ // $this->assertEqual($data['is_activated'], 0);
+ }
}
@@ -0,0 +1,106 @@
+<?php
+/**
+ *
+ * ThinkUp/tests/TestOfToggleActiveOwnerController.php
+ *
+ * Copyright (c) 2009-2010 Gina Trapani
+ *
+ * @author Randi Miller <techrandy[at]gmail[dot]com>
+ *
+ * LICENSE:
+ *
+ * This file is part of ThinkUp (http://thinkupapp.com).
+ *
+ * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
+ * later version.
+ *
+ * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ *
+ * @author Gina Trapani <ginatrapani[at]gmail[dot]com>
+ * @license http://www.gnu.org/licenses/gpl.html
+ * @copyright 2009-2010 Gina Trapani
+ */
+require_once dirname(__FILE__).'/init.tests.php';
+require_once THINKUP_ROOT_PATH.'webapp/_lib/extlib/simpletest/autorun.php';
+require_once THINKUP_ROOT_PATH.'webapp/config.inc.php';
+
+class TestOfToggleActiveOwnerController extends ThinkUpUnitTestCase {
+
+ public function __construct() {
+ $this->UnitTestCase('ToggleActiveOwnerController class test');
+ }
+
+ public function setUp() {
+ parent::setUp();
+ }
+
+ public function tearDown() {
+ parent::tearDown();
+ }
+
+ public function testConstructor() {
+ $controller = new ToggleActiveOwnerController(true);
+ $this->assertTrue(isset($controller));
+ }
+
+ public function testNotLoggedIn() {
+ $controller = new ToggleActiveOwnerController(true);
+ $results = $controller->go();
+ $v_mgr = $controller->getViewManager();
+ $config = Config::getInstance();
+ $this->assertEqual('You must <a href="'.$config->getValue('site_root_path').
+ 'session/login.php">log in</a> to do this.', $v_mgr->getTemplateDataItem('errormsg'));
+ }
+
+ public function testNotAnAdmin() {
+ $this->simulateLogin('me@example.com');
+ $controller = new ToggleActiveOwnerController(true);
+ $results = $controller->go();
+
+ $v_mgr = $controller->getViewManager();
+ $config = Config::getInstance();
+ $this->assertEqual('You must be a ThinkUp admin to do this', $v_mgr->getTemplateDataItem('errormsg'));
+ }
+
+ public function testMissingOwnerIdParam() {
+ $this->simulateLogin('me@example.com', true);
+ $_GET['a'] = 1;
+ $controller = new ToggleActiveOwnerController(true);
+ $results = $controller->go();
+ $this->assertEqual($results, 'Missing required parameters.', $results);
+ }
+
+ public function testMissingActiveParam() {
+ $this->simulateLogin('me@example.com', true);
+ $_GET['oid'] = 1;
+ $controller = new ToggleActiveOwnerController(true);
+ $results = $controller->go();
+ $this->assertEqual($results, 'Missing required parameters.', $results);
+ }
+
+ public function testBothParamsNonExistentInstance() {
+ $this->simulateLogin('me@example.com', true);
+ $_GET['oid'] = 1;
+ $_GET['a'] = 1;
+ $controller = new ToggleActiveOwnerController(true);
+ $results = $controller->go();
+ $this->assertEqual($results, 0, $results);
+ }
+
+ public function testBothParamsExistentInstance() {
+ $builder = FixtureBuilder::build('owners', array('id'=>51, 'email'=>'me123@example.com', 'is_active'=>0));
+ $this->simulateLogin('me@example.com', true);
+ $_GET['oid'] = '51';
+ $_GET['a'] = '1';
+ $controller = new ToggleActiveOwnerController(true);
+ $results = $controller->go();
+ $this->assertEqual($results, 1);
+ }
+}
@@ -54,6 +54,7 @@
$controller_test->addTestCase(new TestOfTestAuthController());
$controller_test->addTestCase(new TestOfTestAdminController());
$controller_test->addTestCase(new TestOfToggleActiveInstanceController());
+$controller_test->addTestCase(new TestOfToggleActiveOwnerController());
$controller_test->addTestCase(new TestOfToggleActivePluginController());
$controller_test->addTestCase(new TestOfTogglePublicInstanceController());
$controller_test->addTestCase(new TestOfUserController());
@@ -62,6 +63,8 @@
$controller_test->addTestCase(new TestOfRSSController());
$controller_test->addTestCase(new TestOfUpgradeController());
+
+
$tr = new TextReporter();
$controller_test->run( $tr );
if (isset($RUNNING_ALL_TESTS) && $RUNNING_ALL_TESTS) {
@@ -0,0 +1,62 @@
+<?php
+/**
+ *
+ * ThinkUp/webapp/_lib/controller/class.ToggleActiveOwnerController.php
+ *
+ * Copyright (c) 2009-2010 Gina Trapani
+ *
+ * LICENSE:
+ *
+ * This file is part of ThinkUp (http://thinkupapp.com).
+ *
+ * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
+ * later version.
+ *
+ * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ *
+ * Toggle Active Owner Controller
+ * Activate or deactivate an owner.
+ * @license http://www.gnu.org/licenses/gpl.html
+ * @copyright 2009-2010 Gina Trapani
+ * @author Gina Trapani <ginatrapani[at]gmail[dot]com>
+ */
+class ToggleActiveOwnerController extends ThinkUpAdminController {
+ /**
+ * Required query string parameters
+ * @var array oid = owner ID, a = 1 or 0, active or inactive
+ */
+ var $REQUIRED_PARAMS = array('oid', 'a');
+
+ /**
+ *
+ * @var boolean
+ */
+ var $is_missing_param = false;
+
+ public function __construct($session_started=false) {
+ parent::__construct($session_started);
+ $this->setViewTemplate('session.toggle.tpl');
+ foreach ($this->REQUIRED_PARAMS as $param) {
+ if (!isset($_GET[$param]) || $_GET[$param] == '' ) {
+ $this->addInfoMessage('Missing required parameters.');
+ $this->is_missing_param = true;
+ }
+ }
+ }
+
+ public function adminControl(){
+ if (!$this->is_missing_param) {
+ $is_activated = ($_GET["a"] != 1)?false:true;
+ $owner_dao = DAOFactory::getDAO('OwnerDAO');
+ $this->addToView('result', $owner_dao->setOwnerActive($_GET["oid"], $is_activated));
+ }
+ return $this->generateView();
+ }
+}
@@ -244,4 +244,12 @@ public function setAccountStatus($email, $status) {
public function clearAccountStatus($email) {
return $this->setAccountStatus($email, '');
}
+
+ public function setOwnerActive($id, $is_activated) {
+ $q = "UPDATE #prefix#owners
+ SET is_activated=:is_activated
+ WHERE id=:id";
+ $stmt = $this->execute($q, array(':is_activated' => $is_activated, ':id' => $id));
+ return $this->getUpdateCount($stmt);
+ }
}
@@ -176,4 +176,13 @@ public function setAccountStatus($email, $status);
* @return bool True on success
*/
public function clearAccountStatus($email);
+
+ /**
+ * Activates an owner account.
+ *
+ * @param str $owner_id
+ * @param int $is_activated Active = 1, Inactive=0.
+ * @return int number of updated rows.
+ */
+ public function setOwnerActive($id, $is_activated);
}
@@ -136,8 +136,12 @@
<ul class="user-accounts">
{foreach from=$owners key=oid item=o}
<li>
- <b>{$o->full_name} ({$o->email})</b>{if $o->last_login neq '0000-00-00'}, last logged in {$o->last_login}{/if}
- <br />{if $o->is_activated}Activated{else}Not activated{/if}{if $o->is_admin}, Administrator{/if}
+ <b>{$o->full_name} ({$o->email})</b>{if $o->last_login neq '0000-00-00'}, last logged in {$o->last_login}{/if}{if $o->is_admin}, Administrator<br />{/if}
+ {if !$o->is_admin}
+ <input type="submit" name="submit" class="tt-button ui-state-default ui-priority-secondary ui-corner-all toggleOwnerButton" id="user{$o->id}" value="{if $o->is_activated}Deactivate{else}Activate{/if}" />
+ {/if}
+ <span style="display: none;" class='success mt_10' id="message1{$o->id}"></span>
+
{if $o->instances neq null}
<ul>
{foreach from=$o->instances key=iid item=i}
@@ -300,6 +304,75 @@ $(function() {
}
});
});
+
+ $(function() {
+ var activateOwner = function(u) {
+ //removing the "user" from id here to stop conflict with plugin
+ u = u.substr(4);
+ var dataString = 'oid=' + u + "&a=1";
+ $.ajax({
+ type: "GET",
+ url: "{/literal}{$site_root_path}{literal}account/toggle-owneractive.php",
+ data: dataString,
+ success: function() {
+ $('#spanowneractivation' + u).css('display', 'none');
+ $('#message1' + u).html("Activated!").hide().fadeIn(1500, function() {
+ $('#message1' + u);
+ });
+ $('#spanownernamelink' + u).css('display', 'inline');
+ $('#user' + u).val('Deactivate');
+ $('#spanownernametext' + u).css('display', 'none');
+ $('#user' + u).removeClass('btnActivate');
+ $('#user' + u).addClass('btnDectivate');
+ setTimeout(function() {
+ $('#message1' + u).css('display', 'none');
+ $('#spanowneractivation' + u).hide().fadeIn(1500);
+ },
+ 2000
+ );
+ }
+ });
+ return false;
+ };
+
+ var deactivateOwner = function(u) {
+ //removing the "user" from id here to stop conflict with plugin
+ u = u.substr(4);
+ var dataString = 'oid=' + u + "&a=0";
+ $.ajax({
+ type: "GET",
+ url: "{/literal}{$site_root_path}{literal}account/toggle-owneractive.php",
+ data: dataString,
+ success: function() {
+ $('#spanowneractivation' + u).css('display', 'none');
+ $('#message1' + u).html("Deactivated!").hide().fadeIn(1500, function() {
+ $('#message1' + u);
+ });
+ $('#spanownernamelink' + u).css('display', 'none');
+ $('#spanownernametext' + u).css('display', 'inline');
+ $('#user' + u).val('Activate');
+ $('#user' + u).removeClass('btnDeactivate');
+ $('#user' + u).addClass('btnActivate');
+ setTimeout(function() {
+ $('#message1' + u).css('display', 'none');
+ $('#spanowneractivation' + u).hide().fadeIn(1500);
+ },
+ 2000
+ );
+ }
+ });
+ return false;
+ };
+
+ $(".toggleOwnerButton").click(function() {
+ if($(this).val() == 'Activate') {
+ activateOwner($(this).attr("id"));
+ } else {
+ deactivateOwner($(this).attr("id"));
+ }
+ });
+ });
+
{/literal}
</script>
@@ -0,0 +1,32 @@
+<?php
+/**
+ *
+ * ThinkUp/webapp/account/toggle-owneractive.php
+ *
+ * Copyright (c) 2009-2010 Gina Trapani
+ *
+ * LICENSE:
+ *
+ * This file is part of ThinkUp (http://thinkupapp.com).
+ *
+ * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
+ * later version.
+ *
+ * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ *
+ * @author Gina Trapani <ginatrapani[at]gmail[dot]com>
+ * @license http://www.gnu.org/licenses/gpl.html
+ * @copyright 2009-2010 Gina Trapani
+ */
+chdir("..");
+require_once 'init.php';
+
+$controller = new ToggleActiveOwnerController();
+echo $controller->go();

0 comments on commit 52b3631

Please sign in to comment.