Skip to content

Commit

Permalink
Reapply Terms of Service API (#1932)
Browse files Browse the repository at this point in the history
This reverts commit b9966cf.
  • Loading branch information
rjmackay committed Aug 21, 2017
1 parent f30d97d commit 1b616bb
Show file tree
Hide file tree
Showing 22 changed files with 526 additions and 3 deletions.
18 changes: 18 additions & 0 deletions application/classes/Controller/Api/Tos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');

/**
* Ushahidi API Tos Controller
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Application\Controllers
* @copyright 2017 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/

class Controller_Api_Tos extends Ushahidi_Rest {

protected function _scope()
{
return 'tos';
}
}
12 changes: 12 additions & 0 deletions application/classes/Ushahidi/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ public static function init()
'update' => $di->lazyNew('Ushahidi_Validator_Tag_Update'),
'delete' => $di->lazyNew('Ushahidi_Validator_Tag_Delete'),
];

$di->params['Ushahidi\Factory\ValidatorFactory']['map']['tos'] = [
'create' => $di->lazyNew('Ushahidi_Validator_Tos_Create'),
];

$di->params['Ushahidi\Factory\ValidatorFactory']['map']['users'] = [
'create' => $di->lazyNew('Ushahidi_Validator_User_Create'),
'update' => $di->lazyNew('Ushahidi_Validator_User_Update'),
Expand Down Expand Up @@ -321,6 +326,7 @@ public static function init()
'permissions' => $di->lazyNew('Ushahidi_Formatter_Permission'),
// Formatter for post exports. Defaults to CSV export
'posts_export' => $di->lazyNew('Ushahidi_Formatter_Post_CSV'),
'tos' => $di->lazyNew('Ushahidi_Formatter_Tos')
];

// Formatter parameters
Expand All @@ -344,6 +350,7 @@ public static function init()
'contact',
'role',
'permission',
'tos',
] as $name)
{
$di->setter['Ushahidi_Formatter_' . Text::ucfirst($name, '_')]['setAuth'] =
Expand Down Expand Up @@ -414,6 +421,7 @@ public static function init()
$di->set('repository.oauth.session', $di->lazyNew('OAuth2_Storage_Session'));
$di->set('repository.oauth.scope', $di->lazyNew('OAuth2_Storage_Scope'));
$di->set('repository.posts_export', $di->lazyNew('Ushahidi_Repository_Post_Export'));
$di->set('repository.tos', $di->lazyNew('Ushahidi_Repository_Tos'));

$di->setter['Ushahidi_Repository_User']['setHasher'] = $di->lazyGet('tool.hasher.password');

Expand Down Expand Up @@ -570,6 +578,10 @@ public static function init()
'role_repo' => $di->lazyGet('repository.role'),
];

$di->params['Ushahidi_Validator_Tos_Create'] = [
'user_repo' => $di->lazyGet('repository.user')
];

$di->params['Ushahidi_Validator_User_Create'] = [
'repo' => $di->lazyGet('repository.user'),
'role_repo' => $di->lazyGet('repository.role'),
Expand Down
28 changes: 28 additions & 0 deletions application/classes/Ushahidi/Formatter/Tos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');

/**
* Ushahidi API Formatter for CSV
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Application
* @copyright 2014 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/

use Ushahidi\Core\Traits\FormatterAuthorizerMetadata;

class Ushahidi_Formatter_Tos extends Ushahidi_Formatter_API
{
use FormatterAuthorizerMetadata;

protected function format_agreement_date($value)
{
return $value ? $value->format(DateTime::W3C) : NULL;
}

protected function format_tos_version_date($value)
{
return $value ? $value->format(DateTime::W3C) : NULL;
}

}
73 changes: 73 additions & 0 deletions application/classes/Ushahidi/Repository/Tos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');

/**
* Ushahidi Tos Repository
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Application
* @copyright 2017 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/

use Ushahidi\Core\Entity;
use Ushahidi\Core\Entity\Tos;
use Ushahidi\Core\Entity\TosRepository;
use Ushahidi\Core\SearchData;
use Ushahidi\Core\Traits\UserContext;



class Ushahidi_Repository_Tos extends Ushahidi_Repository implements
TosRepository
{
use UserContext;


// Ushahidi_Repository
protected function getTable()
{
return 'tos';
}


// CreateRepository
public function create(Entity $entity)
{
$data = $entity->asArray();

// Save the agreement date to the current time and the user ID
$data['agreement_date'] = time();
$data['user_id'] = $this->getUserId();
// Convert tos_version_date to timestamp
$data['tos_version_date'] = $data['tos_version_date']->format("U");

return $this->executeInsert($this->removeNullValues($data));
}

public function getEntity(Array $data = null)
{
return new Tos($data);
}

// SearchRepository
public function getSearchFields()
{
return [];
}

protected function setSearchConditions(SearchData $search)
{

$query = $this->search_query;

$query->where('user_id', '=', $this->getUserId());
}

public function getSearchResults()
{
$query = $this->getSearchQuery();
$results = $query->distinct(TRUE)->execute($this->db);
return $this->getCollection($results->as_array());
}

}
2 changes: 1 addition & 1 deletion application/classes/Ushahidi/Rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ protected function _payload()
protected function _parse_request_body()
{
$payload = json_decode($this->request->body(), true);

// Ensure there were no JSON errors
$error = json_last_error();
if ($error AND $error !== JSON_ERROR_NONE)
Expand Down Expand Up @@ -480,7 +481,6 @@ protected function _prepare_response()

// Add CORS headers to the response
$this->add_cors_headers($this->response);

// Should we prevent this request from being cached?
if ( ! in_array($this->request->method(), $this->_cacheable_methods))
{
Expand Down
53 changes: 53 additions & 0 deletions application/classes/Ushahidi/Validator/Tos/Create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');

/**
* Ushahidi CSV Validator
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Application
* @copyright 2014 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/

use Ushahidi\Core\Tool\Validator;
use Ushahidi\Core\Entity\UserRepository;


class Ushahidi_Validator_Tos_Create extends Validator
{
protected $user_repo;
protected $default_error_source = 'tos';

public function __construct(UserRepository $user_repo)
{
$this->user_repo = $user_repo;
}

protected function getRules()
{
return [
'id' => [
['numeric'],
],
'user_id' => [
['numeric'],
[[$this->user_repo, 'exists'], [':value']],
],
'agreement_date' => [
[[$this, 'validDate'], [':value']],
],
'tos_version_date' => [
[[$this, 'validDate'], [':value']],
],

];
}

public function validDate($str)
{
if ($str instanceof \DateTimeInterface) {
return true;
}
return (strtotime($str) !== FALSE);
}
}
30 changes: 30 additions & 0 deletions migrations/20170712152806_add_tos_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Phinx\Migration\AbstractMigration;

class AddTosTable extends AbstractMigration
{
/**
* Migrate Up.
*/
public function up()
{
$this->table('tos')
->addColumn('user_id', 'integer', ['null' => false])
->addColumn('agreement_date', 'integer', ['null' => false])
->addColumn('tos_version_date', 'integer', ['null' => false])
->addForeignKey('user_id', 'users', 'id', [
'delete' => 'CASCADE',
'update' => 'CASCADE',
])
->create();
}

/**
* Migrate Down.
*/
public function down()
{
$this->dropTable('tos');
}
}
23 changes: 23 additions & 0 deletions migrations/20170713213005_add_tos_to_scope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Phinx\Migration\AbstractMigration;

class AddTosToScope extends AbstractMigration
{

/**
* Migrate Up.
*/
public function up()
{
$this->execute("INSERT INTO oauth_scopes (scope, name) VALUES ('tos', 'tos')");
}

/**
* Migrate Down.
*/
public function down()
{
$this->execute("DELETE FROM oauth_scopes WHERE scope = 'tos'");
}
}
35 changes: 35 additions & 0 deletions phinx.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
paths:
migrations: ./migrations
seeds: %%PHINX_CONFIG_DIR%%/db/seeds

environments:
default_migration_table: phinxlog
default_database: development
production:
adapter: mysql
host: localhost
name: production_db
user: root
pass: ''
port: 3306
charset: utf8

development:
adapter: mysql
host: 192.168.33.110
name: ushahidi
user: homestead
pass: 'secret'
port: 3306
charset: utf8

testing:
adapter: mysql
host: localhost
name: testing_db
user: root
pass: ''
port: 3306
charset: utf8

version_order: creation
1 change: 1 addition & 0 deletions platform-client
Submodule platform-client added at 2d9703
46 changes: 46 additions & 0 deletions src/Core/Entity/Tos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* Ushahidi Tag
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Platform
* @copyright 2014 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/

namespace Ushahidi\Core\Entity;

use Ushahidi\Core\StaticEntity;

class Tos extends StaticEntity
{
protected $id;
protected $user_id;
protected $agreement_date;
protected $tos_version_date;

protected function getDerived()
{
// Foreign key alias
return [
'user_id' => ['user', 'user.id']
];
}

protected function getDefinition()
{
return [
'id' => 'int',
'user_id' => 'int',
'agreement_date' => '*date',
'tos_version_date' => '*date',
];
}

// Entity
public function getResource()
{
return 'tos';
}
}
Loading

0 comments on commit 1b616bb

Please sign in to comment.